1 package org.gffs.filesystem;
4 import java.io.FileNotFoundException;
5 import java.nio.file.Files;
6 import java.nio.file.Path;
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
11 public class FileSystemHelper
13 private static Log _logger = LogFactory.getLog(FileSystemHelper.class);
16 * returns true if the path contains a symbolic link anywhere within it.
18 public static boolean pathContainsLink(String path) throws FileNotFoundException
20 // replace any backslashes with forward slashes.
21 path = path.replaceAll("\\+", "/");
23 // make sure path is absolute.
24 if (!path.startsWith("/")) {
25 String msg = "path passed in was not absolute: '" + path + "'";
27 throw new FileNotFoundException(msg);
30 // replace any double slashes with single ones.
31 path = path.replaceAll("//", "/");
32 String[] components = path.split("/");
34 String currentPath = ""; // never expected to be a link.
35 for (String component : components) {
36 currentPath = currentPath + "/" + component;
37 if (isFileSymLink(new File(currentPath))) {
44 * future: this could be more useful if it returned the position of the link as a component in path, but then we also need to accept a
45 * starting point for the link searching so they can find all of them.
50 * returns true if the path specified is actually a symbolic link.
52 public static boolean isFileSymLink(File path)
54 Path nioPath = path.toPath();
55 return Files.isSymbolicLink(nioPath);
59 * makes the string a more friendly filesystem path for java. this includes turning backslashes into forward slashes.
61 public static String sanitizeFilename(String toClean)
64 return toClean; // can't fix nothing.
65 // _logger.debug("before='" + toClean + "'");
67 String toReturn = toClean.replace('\\', '/');
69 // future: any other cleanings we should do on the path?
71 // _logger.debug("after='" + toReturn + "'");
75 static public void main(String[] args)
77 String uglyPath = "C:\\Program Files\\GenesisII\\";
78 String fixedPath = sanitizeFilename(uglyPath);
79 String expectedPath = "C:/Program Files/GenesisII/";
80 if (!fixedPath.equals(expectedPath)) {
81 System.err.println("FAILURE IN PARSING! result is not right: '" + fixedPath + "' when it should be '" + expectedPath);
84 System.err.println("parsing occurred as expected.");