diff --git a/build.gradle.kts b/build.gradle.kts index 6e5da5af..c4c7423d 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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 diff --git a/nio/src/main/aidl/com/topjohnwu/superuser/internal/IFileSystemService.aidl b/nio/src/main/aidl/com/topjohnwu/superuser/internal/IFileSystemService.aidl index 594c2df4..61cbc67d 100644 --- a/nio/src/main/aidl/com/topjohnwu/superuser/internal/IFileSystemService.aidl +++ b/nio/src/main/aidl/com/topjohnwu/superuser/internal/IFileSystemService.aidl @@ -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); @@ -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); } diff --git a/nio/src/main/java/com/topjohnwu/superuser/internal/FileContainer.java b/nio/src/main/java/com/topjohnwu/superuser/internal/FileContainer.java index 24c57e27..be4c4f5e 100644 --- a/nio/src/main/java/com/topjohnwu/superuser/internal/FileContainer.java +++ b/nio/src/main/java/com/topjohnwu/superuser/internal/FileContainer.java @@ -29,23 +29,23 @@ class FileContainer { private int nextHandle = 0; // pid -> handle -> holder - private final SparseArray> files = new SparseArray<>(); + private final SparseArray> files = new SparseArray<>(); @NonNull - synchronized FileHolder get(int handle) throws IOException { + synchronized OpenFile get(int handle) throws IOException { int pid = Binder.getCallingPid(); - SparseArray pidFiles = files.get(pid); + SparseArray 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 pidFiles = files.get(pid); + SparseArray pidFiles = files.get(pid); if (pidFiles == null) { pidFiles = new SparseArray<>(); files.put(pid, pidFiles); @@ -57,10 +57,10 @@ synchronized int put(FileHolder h) { synchronized void remove(int handle) { int pid = Binder.getCallingPid(); - SparseArray pidFiles = files.get(pid); + SparseArray 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); @@ -70,15 +70,12 @@ synchronized void remove(int handle) { } synchronized void pidDied(int pid) { - SparseArray pidFiles = files.get(pid); + SparseArray 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(); } } } diff --git a/nio/src/main/java/com/topjohnwu/superuser/internal/FileSystemService.java b/nio/src/main/java/com/topjohnwu/superuser/internal/FileSystemService.java index 82a78854..eb40bbf5 100644 --- a/nio/src/main/java/com/topjohnwu/superuser/internal/FileSystemService.java +++ b/nio/src/main/java/com/topjohnwu/superuser/internal/FileSystemService.java @@ -22,9 +22,6 @@ import static android.system.OsConstants.O_RDONLY; import static android.system.OsConstants.O_TRUNC; import static android.system.OsConstants.O_WRONLY; -import static android.system.OsConstants.SEEK_CUR; -import static android.system.OsConstants.SEEK_END; -import static android.system.OsConstants.SEEK_SET; import android.annotation.SuppressLint; import android.os.Binder; @@ -53,17 +50,12 @@ protected File create(String key) { }; @Override - public ParcelValues getCanonicalPath(String path) { - ParcelValues p = new ParcelValues(); + public IOResult getCanonicalPath(String path) { try { - String v = mCache.get(path).getCanonicalPath(); - p.add(null); - p.add(v); + return new IOResult(mCache.get(path).getCanonicalPath()); } catch (IOException e) { - p.add(e); - p.add(null); + return new IOResult(e); } - return p; } @Override @@ -92,17 +84,12 @@ public long length(String path) { } @Override - public ParcelValues createNewFile(String path) { - ParcelValues p = new ParcelValues(); + public IOResult createNewFile(String path) { try { - boolean v = mCache.get(path).createNewFile(); - p.add(null); - p.add(v); + return new IOResult(mCache.get(path).createNewFile()); } catch (IOException e) { - p.add(e); - p.add(null); + return new IOResult(e); } - return p; } @Override @@ -190,30 +177,26 @@ public int getMode(String path) { } @Override - public ParcelValues createLink(String link, String target, boolean soft) { - ParcelValues p = new ParcelValues(); + public IOResult createLink(String link, String target, boolean soft) { try { if (soft) Os.symlink(target, link); else Os.link(target, link); - p.add(null); - p.add(true); + return new IOResult(true); } catch (ErrnoException e) { if (e.errno == OsConstants.EEXIST) { - p.add(null); + return new IOResult(false); } else { - p.add(e); + return new IOResult(e); } - p.add(false); } - return p; } // I/O APIs private final FileContainer openFiles = new FileContainer(); - private final ExecutorService ioPool = Executors.newCachedThreadPool(); + private final ExecutorService streamPool = Executors.newCachedThreadPool(); @Override public void register(IBinder client) { @@ -225,68 +208,55 @@ public void register(IBinder client) { @SuppressWarnings("OctalInteger") @Override - public ParcelValues openChannel(String path, int mode, String fifo) { - ParcelValues values = new ParcelValues(); - values.add(null); - FileHolder h = new FileHolder(); + public IOResult openChannel(String path, int mode, String fifo) { + OpenFile f = new OpenFile(); try { - h.fd = Os.open(path, mode | O_NONBLOCK, 0666); - h.read = Os.open(fifo, O_RDONLY | O_NONBLOCK, 0); - h.write = Os.open(fifo, O_WRONLY | O_NONBLOCK, 0); - values.add(openFiles.put(h)); + f.fd = Os.open(path, mode | O_NONBLOCK, 0666); + f.read = Os.open(fifo, O_RDONLY | O_NONBLOCK, 0); + f.write = Os.open(fifo, O_WRONLY | O_NONBLOCK, 0); + return new IOResult(openFiles.put(f)); } catch (ErrnoException e) { - values.set(0, e); - h.close(); + f.close(); + return new IOResult(e); } - return values; } @Override - public ParcelValues openReadStream(String path, ParcelFileDescriptor fd) { - ParcelValues values = new ParcelValues(); - values.add(null); - FileHolder h = new FileHolder(); + public IOResult openReadStream(String path, ParcelFileDescriptor fd) { + OpenFile f = new OpenFile(); try { - h.fd = Os.open(path, O_RDONLY, 0); - ioPool.execute(() -> { - try { - h.write = FileUtils.createFileDescriptor(fd.detachFd()); - while (h.fdToPipe(PIPE_CAPACITY, -1) > 0); - } catch (ErrnoException | IOException ignored) { - } finally { - h.close(); - } + f.fd = Os.open(path, O_RDONLY, 0); + streamPool.execute(() -> { + try (OpenFile of = f) { + of.write = FileUtils.createFileDescriptor(fd.detachFd()); + while (of.pread(PIPE_CAPACITY, -1) > 0); + } catch (ErrnoException | IOException ignored) {} }); + return new IOResult(); } catch (ErrnoException e) { - values.set(0, e); - h.close(); + f.close(); + return new IOResult(e); } - return values; } @SuppressWarnings("OctalInteger") @Override - public ParcelValues openWriteStream(String path, ParcelFileDescriptor fd, boolean append) { - ParcelValues values = new ParcelValues(); - values.add(null); - FileHolder h = new FileHolder(); + public IOResult openWriteStream(String path, ParcelFileDescriptor fd, boolean append) { + OpenFile f = new OpenFile(); try { int mode = O_CREAT | O_WRONLY | (append ? O_APPEND : O_TRUNC); - h.fd = Os.open(path, mode, 0666); - ioPool.execute(() -> { - try { - h.read = FileUtils.createFileDescriptor(fd.detachFd()); - while (h.pipeToFd(PIPE_CAPACITY, -1, false) > 0); - } catch (ErrnoException | IOException ignored) { - } finally { - h.close(); - } + f.fd = Os.open(path, mode, 0666); + streamPool.execute(() -> { + try (OpenFile of = f) { + of.read = FileUtils.createFileDescriptor(fd.detachFd()); + while (of.pwrite(PIPE_CAPACITY, -1, false) > 0); + } catch (ErrnoException | IOException ignored) {} }); + return new IOResult(); } catch (ErrnoException e) { - values.set(0, e); - h.close(); + f.close(); + return new IOResult(e); } - return values; } @Override @@ -295,102 +265,59 @@ public void close(int handle) { } @Override - public ParcelValues pread(int handle, int len, long offset) { - ParcelValues values = new ParcelValues(); - values.add(null); + public IOResult pread(int handle, int len, long offset) { try { - final FileHolder h = openFiles.get(handle); - synchronized (h) { - values.add(h.fdToPipe(len, offset)); - } + return new IOResult(openFiles.get(handle).pread(len, offset)); } catch (IOException | ErrnoException e) { - values.set(0, e); + return new IOResult(e); } - return values; } @Override - public ParcelValues pwrite(int handle, int len, long offset) { - ParcelValues values = new ParcelValues(); - values.add(null); + public IOResult pwrite(int handle, int len, long offset) { try { - final FileHolder h = openFiles.get(handle); - synchronized (h) { - h.pipeToFd(len, offset, true); - } + openFiles.get(handle).pwrite(len, offset, true); + return new IOResult(); } catch (IOException | ErrnoException e) { - values.set(0, e); + return new IOResult(e); } - return values; } @Override - public ParcelValues lseek(int handle, long offset, int whence) { - ParcelValues values = new ParcelValues(); - values.add(null); + public IOResult lseek(int handle, long offset, int whence) { try { - final FileHolder h = openFiles.get(handle); - synchronized (h) { - h.ensureOpen(); - values.add(Os.lseek(h.fd, offset, whence)); - } + return new IOResult(openFiles.get(handle).lseek(offset, whence)); } catch (IOException | ErrnoException e) { - values.set(0, e); + return new IOResult(e); } - return values; } @Override - public ParcelValues size(int handle) { - ParcelValues values = new ParcelValues(); - values.add(null); + public IOResult size(int handle) { try { - final FileHolder h = openFiles.get(handle); - synchronized (h) { - h.ensureOpen(); - long cur = Os.lseek(h.fd, 0, SEEK_CUR); - Os.lseek(h.fd, 0, SEEK_END); - values.add(Os.lseek(h.fd, 0, SEEK_CUR)); - Os.lseek(h.fd, cur, SEEK_SET); - } + return new IOResult(openFiles.get(handle).size()); } catch (IOException | ErrnoException e) { - values.set(0, e); + return new IOResult(e); } - return values; } @Override - public ParcelValues ftruncate(int handle, long length) { - ParcelValues values = new ParcelValues(); - values.add(null); + public IOResult ftruncate(int handle, long length) { try { - final FileHolder h = openFiles.get(handle); - synchronized (h) { - h.ensureOpen(); - Os.ftruncate(h.fd, length); - } + openFiles.get(handle).ftruncate(length); + return new IOResult(); } catch (IOException | ErrnoException e) { - values.set(0, e); + return new IOResult(e); } - return values; } @Override - public ParcelValues sync(int handle, boolean metaData) { - ParcelValues values = new ParcelValues(); - values.add(null); + public IOResult sync(int handle, boolean metadata) { try { - final FileHolder h = openFiles.get(handle); - synchronized (h) { - h.ensureOpen(); - if (metaData) - Os.fsync(h.fd); - else - Os.fdatasync(h.fd); - } + openFiles.get(handle).sync(metadata); + return new IOResult(); } catch (IOException | ErrnoException e) { - values.set(0, e); + return new IOResult(e); } - return values; } } diff --git a/nio/src/main/java/com/topjohnwu/superuser/internal/FileUtils.java b/nio/src/main/java/com/topjohnwu/superuser/internal/FileUtils.java index 96b77ffd..73782309 100644 --- a/nio/src/main/java/com/topjohnwu/superuser/internal/FileUtils.java +++ b/nio/src/main/java/com/topjohnwu/superuser/internal/FileUtils.java @@ -55,8 +55,6 @@ @SuppressLint("DiscouragedPrivateApi") class FileUtils { - private static final String REMOTE_ERR_MSG = "Exception thrown on remote process"; - private static Object os; private static Method splice; private static Method sendfile; @@ -200,18 +198,6 @@ static long sendfile( } } - static void checkException(ParcelValues values) throws IOException { - Throwable err = values.getTyped(0); - if (err != null) { - throw new IOException(REMOTE_ERR_MSG, err); - } - } - - static T tryAndGet(ParcelValues values) throws IOException { - checkException(values); - return values.getTyped(1); - } - @SuppressWarnings("OctalInteger") static File createTempFIFO() throws ErrnoException, IOException { File fifo = File.createTempFile("libsu-fifo-", null); diff --git a/nio/src/main/java/com/topjohnwu/superuser/internal/IOResult.java b/nio/src/main/java/com/topjohnwu/superuser/internal/IOResult.java new file mode 100644 index 00000000..6f3ed3b1 --- /dev/null +++ b/nio/src/main/java/com/topjohnwu/superuser/internal/IOResult.java @@ -0,0 +1,75 @@ +/* + * Copyright 2023 John "topjohnwu" Wu + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.topjohnwu.superuser.internal; + +import android.os.Parcel; +import android.os.Parcelable; + +import java.io.IOException; + +class IOResult implements Parcelable { + + private static final String REMOTE_ERR_MSG = "Exception thrown on remote process"; + private static final ClassLoader cl = IOResult.class.getClassLoader(); + + private final Object val; + + IOResult() { + val = null; + } + + IOResult(Object v) { + val = v; + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeValue(val); + } + + void checkException() throws IOException { + if (val instanceof Throwable) + throw new IOException(REMOTE_ERR_MSG, (Throwable) val); + } + + @SuppressWarnings("unchecked") + T tryAndGet() throws IOException { + checkException(); + return (T) val; + } + + @Override + public int describeContents() { + return 0; + } + + private IOResult(Parcel in) { + val = in.readValue(cl); + } + + static final Creator CREATOR = new Creator() { + @Override + public IOResult createFromParcel(Parcel in) { + return new IOResult(in); + } + + @Override + public IOResult[] newArray(int size) { + return new IOResult[size]; + } + }; +} diff --git a/nio/src/main/java/com/topjohnwu/superuser/internal/NIOFactory.java b/nio/src/main/java/com/topjohnwu/superuser/internal/NIOFactory.java index ae944efb..857fef1b 100644 --- a/nio/src/main/java/com/topjohnwu/superuser/internal/NIOFactory.java +++ b/nio/src/main/java/com/topjohnwu/superuser/internal/NIOFactory.java @@ -95,7 +95,7 @@ public FileChannel openChannel(@NonNull File file, int mode) throws IOException public static FileSystemManager createRemote(IBinder b) throws RemoteException { IFileSystemService fs = IFileSystemService.Stub.asInterface(b); - if (fs == null || !IFileSystemService.DESCRIPTOR.equals(b.getInterfaceDescriptor())) + if (fs == null) throw new IllegalArgumentException("The IBinder provided is invalid"); fs.register(new Binder()); diff --git a/nio/src/main/java/com/topjohnwu/superuser/internal/FileHolder.java b/nio/src/main/java/com/topjohnwu/superuser/internal/OpenFile.java similarity index 76% rename from nio/src/main/java/com/topjohnwu/superuser/internal/FileHolder.java rename to nio/src/main/java/com/topjohnwu/superuser/internal/OpenFile.java index 92a05c0e..2a55e4d2 100644 --- a/nio/src/main/java/com/topjohnwu/superuser/internal/FileHolder.java +++ b/nio/src/main/java/com/topjohnwu/superuser/internal/OpenFile.java @@ -16,6 +16,10 @@ package com.topjohnwu.superuser.internal; +import static android.system.OsConstants.SEEK_CUR; +import static android.system.OsConstants.SEEK_END; +import static android.system.OsConstants.SEEK_SET; + import android.os.Build; import android.system.ErrnoException; import android.system.Int64Ref; @@ -30,9 +34,7 @@ import java.nio.ByteBuffer; import java.nio.channels.ClosedChannelException; -class FileHolder implements Closeable { - - // All methods in this class has to be synchronized +class OpenFile implements Closeable { // This is only for testing purpose private static final boolean FORCE_NO_SPLICE = false; @@ -57,37 +59,61 @@ private StructStat getStat() throws ErrnoException { return st; } + private void ensureOpen() throws ClosedChannelException { + if (fd == null) + throw new ClosedChannelException(); + } + @Override - public void close() { + synchronized public void close() { if (fd != null) { try { Os.close(fd); - } catch (ErrnoException ignored) { - } + } catch (ErrnoException ignored) {} fd = null; } if (read != null) { try { Os.close(read); - } catch (ErrnoException ignored) { - } + } catch (ErrnoException ignored) {} read = null; } if (write != null) { try { Os.close(write); - } catch (ErrnoException ignored) { - } + } catch (ErrnoException ignored) {} write = null; } } - void ensureOpen() throws ClosedChannelException { - if (fd == null) - throw new ClosedChannelException(); + synchronized long lseek(long offset, int whence) throws ErrnoException, IOException { + ensureOpen(); + return Os.lseek(fd, offset, whence); + } + + synchronized long size() throws ErrnoException, IOException { + ensureOpen(); + long cur = Os.lseek(fd, 0, SEEK_CUR); + Os.lseek(fd, 0, SEEK_END); + long sz = Os.lseek(fd, 0, SEEK_CUR); + Os.lseek(fd, cur, SEEK_SET); + return sz; + } + + synchronized void ftruncate(long length) throws ErrnoException, IOException { + ensureOpen(); + Os.ftruncate(fd, length); + } + + synchronized void sync(boolean metadata) throws ErrnoException, IOException { + ensureOpen(); + if (metadata) + Os.fsync(fd); + else + Os.fdatasync(fd); } - int fdToPipe(int len, long offset) throws ErrnoException, IOException { + synchronized int pread(int len, long offset) throws ErrnoException, IOException { if (fd == null || write == null) throw new ClosedChannelException(); final long result; @@ -120,7 +146,7 @@ int fdToPipe(int len, long offset) throws ErrnoException, IOException { return (int) result; } - int pipeToFd(int len, long offset, boolean exact) throws ErrnoException, IOException { + synchronized int pwrite(int len, long offset, boolean exact) throws ErrnoException, IOException { if (fd == null || read == null) throw new ClosedChannelException(); if (!FORCE_NO_SPLICE && Build.VERSION.SDK_INT >= 28) { diff --git a/nio/src/main/java/com/topjohnwu/superuser/internal/ParcelValues.java b/nio/src/main/java/com/topjohnwu/superuser/internal/ParcelValues.java deleted file mode 100644 index 447a6280..00000000 --- a/nio/src/main/java/com/topjohnwu/superuser/internal/ParcelValues.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright 2023 John "topjohnwu" Wu - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.topjohnwu.superuser.internal; - -import android.os.Parcel; -import android.os.Parcelable; - -import java.util.ArrayList; - -class ParcelValues extends ArrayList implements Parcelable { - - private static final ClassLoader cl = ParcelValues.class.getClassLoader(); - - static final Creator CREATOR = new Creator() { - @Override - public ParcelValues createFromParcel(Parcel in) { - return new ParcelValues(in); - } - - @Override - public ParcelValues[] newArray(int size) { - return new ParcelValues[size]; - } - }; - - ParcelValues() {} - - private ParcelValues(Parcel in) { - int size = in.readInt(); - ensureCapacity(size); - for (int i = 0; i < size; ++i) { - add(in.readValue(cl)); - } - } - - @SuppressWarnings("unchecked") - public T getTyped(int index) { - return (T) get(index); - } - - @Override - public int describeContents() { - return CONTENTS_FILE_DESCRIPTOR; - } - - @Override - public void writeToParcel(Parcel dest, int flags) { - dest.writeInt(size()); - for (Object o : this) { - dest.writeValue(o); - } - } -} diff --git a/nio/src/main/java/com/topjohnwu/superuser/internal/RemoteFile.java b/nio/src/main/java/com/topjohnwu/superuser/internal/RemoteFile.java index 8705f329..30667808 100644 --- a/nio/src/main/java/com/topjohnwu/superuser/internal/RemoteFile.java +++ b/nio/src/main/java/com/topjohnwu/superuser/internal/RemoteFile.java @@ -61,7 +61,7 @@ protected RemoteFile[] createArray(int n) { @NonNull public String getCanonicalPath() throws IOException { try { - return FileUtils.tryAndGet(fs.getCanonicalPath(getPath())); + return fs.getCanonicalPath(getPath()).tryAndGet(); } catch (RemoteException e) { throw new IOException(e); } @@ -188,7 +188,7 @@ public long length() { @Override public boolean createNewFile() throws IOException { try { - return FileUtils.tryAndGet(fs.createNewFile(getPath())); + return fs.createNewFile(getPath()).tryAndGet(); } catch (RemoteException e) { throw new IOException(e); } @@ -197,7 +197,7 @@ public boolean createNewFile() throws IOException { @Override public boolean createNewLink(String existing) throws IOException { try { - return FileUtils.tryAndGet(fs.createLink(getPath(), existing, false)); + return fs.createLink(getPath(), existing, false).tryAndGet(); } catch (RemoteException e) { throw new IOException(e); } @@ -206,7 +206,7 @@ public boolean createNewLink(String existing) throws IOException { @Override public boolean createNewSymlink(String target) throws IOException { try { - return FileUtils.tryAndGet(fs.createLink(getPath(), target, true)); + return fs.createLink(getPath(), target, true).tryAndGet(); } catch (RemoteException e) { throw new IOException(e); } @@ -339,7 +339,7 @@ public long getUsableSpace() { public InputStream newInputStream() throws IOException { ParcelFileDescriptor[] pipe = ParcelFileDescriptor.createPipe(); try { - FileUtils.checkException(fs.openReadStream(getPath(), pipe[1])); + fs.openReadStream(getPath(), pipe[1]).checkException(); } catch (RemoteException e) { pipe[0].close(); throw new IOException(e); @@ -354,7 +354,7 @@ public InputStream newInputStream() throws IOException { public OutputStream newOutputStream(boolean append) throws IOException { ParcelFileDescriptor[] pipe = ParcelFileDescriptor.createPipe(); try { - FileUtils.checkException(fs.openWriteStream(getPath(), pipe[0], append)); + fs.openWriteStream(getPath(), pipe[0], append).checkException(); } catch (RemoteException e) { pipe[1].close(); throw new IOException(e); diff --git a/nio/src/main/java/com/topjohnwu/superuser/internal/RemoteFileChannel.java b/nio/src/main/java/com/topjohnwu/superuser/internal/RemoteFileChannel.java index b2b2cd99..11ed02f5 100644 --- a/nio/src/main/java/com/topjohnwu/superuser/internal/RemoteFileChannel.java +++ b/nio/src/main/java/com/topjohnwu/superuser/internal/RemoteFileChannel.java @@ -65,7 +65,7 @@ class RemoteFileChannel extends FileChannel { // Open the file on the remote process int posixMode = FileUtils.modeToPosix(mode); - handle = FileUtils.tryAndGet(fs.openChannel(file.getAbsolutePath(), posixMode, fifo.getPath())); + handle = fs.openChannel(file.getAbsolutePath(), posixMode, fifo.getPath()).tryAndGet(); // Since we do not have the machinery to interrupt native pthreads, we // have to make sure none of our I/O can block in all operations. @@ -117,7 +117,7 @@ private int read0(ByteBuffer dst, long offset) throws IOException { synchronized (fdLock) { if (!isOpen() || Thread.interrupted()) return -1; - len = FileUtils.tryAndGet(fs.pread(handle, limit - pos, offset)); + len = fs.pread(handle, limit - pos, offset).tryAndGet(); if (len == 0) break; dst.limit(pos + len); @@ -174,7 +174,7 @@ private int write0(ByteBuffer src, long offset) throws IOException { if (!isOpen() || Thread.interrupted()) return -1; len = Os.write(write, src); - FileUtils.checkException(fs.pwrite(handle, len, offset)); + fs.pwrite(handle, len, offset).checkException(); } if (offset >= 0) { offset += len; @@ -216,7 +216,7 @@ public long write(ByteBuffer[] srcs, int offset, int length) throws IOException public long position() throws IOException { ensureOpen(); try { - return FileUtils.tryAndGet(fs.lseek(handle, 0, OsConstants.SEEK_CUR)); + return fs.lseek(handle, 0, OsConstants.SEEK_CUR).tryAndGet(); } catch (RemoteException e) { throw new IOException(e); } @@ -228,7 +228,7 @@ public RemoteFileChannel position(long newPosition) throws IOException { if (newPosition < 0) throw new IllegalArgumentException(); try { - FileUtils.checkException(fs.lseek(handle, newPosition, OsConstants.SEEK_SET)); + fs.lseek(handle, newPosition, OsConstants.SEEK_SET).checkException(); return this; } catch (RemoteException e) { throw new IOException(e); @@ -239,7 +239,7 @@ public RemoteFileChannel position(long newPosition) throws IOException { public long size() throws IOException { ensureOpen(); try { - return FileUtils.tryAndGet(fs.size(handle)); + return fs.size(handle).tryAndGet(); } catch (RemoteException e) { throw new IOException(e); } @@ -253,7 +253,7 @@ public RemoteFileChannel truncate(long size) throws IOException { if (!writable()) throw new NonWritableChannelException(); try { - FileUtils.checkException(fs.ftruncate(handle, size)); + fs.ftruncate(handle, size).checkException(); return this; } catch (RemoteException e) { throw new IOException(e); @@ -264,7 +264,7 @@ public RemoteFileChannel truncate(long size) throws IOException { public void force(boolean metaData) throws IOException { ensureOpen(); try { - FileUtils.checkException(fs.sync(handle, metaData)); + fs.sync(handle, metaData).checkException(); } catch (RemoteException e) { throw new IOException(e); } diff --git a/service/src/main/assets/main.jar b/service/src/main/assets/main.jar index e4b11452..123c909d 100644 Binary files a/service/src/main/assets/main.jar and b/service/src/main/assets/main.jar differ