public boolean isSameFile(PathFileObject other) {
   try {
     return Files.isSameFile(path, other.path);
   } catch (IOException e) {
     return false;
   }
 }
Ejemplo n.º 2
0
  public String normalizePath(Path path) {
    for (PathAttribute attr : attributes) {
      if (attr.path == null) continue;

      try {
        if (path.startsWith(attr.path)
            || path.equals(attr.path)
            || Files.isSameFile(path, attr.path)) {
          return URIUtil.addPaths("${" + attr.key + "}", attr.path.relativize(path).toString());
        }
      } catch (IOException ignore) {
        LOG.ignore(ignore);
      }
    }

    return path.toString();
  }
Ejemplo n.º 3
0
  /**
   * Delete older logs from disk.
   *
   * @param time The minimum age of the logs.
   */
  public void recycle(long time) {
    List<Path> toRecycle = new ArrayList<>();

    // Check if the log directory exists. If it doesn't
    // then there isn't anything to recycle.
    Path logDir = Paths.get(db.getLogPath()).toAbsolutePath();
    if (!Files.exists(logDir)) {
      return;
    }

    try {
      DirectoryStream<Path> stream = Files.newDirectoryStream(logDir, "log_*");

      for (Path entry : stream) {
        // System.out.printf("investigating log %s\n", entry);
        // Do not include the current block.
        if (logPath == null || !Files.isSameFile(entry, logPath)) {
          BasicFileAttributes view =
              Files.getFileAttributeView(entry, BasicFileAttributeView.class).readAttributes();

          if (view.lastModifiedTime().toMillis() < time) {
            // We no longer need this entry. Add to our
            // recycling list.
            toRecycle.add(entry);
          }
        }
      }

      // Now delete all the values.
      for (Path p : toRecycle) {
        Files.delete(p);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Ejemplo n.º 4
0
  public static void main(String[] args) throws IOException {
    Path path1 = Paths.get("Test");
    Path path2 = Paths.get("D:\\OCPJP7\\programs\\NIO2\\Test");

    System.out.println("Files.isSameFile(path1, path2) is: " + Files.isSameFile(path1, path2));
  }
Ejemplo n.º 5
0
  @Override
  public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
    Path relFilePath = m_srcRoot.relativize(file);

    if (isSrcIgnored(relFilePath)) {
      m_progress.reportProgress(CloneProgressReporter.EventType.FILE_SKIP_IGNORE, file, null, null);
      return FileVisitResult.CONTINUE;
    }

    Path newPathCandidate = m_destRoot.resolve(relFilePath);
    Iterator<FileHandler> it = m_fileHandlers.iterator();
    while (it.hasNext()) {
      FileHandler handler = it.next();
      try {
        Path newPath = handler.canHandleFile(file, attrs, newPathCandidate);
        if (newPath != null) {
          // record the visited file even if we don't process it
          recordVisitedFile(file, newPath);

          // does the new path exist
          if (Files.exists(newPath, LinkOption.NOFOLLOW_LINKS)) {
            // if it's the same file, then skip
            if (Files.isSameFile(file, newPath)) {
              m_progress.reportProgress(
                  CloneProgressReporter.EventType.FILE_SKIP_SAME, file, newPath, null);
            } else { // process file
              BasicFileAttributes targetAttrs =
                  Files.readAttributes(
                      newPath, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
              if (targetAttrs.lastModifiedTime().compareTo(attrs.lastModifiedTime()) < 0) {
                m_progress.reportProgress(
                    CloneProgressReporter.EventType.FILE_UPDATE_EXISTING_BEGIN,
                    file,
                    newPath,
                    null);
                handler.processFile(file, attrs, newPathCandidate, m_runType);
                m_progress.reportProgress(
                    CloneProgressReporter.EventType.FILE_UPDATE_EXISTING_END, file, newPath, null);
              } else {
                m_progress.reportProgress(
                    CloneProgressReporter.EventType.FILE_SKIP_NEWER, file, newPath, null);
              }
            }
          } else { // otherwise process it
            m_progress.reportProgress(
                CloneProgressReporter.EventType.FILE_CREATE_NEW_BEGIN, file, newPath, null);
            handler.processFile(file, attrs, newPathCandidate, m_runType);
            m_progress.reportProgress(
                CloneProgressReporter.EventType.FILE_CREATE_NEW_END, file, newPath, null);
          }

          // file has been handled
          break;
        }
      } catch (FileCloningError e) {
        m_progress.reportProgress(CloneProgressReporter.EventType.ERROR, file, null, e);
        // TODO: should we continue and try next handler in list?
      }
    }
    return FileVisitResult.CONTINUE;
  }