Search in sources :

Example 31 with BasicFileAttributes

use of java.nio.file.attribute.BasicFileAttributes in project intellij-community by JetBrains.

the class Digester method digestRegularFile.

public static long digestRegularFile(File file, boolean normalize) throws IOException {
    Path path = file.toPath();
    BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
    if (attrs.isSymbolicLink()) {
        Path target = Files.readSymbolicLink(path);
        if (target.isAbsolute())
            throw new IOException("Absolute link: " + file + " -> " + target);
        return digestStream(new ByteArrayInputStream(target.toString().getBytes("UTF-8"))) | LINK_MASK;
    }
    if (attrs.isDirectory())
        return DIRECTORY;
    try (InputStream in = new BufferedInputStream(Utils.newFileInputStream(file, normalize))) {
        return digestStream(in);
    }
}
Also used : Path(java.nio.file.Path) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 32 with BasicFileAttributes

use of java.nio.file.attribute.BasicFileAttributes in project Glowstone by GlowstoneMC.

the class GlowServer method checkTransfer.

private void checkTransfer(String name, String suffix, Environment environment) {
    // todo: import things like per-dimension villages.dat when those are implemented
    Path srcPath = new File(new File(getWorldContainer(), name), "DIM" + environment.getId()).toPath();
    Path destPath = new File(getWorldContainer(), name + suffix).toPath();
    if (Files.exists(srcPath) && !Files.exists(destPath)) {
        logger.info("Importing " + destPath + " from " + srcPath);
        try {
            Files.walkFileTree(srcPath, new FileVisitor<Path>() {

                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                    Path target = destPath.resolve(srcPath.relativize(dir));
                    if (!Files.exists(target)) {
                        Files.createDirectory(target);
                    }
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    Files.copy(file, destPath.resolve(srcPath.relativize(file)), StandardCopyOption.COPY_ATTRIBUTES);
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                    logger.warning("Importing file " + srcPath.relativize(file) + " + failed: " + exc);
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                    return FileVisitResult.CONTINUE;
                }
            });
            Files.copy(srcPath.resolve("../level.dat"), destPath.resolve("level.dat"));
        } catch (IOException e) {
            logger.log(Level.WARNING, "Import of " + srcPath + " failed", e);
        }
    }
}
Also used : IOException(java.io.IOException) UuidListFile(net.glowstone.util.bans.UuidListFile) File(java.io.File) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 33 with BasicFileAttributes

use of java.nio.file.attribute.BasicFileAttributes in project asterixdb by apache.

the class LocalFileSystemUtils method traverse.

public static void traverse(final List<File> files, File root, final String expression, final LinkedList<Path> dirs) throws IOException {
    final Path path = root.toPath();
    if (!Files.exists(path)) {
        throw new RuntimeDataException(ErrorCode.UTIL_LOCAL_FILE_SYSTEM_UTILS_PATH_NOT_FOUND, path.toString());
    }
    if (!Files.isDirectory(path)) {
        validateAndAdd(path, expression, files);
    }
    Files.walkFileTree(path, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult preVisitDirectory(Path path, BasicFileAttributes attrs) throws IOException {
            if (!Files.exists(path, LinkOption.NOFOLLOW_LINKS)) {
                return FileVisitResult.TERMINATE;
            }
            if (Files.isDirectory(path, LinkOption.NOFOLLOW_LINKS)) {
                if (dirs != null) {
                    dirs.add(path);
                }
                //get immediate children files
                File[] content = path.toFile().listFiles();
                for (File file : content) {
                    if (!file.isDirectory()) {
                        validateAndAdd(file.toPath(), expression, files);
                    }
                }
            } else {
                // Path is a file, add to list of files if it matches the expression
                validateAndAdd(path, expression, files);
            }
            return FileVisitResult.CONTINUE;
        }
    });
}
Also used : Path(java.nio.file.Path) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) File(java.io.File) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) RuntimeDataException(org.apache.asterix.common.exceptions.RuntimeDataException)

Example 34 with BasicFileAttributes

use of java.nio.file.attribute.BasicFileAttributes in project beam by apache.

the class ApexYarnLauncher method createJar.

/**
   * Create a jar file from the given directory.
   * @param dir source directory
   * @param jarFile jar file name
   * @throws IOException when file cannot be created
   */
public static void createJar(File dir, File jarFile) throws IOException {
    final Map<String, ?> env = Collections.singletonMap("create", "true");
    if (jarFile.exists() && !jarFile.delete()) {
        throw new RuntimeException("Failed to remove " + jarFile);
    }
    URI uri = URI.create("jar:" + jarFile.toURI());
    try (final FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {
        File manifestFile = new File(dir, JarFile.MANIFEST_NAME);
        Files.createDirectory(zipfs.getPath("META-INF"));
        try (final OutputStream out = Files.newOutputStream(zipfs.getPath(JarFile.MANIFEST_NAME))) {
            if (!manifestFile.exists()) {
                new Manifest().write(out);
            } else {
                FileUtils.copyFile(manifestFile, out);
            }
        }
        final java.nio.file.Path root = dir.toPath();
        Files.walkFileTree(root, new java.nio.file.SimpleFileVisitor<Path>() {

            String relativePath;

            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                relativePath = root.relativize(dir).toString();
                if (!relativePath.isEmpty()) {
                    if (!relativePath.endsWith("/")) {
                        relativePath += "/";
                    }
                    if (!relativePath.equals("META-INF/")) {
                        final Path dstDir = zipfs.getPath(relativePath);
                        Files.createDirectory(dstDir);
                    }
                }
                return super.preVisitDirectory(dir, attrs);
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                String name = relativePath + file.getFileName();
                if (!JarFile.MANIFEST_NAME.equals(name)) {
                    try (final OutputStream out = Files.newOutputStream(zipfs.getPath(name))) {
                        FileUtils.copyFile(file.toFile(), out);
                    }
                }
                return super.visitFile(file, attrs);
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                relativePath = root.relativize(dir.getParent()).toString();
                if (!relativePath.isEmpty() && !relativePath.endsWith("/")) {
                    relativePath += "/";
                }
                return super.postVisitDirectory(dir, exc);
            }
        });
    }
}
Also used : Path(java.nio.file.Path) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) Manifest(java.util.jar.Manifest) URI(java.net.URI) FileSystem(java.nio.file.FileSystem) JarFile(java.util.jar.JarFile) File(java.io.File) Path(java.nio.file.Path) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 35 with BasicFileAttributes

use of java.nio.file.attribute.BasicFileAttributes in project cloudstack by apache.

the class HypervisorUtils method checkVolumeFileForActivity.

public static void checkVolumeFileForActivity(final String filePath, int timeoutSeconds, long inactiveThresholdMilliseconds, long minimumFileSize) throws IOException {
    File file = new File(filePath);
    if (!file.exists()) {
        throw new CloudRuntimeException("File " + file.getAbsolutePath() + " not found");
    }
    if (file.length() < minimumFileSize) {
        s_logger.debug("VM disk file too small, fresh clone? skipping modify check");
        return;
    }
    int waitedSeconds = 0;
    int intervalSeconds = 1;
    while (true) {
        BasicFileAttributes attrs = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
        long modifyIdle = System.currentTimeMillis() - attrs.lastModifiedTime().toMillis();
        long accessIdle = System.currentTimeMillis() - attrs.lastAccessTime().toMillis();
        if (modifyIdle > inactiveThresholdMilliseconds && accessIdle > inactiveThresholdMilliseconds) {
            s_logger.debug("File " + filePath + " has not been accessed or modified for at least " + inactiveThresholdMilliseconds + " ms");
            return;
        } else {
            s_logger.debug("File was modified " + modifyIdle + "ms ago, accessed " + accessIdle + "ms ago, waiting for inactivity threshold of " + inactiveThresholdMilliseconds + "ms or timeout of " + timeoutSeconds + "s (waited " + waitedSeconds + "s)");
        }
        try {
            TimeUnit.SECONDS.sleep(intervalSeconds);
        } catch (InterruptedException ex) {
            throw new CloudRuntimeException("Interrupted while waiting for activity on " + filePath + " to subside", ex);
        }
        waitedSeconds += intervalSeconds;
        if (waitedSeconds >= timeoutSeconds) {
            throw new CloudRuntimeException("Reached timeout while waiting for activity on " + filePath + " to subside");
        }
    }
}
Also used : CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) File(java.io.File) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Aggregations

BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)119 Path (java.nio.file.Path)93 IOException (java.io.IOException)87 FileVisitResult (java.nio.file.FileVisitResult)66 File (java.io.File)18 Test (org.junit.Test)13 SimpleFileVisitor (java.nio.file.SimpleFileVisitor)11 ArrayList (java.util.ArrayList)11 HashSet (java.util.HashSet)8 FileNotFoundException (java.io.FileNotFoundException)7 InputStream (java.io.InputStream)6 HashMap (java.util.HashMap)6 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)5 BasicFileAttributeView (java.nio.file.attribute.BasicFileAttributeView)5 SourcePath (com.facebook.buck.rules.SourcePath)4 ImmutableList (com.google.common.collect.ImmutableList)4 OutputStream (java.io.OutputStream)4 URI (java.net.URI)4 FileSystem (java.nio.file.FileSystem)4 FileVisitor (java.nio.file.FileVisitor)4