Created
July 13, 2011 16:03
-
-
Save ArildF/1080619 to your computer and use it in GitHub Desktop.
my code so far
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
package FoldersPack; | |
import java.io.IOException; | |
import java.nio.file.*; | |
import java.nio.file.attribute.BasicFileAttributes; | |
import java.nio.file.attribute.FileAttribute; | |
import java.nio.file.attribute.PosixFileAttributes; | |
import java.nio.file.attribute.PosixFilePermission; | |
import java.nio.file.attribute.PosixFilePermissions; | |
import java.nio.file.spi.FileSystemProvider; | |
import java.util.Set; | |
import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List; | |
import static java.nio.file.FileVisitResult.*; | |
public class FolderMatch extends SimpleFileVisitor<Path> | |
{ | |
Path path1; | |
Path path2; | |
boolean isUNIXSystem; | |
boolean firstPass = true; | |
public FolderMatch(String path1, String path2) throws Exception | |
{ | |
this.path1 = Paths.get(path1); | |
this.path2 = Paths.get(path2); | |
if (!Files.exists(this.path1, LinkOption.NOFOLLOW_LINKS)) | |
{ | |
throw new Exception("Error:" + path1 + " not found."); | |
} | |
if (!Files.exists(this.path2, LinkOption.NOFOLLOW_LINKS)) | |
{ | |
throw new Exception("Error:" + path2 + " not found."); | |
} | |
isUNIXSystem = isUNIX(); | |
// On the first pass add any new files or directories that should be | |
// there | |
// in the second path | |
firstPass = true; | |
Files.walkFileTree(this.path1, this); | |
firstPass = false; | |
// on the second pass delete any files or directories that shouldn't be | |
// there | |
// in the second path | |
} | |
// Print each directory visited. | |
@Override | |
public FileVisitResult postVisitDirectory(Path dir, IOException exc) | |
{ | |
if (firstPass) | |
AddNewDirs(dir); | |
else | |
RemoveOldDirs(dir); | |
return CONTINUE; | |
} | |
private void AddNewDirs(Path dir) | |
{ | |
FileAttribute<Set<PosixFilePermission>> perms = null; | |
System.out.format("Directory: %s%n", dir); | |
// Get the file permission attributes of the dir | |
if (isUNIXSystem) | |
perms = getPermissions(dir); | |
// get the basic attributes | |
// FileAttribute attrs = Files.readAttributes(dir, | |
// dir.getFileSystem().), LinkOption.NOFOLLOW_LINKS) | |
// see if dir exists in second path | |
Path dirSecondPath = getNewPath(dir); | |
if (Files.exists(dirSecondPath, LinkOption.NOFOLLOW_LINKS)) | |
{ | |
// set attributes on second file to match the first | |
FileAttribute<Set<PosixFilePermission>> secondDirPerms = null; | |
if (isUNIXSystem) | |
{ | |
secondDirPerms = getPermissions(dirSecondPath); | |
if (secondDirPerms.value() != perms.value()) | |
{ | |
try | |
{ | |
Files.setPosixFilePermissions(dirSecondPath, | |
perms.value()); | |
} | |
catch (IOException e) | |
{ | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
} | |
return; | |
} | |
// Since dir doesn't exist in second path then create it | |
try | |
{ | |
if (isUNIXSystem) | |
Files.createDirectory(dirSecondPath, perms); | |
else | |
Files.createDirectory(dirSecondPath); | |
} | |
catch (IOException e) | |
{ | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
private void RemoveOldDirs(Path dir) | |
{ | |
// TODO Auto-generated method stub | |
} | |
// print each file visited | |
@Override | |
public FileVisitResult visitFile(Path file, BasicFileAttributes attr) | |
{ | |
if (attr.isSymbolicLink()) | |
{ | |
System.out.format("Symbolic link: %s ", file); | |
} else if (attr.isRegularFile()) | |
{ | |
System.out.format("Regular file: %s ", file); | |
} else | |
{ | |
System.out.format("Other: %s ", file); | |
} | |
System.out.println("(" + attr.size() + "bytes)"); | |
FileAttribute<Set<PosixFilePermission>> perms = null; | |
// Get the file permission attributes of the dir | |
if (isUNIXSystem) | |
perms = getPermissions(file); | |
return CONTINUE; | |
} | |
private FileAttribute<Set<PosixFilePermission>> getPermissions(Path p) | |
{ | |
// Get the file attributes of the dir | |
PosixFileAttributes attrs = null; | |
try | |
{ | |
attrs = Files.readAttributes(p, PosixFileAttributes.class); | |
} | |
catch (IOException e1) | |
{ | |
// TODO Auto-generated catch block | |
e1.printStackTrace(); | |
} | |
return PosixFilePermissions.asFileAttribute(attrs.permissions()); | |
} | |
private boolean isUNIX() | |
{ | |
FileSystem fs = FileSystems.getDefault(); | |
Set<String> supportedViews = fs.supportedFileAttributeViews(); | |
if (supportedViews.contains("unix")) | |
return true; | |
else | |
return false; | |
} | |
private Path getNewPath(Path p) | |
{ | |
Path newSecondPath = null; | |
if (!isUNIXSystem) | |
{ | |
// Have to fiddle with backslashes because replace doesn't like them | |
String firstPathBSfixed = path1.toString().replaceAll("\\\\", "/"); | |
String secondPathBSfixed = path2.toString().replaceAll("\\\\", "/"); | |
String dirPathBSFixed = p.toString().replaceAll("\\\\", "/"); | |
String dirSecondPathBSFixed = dirPathBSFixed.replaceFirst( | |
firstPathBSfixed, secondPathBSfixed); | |
// put the backslashes back and convert into a Path | |
dirSecondPathBSFixed = dirSecondPathBSFixed.replaceAll("/", "\\\\"); | |
newSecondPath = Paths.get(dirSecondPathBSFixed); | |
} else | |
{ | |
String dirSecondPathStr = p.toString().replaceFirst( | |
this.path1.toString(), this.path2.toString()); | |
newSecondPath = Paths.get(dirSecondPathStr); | |
} | |
return newSecondPath; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment