use of java.nio.file.NoSuchFileException in project apache-kafka-on-k8s by banzaicloud.
the class StateDirectory method lockGlobalState.
synchronized boolean lockGlobalState() throws IOException {
if (globalStateLock != null) {
log.trace("{} Found cached state dir lock for the global task", logPrefix());
return true;
}
final File lockFile = new File(globalStateDir(), LOCK_FILE_NAME);
final FileChannel channel;
try {
channel = FileChannel.open(lockFile.toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
} catch (NoSuchFileException e) {
// file, in this case we will return immediately indicating locking failed.
return false;
}
final FileLock fileLock = tryLock(channel);
if (fileLock == null) {
channel.close();
return false;
}
globalStateChannel = channel;
globalStateLock = fileLock;
log.debug("{} Acquired global state dir lock", logPrefix());
return true;
}
use of java.nio.file.NoSuchFileException in project apache-kafka-on-k8s by banzaicloud.
the class StateDirectory method lock.
/**
* Get the lock for the {@link TaskId}s directory if it is available
* @param taskId
* @return true if successful
* @throws IOException
*/
synchronized boolean lock(final TaskId taskId) throws IOException {
final File lockFile;
// we already have the lock so bail out here
final LockAndOwner lockAndOwner = locks.get(taskId);
if (lockAndOwner != null && lockAndOwner.owningThread.equals(Thread.currentThread().getName())) {
log.trace("{} Found cached state dir lock for task {}", logPrefix(), taskId);
return true;
} else if (lockAndOwner != null) {
// another thread owns the lock
return false;
}
try {
lockFile = new File(directoryForTask(taskId), LOCK_FILE_NAME);
} catch (ProcessorStateException e) {
// has concurrently deleted the directory
return false;
}
final FileChannel channel;
try {
channel = getOrCreateFileChannel(taskId, lockFile.toPath());
} catch (NoSuchFileException e) {
// file, in this case we will return immediately indicating locking failed.
return false;
}
final FileLock lock = tryLock(channel);
if (lock != null) {
locks.put(taskId, new LockAndOwner(Thread.currentThread().getName(), lock));
log.debug("{} Acquired state dir lock for task {}", logPrefix(), taskId);
}
return lock != null;
}
use of java.nio.file.NoSuchFileException in project graal by oracle.
the class MemoryFileSystem method newByteChannel.
@Override
public SeekableByteChannel newByteChannel(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException {
if (options.contains(StandardOpenOption.APPEND) && options.contains(StandardOpenOption.READ)) {
throw new IllegalArgumentException("READ + APPEND not allowed.");
}
if (options.contains(StandardOpenOption.SYNC) || options.contains(StandardOpenOption.DSYNC)) {
throw new IllegalArgumentException("Not supported yet.");
}
final Path absolutePath = toAbsolutePath(path);
final Path parentPath = absolutePath.getParent();
if (parentPath == null) {
throw new IOException(path.toString() + " is a directory.");
}
boolean read = options.contains(StandardOpenOption.READ);
boolean write = options.contains(StandardOpenOption.WRITE);
boolean append = options.contains(StandardOpenOption.APPEND);
if (!read && !write) {
if (append) {
write = true;
} else {
read = true;
}
}
final Map.Entry<Long, Map<String, Long>> e = readDir(parentPath);
final long parentInode = e.getKey();
final Map<String, Long> parentDirents = e.getValue();
final String fileName = absolutePath.getFileName().toString();
Long inode = parentDirents.get(fileName);
if (inode == null) {
if (!options.contains(StandardOpenOption.WRITE) || !(options.contains(StandardOpenOption.CREATE) || options.contains(StandardOpenOption.CREATE_NEW))) {
throw new NoSuchFileException(path.toString());
}
if (!inodes.get(parentInode).permissions.contains(AccessMode.WRITE)) {
throw new IOException("Read only dir: " + path);
}
inode = nextInode++;
inodes.put(inode, FileInfo.newBuilder(FileType.FILE).permissions(attrs).build());
blocks.put(inode, EMPTY);
parentDirents.put(fileName, inode);
writeDir(parentInode, parentDirents);
} else {
if (options.contains(StandardOpenOption.CREATE_NEW)) {
throw new FileAlreadyExistsException(path.toString());
}
final FileInfo fileInfo = inodes.get(inode);
if (!fileInfo.isFile()) {
throw new IOException(path.toString() + " is a directory.");
}
if (read && !fileInfo.permissions.contains(AccessMode.READ)) {
throw new IOException("Cannot read: " + path);
}
if (write && !fileInfo.permissions.contains(AccessMode.WRITE)) {
throw new IOException("Read only: " + path);
}
}
final boolean deleteOnClose = options.contains(StandardOpenOption.DELETE_ON_CLOSE);
final byte[] origData = blocks.get(inode);
final byte[] data = write && options.contains(StandardOpenOption.TRUNCATE_EXISTING) ? EMPTY : Arrays.copyOf(origData, origData.length);
final long inodeFin = inode;
final BiConsumer<byte[], Long> syncAction = new BiConsumer<byte[], Long>() {
@Override
public void accept(byte[] t, Long u) {
blocks.put(inodeFin, Arrays.copyOf(t, (int) u.longValue()));
}
};
final boolean readFin = read;
final boolean writeFin = write;
final BiConsumer<byte[], Long> metaSyncAction = new BiConsumer<byte[], Long>() {
@Override
public void accept(byte[] t, Long u) {
final long time = System.currentTimeMillis();
final FileInfo fileInfo = inodes.get(inodeFin);
if (readFin) {
fileInfo.atime = time;
}
if (writeFin) {
fileInfo.mtime = time;
}
}
};
final BiConsumer<byte[], Long> closeAction = new BiConsumer<byte[], Long>() {
@Override
public void accept(byte[] t, Long u) {
if (deleteOnClose) {
try {
delete(absolutePath);
} catch (IOException ioe) {
sthrow(ioe);
}
} else {
syncAction.accept(t, u);
metaSyncAction.accept(t, u);
}
}
@SuppressWarnings("unchecked")
private <E extends Throwable> void sthrow(Throwable t) throws E {
throw (E) t;
}
};
return new ChannelImpl(data, closeAction, read, write, append);
}
use of java.nio.file.NoSuchFileException in project graal by oracle.
the class IOHelper method copy.
static void copy(final Path source, final Path target, final FileSystem sourceFileSystem, final FileSystem targetFileSystem, CopyOption... options) throws IOException {
if (source.equals(target)) {
return;
}
final Path sourceReal = sourceFileSystem.toRealPath(source, LinkOption.NOFOLLOW_LINKS);
final Path targetReal = targetFileSystem.toRealPath(target, LinkOption.NOFOLLOW_LINKS);
if (sourceReal.equals(targetReal)) {
return;
}
final Set<LinkOption> linkOptions = new HashSet<>();
final Set<StandardCopyOption> copyOptions = EnumSet.noneOf(StandardCopyOption.class);
for (CopyOption option : options) {
if (option instanceof StandardCopyOption) {
copyOptions.add((StandardCopyOption) option);
} else if (option instanceof LinkOption) {
linkOptions.add((LinkOption) option);
}
}
if (copyOptions.contains(StandardCopyOption.ATOMIC_MOVE)) {
throw new AtomicMoveNotSupportedException(source.getFileName().toString(), target.getFileName().toString(), "Atomic move not supported");
}
final Map<String, Object> sourceAttributes = sourceFileSystem.readAttributes(sourceReal, "basic:isSymbolicLink,isDirectory,lastModifiedTime,lastAccessTime,creationTime", linkOptions.toArray(new LinkOption[linkOptions.size()]));
if ((Boolean) sourceAttributes.getOrDefault("isSymbolicLink", false)) {
throw new IOException("Copying of symbolic links is not supported.");
}
if (copyOptions.contains(StandardCopyOption.REPLACE_EXISTING)) {
try {
targetFileSystem.delete(targetReal);
} catch (NoSuchFileException notFound) {
// Does not exist - nothing to delete
}
} else {
boolean exists;
try {
targetFileSystem.checkAccess(targetReal, EnumSet.noneOf(AccessMode.class));
exists = true;
} catch (IOException ioe) {
exists = false;
}
if (exists) {
throw new FileAlreadyExistsException(target.toString());
}
}
if ((Boolean) sourceAttributes.getOrDefault("isDirectory", false)) {
targetFileSystem.createDirectory(targetReal);
} else {
final Set<StandardOpenOption> readOptions = EnumSet.of(StandardOpenOption.READ);
final Set<StandardOpenOption> writeOptions = EnumSet.of(StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW);
try (final SeekableByteChannel sourceChannel = sourceFileSystem.newByteChannel(sourceReal, readOptions);
final SeekableByteChannel targetChannel = targetFileSystem.newByteChannel(targetReal, writeOptions)) {
final ByteBuffer buffer = ByteBuffer.allocateDirect(1 << 16);
while (sourceChannel.read(buffer) != -1) {
buffer.flip();
while (buffer.hasRemaining()) {
targetChannel.write(buffer);
}
buffer.clear();
}
}
}
if (copyOptions.contains(StandardCopyOption.COPY_ATTRIBUTES)) {
String[] basicMutableAttributes = { "lastModifiedTime", "lastAccessTime", "creationTime" };
try {
for (String key : basicMutableAttributes) {
final Object value = sourceAttributes.get(key);
if (value != null) {
targetFileSystem.setAttribute(targetReal, key, value);
}
}
} catch (Throwable rootCause) {
try {
targetFileSystem.delete(targetReal);
} catch (Throwable suppressed) {
rootCause.addSuppressed(suppressed);
}
throw rootCause;
}
}
}
use of java.nio.file.NoSuchFileException in project bookkeeper by apache.
the class FSCheckpointManagerTest method testFileRenameDirNotExists.
@Test
public void testFileRenameDirNotExists() throws Exception {
File srcDir = new File(rootDir, "src");
srcDir.mkdir();
String srcFilePath = "src/" + runtime.getMethodName();
String destFilePath = "dest/" + runtime.getMethodName();
OutputStream os = cm.openOutputStream(srcFilePath);
os.write(TEST_BYTES);
os.flush();
os.close();
try {
cm.rename(srcFilePath, destFilePath);
fail("Should fail to rename if the dest dir doesn't exist");
} catch (NoSuchFileException e) {
// expected
}
assertFalse(cm.fileExists(destFilePath));
assertTrue(cm.fileExists(srcFilePath));
}
Aggregations