Created
December 19, 2017 22:46
-
-
Save boyter/44c45d9d587120e57f29841b34ca82dc to your computer and use it in GitHub Desktop.
Identify Path Walking Issues in Java without Link Follow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.nio.file.*; | |
import java.nio.file.attribute.BasicFileAttributes; | |
import java.util.EnumSet; | |
public class Main { | |
public static void main(String argv[]) { | |
Main main = new Main(); | |
if (argv.length != 1) { | |
main.printUsage(); | |
return; | |
} | |
try { | |
Path path = Paths.get(argv[0]); | |
main.walkPath(path); | |
} catch (Exception e) { | |
main.printUsage(); | |
} | |
} | |
public void printUsage() { | |
System.out.println("Usage: java Main TARGETDIRECTORY"); | |
} | |
public boolean ignoreFiles(String fileParent) { | |
if (fileParent.endsWith("/.git") || fileParent.contains("/.git/") || fileParent.contains(".git/") || fileParent.equals(".git")) { | |
return true; | |
} | |
if (fileParent.endsWith("/.svn") || fileParent.contains("/.svn/")) { | |
return true; | |
} | |
return false; | |
} | |
public void walkPath(Path path) { | |
try { | |
Files.walkFileTree(path, new SimpleFileVisitor<Path>() { | |
@Override | |
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { | |
Main main = new Main(); | |
if (main.ignoreFiles(file.toString())) { | |
return FileVisitResult.CONTINUE; | |
} | |
System.out.println(file); | |
return FileVisitResult.CONTINUE; | |
} | |
}); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment