use of io.vertx.core.file.FileSystemException in project vert.x by eclipse.
the class FileSystemImpl method moveInternal.
private BlockingAction<Void> moveInternal(String from, String to, Handler<AsyncResult<Void>> handler) {
Objects.requireNonNull(from);
Objects.requireNonNull(to);
return new BlockingAction<Void>(handler) {
public Void perform() {
try {
Path source = vertx.resolveFile(from).toPath();
Path target = vertx.resolveFile(to).toPath();
Files.move(source, target);
} catch (IOException e) {
throw new FileSystemException(e);
}
return null;
}
};
}
use of io.vertx.core.file.FileSystemException in project vert.x by eclipse.
the class FileSystemImpl method readDirInternal.
private BlockingAction<List<String>> readDirInternal(String p, String filter, Handler<AsyncResult<List<String>>> handler) {
Objects.requireNonNull(p);
return new BlockingAction<List<String>>(handler) {
public List<String> perform() {
try {
File file = vertx.resolveFile(p);
if (!file.exists()) {
throw new FileSystemException("Cannot read directory " + file + ". Does not exist");
}
if (!file.isDirectory()) {
throw new FileSystemException("Cannot read directory " + file + ". 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);
}
}
};
}
use of io.vertx.core.file.FileSystemException in project vert.x by eclipse.
the class FileSystemImpl method chmodInternal.
protected BlockingAction<Void> chmodInternal(String path, String perms, String dirPerms, Handler<AsyncResult<Void>> handler) {
Objects.requireNonNull(path);
Set<PosixFilePermission> permissions = PosixFilePermissions.fromString(perms);
Set<PosixFilePermission> dirPermissions = dirPerms == null ? null : PosixFilePermissions.fromString(dirPerms);
return new BlockingAction<Void>(handler) {
public Void perform() {
try {
Path target = vertx.resolveFile(path).toPath();
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 " + path);
} catch (IOException e) {
throw new FileSystemException(e);
}
return null;
}
};
}
use of io.vertx.core.file.FileSystemException in project vert.x by eclipse.
the class FileSystemImpl method chownInternal.
protected BlockingAction<Void> chownInternal(String path, String user, String group, Handler<AsyncResult<Void>> handler) {
Objects.requireNonNull(path);
return new BlockingAction<Void>(handler) {
public Void perform() {
try {
Path target = vertx.resolveFile(path).toPath();
UserPrincipalLookupService service = target.getFileSystem().getUserPrincipalLookupService();
UserPrincipal userPrincipal = user == null ? null : service.lookupPrincipalByName(user);
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 " + path);
} catch (IOException e) {
throw new FileSystemException(e);
}
return null;
}
};
}
use of io.vertx.core.file.FileSystemException in project vert.x by eclipse.
the class FileSystemImpl method props.
private BlockingAction<FileProps> props(String path, boolean followLinks, Handler<AsyncResult<FileProps>> handler) {
Objects.requireNonNull(path);
return new BlockingAction<FileProps>(handler) {
public FileProps perform() {
try {
Path target = vertx.resolveFile(path).toPath();
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);
}
}
};
}
Aggregations