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())));
}
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]);
}
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
}
}
}
}
}
}
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);
}
}
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();
}
}
Aggregations