@Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { // Don't delete the cache directory itself. if (!Files.isSameFile(dir, cacheDirectory)) { Files.deleteIfExists(dir); } return FileVisitResult.CONTINUE; } });
/** * Create a symbolic link pointing at target * * @param link the link to create * @param target where it should point to * @throws IOException on any error. */ @Override public void createSymlink(File link, File target) throws IOException { if (_symlinksDisabled) { throw new IOException("Symlinks have been disabled, this should not be called"); } Path plink = link.toPath().toAbsolutePath(); Path ptarget = target.toPath().toAbsolutePath(); LOG.debug("Creating symlink [{}] to [{}]", plink, ptarget); if (Files.exists(plink)) { if (Files.isSameFile(plink, ptarget)) { //It already points where we want it to return; } FileUtils.forceDelete(link); } Files.createSymbolicLink(plink, ptarget); }
Path installModPath = installPath.resolve(MOD_DIR); Files.createDirectories(installModPath); if (Files.isSameFile(homeModPath, installModPath)) { modPaths = ImmutableList.of(homeModPath); } else {
/** * Returns true if the files located by the given paths exist, are not directories, and contain * the same bytes. * * @throws IOException if an I/O error occurs * @since 22.0 */ public static boolean equal(Path path1, Path path2) throws IOException { checkNotNull(path1); checkNotNull(path2); if (Files.isSameFile(path1, path2)) { return true; } /* * Some operating systems may return zero as the length for files denoting system-dependent * entities such as devices or pipes, in which case we must fall back on comparing the bytes * directly. */ ByteSource source1 = asByteSource(path1); ByteSource source2 = asByteSource(path2); long len1 = source1.sizeIfKnown().or(0L); long len2 = source2.sizeIfKnown().or(0L); if (len1 != 0 && len2 != 0 && len1 != len2) { return false; } return source1.contentEquals(source2); }
/** * 检查两个文件是否是同一个文件<br> * 所谓文件相同,是指File对象是否指向同一个文件或文件夹 * * @param file1 文件1 * @param file2 文件2 * @return 是否相同 * @throws IORuntimeException IO异常 * @see Files#isSameFile(Path, Path) */ public static boolean equals(File file1, File file2) throws IORuntimeException { Assert.notNull(file1); Assert.notNull(file2); if (false == file1.exists() || false == file2.exists()) { // 两个文件都不存在判断其路径是否相同 if (false == file1.exists() && false == file2.exists() && pathEquals(file1, file2)) { return true; } // 对于一个存在一个不存在的情况,一定不相同 return false; } try { return Files.isSameFile(file1.toPath(), file2.toPath()); } catch (IOException e) { throw new IORuntimeException(e); } }
/** * 检查两个文件是否是同一个文件<br> * 所谓文件相同,是指File对象是否指向同一个文件或文件夹 * * @param file1 文件1 * @param file2 文件2 * @return 是否相同 * @throws IORuntimeException IO异常 * @see Files#isSameFile(Path, Path) */ public static boolean equals(File file1, File file2) throws IORuntimeException { Assert.notNull(file1); Assert.notNull(file2); if (false == file1.exists() || false == file2.exists()) { // 两个文件都不存在判断其路径是否相同 if (false == file1.exists() && false == file2.exists() && pathEquals(file1, file2)) { return true; } // 对于一个存在一个不存在的情况,一定不相同 return false; } try { return Files.isSameFile(file1.toPath(), file2.toPath()); } catch (IOException e) { throw new IORuntimeException(e); } }
/** * Returns true if the files located by the given paths exist, are not directories, and contain * the same bytes. * * @throws IOException if an I/O error occurs * @since 22.0 */ public static boolean equal(Path path1, Path path2) throws IOException { checkNotNull(path1); checkNotNull(path2); if (Files.isSameFile(path1, path2)) { return true; } /* * Some operating systems may return zero as the length for files denoting system-dependent * entities such as devices or pipes, in which case we must fall back on comparing the bytes * directly. */ ByteSource source1 = asByteSource(path1); ByteSource source2 = asByteSource(path2); long len1 = source1.sizeIfKnown().or(0L); long len2 = source2.sizeIfKnown().or(0L); if (len1 != 0 && len2 != 0 && len1 != len2) { return false; } return source1.contentEquals(source2); }
if (!Files.isSameFile(path0, path1)) throw new IgniteException("Failed to set IGNITE_HOME after it has been already resolved " + "[igniteHome=" + path0 + ", newIgniteHome=" + path1 + ']');
/** * Returns true if the files located by the given paths exist, are not directories, and contain * the same bytes. * * @throws IOException if an I/O error occurs * @since 22.0 */ public static boolean equal(Path path1, Path path2) throws IOException { checkNotNull(path1); checkNotNull(path2); if (Files.isSameFile(path1, path2)) { return true; } /* * Some operating systems may return zero as the length for files denoting system-dependent * entities such as devices or pipes, in which case we must fall back on comparing the bytes * directly. */ ByteSource source1 = asByteSource(path1); ByteSource source2 = asByteSource(path2); long len1 = source1.sizeIfKnown().or(0L); long len2 = source2.sizeIfKnown().or(0L); if (len1 != 0 && len2 != 0 && len1 != len2) { return false; } return source1.contentEquals(source2); }
if (!Files.isSameFile(given, Paths.get(wsRoot))) { String linkedTo = Files.readSymbolicLink(given).toRealPath().toString(); if (linkedTo.regionMatches(0, wsRoot, 0, wsRoot.length())) {
@Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { if (Files.isSameFile(dir, root)) { return FileVisitResult.CONTINUE; } final Path relativePath = root.relativize(dir); if (exclude(ignoreMatchers, relativePath)) { return FileVisitResult.CONTINUE; } final TarArchiveEntry entry = new TarArchiveEntry(dir.toFile()); entry.setName(relativePath.toString()); entry.setMode(getFileMode(dir)); tarStream.putArchiveEntry(entry); tarStream.closeArchiveEntry(); return FileVisitResult.CONTINUE; }
public void testEqual() throws IOException { try (FileSystem fs = Jimfs.newFileSystem(Configuration.unix())) { Path fooPath = fs.getPath("foo"); Path barPath = fs.getPath("bar"); MoreFiles.asCharSink(fooPath, UTF_8).write("foo"); MoreFiles.asCharSink(barPath, UTF_8).write("barbar"); assertThat(MoreFiles.equal(fooPath, barPath)).isFalse(); assertThat(MoreFiles.equal(fooPath, fooPath)).isTrue(); assertThat(MoreFiles.asByteSource(fooPath).contentEquals(MoreFiles.asByteSource(fooPath))) .isTrue(); Path fooCopy = Files.copy(fooPath, fs.getPath("fooCopy")); assertThat(Files.isSameFile(fooPath, fooCopy)).isFalse(); assertThat(MoreFiles.equal(fooPath, fooCopy)).isTrue(); MoreFiles.asCharSink(fooCopy, UTF_8).write("boo"); assertThat(MoreFiles.asByteSource(fooPath).size()) .isEqualTo(MoreFiles.asByteSource(fooCopy).size()); assertThat(MoreFiles.equal(fooPath, fooCopy)).isFalse(); // should also assert that a Path that erroneously reports a size 0 can still be compared, // not sure how to do that with the Path API } }
/** * Returns true if the files located by the given paths exist, are not directories, and contain * the same bytes. * * @throws IOException if an I/O error occurs * @since 22.0 */ public static boolean equal(Path path1, Path path2) throws IOException { checkNotNull(path1); checkNotNull(path2); if (Files.isSameFile(path1, path2)) { return true; } /* * Some operating systems may return zero as the length for files denoting system-dependent * entities such as devices or pipes, in which case we must fall back on comparing the bytes * directly. */ ByteSource source1 = asByteSource(path1); ByteSource source2 = asByteSource(path2); long len1 = source1.sizeIfKnown().or(0L); long len2 = source2.sizeIfKnown().or(0L); if (len1 != 0 && len2 != 0 && len1 != len2) { return false; } return source1.contentEquals(source2); }
@Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { if (Files.isSameFile(dir, basePath)) { finishWalk(); } return FileVisitResult.CONTINUE; }
@Override public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { log.error("Error occured at {}: {}", file, exc.getMessage(), exc); if (basePath!=null && Files.isSameFile(file, basePath)) { finishWalk(); } return FileVisitResult.CONTINUE; }
@Test public void testIsSameFileOnEqualPath() throws IOException { Path file = getNonExistingPath(); assertThat( Files.isSameFile( file, file ) ).isTrue(); }
@Test @Category({ CaseInsensitive.class, Writable.class }) public void testCaseInsensitivePathsPointToSameFile() throws IOException { Path file = dirTA().resolve( nameD() ); // create file where last filename is mixed case Files.write( mixCase( file ), CONTENT ); assertThat( Files.isSameFile( file, mixCase( file ) ) ).isTrue(); }
@Test @Category( { SymLink.class } ) public void testMoveARelSymLink2() throws IOException { Files.createSymbolicLink( symLink(), FS.getPath( ".." ).resolve( nameA() ) ); Path to = dirTB().resolve( nameB() ); Files.move( symLink(), to ); Files.write( to, CONTENT ); assertThat( Files.isSameFile( to, absTA() ) ).isTrue(); }
private void assertFilesEquals(String s1, String s2) throws IOException { File f1 = new File(s1); File f2 = new File(s2); assertTrue(Files.isSameFile(f1.toPath(), f2.toPath())); } }
@Test @Category( { SymLink.class, DirSymLink.class } ) public void testDeepSymLinkIsSameFile() throws IOException { Files.createSymbolicLink( symLink(), targetDir() ); Files.write( linkKid(), CONTENT ); assertThat( Files.isSameFile( linkKid(), targetDir().resolve( linkKid().getFileName() ) ) ).isTrue(); }