Search in sources :

Example 6 with FileSystemException

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;
        }
    };
}
Also used : Path(java.nio.file.Path) FileSystemException(io.vertx.core.file.FileSystemException) IOException(java.io.IOException)

Example 7 with FileSystemException

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);
            }
        }
    };
}
Also used : FilenameFilter(java.io.FilenameFilter) FileSystemException(io.vertx.core.file.FileSystemException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) AsyncFile(io.vertx.core.file.AsyncFile) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 8 with FileSystemException

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;
        }
    };
}
Also used : Path(java.nio.file.Path) FileSystemException(io.vertx.core.file.FileSystemException) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 9 with FileSystemException

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;
        }
    };
}
Also used : Path(java.nio.file.Path) UserPrincipalLookupService(java.nio.file.attribute.UserPrincipalLookupService) FileSystemException(io.vertx.core.file.FileSystemException) GroupPrincipal(java.nio.file.attribute.GroupPrincipal) IOException(java.io.IOException) UserPrincipal(java.nio.file.attribute.UserPrincipal) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView)

Example 10 with FileSystemException

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);
            }
        }
    };
}
Also used : Path(java.nio.file.Path) FileSystemException(io.vertx.core.file.FileSystemException) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Aggregations

FileSystemException (io.vertx.core.file.FileSystemException)14 IOException (java.io.IOException)14 Path (java.nio.file.Path)11 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)3 RandomAccessFile (java.io.RandomAccessFile)2 FileVisitResult (java.nio.file.FileVisitResult)2 Buffer (io.vertx.core.buffer.Buffer)1 AsyncFile (io.vertx.core.file.AsyncFile)1 File (java.io.File)1 FilenameFilter (java.io.FilenameFilter)1 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)1 FileStore (java.nio.file.FileStore)1 GroupPrincipal (java.nio.file.attribute.GroupPrincipal)1 PosixFileAttributeView (java.nio.file.attribute.PosixFileAttributeView)1 PosixFilePermission (java.nio.file.attribute.PosixFilePermission)1 UserPrincipal (java.nio.file.attribute.UserPrincipal)1 UserPrincipalLookupService (java.nio.file.attribute.UserPrincipalLookupService)1 ArrayList (java.util.ArrayList)1