Search in sources :

Example 81 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project neo4j by neo4j.

the class EphemeralFileSystemAbstraction method write.

@Override
public synchronized StoreChannel write(Path fileName) throws IOException {
    Path parentFile = fileName.getParent();
    if (parentFile != null && /*means that this is the 'default location'*/
    !fileExists(parentFile)) {
        throw new NoSuchFileException("'" + fileName + "' (The system cannot find the path specified)");
    }
    EphemeralFileData data = files.computeIfAbsent(canonicalFile(fileName), key -> new EphemeralFileData(key, clock));
    return new StoreFileChannel(new EphemeralFileChannel(data, new EphemeralFileStillOpenException(fileName.toString())));
}
Also used : Path(java.nio.file.Path) NoSuchFileException(java.nio.file.NoSuchFileException)

Example 82 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project neo4j by neo4j.

the class EphemeralFileSystemAbstraction method listFiles.

@Override
public Path[] listFiles(Path directory) throws IOException {
    directory = canonicalFile(directory);
    if (files.containsKey(directory)) {
        throw new NotDirectoryException(directory.toString());
    }
    if (!directories.contains(directory)) {
        throw new NoSuchFileException(directory.toString());
    }
    Set<Path> found = new HashSet<>();
    Iterator<Path> filesAndFolders = new CombiningIterator<>(asList(this.files.keySet().iterator(), directories.iterator()));
    while (filesAndFolders.hasNext()) {
        Path file = filesAndFolders.next();
        if (directory.equals(file.getParent())) {
            found.add(file);
        }
    }
    return found.toArray(new Path[0]);
}
Also used : Path(java.nio.file.Path) NotDirectoryException(java.nio.file.NotDirectoryException) NoSuchFileException(java.nio.file.NoSuchFileException) HashSet(java.util.HashSet) CombiningIterator(org.neo4j.internal.helpers.collection.CombiningIterator)

Example 83 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project flink by apache.

the class CompressionUtils method extractZipFileWithPermissions.

public static void extractZipFileWithPermissions(String zipFilePath, String targetPath) throws IOException {
    try (ZipFile zipFile = new ZipFile(zipFilePath)) {
        Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
        boolean isUnix = isUnix();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        String canonicalTargetPath = new File(targetPath).getCanonicalPath() + File.separator;
        while (entries.hasMoreElements()) {
            ZipArchiveEntry entry = entries.nextElement();
            File outputFile = new File(canonicalTargetPath, entry.getName());
            if (!outputFile.getCanonicalPath().startsWith(canonicalTargetPath)) {
                throw new IOException("Expand " + entry.getName() + " would create a file outside of " + targetPath);
            }
            if (entry.isDirectory()) {
                if (!outputFile.exists()) {
                    if (!outputFile.mkdirs()) {
                        throw new IOException("Create dir: " + outputFile.getAbsolutePath() + " failed!");
                    }
                }
            } else {
                File parentDir = outputFile.getParentFile();
                if (!parentDir.exists()) {
                    if (!parentDir.mkdirs()) {
                        throw new IOException("Create dir: " + outputFile.getAbsolutePath() + " failed!");
                    }
                }
                if (entry.isUnixSymlink()) {
                    // the content of the file is the target path of the symlink
                    baos.reset();
                    IOUtils.copyBytes(zipFile.getInputStream(entry), baos);
                    Files.createSymbolicLink(outputFile.toPath(), new File(parentDir, baos.toString()).toPath());
                } else if (outputFile.createNewFile()) {
                    OutputStream output = new FileOutputStream(outputFile);
                    IOUtils.copyBytes(zipFile.getInputStream(entry), output);
                } else {
                    throw new IOException("Create file: " + outputFile.getAbsolutePath() + " failed!");
                }
            }
            if (isUnix) {
                int mode = entry.getUnixMode();
                if (mode != 0) {
                    Path outputPath = Paths.get(outputFile.toURI());
                    Set<PosixFilePermission> permissions = new HashSet<>();
                    addIfBitSet(mode, 8, permissions, PosixFilePermission.OWNER_READ);
                    addIfBitSet(mode, 7, permissions, PosixFilePermission.OWNER_WRITE);
                    addIfBitSet(mode, 6, permissions, PosixFilePermission.OWNER_EXECUTE);
                    addIfBitSet(mode, 5, permissions, PosixFilePermission.GROUP_READ);
                    addIfBitSet(mode, 4, permissions, PosixFilePermission.GROUP_WRITE);
                    addIfBitSet(mode, 3, permissions, PosixFilePermission.GROUP_EXECUTE);
                    addIfBitSet(mode, 2, permissions, PosixFilePermission.OTHERS_READ);
                    addIfBitSet(mode, 1, permissions, PosixFilePermission.OTHERS_WRITE);
                    addIfBitSet(mode, 0, permissions, PosixFilePermission.OTHERS_EXECUTE);
                    // TODO: support setting the permission without following links
                    try {
                        Files.setPosixFilePermissions(outputPath, permissions);
                    } catch (NoSuchFileException e) {
                    // this may happens when the target file of the symlink is still not
                    // extracted
                    }
                }
            }
        }
    }
}
Also used : Path(java.nio.file.Path) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) NoSuchFileException(java.nio.file.NoSuchFileException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) FileOutputStream(java.io.FileOutputStream) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) HashSet(java.util.HashSet)

Example 84 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project gerrit by GerritCodeReview.

the class HtmlDomUtil method parseFile.

/**
 * Parse an XHTML file from the local drive and return the instance.
 */
public static Document parseFile(Path path) throws IOException {
    try (InputStream in = Files.newInputStream(path)) {
        Document doc = newBuilder().parse(in);
        compact(doc);
        return doc;
    } catch (NoSuchFileException e) {
        return null;
    } catch (SAXException | ParserConfigurationException | IOException e) {
        throw new IOException("Error reading " + path, e);
    }
}
Also used : InputStream(java.io.InputStream) NoSuchFileException(java.nio.file.NoSuchFileException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException)

Example 85 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project gerrit by GerritCodeReview.

the class InitUtil method copy.

public static void copy(Path dst, byte[] buf) throws FileNotFoundException, IOException {
    // 
    try (InputStream in = Files.newInputStream(dst)) {
        if (Arrays.equals(buf, ByteStreams.toByteArray(in))) {
            return;
        }
    } catch (NoSuchFileException notFound) {
    // Fall through and write the file.
    }
    Files.createDirectories(dst.getParent());
    LockFile lf = new LockFile(dst.toFile());
    if (!lf.lock()) {
        throw new IOException("Cannot lock " + dst);
    }
    try {
        try (InputStream in = new ByteArrayInputStream(buf);
            OutputStream out = lf.getOutputStream()) {
            ByteStreams.copy(in, out);
        }
        if (!lf.commit()) {
            throw new IOException("Cannot commit " + dst);
        }
    } finally {
        lf.unlock();
    }
}
Also used : LockFile(org.eclipse.jgit.internal.storage.file.LockFile) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException)

Aggregations

NoSuchFileException (java.nio.file.NoSuchFileException)262 IOException (java.io.IOException)107 Path (java.nio.file.Path)104 FileNotFoundException (java.io.FileNotFoundException)41 Test (org.junit.Test)35 InputStream (java.io.InputStream)31 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)25 File (java.io.File)22 NotDirectoryException (java.nio.file.NotDirectoryException)19 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)18 ArrayList (java.util.ArrayList)16 HashSet (java.util.HashSet)16 OutputStream (java.io.OutputStream)15 DirectoryNotEmptyException (java.nio.file.DirectoryNotEmptyException)15 FileChannel (java.nio.channels.FileChannel)14 AccessDeniedException (java.nio.file.AccessDeniedException)14 ByteBuffer (java.nio.ByteBuffer)13 HashMap (java.util.HashMap)13 Map (java.util.Map)12 SeekableByteChannel (java.nio.channels.SeekableByteChannel)11