Class SuFile
- All Implemented Interfaces:
Serializable
,Comparable<File>
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: rm
, rmdir
,
mv
, ls
, ln
, and mkdir
.
The following commands require toybox
on Android 6.0 and higher, or busybox
to support legacy devices: readlink
, touch
, and stat
.
This class has a few factory methods 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!
- See Also:
-
Field Summary
Fields inherited from class java.io.File
pathSeparator, pathSeparatorChar, separator, separatorChar
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
boolean
canRead()
boolean
canWrite()
boolean
clear()
Clear the content of the file denoted by this abstract pathname.boolean
boolean
createNewLink
(String existing) Creates a new hard link named by this abstract pathname of an existing file if and only if a file with this name does not yet exist.boolean
createNewSymlink
(String target) Creates a new symbolic link named by this abstract pathname to a target file if and only if a file with this name does not yet exist.boolean
delete()
Deletes the file or directory denoted by this abstract pathname.void
Unsupportedboolean
Deletes the file or directory denoted by this abstract pathname.boolean
exists()
Returns the canonical form of this abstract pathname.Returns the canonical pathname string of this abstract pathname.getChildFile
(String name) Create a child relative to the abstract pathname using the same file system backend.Converts this abstract pathname into a pathname string suitable for shell commands.long
Returns the number of unallocated bytes in the partition.getShell()
long
Returns the size of the partition.long
Returns the number of bytes available to this process on the partition.boolean
isBlock()
boolean
boolean
boolean
isFile()
boolean
boolean
isSocket()
boolean
long
Returns the time that the file denoted by this abstract pathname was last modified.long
length()
Returns the length of the file denoted by this abstract pathname.String[]
list()
Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname.String[]
list
(FilenameFilter filter) Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.SuFile[]
Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname.SuFile[]
listFiles
(FileFilter filter) Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname that satisfy the specified filter.SuFile[]
listFiles
(FilenameFilter filter) Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname that satisfy the specified filter.boolean
mkdir()
Creates the directory named by this abstract pathname.boolean
mkdirs()
Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories.Opens an InputStream with the matching file system backend of the file.newOutputStream
(boolean append) Opens an OutputStream with the matching file system backend of the file.static ExtendedFile
static ExtendedFile
static ExtendedFile
static ExtendedFile
boolean
Renames the file denoted by this abstract pathname.boolean
setExecutable
(boolean executable, boolean ownerOnly) Sets the owner's or everybody's execute permission for this abstract pathname.boolean
setLastModified
(long time) Sets the last-modified time of the file or directory named by this abstract pathname.boolean
setReadable
(boolean readable, boolean ownerOnly) Sets the owner's or everybody's read permission for this abstract pathname.boolean
Marks the file or directory named by this abstract pathname so that only read operations are allowed.void
Set theShell
instance to be used internally for all operations.boolean
setWritable
(boolean writable, boolean ownerOnly) Sets the owner's or everybody's write permission for this abstract pathname.Methods inherited from class com.topjohnwu.superuser.nio.ExtendedFile
newOutputStream
Methods inherited from class java.io.File
compareTo, createTempFile, createTempFile, equals, getName, getParent, getPath, hashCode, isAbsolute, isHidden, listRoots, setExecutable, setReadable, setWritable, toPath, toString, toURI
-
Constructor Details
-
SuFile
-
SuFile
-
SuFile
-
SuFile
-
-
Method Details
-
open
-
open
-
open
-
open
-
getChildFile
Description copied from class:ExtendedFile
Create a child relative to the abstract pathname using the same file system backend.- Specified by:
getChildFile
in classExtendedFile
- See Also:
-
setShell
Set theShell
instance to be used internally for all operations. This shell is also used inSuFileInputStream
,SuFileOutputStream
, andSuRandomAccessFile
. -
getShell
-
getEscapedPath
Converts this abstract pathname into a pathname string suitable for shell commands.- Returns:
- the formatted string form of this abstract pathname
-
canExecute
public boolean canExecute()- Overrides:
canExecute
in classFile
-
canRead
public boolean canRead() -
canWrite
public boolean canWrite() -
createNewFile
public boolean createNewFile()- Overrides:
createNewFile
in classFile
-
createNewLink
Creates a new hard link named by this abstract pathname of an existing file if and only if a file with this name does not yet exist.Requires command
ln
.- Specified by:
createNewLink
in classExtendedFile
- Parameters:
existing
- a path to an existing file.- Returns:
true
if the named file does not exist and was successfully created;false
if the creation failed.
-
createNewSymlink
Creates a new symbolic link named by this abstract pathname to a target file if and only if a file with this name does not yet exist.Requires command
ln
.- Specified by:
createNewSymlink
in classExtendedFile
- Parameters:
target
- the target of the symbolic link.- Returns:
true
if the named file does not exist and was successfully created;false
if the creation failed.
-
delete
public boolean delete()Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the directory must be empty in order to be deleted.Requires command
rm
for files, andrmdir
for directories. -
deleteRecursive
public boolean deleteRecursive()Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the directory will be recursively removed.Requires command
rm
.- See Also:
-
clear
public boolean clear()Clear the content of the file denoted by this abstract pathname. Creates a new file if it does not already exist.- Returns:
- true if the operation succeeded
-
deleteOnExit
public void deleteOnExit()Unsupported- Overrides:
deleteOnExit
in classFile
-
exists
public boolean exists() -
getAbsolutePath
- Overrides:
getAbsolutePath
in classFile
-
getAbsoluteFile
Description copied from class:ExtendedFile
- Specified by:
getAbsoluteFile
in classExtendedFile
-
getCanonicalPath
Returns the canonical pathname string of this abstract pathname.Requires command
readlink
.- Overrides:
getCanonicalPath
in classFile
- See Also:
-
getCanonicalFile
Returns the canonical form of this abstract pathname.Requires command
readlink
.- Specified by:
getCanonicalFile
in classExtendedFile
- See Also:
-
getParentFile
Description copied from class:ExtendedFile
- Specified by:
getParentFile
in classExtendedFile
-
getFreeSpace
public long getFreeSpace()Returns the number of unallocated bytes in the partition.Requires command
stat
.- Overrides:
getFreeSpace
in classFile
- See Also:
-
getTotalSpace
public long getTotalSpace()Returns the size of the partition.Requires command
stat
.- Overrides:
getTotalSpace
in classFile
- See Also:
-
getUsableSpace
public long getUsableSpace()Returns the number of bytes available to this process on the partition.Requires command
stat
.- Overrides:
getUsableSpace
in classFile
- See Also:
-
isDirectory
public boolean isDirectory()- Overrides:
isDirectory
in classFile
-
isFile
public boolean isFile() -
isBlock
public boolean isBlock()- Specified by:
isBlock
in classExtendedFile
- Returns:
- true if the abstract pathname denotes a block device.
-
isCharacter
public boolean isCharacter()- Specified by:
isCharacter
in classExtendedFile
- Returns:
- true if the abstract pathname denotes a character device.
-
isSymlink
public boolean isSymlink()- Specified by:
isSymlink
in classExtendedFile
- Returns:
- true if the abstract pathname denotes a symbolic link.
-
isNamedPipe
public boolean isNamedPipe()- Specified by:
isNamedPipe
in classExtendedFile
- Returns:
- true if the abstract pathname denotes a named pipe (FIFO).
-
isSocket
public boolean isSocket()- Specified by:
isSocket
in classExtendedFile
- Returns:
- true if the abstract pathname denotes a socket file.
-
lastModified
public long lastModified()Returns the time that the file denoted by this abstract pathname was last modified.Requires command
stat
.- Overrides:
lastModified
in classFile
- See Also:
-
length
public long length()Returns the length of the file denoted by this abstract pathname.Requires command
stat
. -
mkdir
public boolean mkdir()Creates the directory named by this abstract pathname.Requires command
mkdir
. -
mkdirs
public boolean mkdirs()Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories.Requires command
mkdir
. -
renameTo
Renames the file denoted by this abstract pathname.Requires command
mv
. -
setExecutable
public boolean setExecutable(boolean executable, boolean ownerOnly) Sets the owner's or everybody's execute permission for this abstract pathname.Requires command
stat
andchmod
.- Overrides:
setExecutable
in classFile
- See Also:
-
setReadable
public boolean setReadable(boolean readable, boolean ownerOnly) Sets the owner's or everybody's read permission for this abstract pathname.Requires command
stat
andchmod
.- Overrides:
setReadable
in classFile
- See Also:
-
setWritable
public boolean setWritable(boolean writable, boolean ownerOnly) Sets the owner's or everybody's write permission for this abstract pathname.Requires command
stat
andchmod
.- Overrides:
setWritable
in classFile
- See Also:
-
setReadOnly
public boolean setReadOnly()Marks the file or directory named by this abstract pathname so that only read operations are allowed.Requires command
stat
andchmod
.- Overrides:
setReadOnly
in classFile
- See Also:
-
setLastModified
public 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
touch
command accepts a different timestamp format than GNUtouch
. This implementation uses the format accepted in GNU coreutils, which is the same format accepted by toybox and busybox, so this operation may fail on older Android versions without busybox.- Overrides:
setLastModified
in classFile
- Parameters:
time
- The new last-modified time, measured in milliseconds since the epoch.- Returns:
true
if and only if the operation succeeded;false
otherwise.
-
list
Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname.Requires command
ls
. -
list
Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.Requires command
ls
. -
listFiles
Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname.Requires command
ls
.- Specified by:
listFiles
in classExtendedFile
- See Also:
-
listFiles
Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname that satisfy the specified filter.Requires command
ls
.- Specified by:
listFiles
in classExtendedFile
- See Also:
-
listFiles
Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname that satisfy the specified filter.Requires command
ls
.- Specified by:
listFiles
in classExtendedFile
- See Also:
-
newInputStream
Description copied from class:ExtendedFile
Opens an InputStream with the matching file system backend of the file.- Specified by:
newInputStream
in classExtendedFile
- Throws:
IOException
- See Also:
-
newOutputStream
Description copied from class:ExtendedFile
Opens an OutputStream with the matching file system backend of the file.- Specified by:
newOutputStream
in classExtendedFile
- Throws:
IOException
- See Also:
-