Class CommonFiles


  • public final class CommonFiles
    extends java.lang.Object
    Tool for creating a system-wide, all-user-writable application file.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static boolean IS_WINDOWS  
      private static org.slf4j.Logger LOGGER  
      private static java.util.EnumMap<java.nio.file.attribute.PosixFilePermission,​java.nio.file.attribute.PosixFilePermission> OWNER_TO_OTHER_MAPPING
      Maps OWNER permissions into the corresponding OTHER permission.
    • Constructor Summary

      Constructors 
      Constructor Description
      CommonFiles()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static void copyOwnerPermissions​(java.nio.file.Path path)
      Grant all permissions held by the owner to other users.
      static java.nio.file.Path createCommonAppFile​(java.nio.file.Path path)
      Creates a file, using the relative path provided, in the system-appropriate directory for persistent, cross-process, cross-user data.
      private static void logAcl​(java.lang.String description, org.slf4j.event.Level logLevel, java.nio.file.Path path)
      Logs the ACL for a Path at the logging level specified.
      private static void logPermissions​(java.lang.String description, org.slf4j.event.Level logLevel, java.nio.file.Path path)
      Logs the POSIX permissions for a Path at the logging level specified.
      private static void updateAcl​(java.nio.file.Path path)
      Updates ACL-based permissions.
      private static void updatePosixPermissions​(java.nio.file.Path path)
      Updates POSIX-based permissions.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • LOGGER

        private static final org.slf4j.Logger LOGGER
      • IS_WINDOWS

        private static final boolean IS_WINDOWS
      • OWNER_TO_OTHER_MAPPING

        private static final java.util.EnumMap<java.nio.file.attribute.PosixFilePermission,​java.nio.file.attribute.PosixFilePermission> OWNER_TO_OTHER_MAPPING
        Maps OWNER permissions into the corresponding OTHER permission.
    • Constructor Detail

      • CommonFiles

        public CommonFiles()
    • Method Detail

      • createCommonAppFile

        public static java.nio.file.Path createCommonAppFile​(java.nio.file.Path path)
                                                      throws java.io.IOException
        Creates a file, using the relative path provided, in the system-appropriate directory for persistent, cross-process, cross-user data.

        On Windows machines, the Windows directory assigned to the CommonApplicationData special folder is used. On *NIX machines, /var/tmp (not java.io.tmpdir) is used.

        Parameters:
        path - the relative path to the file to create within the common directory
        Returns:
        the path of the created file
        Throws:
        java.io.FileNotFoundException - if the system-appropriate directory does not exist
        java.io.IOException - if an error is raised while attempting to create the file
        java.lang.IllegalArgumentException - if path is not relative or is empty
        See Also:
        Environment.SpecialFolder Enum, Filesystem Hierarchy Standard
      • copyOwnerPermissions

        private static void copyOwnerPermissions​(java.nio.file.Path path)
        Grant all permissions held by the owner to other users.
        Parameters:
        path - the path on which permissions are altered
      • updateAcl

        private static void updateAcl​(java.nio.file.Path path)
        Updates ACL-based permissions. This method copies the owner permissions into an ACL entry representing all users.

        NOTE

        The JDK does not expose the INHERITED_ACE header bit designating whether or not an ACE is actually present in the ACL or inherited from a parent ACL. (This not the only thing in a Windows ACL not exposed by the JDK.) Because of this lack, all ACEs, not only the owner and "All Users" ACEs, are explicitly replicated breaking the rights inheritance chain that existed.
        Parameters:
        path - the file for which permissions are altered
      • updatePosixPermissions

        private static void updatePosixPermissions​(java.nio.file.Path path)
        Updates POSIX-based permissions. This method ensures that "other" includes all permissions granted to "owner".
        Parameters:
        path - the file for which permissions are altered
      • logPermissions

        private static void logPermissions​(java.lang.String description,
                                           org.slf4j.event.Level logLevel,
                                           java.nio.file.Path path)
        Logs the POSIX permissions for a Path at the logging level specified.
        Parameters:
        description - a description to write ahead of the logged permissions
        logLevel - the level at which the ACL is to be logged
        path - the path for which the ACL is logged
      • logAcl

        private static void logAcl​(java.lang.String description,
                                   org.slf4j.event.Level logLevel,
                                   java.nio.file.Path path)
        Logs the ACL for a Path at the logging level specified. If running on Windows, the ICACLS to get the Windows view of the ACL.
        Parameters:
        description - a description to write ahead of the logged ACL
        logLevel - the level at which the ACL is to be logged
        path - the path for which the ACL is logged