FileUtil { public static int symLink(String target, String linkname) throws IOException{ if (target == null || linkname == null) { LOG.warn("Can not create a symLink with a target = " + target + " and link =" + linkname); return 1; } 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 void fullyDeleteOnExit(final File file); 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 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 makeSecureShellPath(File file); static String makeShellPath(File file, boolean makeCanonicalPath); static long getDU(File dir); static void unZip(InputStream inputStream, File toDir); static void unZip(File inFile, File unzipDir); static void unTar(InputStream inputStream, File untarDir,
                           boolean gzipped); 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 List<Path> getJarsInDirectory(String path); static List<Path> getJarsInDirectory(String path, boolean useLocal); static boolean compareFs(FileSystem srcFs, FileSystem destFs); static FileSystem write(final FileSystem fs, final Path path,
      final byte[] bytes); static FileContext write(final FileContext fileContext,
      final Path path, final byte[] bytes); static FileSystem write(final FileSystem fs, final Path path,
      final Iterable<? extends CharSequence> lines, final Charset cs); static FileContext write(final FileContext fileContext,
      final Path path, final Iterable<? extends CharSequence> lines,
      final Charset cs); static FileSystem write(final FileSystem fs, final Path path,
      final CharSequence charseq, final Charset cs); static FileContext write(final FileContext fs, final Path path,
      final CharSequence charseq, final Charset cs); static FileSystem write(final FileSystem fs, final Path path,
      final CharSequence charseq); static FileContext write(final FileContext fileContext,
      final Path path, final CharSequence charseq); static final int SYMLINK_NO_PRIVILEGE; }
@Test (timeout = 30000) public void testSymlink() throws Exception { 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 { 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 { 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 public void testSymlinkWithNullInput() throws IOException { File file = new File(del, FILE); File link = new File(del, "_link"); int result = FileUtil.symLink(null, null); Assert.assertEquals(1, result); result = FileUtil.symLink(file.getAbsolutePath(), null); Assert.assertEquals(1, result); result = FileUtil.symLink(null, link.getAbsolutePath()); Assert.assertEquals(1, result); }
@Test public void testSymlinkFileAlreadyExists() throws IOException { File file = new File(del, FILE); File link = new File(del, "_link"); int result1 = FileUtil.symLink(file.getAbsolutePath(), link.getAbsolutePath()); Assert.assertEquals(0, result1); result1 = FileUtil.symLink(file.getAbsolutePath(), link.getAbsolutePath()); Assert.assertEquals(1, result1); }
@Test public void testSymlinkSameFile() throws IOException { File file = new File(del, FILE); file.delete(); int result = FileUtil.symLink(file.getAbsolutePath(), file.getAbsolutePath()); Assert.assertEquals(0, result); }
@Test public void testSymlink2DifferentFile() throws IOException { File file = new File(del, FILE); File fileSecond = new File(del, FILE + "_1"); File link = new File(del, "_link"); int result = FileUtil.symLink(file.getAbsolutePath(), link.getAbsolutePath()); Assert.assertEquals(0, result); result = FileUtil.symLink(fileSecond.getAbsolutePath(), link.getAbsolutePath()); Assert.assertEquals(1, result); }
@Test public void testSymlink2DifferentLinks() throws IOException { File file = new File(del, FILE); File link = new File(del, "_link"); File linkSecond = new File(del, "_link_1"); int result = FileUtil.symLink(file.getAbsolutePath(), link.getAbsolutePath()); Assert.assertEquals(0, result); result = FileUtil.symLink(file.getAbsolutePath(), linkSecond.getAbsolutePath()); Assert.assertEquals(0, result); }