Module org.htmlunit

Class FileUtils

java.lang.Object
org.htmlunit.util.FileUtils

public final class FileUtils extends Object
File utility class for functions not covered by third-party libraries.
Author:
Ronald Brill
  • Field Details

    • USER_ONLY_DIRECTORY

      public static final FileAttribute<Set<PosixFilePermission>> USER_ONLY_DIRECTORY
      The posix permissions of a directory accessible for the owner only (rwx------).
    • USER_ONLY_FILE

      public static final FileAttribute<Set<PosixFilePermission>> USER_ONLY_FILE
      The posix permissions of a file readable and writable for the owner only (rw-------).
  • Method Details

    • sanitizeFilename

      public static String sanitizeFilename(String name)
      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.txt becomes _.._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 CON or NUL) and the maximum length of a file name are not taken into account.

      Parameters:
      name - the name to sanitize, can be null
      Returns:
      the sanitized name or null if the name is null or nothing is left
    • createUserOnlyDirectory

      public static void createUserOnlyDirectory(Path directory) throws IOException
      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 FileAlreadyExistsException is 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 exists
      IOException - if the directory can't be created
    • newUserOnlyOutputStream

      public static OutputStream newUserOnlyOutputStream(Path file) throws IOException
      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 exists
      IOException - if the file can't be created