google / google/jimfs

JimFS doesn't handle file locks in documented way (OverlappingFileLockException)

Open
#67 0 comments 0 reactions 0 assignees View on GitHub
P3
Dominant language
Java
Stars
2.6k
Forks
296
Avg merge
9m
Merged PRs (30d)
7

Description

If I try to lock regular file then locking it the second time will raise OverlappingFileLockException as described in [FileChannel documentation](https://docs.oracle.com/javase/7/docs/api/java/nio/channels/FileChannel.html)

JimFS allows to lock the same file multiple times. Here is a test code

```java
public void testJimfsLock() throws IOException {

FileSystem jimFs = Jimfs.newFileSystem();
FileSystem fs = FileSystems.getDefault();

checkLock(jimFs.getPath("/lockfile"));
checkLock(fs.getPath("/tmp/lockfile"));

}

private void checkLock(Path lockFilePath) throws IOException {
System.out.println("lockFilePath=" + lockFilePath);
FileChannel fileChannel = FileChannel.open(lockFilePath, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
FileLock lock1 = fileChannel.lock();
FileLock lock2 = null;
try {
lock2 = fileChannel.tryLock();
} catch (OverlappingFileLockException e) {
System.out.println("got OverlappingFileLockException");
}

System.out.println("lock1=" + lock1);
System.out.println("lock2=" + lock2);
}
```

It will print the following output:
```
lockFilePath=/lockfile
lock1=com.google.common.jimfs.JimfsFileChannel$FakeFileLock[0:9223372036854775807 exclusive valid]
lock2=com.google.common.jimfs.JimfsFileChannel$FakeFileLock[0:9223372036854775807 exclusive valid]
lockFilePath=/tmp/lockfile
got OverlappingFileLockException
lock1=sun.nio.ch.FileLockImpl[0:9223372036854775807 exclusive valid]
lock2=null
```

As you see attempting to lock file for the second time on regular file system leads to OverlappingFileLockException.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.