我正在编写一个定制的fuse镜像文件系统(在Ubuntu中使用FUSE)。我的意思是,它将从本地文件系统的目录中读取并写入其中。
我实现了getattr、create和read操作,如下所示。所有这些都很完美。
...
private final String mirroredFolder = "./target/mirrored";
...
...
public int getattr(final String path, final StatWrapper stat)
{
File f = new File(mirroredFolder+path);
//if current path is of file
if (f.isFile())
{
stat.setMode(NodeType.FILE,true,true,true,true,true,true,true,true,true);
stat.size(f.length());
stat.atime(f.lastModified()/ 1000L);
stat.mtime(0);
stat.nlink(1);
stat.uid(0);
stat.gid(0);
stat.blocks((int) ((f.length() + 511L) / 512L));
return 0;
}
//if current file is of Directory
else if(f.isDirectory())
{
stat.setMode(NodeType.DIRECTORY);
return 0;
}
return -ErrorCodes.ENOENT();
}下面的create方法在镜像文件夹中创建新文件
public int create(final String path, final ModeWrapper mode, final FileInfoWrapper info)
{
File f = new File(mirroredFolder+path);
try {
f.createNewFile();
mode.setMode(NodeType.FILE, true, true, true);
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}read方法从镜像文件夹读取文件。
public int read(final String path, final ByteBuffer buffer, final long size, final long offset, final FileInfoWrapper info)
{
String contentOfFile=null;
try {
contentOfFile= readFile(mirroredFolder+path);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
final String s = contentOfFile.substring((int) offset,
(int) Math.max(offset, Math.min(contentOfFile.length() - offset, offset + size)));
buffer.put(s.getBytes());
return s.getBytes().length;
}但我的写操作不起作用。
下面是我的写作方法,这是不完整的。
public int write(final String path, final ByteBuffer buf, final long bufSize, final long writeOffset,
final FileInfoWrapper wrapper)
{
return (int) bufSize;
}在调试器模式下运行它时,path参数显示Path=/..goutputstream xxx (其中xxx是随机字母数字,每次调用写方法时都是随机字母数字)。
请指导我如何正确地执行写操作。
发布于 2014-06-15 20:59:46
给你的文件名写上就行了。How do I create a file and write to it in Java?
你看到path=/.goutputstream-xxx的原因是因为https://askubuntu.com/a/151124。这不是保险丝的漏洞-jna。
https://stackoverflow.com/questions/24224404
复制相似问题