Class HardLink


  • public class HardLink
    extends java.lang.Object
    Class for creating hardlinks. Supports Unix/Linux, WinXP/2003/Vista via Cygwin, and Mac OS X.

    The HardLink class was formerly a static inner class of FSUtil, and the methods provided were blatantly non-thread-safe. To enable volume-parallel Update snapshots, we now provide static threadsafe methods that allocate new buffer string arrays upon each call. We also provide an API to hardlink all files in a directory with a single command, which is up to 128 times more efficient - and minimizes the impact of the extra buffer creations.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      (package private) static class  HardLink.HardLinkCGUnix
      Implementation of HardLinkCommandGetter class for Unix.
      (package private) static class  HardLink.HardLinkCGWin
      Implementation of HardLinkCommandGetter class for Windows.
      static class  HardLink.LinkStats
      HardLink statistics counters and methods.
      static class  HardLink.OSType
      OS Types.
    • Constructor Summary

      Constructors 
      Constructor Description
      HardLink()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void createHardLink​(java.io.File file, java.io.File linkName)
      Creates a hardlink.
      static void createHardLinkMult​(java.io.File parentDir, java.lang.String[] fileBaseNames, java.io.File linkDir)
      Creates hardlinks from multiple existing files within one parent directory, into one target directory.
      protected static int createHardLinkMult​(java.io.File parentDir, java.lang.String[] fileBaseNames, java.io.File linkDir, int maxLength)  
      (package private) static void enableJdkLinkApi​(boolean enable)  
      static int getLinkCount​(java.io.File fileName)
      Retrieves the number of links to the specified file.
      protected static int getLinkMultArgLength​(java.io.File fileDir, java.lang.String[] fileBaseNames, java.io.File linkDir)
      Calculate the nominal length of all contributors to the total commandstring length, including fixed overhead of the OS-dependent command.
      protected static int getMaxAllowedCmdArgLength()
      Return this private value for use by unit tests.
      static java.lang.String makeShellPath​(java.io.File file)
      Convert a os-native filename to a path that works for the shell.
      • Methods inherited from class java.lang.Object

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

      • HardLink

        public HardLink()
    • Method Detail

      • getLinkMultArgLength

        protected static int getLinkMultArgLength​(java.io.File fileDir,
                                                  java.lang.String[] fileBaseNames,
                                                  java.io.File linkDir)
                                           throws java.io.IOException
        Calculate the nominal length of all contributors to the total commandstring length, including fixed overhead of the OS-dependent command. It's protected rather than private, to assist unit testing, but real clients are not expected to need it -- see the way createHardLinkMult() uses it internally so the user doesn't need to worry about it.
        Parameters:
        fileDir - - source directory, parent of fileBaseNames
        fileBaseNames - - array of path-less file names, relative to the source directory
        linkDir - - target directory where the hardlinks will be put
        Returns:
        - total data length (must not exceed maxAllowedCmdArgLength)
        Throws:
        java.io.IOException
      • getMaxAllowedCmdArgLength

        protected static int getMaxAllowedCmdArgLength()
        Return this private value for use by unit tests. Shell commands are not allowed to have a total string length exceeding this size.
      • enableJdkLinkApi

        static void enableJdkLinkApi​(boolean enable)
      • createHardLink

        public static void createHardLink​(java.io.File file,
                                          java.io.File linkName)
                                   throws java.io.IOException
        Creates a hardlink.
        Parameters:
        file - - existing source file
        linkName - - desired target link file
        Throws:
        java.io.IOException
      • createHardLinkMult

        public static void createHardLinkMult​(java.io.File parentDir,
                                              java.lang.String[] fileBaseNames,
                                              java.io.File linkDir)
                                       throws java.io.IOException
        Creates hardlinks from multiple existing files within one parent directory, into one target directory.
        Parameters:
        parentDir - - directory containing source files
        fileBaseNames - - list of path-less file names, as returned by parentDir.list()
        linkDir - - where the hardlinks should be put. It must already exist. If the list of files is too long (overflows maxAllowedCmdArgLength), we will automatically split it into multiple invocations of the underlying method.
        Throws:
        java.io.IOException
      • createHardLinkMult

        protected static int createHardLinkMult​(java.io.File parentDir,
                                                java.lang.String[] fileBaseNames,
                                                java.io.File linkDir,
                                                int maxLength)
                                         throws java.io.IOException
        Throws:
        java.io.IOException
      • getLinkCount

        public static int getLinkCount​(java.io.File fileName)
                                throws java.io.IOException
        Retrieves the number of links to the specified file.
        Throws:
        java.io.IOException
      • makeShellPath

        public static java.lang.String makeShellPath​(java.io.File file)
                                              throws java.io.IOException
        Convert a os-native filename to a path that works for the shell.
        Parameters:
        file - The file to convert
        Returns:
        The unix pathname
        Throws:
        java.io.IOException - on windows, there can be problems with the subprocess