FileUtil { public static int symLink(String target, String linkname) throws IOException{ File targetFile = new File( Path.getPathWithoutSchemeAndAuthority(new Path(target)).toString()); File linkFile = new File( Path.getPathWithoutSchemeAndAuthority(new Path(linkname)).toString()); String[] cmd = Shell.getSymlinkCommand( targetFile.toString(), linkFile.toString()); ShellCommandExecutor shExec; try { if (Shell.WINDOWS && linkFile.getParentFile() != null && !new Path(target).isAbsolute()) { shExec = new ShellCommandExecutor(cmd, linkFile.getParentFile()); } else { shExec = new ShellCommandExecutor(cmd); } shExec.execute(); } catch (Shell.ExitCodeException ec) { int returnVal = ec.getExitCode(); if (Shell.WINDOWS && returnVal == SYMLINK_NO_PRIVILEGE) { LOG.warn("Fail to create symbolic links on Windows. " + "The default security settings in Windows disallow non-elevated " + "administrators and all non-administrators from creating symbolic links. " + "This behavior can be changed in the Local Security Policy management console"); } else if (returnVal != 0) { LOG.warn("Command '" + StringUtils.join(" ", cmd) + "' failed " + returnVal + " with: " + ec.getMessage()); } return returnVal; } catch (IOException e) { if (LOG.isDebugEnabled()) { LOG.debug("Error while create symlink " + linkname + " to " + target + "." + " Exception: " + StringUtils.stringifyException(e)); } throw e; } return shExec.getExitCode(); }  static Path[] stat2Paths(FileStatus[] stats); static Path[] stat2Paths(FileStatus[] stats, Path path); static boolean fullyDelete(final File dir); static boolean fullyDelete(final File dir, boolean tryGrantPermissions); static String readLink(File f); static boolean fullyDeleteContents(final File dir); static boolean fullyDeleteContents(final File dir, final boolean tryGrantPermissions); @Deprecated static void fullyDelete(FileSystem fs, Path dir); static boolean copy(FileSystem srcFS, Path src,
                             FileSystem dstFS, Path dst,
                             boolean deleteSource,
                             Configuration conf); static boolean copy(FileSystem srcFS, Path[] srcs,
                             FileSystem dstFS, Path dst,
                             boolean deleteSource,
                             boolean overwrite, Configuration conf); static boolean copy(FileSystem srcFS, Path src,
                             FileSystem dstFS, Path dst,
                             boolean deleteSource,
                             boolean overwrite,
                             Configuration conf); static boolean copy(FileSystem srcFS, FileStatus srcStatus,
                             FileSystem dstFS, Path dst,
                             boolean deleteSource,
                             boolean overwrite,
                             Configuration conf); static boolean copyMerge(FileSystem srcFS, Path srcDir,
                                  FileSystem dstFS, Path dstFile,
                                  boolean deleteSource,
                                  Configuration conf, String addString); static boolean copy(File src,
                             FileSystem dstFS, Path dst,
                             boolean deleteSource,
                             Configuration conf); static boolean copy(FileSystem srcFS, Path src,
                             File dst, boolean deleteSource,
                             Configuration conf); static String makeShellPath(String filename); static String makeShellPath(File file); static String makeShellPath(File file, boolean makeCanonicalPath); static long getDU(File dir); static void unZip(File inFile, File unzipDir); static void unTar(File inFile, File untarDir); static int symLink(String target, String linkname); static int chmod(String filename, String perm
                          ); static int chmod(String filename, String perm, boolean recursive); static void setOwner(File file, String username,
      String groupname); static boolean setReadable(File f, boolean readable); static boolean setWritable(File f, boolean writable); static boolean setExecutable(File f, boolean executable); static boolean canRead(File f); static boolean canWrite(File f); static boolean canExecute(File f); static void setPermission(File f, FsPermission permission
                                   ); static final File createLocalTempFile(final File basefile,
                                               final String prefix,
                                               final boolean isDeleteOnExit); static void replaceFile(File src, File target); static File[] listFiles(File dir); static String[] list(File dir); static String[] createJarWithClassPath(String inputClassPath, Path pwd,
      Map<String, String> callerEnv); static String[] createJarWithClassPath(String inputClassPath, Path pwd,
      Path targetDir,
      Map<String, String> callerEnv); static boolean compareFs(FileSystem srcFs, FileSystem destFs); static final int SYMLINK_NO_PRIVILEGE; }
@Test (timeout = 30000) public void testSymlink() throws Exception { Assert.assertFalse(del.exists()); del.mkdirs(); byte[] data = "testSymLink".getBytes(); File file = new File(del, FILE); File link = new File(del, "_link"); FileOutputStream os = new FileOutputStream(file); os.write(data); os.close(); FileUtil.symLink(file.getAbsolutePath(), link.getAbsolutePath()); Assert.assertEquals(data.length, file.length()); Assert.assertEquals(data.length, link.length()); FileInputStream in = new FileInputStream(link); long len = 0; while (in.read() > 0) { len++; } in.close(); Assert.assertEquals(data.length, len); }
@Test (timeout = 30000) public void testSymlinkRenameTo() throws Exception { Assert.assertFalse(del.exists()); del.mkdirs(); File file = new File(del, FILE); file.createNewFile(); File link = new File(del, "_link"); FileUtil.symLink(file.getAbsolutePath(), link.getAbsolutePath()); Assert.assertTrue(file.exists()); Assert.assertTrue(link.exists()); File link2 = new File(del, "_link2"); Assert.assertTrue(link.renameTo(link2)); Assert.assertTrue(file.exists()); Assert.assertTrue(link2.exists()); Assert.assertFalse(link.exists()); }
@Test (timeout = 30000) public void testSymlinkDelete() throws Exception { Assert.assertFalse(del.exists()); del.mkdirs(); File file = new File(del, FILE); file.createNewFile(); File link = new File(del, "_link"); FileUtil.symLink(file.getAbsolutePath(), link.getAbsolutePath()); Assert.assertTrue(file.exists()); Assert.assertTrue(link.exists()); Assert.assertTrue(link.delete()); Assert.assertFalse(link.exists()); Assert.assertTrue(file.exists()); }
@Test (timeout = 30000) public void testSymlinkLength() throws Exception { Assert.assertFalse(del.exists()); del.mkdirs(); byte[] data = "testSymLinkData".getBytes(); File file = new File(del, FILE); File link = new File(del, "_link"); FileOutputStream os = new FileOutputStream(file); os.write(data); os.close(); Assert.assertEquals(0, link.length()); FileUtil.symLink(file.getAbsolutePath(), link.getAbsolutePath()); Assert.assertEquals(data.length, file.length()); Assert.assertEquals(data.length, link.length()); file.delete(); Assert.assertFalse(file.exists()); Assert.assertEquals(0, link.length()); link.delete(); Assert.assertFalse(link.exists()); }