Package 

Class SuFile

  • All Implemented Interfaces:
    java.io.Serializable , java.lang.Comparable

    
    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: {@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 String escapedPath
    • Method Summary

      Modifier and Type Method Description
      String getEscapedPath() Converts this abstract pathname into a pathname string suitablefor shell commands.
      static ExtendedFile open(String pathname)
      static ExtendedFile open(String parent, String child)
      static ExtendedFile open(File parent, String child)
      static ExtendedFile open(URI uri)
      SuFile getChildFile(String name)
      void setShell(Shell shell) Set the {@code Shell} instance to be used internally for all operations.
      Shell getShell()
      boolean canExecute()
      boolean canRead()
      boolean canWrite()
      boolean createNewFile()
      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.
      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.
      boolean delete() Deletes the file or directory denoted by this abstract pathname.
      boolean deleteRecursive() Deletes the file or directory denoted by this abstract pathname.
      boolean clear() Clear the content of the file denoted by this abstract pathname.Creates a new file if it does not already exist.
      void deleteOnExit() Unsupported
      boolean exists()
      String getAbsolutePath()
      SuFile getAbsoluteFile()
      String getCanonicalPath() Returns the canonical pathname string of this abstract pathname.
      SuFile getCanonicalFile() Returns the canonical form of this abstract pathname.
      SuFile getParentFile()
      long getFreeSpace() Returns the number of unallocated bytes in the partition.
      long getTotalSpace() Returns the size of the partition.
      long getUsableSpace() Returns the number of bytes available to this process on the partition.
      boolean isDirectory()
      boolean isFile()
      boolean isBlock()
      boolean isCharacter()
      boolean isSymlink()
      boolean isNamedPipe()
      boolean isSocket()
      long lastModified() Returns the time that the file denoted by this abstract pathname waslast modified.
      long length() Returns the length of the file denoted by this abstract pathname.
      boolean mkdir() Creates the directory named by this abstract pathname.
      boolean mkdirs() Creates the directory named by this abstract pathname, including anynecessary but nonexistent parent directories.
      boolean renameTo(File dest) 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 abstractpathname.
      boolean setReadable(boolean readable, boolean ownerOnly) Sets the owner's or everybody's read permission for this abstractpathname.
      boolean setWritable(boolean writable, boolean ownerOnly) Sets the owner's or everybody's write permission for this abstractpathname.
      boolean setReadOnly() Marks the file or directory named by this abstract pathname so thatonly read operations are allowed.
      boolean setLastModified(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.
      InputStream newInputStream()
      OutputStream newOutputStream(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

      • open

         static ExtendedFile open(URI uri)
      • 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.

      • getCanonicalPath

        @NonNull() String getCanonicalPath()

        Returns the canonical pathname string of this abstract pathname.

        Requires command {@code readlink}.

      • 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}.

      • 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}.