- Author:
- Ronald Brill
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final FileAttribute<Set<PosixFilePermission>>The posix permissions of a directory accessible for the owner only (rwx------).static final FileAttribute<Set<PosixFilePermission>>The posix permissions of a file readable and writable for the owner only (rw-------). -
Method Summary
Modifier and TypeMethodDescriptionstatic voidcreateUserOnlyDirectory(Path directory) Creates the given directory and all the nonexistent parent directories; the directories created by this method are accessible for the owner only (rwx------).static OutputStreamnewUserOnlyOutputStream(Path file) Creates the given file (it must not exist) and opens it for writing; the file is readable for the owner only (rw-------).static StringsanitizeFilename(String name) Makes the given name usable as the name of a file, the way the browsers do it for downloaded files.
-
Field Details
-
USER_ONLY_DIRECTORY
The posix permissions of a directory accessible for the owner only (rwx------). -
USER_ONLY_FILE
The posix permissions of a file readable and writable for the owner only (rw-------).
-
-
Method Details
-
sanitizeFilename
Makes the given name usable as the name of a file, the way the browsers do it for downloaded files.- Characters that are not allowed in file names on at least one platform
(
\ / : * ? " < > |), the control characters (U+0000 - U+001F) and DEL (U+007F) are replaced by an underscore. Because the path separators are replaced, the result is always a plain file name; e.g.../../evil.txtbecomes_.._evil.txt, so the name can be resolved against a folder without leaving it. - Leading dots and white space (no hidden files) and trailing dots and white space are removed.
The name is not checked any further; e.g. names reserved by Windows (like
CONorNUL) and the maximum length of a file name are not taken into account.- Parameters:
name- the name to sanitize, can benull- Returns:
- the sanitized name or
nullif the name isnullor nothing is left
- Characters that are not allowed in file names on at least one platform
(
-
createUserOnlyDirectory
Creates the given directory and all the nonexistent parent directories; the directories created by this method are accessible for the owner only (rwx------). This is relevant for directories inside the shared temp directory, where the default permissions (taken from the umask) usually allow every other user of the machine to read the content.The directory itself must not exist (otherwise a
FileAlreadyExistsExceptionis thrown); a directory with the same name created by someone else (e.g. another user of the shared temp directory) is never used. Existing parent directories are used as they are.On file systems without posix support the permissions are not set, the directories are created with the defaults of the platform.
On non-POSIX file systems (e.g. Windows) no permissions are set; the file inherits the access rules of its parent folder. The default temp folder on Windows is already restricted to the current user.
- Parameters:
directory- the directory to create- Throws:
FileAlreadyExistsException- if the directory already existsIOException- if the directory can't be created
-
newUserOnlyOutputStream
Creates the given file (it must not exist) and opens it for writing; the file is readable for the owner only (rw-------). The file is created with these permissions (no window where it is readable for others) and an existing file or link with the same name is never used.On file systems without posix support the permissions are not set, the file is created with the defaults of the platform.
On non-POSIX file systems (e.g. Windows) no permissions are set; the file inherits the access rules of its parent folder. The default temp folder on Windows is already restricted to the current user.
- Parameters:
file- the file to create- Returns:
- the output stream to write the content
- Throws:
FileAlreadyExistsException- if the file already existsIOException- if the file can't be created
-