Class DaemonFileUtils
java.lang.Object
com.aoindustries.aoserv.daemon.util.DaemonFileUtils
Reusable file utilities.
- Author:
- AO Industries, Inc.
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleanatomicWrite(PosixFile file, byte[] newContents, long mode, int uid, int gid, PosixFile backupFile, Set<PosixFile> restorecon) Atomically replaces a file.static voidcopyResource(Class<?> clazz, String resource, OutputStream out) Copies a resource to the provided output stream.static voidcopyResource(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 voidcreateEmptyFile(PosixFile uf, int mode, int uid, int gid) Creates an empty file, if needed.static PosixFilefindUnusedBackup(String prefix, String separator, String extension) Finds an unused file, adding separator and "2" for second try, "3" for third, ...static booleanCreates a symbolic link.static booleanCreates a symbolic link.static booleanCreates a symbolic link.static booleanCreates a symbolic link.static booleanlnAll(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 booleanCreates symbolic links to all items in a directory, updating any existing link targets and ownership.static booleanCreates a directory, if needed.static booleanCreates a directory, if needed.static booleanCreates a directory, if needed.static booleanCreates a directory, if needed.static voidrestorecon(Set<PosixFile> restorecon) Calls "restorecon" on the given set of paths if this server is CentOS 7 or Rocky 9 and has selinux installed.static voidstripFilePrefix(PosixFile uf, String prefix, int uidMin, int gidMin) If the file starts with the provided prefix, strips that prefix from the file.
-
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
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 anIOExceptionoccurs; exception is then rethrown.- Returns:
trueif 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 anIOExceptionoccurs; 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:
trueif any modification was made- Throws:
IOException
-
ln
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 anIOExceptionoccurs; exception is then rethrown.- Returns:
trueif 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 anIOExceptionoccurs; 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:
trueif any modification was made- Throws:
IOException
-
lnAll
Creates symbolic links to all items in a directory, updating any existing link targets and ownership.- Returns:
trueif 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:
trueif any modification was made- Throws:
IOException- See Also:
-
mkdir
Creates a directory, if needed. If already exists makes sure it is a directory. Also sets or resets the ownership and permissions.- Returns:
trueif 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:
trueif any modification was made- Throws:
IOException
-
mkdir
Creates a directory, if needed. If already exists makes sure it is a directory. Also sets or resets the ownership and permissions.- Returns:
trueif 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:
trueif any modification was made- Throws:
IOException
-
createEmptyFile
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 overwritebackupFile- the optional backup filerestorecon- when not null, any file moved into place that might need "restorecon" will be added to the set- Returns:
truewhen the file is replaced with new content, has ownership updated, or has permissions updated- Throws:
IOException
-
restorecon
Calls "restorecon" on the given set of paths if this server is CentOS 7 or Rocky 9 and has selinux installed.- Throws:
IOExceptionSQLException
-
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
-
