java.lang.Object
com.aoindustries.aoserv.daemon.util.DaemonFileUtils

public final class DaemonFileUtils extends Object
Reusable file utilities.
Author:
AO Industries, Inc.
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    atomicWrite(PosixFile file, byte[] newContents, long mode, int uid, int gid, PosixFile backupFile, Set<PosixFile> restorecon)
    Atomically replaces a file.
    static void
    copyResource(Class<?> clazz, String resource, OutputStream out)
    Copies a resource to the provided output stream.
    static void
    copyResource(Class<?> clazz, String resource, String filename, int uid, int gid, int mode, int uidMin, int gidMin)
    Copies a resource to the provided filename, will not overwrite any existing file.
    static void
    createEmptyFile(PosixFile uf, int mode, int uid, int gid)
    Creates an empty file, if needed.
    static PosixFile
    findUnusedBackup(String prefix, String separator, String extension)
    Finds an unused file, adding separator and "2" for second try, "3" for third, ...
    static boolean
    ln(String target, PosixFile uf, int uid, int gid)
    Creates a symbolic link.
    static boolean
    ln(String target, PosixFile uf, int uid, int gid, PosixFile backup)
    Creates a symbolic link.
    static boolean
    ln(String target, String filename, int uid, int gid)
    Creates a symbolic link.
    static boolean
    ln(String target, String filename, int uid, int gid, PosixFile backup)
    Creates a symbolic link.
    static boolean
    lnAll(String targetBase, PosixFile src, int uid, int gid, String backupSuffix, String backupSeparator, String backupExtension)
    Creates symbolic links to all items in a directory, updating any existing link targets and ownership.
    static boolean
    lnAll(String targetBase, String srcBase, int uid, int gid)
    Creates symbolic links to all items in a directory, updating any existing link targets and ownership.
    static boolean
    mkdir(PosixFile uf, int mode, int uid, int gid)
    Creates a directory, if needed.
    static boolean
    mkdir(PosixFile uf, int mode, int uid, int gid, PosixFile backup)
    Creates a directory, if needed.
    static boolean
    mkdir(String dirName, int mode, int uid, int gid)
    Creates a directory, if needed.
    static boolean
    mkdir(String dirName, int mode, int uid, int gid, String backupName)
    Creates a directory, if needed.
    static void
    restorecon(Set<PosixFile> restorecon)
    Calls "restorecon" on the given set of paths if this server is CentOS 7 and has selinux installed.
    static void
    stripFilePrefix(PosixFile uf, String prefix, int uidMin, int gidMin)
    If the file starts with the provided prefix, strips that prefix from the file.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • copyResource

      public static void copyResource(Class<?> clazz, String resource, OutputStream out) throws IOException
      Copies a resource to the provided output stream.
      Throws:
      IOException
    • copyResource

      public static void copyResource(Class<?> clazz, String resource, String filename, int uid, int gid, int mode, int uidMin, int gidMin) throws IOException
      Copies a resource to the provided filename, will not overwrite any existing file.

      TODO: Copy to a temp file and rename into place.

      Throws:
      IOException
    • ln

      public static boolean ln(String target, String filename, int uid, int gid) throws IOException
      Creates a symbolic link. If the link exists, verifies and updates the target, uid, and gid. To aid in debugging, writes the filename and target to System.err if an IOException occurs; exception is then rethrown.
      Returns:
      true if any modification was made
      Throws:
      IOException
    • ln

      public static boolean ln(String target, String filename, int uid, int gid, PosixFile backup) throws IOException
      Creates a symbolic link. If the link exists, verifies and updates the target, uid, and gid. To aid in debugging, writes the filename and target to System.err if an IOException occurs; exception is then rethrown.
      Parameters:
      backup - If exists but is not a symbolic link or has a mismatched target, the existing is renamed to backup
      Returns:
      true if any modification was made
      Throws:
      IOException
    • ln

      public static boolean ln(String target, PosixFile uf, int uid, int gid) throws IOException
      Creates a symbolic link. If the link exists, verifies and updates the target, uid, and gid. To aid in debugging, writes the filename and target to System.err if an IOException occurs; exception is then rethrown.
      Returns:
      true if any modification was made
      Throws:
      IOException
    • ln

      public static boolean ln(String target, PosixFile uf, int uid, int gid, PosixFile backup) throws IOException
      Creates a symbolic link. If the link exists, verifies and updates the target, uid, and gid. To aid in debugging, writes the filename and target to System.err if an IOException occurs; exception is then rethrown.
      Parameters:
      backup - If exists but is not a symbolic link or has a mismatched target, the existing is renamed to backup
      Returns:
      true if any modification was made
      Throws:
      IOException
    • lnAll

      public static boolean lnAll(String targetBase, String srcBase, int uid, int gid) throws IOException
      Creates symbolic links to all items in a directory, updating any existing link targets and ownership.
      Returns:
      true if any modification was made
      Throws:
      IOException
      See Also:
    • lnAll

      public static boolean lnAll(String targetBase, PosixFile src, int uid, int gid, String backupSuffix, String backupSeparator, String backupExtension) throws IOException
      Creates symbolic links to all items in a directory, updating any existing link targets and ownership. Also, any files that are not found in the target are also renamed to backup.
      Returns:
      true if any modification was made
      Throws:
      IOException
      See Also:
    • mkdir

      public static boolean mkdir(String dirName, int mode, int uid, int gid) throws IOException
      Creates a directory, if needed. If already exists makes sure it is a directory. Also sets or resets the ownership and permissions.
      Returns:
      true if any modification was made
      Throws:
      IOException
    • mkdir

      public static boolean mkdir(String dirName, int mode, int uid, int gid, String backupName) throws IOException
      Creates a directory, if needed. If already exists makes sure it is a directory. Also sets or resets the ownership and permissions.
      Parameters:
      backupName - If exists but is not a directory, the existing is renamed to backupName
      Returns:
      true if any modification was made
      Throws:
      IOException
    • mkdir

      public static boolean mkdir(PosixFile uf, int mode, int uid, int gid) throws IOException
      Creates a directory, if needed. If already exists makes sure it is a directory. Also sets or resets the ownership and permissions.
      Returns:
      true if any modification was made
      Throws:
      IOException
    • mkdir

      public static boolean mkdir(PosixFile uf, int mode, int uid, int gid, PosixFile backup) throws IOException
      Creates a directory, if needed. If already exists makes sure it is a directory. Also sets or resets the ownership and permissions.
      Parameters:
      backup - If exists but is not a directory, the existing is renamed to backup
      Returns:
      true if any modification was made
      Throws:
      IOException
    • createEmptyFile

      public static void createEmptyFile(PosixFile uf, int mode, int uid, int gid) throws IOException
      Creates an empty file, if needed. If the file exists its contents are not altered. If already exists makes sure it is a file. Also sets or resets the ownership and permissions.
      Throws:
      IOException
    • stripFilePrefix

      public static void stripFilePrefix(PosixFile uf, String prefix, int uidMin, int gidMin) throws IOException
      If the file starts with the provided prefix, strips that prefix from the file. A new temp file is created and then renamed over the old.
      Throws:
      IOException
    • atomicWrite

      public static boolean atomicWrite(PosixFile file, byte[] newContents, long mode, int uid, int gid, PosixFile backupFile, Set<PosixFile> restorecon) throws IOException
      Atomically replaces a file. The file will always exist and will always be either the old of new version.

      Will not overwrite the file if the contents already match, but the permissions and ownership will still be verified.

      New file contents, both for the file and its optional backup, are written to temp files and then atomically renamed into place, with the backup renamed into place first.

      Parameters:
      file - the file to overwrite
      backupFile - the optional backup file
      restorecon - when not null, any file moved into place that might need "restorecon" will be added to the set
      Returns:
      true when the file is replaced with new content, has ownership updated, or has permissions updated
      Throws:
      IOException
    • restorecon

      public static void restorecon(Set<PosixFile> restorecon) throws IOException, SQLException
      Calls "restorecon" on the given set of paths if this server is CentOS 7 and has selinux installed.
      Throws:
      IOException
      SQLException
    • findUnusedBackup

      public static PosixFile findUnusedBackup(String prefix, String separator, String extension) throws IOException
      Finds an unused file, adding separator and "2" for second try, "3" for third, ...
      Throws:
      IOException