Skip to content

Commit

Permalink
Better remote IO implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
topjohnwu committed Mar 27, 2023
1 parent b934017 commit 8e6a340
Show file tree
Hide file tree
Showing 12 changed files with 217 additions and 273 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ buildscript {
}

dependencies {
classpath("com.android.tools.build:gradle:7.3.1")
classpath("com.android.tools.build:gradle:7.4.2")

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// IFileSystemService.aidl
package com.topjohnwu.superuser.internal;

parcelable ParcelValues;
parcelable IOResult;

interface IFileSystemService {
// File APIs
/* (err, String) */ ParcelValues getCanonicalPath(String path);
/* (err, String) */ IOResult getCanonicalPath(String path);
boolean isDirectory(String path);
boolean isFile(String path);
boolean isHidden(String path);
long lastModified(String path);
long length(String path);
/* (err, bool) */ ParcelValues createNewFile(String path);
/* (err, bool) */ IOResult createNewFile(String path);
boolean delete(String path);
String[] list(String path);
boolean mkdir(String path);
Expand All @@ -27,18 +27,18 @@ interface IFileSystemService {
long getFreeSpace(String path);
long getUsableSpace(String path);
int getMode(String path);
/* (err, bool) */ ParcelValues createLink(String link, String target, boolean soft);
/* (err, bool) */ IOResult createLink(String link, String target, boolean soft);

// I/O APIs
oneway void register(IBinder client);
/* (err, int) */ ParcelValues openChannel(String path, int mode, String fifo);
/* (err) */ ParcelValues openReadStream(String path, in ParcelFileDescriptor fd);
/* (err) */ ParcelValues openWriteStream(String path, in ParcelFileDescriptor fd, boolean append);
/* (err, int) */ IOResult openChannel(String path, int mode, String fifo);
/* (err) */ IOResult openReadStream(String path, in ParcelFileDescriptor fd);
/* (err) */ IOResult openWriteStream(String path, in ParcelFileDescriptor fd, boolean append);
oneway void close(int handle);
/* (err, int) */ ParcelValues pread(int handle, int len, long offset);
/* (err) */ ParcelValues pwrite(int handle, int len, long offset);
/* (err, long) */ ParcelValues lseek(int handle, long offset, int whence);
/* (err, long) */ ParcelValues size(int handle);
/* (err) */ ParcelValues ftruncate(int handle, long length);
/* (err) */ ParcelValues sync(int handle, boolean metaData);
/* (err, int) */ IOResult pread(int handle, int len, long offset);
/* (err) */ IOResult pwrite(int handle, int len, long offset);
/* (err, long) */ IOResult lseek(int handle, long offset, int whence);
/* (err, long) */ IOResult size(int handle);
/* (err) */ IOResult ftruncate(int handle, long length);
/* (err) */ IOResult sync(int handle, boolean metadata);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,23 @@ class FileContainer {

private int nextHandle = 0;
// pid -> handle -> holder
private final SparseArray<SparseArray<FileHolder>> files = new SparseArray<>();
private final SparseArray<SparseArray<OpenFile>> files = new SparseArray<>();

@NonNull
synchronized FileHolder get(int handle) throws IOException {
synchronized OpenFile get(int handle) throws IOException {
int pid = Binder.getCallingPid();
SparseArray<FileHolder> pidFiles = files.get(pid);
SparseArray<OpenFile> pidFiles = files.get(pid);
if (pidFiles == null)
throw new IOException(ERROR_MSG);
FileHolder h = pidFiles.get(handle);
OpenFile h = pidFiles.get(handle);
if (h == null)
throw new IOException(ERROR_MSG);
return h;
}

synchronized int put(FileHolder h) {
synchronized int put(OpenFile h) {
int pid = Binder.getCallingPid();
SparseArray<FileHolder> pidFiles = files.get(pid);
SparseArray<OpenFile> pidFiles = files.get(pid);
if (pidFiles == null) {
pidFiles = new SparseArray<>();
files.put(pid, pidFiles);
Expand All @@ -57,10 +57,10 @@ synchronized int put(FileHolder h) {

synchronized void remove(int handle) {
int pid = Binder.getCallingPid();
SparseArray<FileHolder> pidFiles = files.get(pid);
SparseArray<OpenFile> pidFiles = files.get(pid);
if (pidFiles == null)
return;
FileHolder h = pidFiles.get(handle);
OpenFile h = pidFiles.get(handle);
if (h == null)
return;
pidFiles.remove(handle);
Expand All @@ -70,15 +70,12 @@ synchronized void remove(int handle) {
}

synchronized void pidDied(int pid) {
SparseArray<FileHolder> pidFiles = files.get(pid);
SparseArray<OpenFile> pidFiles = files.get(pid);
if (pidFiles == null)
return;
files.remove(pid);
for (int i = 0; i < pidFiles.size(); ++i) {
FileHolder h = pidFiles.valueAt(i);
synchronized (h) {
h.close();
}
pidFiles.valueAt(i).close();
}
}
}
Loading

0 comments on commit 8e6a340

Please sign in to comment.