Class HardLink

java.lang.Object
org.apache.bookkeeper.util.HardLink

public class HardLink extends 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.

  • Field Details

  • Constructor Details

    • HardLink

      public HardLink()
  • Method Details

    • getLinkMultArgLength

      protected static int getLinkMultArgLength(File fileDir, String[] fileBaseNames, File linkDir) throws 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:
      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(File file, File linkName) throws IOException
      Creates a hardlink.
      Parameters:
      file - - existing source file
      linkName - - desired target link file
      Throws:
      IOException
    • createHardLinkMult

      public static void createHardLinkMult(File parentDir, String[] fileBaseNames, File linkDir) throws 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:
      IOException
    • createHardLinkMult

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

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

      public static String makeShellPath(File file) throws 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:
      IOException - on windows, there can be problems with the subprocess