Skip to content

Commit ff03d33

Browse files
committed
fix: attempt to read without locking when locking fails; Some file system mount circumstances can lead to files being read/writeable, but not lockable. In these cases we should attempt to read, without locking, and immediately materialize as a best-effort attempt at ensuring valid data
Signed-off-by: Caleb Hulbert <cmhulbert@gmail.com>
1 parent c89ad0d commit ff03d33

1 file changed

Lines changed: 27 additions & 6 deletions

File tree

src/main/java/org/janelia/saalfeldlab/n5/FileSystemKeyValueAccess.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
*/
2929
package org.janelia.saalfeldlab.n5;
3030

31+
import java.io.Closeable;
3132
import java.nio.file.StandardOpenOption;
3233
import org.janelia.saalfeldlab.n5.N5Exception.N5IOException;
3334
import org.janelia.saalfeldlab.n5.N5Exception.N5NoSuchKeyException;
@@ -81,12 +82,20 @@ public FileSystemKeyValueAccess(final FileSystem fileSystem) {
8182
}
8283

8384
@Override
84-
public VolatileReadData createReadData(final String normalPath) {
85+
public VolatileReadData createReadData(final String normalPath) throws N5IOException {
86+
Path path = fileSystem.getPath(normalPath);
87+
FileLazyRead fileLazyRead = null;
8588
try {
86-
return VolatileReadData.from(new FileLazyRead(fileSystem.getPath(normalPath)));
87-
} catch (N5NoSuchKeyException e) {
88-
return null;
89+
fileLazyRead = new FileLazyRead(path);
90+
} catch (N5NoSuchKeyException e) {
91+
throw e;
92+
} catch (N5IOException e) {
93+
/* Try to create without locking, and immediately materialize */
94+
fileLazyRead = new FileLazyRead(path, false);
95+
fileLazyRead.materialize(0, 0);
8996
}
97+
98+
return VolatileReadData.from(fileLazyRead);
9099
}
91100

92101
@Override
@@ -491,13 +500,25 @@ protected static void createAndCheckIsDirectory(
491500

492501
private static class FileLazyRead implements LazyRead {
493502

503+
private static final Closeable NO_OP = () -> { };
494504

495505
private final Path path;
496-
private LockedFileChannel lock;
506+
private Closeable lock;
497507

498508
FileLazyRead(final Path path) {
509+
this(path, true);
510+
}
511+
512+
FileLazyRead(final Path path, final Boolean requireLock ) {
499513
this.path = path;
500-
lock = lockForReading(path);
514+
try {
515+
lock = lockForReading(path);
516+
} catch (Exception e) {
517+
if (requireLock) {
518+
throw e;
519+
}
520+
lock = NO_OP;
521+
}
501522
}
502523

503524
@Override

0 commit comments

Comments
 (0)