6

我想使用 Google 的 JIMFS 来创建一个用于测试目的的虚拟文件系统。不过,我很难开始。

我看了这个教程:http ://www.hascode.com/2015/03/creating-in-memory-file-systems-with-googles-jimfs/

但是,当我创建文件系统时,它实际上是在现有文件系统中创建的,即我不能这样做:

Files.createDirectory("/virtualfolder");

因为我被拒绝访问。

我错过了什么吗?

目前,我的代码如下所示:

测试类:

FileSystem fs = Jimfs.newFileSystem(Configuration.unix());
Path vTargetFolder = fs.getPath("/Store/homes/linux/abc/virtual");

TestedClass test = new TestedClass(vTargetFolder.toAbsolutePath().toString());

某处的Java类:

targetPath = Paths.get(targetName);
Files.createDirectory(targetPath);

// etc., creating files and writing them to the target directory

但是,我创建了一个单独的类来测试 JIMFS,这里目录的创建没有失败,但是我不能像这样创建一个新文件:

FileSystem fs = Jimfs.newFileSystem(Configuration.unix());
Path data = fs.getPath("/virtual");
Path dir = Files.createDirectory(data);
        
Path file = Files.createFile(Paths.get(dir + "/abc.txt")); // throws NoSuchFileException

我究竟做错了什么?

4

4 回答 4

13

问题是默认文件系统和新文件系统的混合。

问题1:

Files.createDirectory("/virtualfolder"); 

这实际上不会编译,所以我怀疑你的意思是:

Files.createDirectory( Paths.get("/virtualfolder"));

这会尝试在默认文件系统的根目录中创建一个目录。您需要特权才能做到这一点,并且可能不应该将其作为测试。我怀疑你试图通过使用字符串来解决这个问题并遇到

问题2:

让我们用注释来看看你的代码

FileSystem fs = Jimfs.newFileSystem(Configuration.unix());
// now get path in the new FileSystem
Path data = fs.getPath("/virtual");    
// create a directory in the new FileSystem
Path dir = Files.createDirectory(data);
// create a file in the default FileSystem 
// with a parent that was never created there
Path file = Files.createFile(Paths.get(dir + "/abc.txt")); // throws NoSuchFileException

让我们看看最后一行:

dir + "/abc.txt"            >> is the string "/virtual/abc.txt"
Paths.get(dir + "/abc.txt") >> is this as path in the default filesystem

请记住,虚拟文件​​系统与默认文件系统并行存在。路径有一个文件系统,不能在其他文件系统中使用。它们不仅仅是名字。

笔记:

  • 使用虚拟文件系统避免使用 Paths 类。此类将始终在默认文件系统中工作。文件没问题,因为您首先在正确的文件系统中创建了一个路径。

  • 如果您最初的计划是使用安装到默认文件系统的虚拟文件系统,则需要更多。我有一个项目,我在其中创建基于虚拟文件系统的 Webdav 服务器,然后使用 OS 内置方法将其挂载为卷。

于 2015-06-25T13:15:21.080 回答
0

我建议您改用memoryfilesystem。它的实现比 Jimfs 更完整;特别是,它在创建“Linux”文件系统等时支持 POSIX 属性。

使用它,您的代码将真正起作用:

try (
    final FileSystem fs = MemoryFileSystemBuilder.newLinux()
        .build("testfs");
) {
    // create a directory, a file within this directory etc
}
于 2015-04-14T10:47:01.307 回答
0

In your shell try ls / the output should contain the "/virtual" directory.

If this is not the case which I suspect it is then:

The program is masking a:

java.nio.file.AccessDeniedException: /virtual/abc.txt

In reality the code should be failing at Path dir = Files.createDirectory(data);

But for some reason this exception is silent and the program continues without creating the directory (or thinking it has) and attempts to write to the directory that doesn't exist

Leaving a misleading java.nio.file.NoSuchFileException

于 2015-04-10T12:51:22.527 回答
0

似乎而不是

Path file = Files.createFile(Paths.get(dir + "/abc.txt"));

你应该做

Path file = Files.createFile(dir.resolve("/abc.txt"))

这样,dir(它的文件系统)的上下文就不会丢失。

于 2020-02-28T21:55:37.277 回答