Skip to content

Commit a17945d

Browse files
authored
Add support for file attribute views in the union file system provider (#18)
* Add support for file attribute views in the union file system provider Fixes #17 (crash with JetBrains JVM) * Add file attribute tests
1 parent 218ceaa commit a17945d

2 files changed

Lines changed: 80 additions & 4 deletions

File tree

src/main/java/cpw/mods/niofs/union/UnionFileSystemProvider.java

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
import java.net.URISyntaxException;
77
import java.nio.channels.SeekableByteChannel;
88
import java.nio.file.*;
9-
import java.nio.file.attribute.BasicFileAttributes;
10-
import java.nio.file.attribute.FileAttribute;
11-
import java.nio.file.attribute.FileAttributeView;
9+
import java.nio.file.attribute.*;
1210
import java.nio.file.spi.FileSystemProvider;
1311
import java.util.*;
1412
import java.util.function.BiPredicate;
@@ -205,7 +203,10 @@ public void checkAccess(final Path path, final AccessMode... modes) throws IOExc
205203

206204
@Override
207205
public <V extends FileAttributeView> V getFileAttributeView(final Path path, final Class<V> type, final LinkOption... options) {
208-
throw new UnsupportedOperationException();
206+
if (path instanceof UnionPath && type == BasicFileAttributeView.class) {
207+
return (V) new UnionBasicFileAttributeView(path, options);
208+
}
209+
return null;
209210
}
210211

211212
@Override
@@ -231,4 +232,30 @@ void removeFileSystem(UnionFileSystem fs) {
231232
fileSystems.remove(fs.getKey());
232233
}
233234
}
235+
236+
private class UnionBasicFileAttributeView implements BasicFileAttributeView {
237+
238+
private final Path path;
239+
private final LinkOption[] options;
240+
241+
public UnionBasicFileAttributeView(Path path, LinkOption[] options) {
242+
this.path = path;
243+
this.options = options;
244+
}
245+
246+
@Override
247+
public String name() {
248+
return "union";
249+
}
250+
251+
@Override
252+
public BasicFileAttributes readAttributes() throws IOException {
253+
return UnionFileSystemProvider.this.readAttributes(path, BasicFileAttributes.class, options);
254+
}
255+
256+
@Override
257+
public void setTimes(FileTime lastModifiedTime, FileTime lastAccessTime, FileTime createTime) {
258+
}
259+
260+
}
234261
}

src/test/java/cpw/mods/niofs/union/TestUnionFS.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
import java.io.IOException;
66
import java.nio.file.*;
7+
import java.nio.file.attribute.BasicFileAttributeView;
8+
import java.nio.file.attribute.BasicFileAttributes;
9+
import java.nio.file.attribute.FileAttributeView;
710
import java.nio.file.spi.FileSystemProvider;
811
import java.util.List;
912
import java.util.Map;
@@ -161,4 +164,50 @@ void testNested() {
161164
var input = assertDoesNotThrow(() -> Files.newInputStream(npath));
162165
var data = assertDoesNotThrow(() -> input.readAllBytes());
163166
}
167+
168+
@Test
169+
void testFileAttributes() {
170+
final var dir1 = Paths.get("src", "test", "resources", "dir1.zip").toAbsolutePath().normalize();
171+
var fsp = (UnionFileSystemProvider)FileSystemProvider.installedProviders().stream().filter(fs-> fs.getScheme().equals("union")).findFirst().orElseThrow();
172+
var ufs = fsp.newFileSystem((a,b) -> true, dir1);
173+
var path = (UnionPath) ufs.getPath("subdir1");
174+
var nonExistentPath = (UnionPath) ufs.getPath("non-existent-path");
175+
176+
// Non-union path
177+
assertDoesNotThrow(() -> {
178+
assertNull(fsp.getFileAttributeView(Paths.get("subdir1"), BasicFileAttributeView.class));
179+
});
180+
// Unsupported attribute view
181+
assertDoesNotThrow(() -> {
182+
assertNull(fsp.getFileAttributeView(path, FileAttributeView.class));
183+
});
184+
// Non-existent path w/ supported attribute view
185+
assertThrows(NoSuchFileException.class, () -> {
186+
var nonExistentView = assertDoesNotThrow(() -> {
187+
var view = fsp.getFileAttributeView(nonExistentPath, BasicFileAttributeView.class);
188+
assertNotNull(view);
189+
return view;
190+
});
191+
nonExistentView.readAttributes();
192+
});
193+
// Non-existent path
194+
assertThrows(NoSuchFileException.class, () -> {
195+
ufs.readAttributes(nonExistentPath, BasicFileAttributes.class);
196+
});
197+
198+
// Union path w/ supported attribute view
199+
var validViewAttributes = assertDoesNotThrow(() -> {
200+
var view = fsp.getFileAttributeView(path, BasicFileAttributeView.class);
201+
assertNotNull(view);
202+
return view.readAttributes();
203+
});
204+
// Known existing path
205+
var validAttributes = assertDoesNotThrow(() -> {
206+
var attributes = ufs.readAttributes(path, BasicFileAttributes.class);
207+
assertNotNull(attributes);
208+
return attributes;
209+
});
210+
// Ensure the attributes are the same through both methods
211+
assertEquals(validAttributes, validViewAttributes);
212+
}
164213
}

0 commit comments

Comments
 (0)