Class SuFile

All Implemented Interfaces:
Serializable, Comparable<File>

public class SuFile extends ExtendedFile
A 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:
  • Constructor Details

    • SuFile

      public SuFile(String pathname)
    • SuFile

      public SuFile(String parent, String child)
    • SuFile

      public SuFile(File parent, String child)
    • SuFile

      public SuFile(URI uri)
  • Method Details

    • open

      public static ExtendedFile open(String pathname)
    • open

      public static ExtendedFile open(String parent, String child)
    • open

      public static ExtendedFile open(File parent, String child)
    • open

      public static ExtendedFile open(URI uri)
    • getChildFile

      @NonNull public SuFile getChildFile(String name)
      Description copied from class: ExtendedFile
      Create a child relative to the abstract pathname using the same file system backend.
      Specified by:
      getChildFile in class ExtendedFile
      See Also:
    • setShell

      public void setShell(Shell shell)
      Set the Shell instance to be used internally for all operations. This shell is also used in SuFileInputStream, SuFileOutputStream, and SuRandomAccessFile.
    • getShell

      public Shell getShell()
    • getEscapedPath

      @NonNull public String 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 class File
    • canRead

      public boolean canRead()
      Overrides:
      canRead in class File
    • canWrite

      public boolean canWrite()
      Overrides:
      canWrite in class File
    • createNewFile

      public boolean createNewFile()
      Overrides:
      createNewFile in class File
    • createNewLink

      public 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.

      Requires command ln.

      Specified by:
      createNewLink in class ExtendedFile
      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

      public 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.

      Requires command ln.

      Specified by:
      createNewSymlink in class ExtendedFile
      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, and rmdir for directories.

      Overrides:
      delete in class File
      See Also:
    • 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 class File
    • exists

      public boolean exists()
      Overrides:
      exists in class File
    • getAbsolutePath

      @NonNull public String getAbsolutePath()
      Overrides:
      getAbsolutePath in class File
    • getAbsoluteFile

      @NonNull public SuFile getAbsoluteFile()
      Description copied from class: ExtendedFile
      Specified by:
      getAbsoluteFile in class ExtendedFile
    • getCanonicalPath

      @NonNull public String getCanonicalPath()
      Returns the canonical pathname string of this abstract pathname.

      Requires command readlink.

      Overrides:
      getCanonicalPath in class File
      See Also:
    • getCanonicalFile

      @NonNull public SuFile getCanonicalFile()
      Returns the canonical form of this abstract pathname.

      Requires command readlink.

      Specified by:
      getCanonicalFile in class ExtendedFile
      See Also:
    • getParentFile

      public SuFile getParentFile()
      Description copied from class: ExtendedFile
      Specified by:
      getParentFile in class ExtendedFile
    • getFreeSpace

      public long getFreeSpace()
      Returns the number of unallocated bytes in the partition.

      Requires command stat.

      Overrides:
      getFreeSpace in class File
      See Also:
    • getTotalSpace

      public long getTotalSpace()
      Returns the size of the partition.

      Requires command stat.

      Overrides:
      getTotalSpace in class File
      See Also:
    • getUsableSpace

      public long getUsableSpace()
      Returns the number of bytes available to this process on the partition.

      Requires command stat.

      Overrides:
      getUsableSpace in class File
      See Also:
    • isDirectory

      public boolean isDirectory()
      Overrides:
      isDirectory in class File
    • isFile

      public boolean isFile()
      Overrides:
      isFile in class File
    • isBlock

      public boolean isBlock()
      Specified by:
      isBlock in class ExtendedFile
      Returns:
      true if the abstract pathname denotes a block device.
    • isCharacter

      public boolean isCharacter()
      Specified by:
      isCharacter in class ExtendedFile
      Returns:
      true if the abstract pathname denotes a character device.
    • isSymlink

      public boolean isSymlink()
      Specified by:
      isSymlink in class ExtendedFile
      Returns:
      true if the abstract pathname denotes a symbolic link.
    • isNamedPipe

      public boolean isNamedPipe()
      Specified by:
      isNamedPipe in class ExtendedFile
      Returns:
      true if the abstract pathname denotes a named pipe (FIFO).
    • isSocket

      public boolean isSocket()
      Specified by:
      isSocket in class ExtendedFile
      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 class File
      See Also:
    • length

      public long length()
      Returns the length of the file denoted by this abstract pathname.

      Requires command stat.

      Overrides:
      length in class File
      See Also:
    • mkdir

      public boolean mkdir()
      Creates the directory named by this abstract pathname.

      Requires command mkdir.

      Overrides:
      mkdir in class File
      See Also:
    • mkdirs

      public boolean mkdirs()
      Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories.

      Requires command mkdir.

      Overrides:
      mkdirs in class File
      See Also:
    • renameTo

      public boolean renameTo(File dest)
      Renames the file denoted by this abstract pathname.

      Requires command mv.

      Overrides:
      renameTo in class File
      See Also:
    • setExecutable

      public boolean setExecutable(boolean executable, boolean ownerOnly)
      Sets the owner's or everybody's execute permission for this abstract pathname.

      Requires command stat and chmod.

      Overrides:
      setExecutable in class File
      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 and chmod.

      Overrides:
      setReadable in class File
      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 and chmod.

      Overrides:
      setWritable in class File
      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 and chmod.

      Overrides:
      setReadOnly in class File
      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 GNU touch. 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 class File
      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

      public String[] list()
      Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname.

      Requires command ls.

      Overrides:
      list in class File
      See Also:
    • list

      public 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.

      Requires command ls.

      Overrides:
      list in class File
      See Also:
    • listFiles

      @Nullable public SuFile[] 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 class ExtendedFile
      See Also:
    • listFiles

      @Nullable public 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.

      Requires command ls.

      Specified by:
      listFiles in class ExtendedFile
      See Also:
    • listFiles

      @Nullable public 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.

      Requires command ls.

      Specified by:
      listFiles in class ExtendedFile
      See Also:
    • newInputStream

      @NonNull public InputStream newInputStream() throws IOException
      Description copied from class: ExtendedFile
      Opens an InputStream with the matching file system backend of the file.
      Specified by:
      newInputStream in class ExtendedFile
      Throws:
      IOException
      See Also:
    • newOutputStream

      @NonNull public OutputStream newOutputStream(boolean append) throws IOException
      Description copied from class: ExtendedFile
      Opens an OutputStream with the matching file system backend of the file.
      Specified by:
      newOutputStream in class ExtendedFile
      Throws:
      IOException
      See Also: