-
- All Implemented Interfaces:
-
java.io.Serializable,java.lang.Comparable
public class SuFile extends ExtendedFileA File implementation using root shell.
All methods of this class are implemented by executing commands via the main shell.
This class has the same behavior as a normal File, however none of the operations are atomic. This is a limitation for using shell commands.
Each method description in this class will list out its required commands. The following commands exist on all Android versions:
{@code rm},{@code rmdir},{@code mv},{@code ls},{@code ln}, and{@code mkdir}. The following commands require{@code toybox}on Android 6.0 and higher, or{@code busybox}to support legacy devices:{@code readlink},{@code touch}, and{@code stat}.This class has a few factory methods
{@code SuFile.open(...)}for obtaining File instances. These factory methods will return a normal File instance if the main shell does not have root access, or else return a SuFile instance. Warning: these factory methods may block the calling thread if a main shell has not been created yet!
-
-
Field Summary
Fields Modifier and Type Field Description private final StringescapedPath
-
Method Summary
Modifier and Type Method Description StringgetEscapedPath()Converts this abstract pathname into a pathname string suitablefor shell commands. static ExtendedFileopen(String pathname)static ExtendedFileopen(String parent, String child)static ExtendedFileopen(File parent, String child)static ExtendedFileopen(URI uri)SuFilegetChildFile(String name)voidsetShell(Shell shell)Set the {@code Shell}instance to be used internally for all operations.ShellgetShell()booleancanExecute()booleancanRead()booleancanWrite()booleancreateNewFile()booleancreateNewLink(String existing)Creates a new hard link named by this abstract pathname of an existing fileif and only if a file with this name does not yet exist. booleancreateNewSymlink(String target)Creates a new symbolic link named by this abstract pathname to a target fileif and only if a file with this name does not yet exist. booleandelete()Deletes the file or directory denoted by this abstract pathname. booleandeleteRecursive()Deletes the file or directory denoted by this abstract pathname. booleanclear()Clear the content of the file denoted by this abstract pathname.Creates a new file if it does not already exist. voiddeleteOnExit()Unsupported booleanexists()StringgetAbsolutePath()SuFilegetAbsoluteFile()StringgetCanonicalPath()Returns the canonical pathname string of this abstract pathname. SuFilegetCanonicalFile()Returns the canonical form of this abstract pathname. SuFilegetParentFile()longgetFreeSpace()Returns the number of unallocated bytes in the partition. longgetTotalSpace()Returns the size of the partition. longgetUsableSpace()Returns the number of bytes available to this process on the partition. booleanisDirectory()booleanisFile()booleanisBlock()booleanisCharacter()booleanisSymlink()booleanisNamedPipe()booleanisSocket()longlastModified()Returns the time that the file denoted by this abstract pathname waslast modified. longlength()Returns the length of the file denoted by this abstract pathname. booleanmkdir()Creates the directory named by this abstract pathname. booleanmkdirs()Creates the directory named by this abstract pathname, including anynecessary but nonexistent parent directories. booleanrenameTo(File dest)Renames the file denoted by this abstract pathname. booleansetExecutable(boolean executable, boolean ownerOnly)Sets the owner's or everybody's execute permission for this abstractpathname. booleansetReadable(boolean readable, boolean ownerOnly)Sets the owner's or everybody's read permission for this abstractpathname. booleansetWritable(boolean writable, boolean ownerOnly)Sets the owner's or everybody's write permission for this abstractpathname. booleansetReadOnly()Marks the file or directory named by this abstract pathname so thatonly read operations are allowed. booleansetLastModified(long time)Sets the last-modified time of the file or directory named by this abstract pathname. Array<String>list()Returns an array of strings naming the files and directories in thedirectory denoted by this abstract pathname. Array<String>list(FilenameFilter filter)Returns an array of strings naming the files and directories in thedirectory denoted by this abstract pathname that satisfy the specified filter. Array<SuFile>listFiles()Returns an array of abstract pathnames denoting the files in thedirectory denoted by this abstract pathname. Array<SuFile>listFiles(FilenameFilter filter)Returns an array of abstract pathnames denoting the files in thedirectory denoted by this abstract pathname that satisfy the specified filter. Array<SuFile>listFiles(FileFilter filter)Returns an array of abstract pathnames denoting the files in thedirectory denoted by this abstract pathname that satisfy the specified filter. InputStreamnewInputStream()OutputStreamnewOutputStream(boolean append)-
Methods inherited from class com.topjohnwu.superuser.nio.ExtendedFile
createNewLink, createNewSymlink, getAbsoluteFile, getCanonicalFile, getChildFile, getParentFile, isBlock, isCharacter, isNamedPipe, isSocket, isSymlink, listFiles, newInputStream, newOutputStream -
Methods inherited from class java.io.File
canExecute, canRead, canWrite, compareTo, createNewFile, createTempFile, delete, deleteOnExit, equals, exists, getAbsoluteFile, getAbsolutePath, getCanonicalFile, getCanonicalPath, getFreeSpace, getName, getParent, getParentFile, getPath, getTotalSpace, getUsableSpace, hashCode, isAbsolute, isDirectory, isFile, isHidden, lastModified, length, list, listFiles, listRoots, mkdir, mkdirs, renameTo, setExecutable, setLastModified, setReadOnly, setReadable, setWritable, toPath, toString, toURI, toURL -
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
-
Method Detail
-
getEscapedPath
@NonNull() String getEscapedPath()
Converts this abstract pathname into a pathname string suitablefor shell commands.
-
getChildFile
@NonNull() SuFile getChildFile(String name)
-
setShell
void setShell(Shell shell)
Set the
{@code Shell}instance to be used internally for all operations.This shell is also used in SuFileInputStream, SuFileOutputStream, and SuRandomAccessFile.
-
getShell
Shell getShell()
-
canExecute
boolean canExecute()
-
canRead
boolean canRead()
-
canWrite
boolean canWrite()
-
createNewFile
boolean createNewFile()
-
createNewLink
boolean createNewLink(String existing)
Creates a new hard link named by this abstract pathname of an existing fileif and only if a file with this name does not yet exist.
Requires command
{@code ln}.- Parameters:
existing- a path to an existing file.
-
createNewSymlink
boolean createNewSymlink(String target)
Creates a new symbolic link named by this abstract pathname to a target fileif and only if a file with this name does not yet exist.
Requires command
{@code ln}.- Parameters:
target- the target of the symbolic link.
-
delete
boolean delete()
Deletes the file or directory denoted by this abstract pathname. Ifthis pathname denotes a directory, then the directory must be empty inorder to be deleted.
Requires command
{@code rm}for files, and{@code rmdir}for directories.
-
deleteRecursive
boolean deleteRecursive()
Deletes the file or directory denoted by this abstract pathname. Ifthis pathname denotes a directory, then the directory will be recursivelyremoved.
Requires command
{@code rm}.
-
clear
boolean clear()
Clear the content of the file denoted by this abstract pathname.Creates a new file if it does not already exist.
-
deleteOnExit
void deleteOnExit()
Unsupported
-
exists
boolean exists()
-
getAbsolutePath
@NonNull() String getAbsolutePath()
-
getAbsoluteFile
@NonNull() SuFile getAbsoluteFile()
-
getCanonicalPath
@NonNull() String getCanonicalPath()
Returns the canonical pathname string of this abstract pathname.
Requires command
{@code readlink}.
-
getCanonicalFile
@NonNull() SuFile getCanonicalFile()
Returns the canonical form of this abstract pathname.
Requires command
{@code readlink}.
-
getParentFile
SuFile getParentFile()
-
getFreeSpace
long getFreeSpace()
Returns the number of unallocated bytes in the partition.
Requires command
{@code stat}.
-
getTotalSpace
long getTotalSpace()
Returns the size of the partition.
Requires command
{@code stat}.
-
getUsableSpace
long getUsableSpace()
Returns the number of bytes available to this process on the partition.
Requires command
{@code stat}.
-
isDirectory
boolean isDirectory()
-
isFile
boolean isFile()
-
isBlock
boolean isBlock()
-
isCharacter
boolean isCharacter()
-
isSymlink
boolean isSymlink()
-
isNamedPipe
boolean isNamedPipe()
-
isSocket
boolean isSocket()
-
lastModified
long lastModified()
Returns the time that the file denoted by this abstract pathname waslast modified.
Requires command
{@code stat}.
-
length
long length()
Returns the length of the file denoted by this abstract pathname.
Requires command
{@code stat}.
-
mkdir
boolean mkdir()
Creates the directory named by this abstract pathname.
Requires command
{@code mkdir}.
-
mkdirs
boolean mkdirs()
Creates the directory named by this abstract pathname, including anynecessary but nonexistent parent directories.
Requires command
{@code mkdir}.
-
renameTo
boolean renameTo(File dest)
Renames the file denoted by this abstract pathname.
Requires command
{@code mv}.
-
setExecutable
boolean setExecutable(boolean executable, boolean ownerOnly)
Sets the owner's or everybody's execute permission for this abstractpathname.
Requires command
{@code stat}and{@code chmod}.
-
setReadable
boolean setReadable(boolean readable, boolean ownerOnly)
Sets the owner's or everybody's read permission for this abstractpathname.
Requires command
{@code stat}and{@code chmod}.
-
setWritable
boolean setWritable(boolean writable, boolean ownerOnly)
Sets the owner's or everybody's write permission for this abstractpathname.
Requires command
{@code stat}and{@code chmod}.
-
setReadOnly
boolean setReadOnly()
Marks the file or directory named by this abstract pathname so thatonly read operations are allowed.
Requires command
{@code stat}and{@code chmod}.
-
setLastModified
boolean setLastModified(long time)
Sets the last-modified time of the file or directory named by this abstract pathname.
Note: On Android 5.1 and lower, the
{@code touch}command accepts a different timestampformat than GNU{@code touch}. This implementation uses the format accepted in GNUcoreutils, which is the same format accepted by toybox and busybox, so this operationmay fail on older Android versions without busybox.- Parameters:
time- The new last-modified time, measured in milliseconds since the epoch.
-
list
Array<String> list()
Returns an array of strings naming the files and directories in thedirectory denoted by this abstract pathname.
Requires command
{@code ls}.
-
list
Array<String> list(FilenameFilter filter)
Returns an array of strings naming the files and directories in thedirectory denoted by this abstract pathname that satisfy the specified filter.
Requires command
{@code ls}.
-
listFiles
@Nullable() Array<SuFile> listFiles()
Returns an array of abstract pathnames denoting the files in thedirectory denoted by this abstract pathname.
Requires command
{@code ls}.
-
listFiles
@Nullable() Array<SuFile> listFiles(FilenameFilter filter)
Returns an array of abstract pathnames denoting the files in thedirectory denoted by this abstract pathname that satisfy the specified filter.
Requires command
{@code ls}.
-
listFiles
@Nullable() Array<SuFile> listFiles(FileFilter filter)
Returns an array of abstract pathnames denoting the files in thedirectory denoted by this abstract pathname that satisfy the specified filter.
Requires command
{@code ls}.
-
newInputStream
@NonNull() InputStream newInputStream()
-
newOutputStream
@NonNull() OutputStream newOutputStream(boolean append)
-
-
-
-