示例#1
0
 private BlockingAction<Void> moveInternal(
     String from, String to, Handler<AsyncResult<Void>> handler) {
   final Path source = PathAdjuster.adjust(vertx, Paths.get(from));
   final Path target = PathAdjuster.adjust(vertx, Paths.get(to));
   return new BlockingAction<Void>(handler) {
     public Void perform() {
       try {
         Files.move(source, target);
       } catch (IOException e) {
         throw new FileSystemException(e);
       }
       return null;
     }
   };
 }
示例#2
0
  protected BlockingAction<Void> chownInternal(
      String path, final String user, final String group, Handler<AsyncResult<Void>> handler) {
    final Path target = PathAdjuster.adjust(vertx, Paths.get(path));
    final UserPrincipalLookupService service =
        target.getFileSystem().getUserPrincipalLookupService();
    return new BlockingAction<Void>(handler) {
      public Void perform() {

        try {
          final UserPrincipal userPrincipal =
              user == null ? null : service.lookupPrincipalByName(user);
          final GroupPrincipal groupPrincipal =
              group == null ? null : service.lookupPrincipalByGroupName(group);
          if (groupPrincipal != null) {
            PosixFileAttributeView view =
                Files.getFileAttributeView(
                    target, PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
            if (view == null) {
              throw new FileSystemException("Change group of file not supported");
            }
            view.setGroup(groupPrincipal);
          }
          if (userPrincipal != null) {
            Files.setOwner(target, userPrincipal);
          }
        } catch (SecurityException e) {
          throw new FileSystemException("Accessed denied for chown on " + target);
        } catch (IOException e) {
          throw new FileSystemException(e);
        }
        return null;
      }
    };
  }
示例#3
0
 private BlockingAction<Void> truncateInternal(
     String p, final long len, Handler<AsyncResult<Void>> handler) {
   final String path = PathAdjuster.adjust(vertx, p);
   return new BlockingAction<Void>(handler) {
     public Void perform() {
       if (len < 0) {
         throw new FileSystemException("Cannot truncate file to size < 0");
       }
       if (!Files.exists(Paths.get(path))) {
         throw new FileSystemException("Cannot truncate file " + path + ". Does not exist");
       }
       RandomAccessFile raf = null;
       try {
         try {
           raf = new RandomAccessFile(path, "rw");
           raf.setLength(len);
         } finally {
           if (raf != null) raf.close();
         }
       } catch (IOException e) {
         throw new FileSystemException(e);
       }
       return null;
     }
   };
 }
示例#4
0
 protected BlockingAction<Void> mkdirInternal(
     String path,
     final String perms,
     final boolean createParents,
     Handler<AsyncResult<Void>> handler) {
   final Path source = PathAdjuster.adjust(vertx, Paths.get(path));
   final FileAttribute<?> attrs =
       perms == null
           ? null
           : PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString(perms));
   return new BlockingAction<Void>(handler) {
     public Void perform() {
       try {
         if (createParents) {
           if (attrs != null) {
             Files.createDirectories(source, attrs);
           } else {
             Files.createDirectories(source);
           }
         } else {
           if (attrs != null) {
             Files.createDirectory(source, attrs);
           } else {
             Files.createDirectory(source);
           }
         }
       } catch (IOException e) {
         throw new FileSystemException(e);
       }
       return null;
     }
   };
 }
示例#5
0
 private BlockingAction<Boolean> existsInternal(
     String path, Handler<AsyncResult<Boolean>> handler) {
   final File file = new File(PathAdjuster.adjust(vertx, path));
   return new BlockingAction<Boolean>(handler) {
     public Boolean perform() {
       return file.exists();
     }
   };
 }
示例#6
0
 private BlockingAction<Void> link(
     String link, String existing, final boolean symbolic, Handler<AsyncResult<Void>> handler) {
   final Path source = PathAdjuster.adjust(vertx, Paths.get(link));
   final Path target = PathAdjuster.adjust(vertx, Paths.get(existing));
   return new BlockingAction<Void>(handler) {
     public Void perform() {
       try {
         if (symbolic) {
           Files.createSymbolicLink(source, target);
         } else {
           Files.createLink(source, target);
         }
       } catch (IOException e) {
         throw new FileSystemException(e);
       }
       return null;
     }
   };
 }
示例#7
0
 private BlockingAction<AsyncFile> openInternal(
     String p, OpenOptions options, Handler<AsyncResult<AsyncFile>> handler) {
   Objects.requireNonNull(options, "no null options accepted");
   final String path = PathAdjuster.adjust(vertx, p);
   return new BlockingAction<AsyncFile>(handler) {
     public AsyncFile perform() {
       return doOpen(path, options, context);
     }
   };
 }
示例#8
0
  private BlockingAction<Void> copyInternal(
      String from, String to, final boolean recursive, Handler<AsyncResult<Void>> handler) {
    final Path source = PathAdjuster.adjust(vertx, Paths.get(from));
    final Path target = PathAdjuster.adjust(vertx, Paths.get(to));
    return new BlockingAction<Void>(handler) {
      public Void perform() {
        try {
          if (recursive) {
            Files.walkFileTree(
                source,
                EnumSet.of(FileVisitOption.FOLLOW_LINKS),
                Integer.MAX_VALUE,
                new SimpleFileVisitor<Path>() {
                  public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
                      throws IOException {
                    Path targetDir = target.resolve(source.relativize(dir));
                    try {
                      Files.copy(dir, targetDir);
                    } catch (FileAlreadyExistsException e) {
                      if (!Files.isDirectory(targetDir)) {
                        throw e;
                      }
                    }
                    return FileVisitResult.CONTINUE;
                  }

                  @Override
                  public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
                      throws IOException {
                    Files.copy(file, target.resolve(source.relativize(file)));
                    return FileVisitResult.CONTINUE;
                  }
                });
          } else {
            Files.copy(source, target);
          }
        } catch (IOException e) {
          throw new FileSystemException(e);
        }
        return null;
      }
    };
  }
示例#9
0
 private BlockingAction<String> readSymlinkInternal(
     String link, Handler<AsyncResult<String>> handler) {
   final Path source = PathAdjuster.adjust(vertx, Paths.get(link));
   return new BlockingAction<String>(handler) {
     public String perform() {
       try {
         return Files.readSymbolicLink(source).toString();
       } catch (IOException e) {
         throw new FileSystemException(e);
       }
     }
   };
 }
示例#10
0
 private BlockingAction<FileSystemProps> fsPropsInternal(
     String path, Handler<AsyncResult<FileSystemProps>> handler) {
   final Path target = PathAdjuster.adjust(vertx, Paths.get(path));
   return new BlockingAction<FileSystemProps>(handler) {
     public FileSystemProps perform() {
       try {
         FileStore fs = Files.getFileStore(target);
         return new FileSystemPropsImpl(
             fs.getTotalSpace(), fs.getUnallocatedSpace(), fs.getUsableSpace());
       } catch (IOException e) {
         throw new FileSystemException(e);
       }
     }
   };
 }
示例#11
0
 private BlockingAction<Void> writeFileInternal(
     String path, final Buffer data, Handler<AsyncResult<Void>> handler) {
   Objects.requireNonNull(data, "no null data accepted");
   final Path target = PathAdjuster.adjust(vertx, Paths.get(path));
   return new BlockingAction<Void>(handler) {
     public Void perform() {
       try {
         Files.write(target, data.getBytes());
         return null;
       } catch (IOException e) {
         throw new FileSystemException(e);
       }
     }
   };
 }
示例#12
0
 private BlockingAction<Buffer> readFileInternal(
     String path, Handler<AsyncResult<Buffer>> handler) {
   final Path target = PathAdjuster.adjust(vertx, Paths.get(path));
   return new BlockingAction<Buffer>(handler) {
     public Buffer perform() {
       try {
         byte[] bytes = Files.readAllBytes(target);
         Buffer buff = Buffer.buffer(bytes);
         return buff;
       } catch (IOException e) {
         throw new FileSystemException(e);
       }
     }
   };
 }
示例#13
0
 private BlockingAction<List<String>> readDirInternal(
     String p, final String filter, Handler<AsyncResult<List<String>>> handler) {
   final String path = PathAdjuster.adjust(vertx, p);
   return new BlockingAction<List<String>>(handler) {
     public List<String> perform() {
       try {
         File file = new File(path);
         if (!file.exists()) {
           throw new FileSystemException("Cannot read directory " + path + ". Does not exist");
         }
         if (!file.isDirectory()) {
           throw new FileSystemException(
               "Cannot read directory " + path + ". It's not a directory");
         } else {
           FilenameFilter fnFilter;
           if (filter != null) {
             fnFilter =
                 new FilenameFilter() {
                   public boolean accept(File dir, String name) {
                     return Pattern.matches(filter, name);
                   }
                 };
           } else {
             fnFilter = null;
           }
           File[] files;
           if (fnFilter == null) {
             files = file.listFiles();
           } else {
             files = file.listFiles(fnFilter);
           }
           List<String> ret = new ArrayList<>(files.length);
           for (File f : files) {
             ret.add(f.getCanonicalPath());
           }
           return ret;
         }
       } catch (IOException e) {
         throw new FileSystemException(e);
       }
     }
   };
 }
示例#14
0
 private BlockingAction<FileProps> props(
     String path, final boolean followLinks, Handler<AsyncResult<FileProps>> handler) {
   final Path target = PathAdjuster.adjust(vertx, Paths.get(path));
   return new BlockingAction<FileProps>(handler) {
     public FileProps perform() {
       try {
         BasicFileAttributes attrs;
         if (followLinks) {
           attrs = Files.readAttributes(target, BasicFileAttributes.class);
         } else {
           attrs =
               Files.readAttributes(target, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
         }
         return new FilePropsImpl(attrs);
       } catch (IOException e) {
         throw new FileSystemException(e);
       }
     }
   };
 }
示例#15
0
  protected BlockingAction<Void> chmodInternal(
      String path, String perms, String dirPerms, Handler<AsyncResult<Void>> handler) {
    final Path target = PathAdjuster.adjust(vertx, Paths.get(path));
    final Set<PosixFilePermission> permissions = PosixFilePermissions.fromString(perms);
    final Set<PosixFilePermission> dirPermissions =
        dirPerms == null ? null : PosixFilePermissions.fromString(dirPerms);
    return new BlockingAction<Void>(handler) {
      public Void perform() {
        try {
          if (dirPermissions != null) {
            Files.walkFileTree(
                target,
                new SimpleFileVisitor<Path>() {
                  public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
                      throws IOException {
                    // The directory entries typically have different permissions to the files, e.g.
                    // execute permission
                    // or can't cd into it
                    Files.setPosixFilePermissions(dir, dirPermissions);
                    return FileVisitResult.CONTINUE;
                  }

                  public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
                      throws IOException {
                    Files.setPosixFilePermissions(file, permissions);
                    return FileVisitResult.CONTINUE;
                  }
                });
          } else {
            Files.setPosixFilePermissions(target, permissions);
          }
        } catch (SecurityException e) {
          throw new FileSystemException("Accessed denied for chmod on " + target);
        } catch (IOException e) {
          throw new FileSystemException(e);
        }
        return null;
      }
    };
  }
示例#16
0
 protected BlockingAction<Void> createFileInternal(
     String p, final String perms, Handler<AsyncResult<Void>> handler) {
   final String path = PathAdjuster.adjust(vertx, p);
   final FileAttribute<?> attrs =
       perms == null
           ? null
           : PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString(perms));
   return new BlockingAction<Void>(handler) {
     public Void perform() {
       try {
         Path target = Paths.get(path);
         if (attrs != null) {
           Files.createFile(target, attrs);
         } else {
           Files.createFile(target);
         }
       } catch (IOException e) {
         throw new FileSystemException(e);
       }
       return null;
     }
   };
 }
示例#17
0
  private BlockingAction<Void> deleteInternal(
      String path, final boolean recursive, Handler<AsyncResult<Void>> handler) {
    final Path source = PathAdjuster.adjust(vertx, Paths.get(path));
    return new BlockingAction<Void>(handler) {
      public Void perform() {
        try {
          if (recursive) {
            Files.walkFileTree(
                source,
                new SimpleFileVisitor<Path>() {
                  public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
                      throws IOException {
                    Files.delete(file);
                    return FileVisitResult.CONTINUE;
                  }

                  public FileVisitResult postVisitDirectory(Path dir, IOException e)
                      throws IOException {
                    if (e == null) {
                      Files.delete(dir);
                      return FileVisitResult.CONTINUE;
                    } else {
                      throw e;
                    }
                  }
                });
          } else {
            Files.delete(source);
          }
        } catch (IOException e) {
          throw new FileSystemException(e);
        }
        return null;
      }
    };
  }