@Override
  public void copyFrom(Directory from, String srcFile, String destFile, IOContext context)
      throws IOException {
    final Directory fromUnwrapped = FilterDirectory.unwrap(from);
    final Directory toUnwrapped = FilterDirectory.unwrap(this);
    // try to unwrap to FSDirectory - we might be able to just create hard-links of these files and
    // save copying
    // the entire file.
    Exception suppressedException = null;
    boolean tryCopy = true;
    if (fromUnwrapped instanceof FSDirectory && toUnwrapped instanceof FSDirectory) {
      final Path fromPath = ((FSDirectory) fromUnwrapped).getDirectory();
      final Path toPath = ((FSDirectory) toUnwrapped).getDirectory();

      if (Files.isReadable(fromPath.resolve(srcFile)) && Files.isWritable(toPath)) {
        // only try hardlinks if we have permission to access the files
        // if not super.copyFrom() will give us the right exceptions
        suppressedException =
            AccessController.doPrivileged(
                (PrivilegedAction<Exception>)
                    () -> {
                      try {
                        Files.createLink(toPath.resolve(destFile), fromPath.resolve(srcFile));
                      } catch (FileNotFoundException
                          | NoSuchFileException
                          | FileAlreadyExistsException ex) {
                        return ex; // in these cases we bubble up since it's a true error condition.
                      } catch (IOException
                          | UnsupportedOperationException // if the FS doesn't support hard-links
                          | SecurityException
                              ex // we don't have permission to use hard-links just fall back to
                                 // byte copy
                      ) {
                        // hard-links are not supported or the files are on different filesystems
                        // we could go deeper and check if their filesstores are the same and opt
                        // out earlier but for now we just fall back to normal file-copy
                        return ex;
                      }
                      return null;
                    });
        tryCopy = suppressedException != null;
      }
    }
    if (tryCopy) {
      try {
        super.copyFrom(from, srcFile, destFile, context);
      } catch (Exception ex) {
        if (suppressedException != null) {
          ex.addSuppressed(suppressedException);
        }
        throw ex;
      }
    }
  }
Esempio n. 2
0
 /**
  * Verifies that the specified directory to which the stream will be saved exists and is writable.
  *
  * @param uploadDir Directory where stream will be saved
  * @throws IOException
  */
 private void validateUploadDir(Path uploadDir) throws IOException {
   if (!Files.isDirectory(uploadDir)) {
     throw new IOException(format("Upload directory {0} doesn''t exist.", uploadDir.toString()));
   }
   if (!Files.isWritable(uploadDir)) {
     throw new IOException(
         format("Upload directory {0} isn''t writable. Check permissions.", uploadDir.toString()));
   }
 }
 public static void main(String[] args) throws IOException {
   Path path = Paths.get(FileService.FILE_WITH_LINES);
   System.out.println("Files.isDirectory = " + Files.isDirectory(path));
   System.out.println("Files.isExecutable = " + Files.isExecutable(path));
   System.out.println("Files.isHidden = " + Files.isHidden(path));
   System.out.println("Files.isReadable = " + Files.isReadable(path));
   System.out.println("Files.isWritable = " + Files.isWritable(path));
   System.out.println("Files.getLastModifiedTime = " + Files.getLastModifiedTime(path));
   System.out.println("Files.getOwner = " + Files.getOwner(path));
   System.out.println("Files.size = " + Files.size(path));
   // change attribute: Files.setAttribute(path, "dos:hidden", false);
 }
Esempio n. 4
0
 private static void initAppDir(String appDir) {
   Path dir = Paths.get(appDir);
   if (Files.exists(dir)) {
     if (!Files.isWritable(dir))
       throw new BitsquareException("Application data directory '%s' is not writeable", dir);
     else return;
   }
   try {
     Files.createDirectory(dir);
   } catch (IOException ex) {
     throw new BitsquareException(ex, "Application data directory '%s' could not be created", dir);
   }
 }
 /**
  * Execute a single filesystem test against the given path.
  *
  * @param path The {@code Path} to test
  * @param test The character code of the test to perform.
  * @return {@code null} if the test passes, or a {@code FileSystemTestFailure} if it fails.
  */
 protected static FileSystemTestFailure testPath(final Path path, char test) {
   switch (test) {
     case 'e':
       return (Files.exists(path))
           ? null
           : new FileSystemTestFailure(path, test, "The path does not exist");
     case 'f':
       return (Files.isRegularFile(path))
           ? null
           : new FileSystemTestFailure(path, test, "The path is not a file");
     case 'd':
       return (Files.isDirectory(path))
           ? null
           : new FileSystemTestFailure(path, test, "The path is not a directory");
     case 'z':
       try {
         if (!Files.isRegularFile(path))
           return new FileSystemTestFailure(path, test, "The path is not a file");
         return (Files.size(path) < 1)
             ? null
             : new FileSystemTestFailure(path, test, "The path is not zero length");
       } catch (IOException ioe) {
         return new FileSystemTestFailure(path, test, "The path could not be read");
       }
     case 's':
       try {
         if (!Files.isRegularFile(path))
           return new FileSystemTestFailure(path, test, "The path is not a file");
         return (Files.size(path) > 0)
             ? null
             : new FileSystemTestFailure(path, test, "The path is a zero length file");
       } catch (IOException ioe) {
         return new FileSystemTestFailure(path, test, "The path could not be read");
       }
     case 'r':
       return (Files.isReadable(path))
           ? null
           : new FileSystemTestFailure(path, test, "The path is not readable");
     case 'w':
       return (Files.isWritable(path))
           ? null
           : new FileSystemTestFailure(path, test, "The path is not writable");
     case 'x':
       return (Files.isExecutable(path))
           ? null
           : new FileSystemTestFailure(path, test, "The path is not executable");
     default:
       throw new IllegalArgumentException("Unrecognized path test: " + test);
   }
 }
Esempio n. 6
0
  @Test
  public void pathUtilTest() throws IOException {

    LOG.info(Files.isDirectory(Paths.get("/tmp/test")) == true ? "True " : "False");
    LOG.info(Files.exists(Paths.get("/tmp/test")) == true ? "True " : "False");
    LOG.info(Files.isWritable(Paths.get("/tmp/test").getParent()) == true ? "True " : "False");

    try {
      LOG.info(CrawlerUtils.getResourcePath("config"));
      LOG.info(CrawlerUtils.getResourcePath(""));
      LOG.info(CrawlerUtils.getCurrentBuildPath());
    } catch (Exception e) {
      LOG.debug(e.toString());
    }
  }
Esempio n. 7
0
 private void intialize() throws PAPException, IOException {
   //
   // Sanity check the repository path
   //
   if (this.repository == null) {
     throw new PAPException("No repository specified.");
   }
   if (Files.notExists(this.repository)) {
     Files.createDirectory(repository);
   }
   if (!Files.isDirectory(this.repository)) {
     throw new PAPException("Repository is NOT a directory: " + this.repository.toAbsolutePath());
   }
   if (!Files.isWritable(this.repository)) {
     throw new PAPException("Repository is NOT writable: " + this.repository.toAbsolutePath());
   }
   //
   // Load our groups
   //
   this.loadGroups();
 }
Esempio n. 8
0
 private void checkPathValidity(Path path, String name)
     throws FileSystemException, NotDirectoryException, FileAlreadyExistsException {
   if (!Files.isWritable(path)) {
     System.out.println(
         "That path doesn't seem to be writable :("
             + LINE_SEPARATOR
             + "Check if you have write permission to that path and try again.");
     throw new java.nio.file.FileSystemException(path.toString());
   }
   if (Files.exists(path.resolve(name))) {
     System.out.println(
         "Aw shucks! It seems like there is already a file of that name at that path :("
             + LINE_SEPARATOR
             + "Try again with another name.");
     throw new FileAlreadyExistsException(path.resolve(name).toString());
   }
   if (!Files.isDirectory(path)) {
     System.out.println(
         "Aw, man. That path does not seem to be a valid directory :("
             + LINE_SEPARATOR
             + "Try with another path again.");
     throw new java.nio.file.NotDirectoryException(path.toString());
   }
 }
 public final boolean apply(@Nonnull Path path) {
   return Files.isWritable(path);
 }
Esempio n. 10
0
 public boolean canWrite() {
   return Files.isWritable(newFile);
 }