Make ExtendedFile part of public API

This commit is contained in:
topjohnwu 2022-04-22 00:34:00 -07:00
parent ed66bdebf7
commit b6a4d3e95b
5 changed files with 127 additions and 26 deletions

View File

@ -23,9 +23,9 @@ import static com.topjohnwu.libsuexample.MainActivity.TAG;
import android.util.Log;
import com.topjohnwu.superuser.Shell;
import com.topjohnwu.superuser.internal.ExtendedFile;
import com.topjohnwu.superuser.internal.IOFactory;
import com.topjohnwu.superuser.io.SuFile;
import com.topjohnwu.superuser.io.ExtendedFile;
import com.topjohnwu.superuser.ipc.utils.FileSystemApi;
import com.topjohnwu.superuser.ipc.utils.RemoteFile;
import com.topjohnwu.superuser.ipc.utils.RemoteFileChannel;
@ -44,7 +44,7 @@ public class StressTest {
private static FileCallback callback;
interface FileCallback {
void onFile(ExtendedFile<?> file) throws Exception;
void onFile(ExtendedFile file) throws Exception;
}
public static void perform(FileSystemApi.Remote s) {
@ -122,12 +122,12 @@ public class StressTest {
}
}
private static void traverse(ExtendedFile<?> base) throws Exception {
private static void traverse(ExtendedFile base) throws Exception {
if (base.isDirectory()) {
ExtendedFile<?>[] ls = base.listFiles();
ExtendedFile[] ls = base.listFiles();
if (ls == null)
return;
for (ExtendedFile<?> file : ls) {
for (ExtendedFile file : ls) {
traverse(file);
}
} else {

View File

@ -22,7 +22,7 @@ import androidx.annotation.NonNull;
import com.topjohnwu.superuser.Shell;
import com.topjohnwu.superuser.ShellUtils;
import com.topjohnwu.superuser.internal.ExtendedFile;
import com.topjohnwu.superuser.internal.FileImpl;
import com.topjohnwu.superuser.internal.Utils;
import java.io.File;
@ -55,7 +55,7 @@ import java.util.Locale;
* shell does not have root access, or else return a {@link SuFile} instance. Warning: these
* factory methods may block the calling thread if a main shell has not been created yet!
*/
public class SuFile extends ExtendedFile<SuFile> {
public class SuFile extends FileImpl<SuFile> {
private final String escapedPath;
private Shell mShell;

View File

@ -20,6 +20,8 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import com.topjohnwu.superuser.io.ExtendedFile;
import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
@ -27,30 +29,15 @@ import java.io.IOException;
import java.util.ArrayList;
@RestrictTo(RestrictTo.Scope.LIBRARY)
public abstract class ExtendedFile<T extends ExtendedFile<?>> extends File {
public abstract class FileImpl<T extends ExtendedFile> extends ExtendedFile {
private final Creator<T> c;
protected ExtendedFile(String absolutePath, Creator<T> creator) {
protected FileImpl(String absolutePath, Creator<T> creator) {
super(absolutePath);
c = creator;
}
/**
* @return true if the abstract pathname denotes a block device.
*/
public abstract boolean isBlock();
/**
* @return true if the abstract pathname denotes a character device.
*/
public abstract boolean isCharacter();
/**
* @return true if the abstract pathname denotes a symbolic link file.
*/
public abstract boolean isSymlink();
@SuppressWarnings("unchecked")
private T asT() {
return (T) this;

View File

@ -0,0 +1,114 @@
/*
* Copyright 2022 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.io;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.URI;
public abstract class ExtendedFile extends File {
/**
* {@inheritDoc}
*/
public ExtendedFile(@NonNull String pathname) {
super(pathname);
}
/**
* {@inheritDoc}
*/
public ExtendedFile(@Nullable String parent, @NonNull String child) {
super(parent, child);
}
/**
* {@inheritDoc}
*/
public ExtendedFile(@Nullable File parent, @NonNull String child) {
super(parent, child);
}
/**
* {@inheritDoc}
*/
public ExtendedFile(@NonNull URI uri) {
super(uri);
}
/**
* @return true if the abstract pathname denotes a block device.
*/
public abstract boolean isBlock();
/**
* @return true if the abstract pathname denotes a character device.
*/
public abstract boolean isCharacter();
/**
* @return true if the abstract pathname denotes a symbolic link file.
*/
public abstract boolean isSymlink();
/**
* {@inheritDoc}
*/
@NonNull
@Override
public abstract ExtendedFile getAbsoluteFile();
/**
* {@inheritDoc}
*/
@NonNull
@Override
public abstract ExtendedFile getCanonicalFile() throws IOException;
/**
* {@inheritDoc}
*/
@Nullable
@Override
public abstract ExtendedFile getParentFile();
/**
* {@inheritDoc}
*/
@Nullable
@Override
public abstract ExtendedFile[] listFiles();
/**
* {@inheritDoc}
*/
@Nullable
@Override
public abstract ExtendedFile[] listFiles(@Nullable FilenameFilter filter);
/**
* {@inheritDoc}
*/
@Nullable
@Override
public abstract ExtendedFile[] listFiles(@Nullable FileFilter filter);
}

View File

@ -21,7 +21,7 @@ import android.system.OsConstants;
import androidx.annotation.NonNull;
import com.topjohnwu.superuser.internal.ExtendedFile;
import com.topjohnwu.superuser.internal.FileImpl;
import com.topjohnwu.superuser.internal.IFileSystemService;
import com.topjohnwu.superuser.internal.ParcelValues;
@ -31,7 +31,7 @@ import java.io.IOException;
/**
* Represents a {@link File} instance on a remote process.
*/
public class RemoteFile extends ExtendedFile<RemoteFile> {
public class RemoteFile extends FileImpl<RemoteFile> {
final IFileSystemService fs;