// copy source to target with verification static void copyAndVerify(Path source, Path target, CopyOption... options) throws IOException { Path result = copy(source, target, options); assertTrue(result == target); // get attributes of source and target file to verify copy boolean followLinks = true; LinkOption[] linkOptions = new LinkOption[0]; boolean copyAttributes = false; for (CopyOption opt : options) { if (opt == NOFOLLOW_LINKS) { followLinks = false; linkOptions = new LinkOption[] {NOFOLLOW_LINKS}; } if (opt == COPY_ATTRIBUTES) copyAttributes = true; } BasicFileAttributes basicAttributes = readAttributes(source, BasicFileAttributes.class, linkOptions); // check hash if regular file if (basicAttributes.isRegularFile()) assertTrue(computeHash(source) == computeHash(target)); // check link target if symbolic link if (basicAttributes.isSymbolicLink()) assert (readSymbolicLink(source).equals(readSymbolicLink(target))); // check that attributes are copied if (copyAttributes && followLinks) { checkBasicAttributes( basicAttributes, readAttributes(source, BasicFileAttributes.class, linkOptions)); // verify other attributes when same provider if (source.getFileSystem().provider() == target.getFileSystem().provider()) { // check POSIX attributes are copied String os = System.getProperty("os.name"); if ((os.equals("SunOS") || os.equals("Linux")) && testPosixAttributes) { checkPosixAttributes( readAttributes(source, PosixFileAttributes.class, linkOptions), readAttributes(target, PosixFileAttributes.class, linkOptions)); } // check DOS attributes are copied if (os.startsWith("Windows")) { checkDosAttributes( readAttributes(source, DosFileAttributes.class, linkOptions), readAttributes(target, DosFileAttributes.class, linkOptions)); } // check named attributes are copied if (followLinks && getFileStore(source).supportsFileAttributeView("xattr") && getFileStore(target).supportsFileAttributeView("xattr")) { checkUserDefinedFileAttributes( readUserDefinedFileAttributes(source), readUserDefinedFileAttributes(target)); } } } }
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (attrs.isRegularFile()) { if (m != null) m.reset(root.relativize(file).toString()); if (m == null || m.matches()) futures.add(exec.submit(new FileLoader(root, file, blocSize))); } return FileVisitResult.CONTINUE; }
static void checkBasicAttributes(BasicFileAttributes attrs1, BasicFileAttributes attrs2) { // check file type assertTrue(attrs1.isRegularFile() == attrs2.isRegularFile()); assertTrue(attrs1.isDirectory() == attrs2.isDirectory()); assertTrue(attrs1.isSymbolicLink() == attrs2.isSymbolicLink()); assertTrue(attrs1.isOther() == attrs2.isOther()); // check last modified time if not a symbolic link if (!attrs1.isSymbolicLink()) { long time1 = attrs1.lastModifiedTime().to(TimeUnit.SECONDS); long time2 = attrs2.lastModifiedTime().to(TimeUnit.SECONDS); if (time1 != time2) { System.err.format("File time for %s is %s\n", attrs1.fileKey(), attrs1.lastModifiedTime()); System.err.format("File time for %s is %s\n", attrs2.fileKey(), attrs2.lastModifiedTime()); assertTrue(false); } } // check size if (attrs1.isRegularFile()) assertTrue(attrs1.size() == attrs2.size()); }
// move source to target with verification static void moveAndVerify(Path source, Path target, CopyOption... options) throws IOException { // read attributes before file is moved BasicFileAttributes basicAttributes = null; PosixFileAttributes posixAttributes = null; DosFileAttributes dosAttributes = null; Map<String, ByteBuffer> namedAttributes = null; // get file attributes of source file String os = System.getProperty("os.name"); if (os.startsWith("Windows")) { dosAttributes = readAttributes(source, DosFileAttributes.class, NOFOLLOW_LINKS); basicAttributes = dosAttributes; } else { posixAttributes = readAttributes(source, PosixFileAttributes.class, NOFOLLOW_LINKS); basicAttributes = posixAttributes; } if (basicAttributes == null) basicAttributes = readAttributes(source, BasicFileAttributes.class, NOFOLLOW_LINKS); // hash file contents if regular file int hash = (basicAttributes.isRegularFile()) ? computeHash(source) : 0; // record link target if symbolic link Path linkTarget = null; if (basicAttributes.isSymbolicLink()) linkTarget = readSymbolicLink(source); // read named attributes if available (and file is not a sym link) if (!basicAttributes.isSymbolicLink() && getFileStore(source).supportsFileAttributeView("xattr")) { namedAttributes = readUserDefinedFileAttributes(source); } // move file Path result = move(source, target, options); assertTrue(result == target); // verify source does not exist assertTrue(notExists(source)); // verify file contents if (basicAttributes.isRegularFile()) { if (computeHash(target) != hash) throw new RuntimeException("Failed to verify move of regular file"); } // verify link target if (basicAttributes.isSymbolicLink()) { if (!readSymbolicLink(target).equals(linkTarget)) throw new RuntimeException("Failed to verify move of symbolic link"); } // verify basic attributes checkBasicAttributes( basicAttributes, readAttributes(target, BasicFileAttributes.class, NOFOLLOW_LINKS)); // verify other attributes when same provider if (source.getFileSystem().provider() == target.getFileSystem().provider()) { // verify POSIX attributes if (posixAttributes != null && !basicAttributes.isSymbolicLink() && testPosixAttributes) { checkPosixAttributes( posixAttributes, readAttributes(target, PosixFileAttributes.class, NOFOLLOW_LINKS)); } // verify DOS attributes if (dosAttributes != null && !basicAttributes.isSymbolicLink()) { DosFileAttributes attrs = readAttributes(target, DosFileAttributes.class, NOFOLLOW_LINKS); checkDosAttributes(dosAttributes, attrs); } // verify named attributes if (namedAttributes != null && getFileStore(target).supportsFileAttributeView("xattr")) { checkUserDefinedFileAttributes(namedAttributes, readUserDefinedFileAttributes(target)); } } }
static void checkBasicAttributes(BasicFileAttributes attrs1, BasicFileAttributes attrs2) { // check file type assertTrue(attrs1.isRegularFile() == attrs2.isRegularFile()); assertTrue(attrs1.isDirectory() == attrs2.isDirectory()); assertTrue(attrs1.isSymbolicLink() == attrs2.isSymbolicLink()); assertTrue(attrs1.isOther() == attrs2.isOther()); // check last modified time long time1 = attrs1.lastModifiedTime().toMillis(); long time2 = attrs2.lastModifiedTime().toMillis(); assertTrue(time1 == time2); // check size if (attrs1.isRegularFile()) assertTrue(attrs1.size() == attrs2.size()); }