1

I am writing a custom fuse Mirror File System (in Ubuntu using FUSE-JNA). By mirror I mean, It will read from and write into a directory of local file system.

I implemented getattr, create, and read operation as below. All these work perfectly.

...
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();
}

below create method creates new file in mirrored folder

    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 method reads file from mirrored folder

    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;

}

But my write operation is not working.

Below is my Write method, which is incomplete.

public int write(final String path, final ByteBuffer buf, final long bufSize, final long writeOffset,
        final FileInfoWrapper wrapper)
{

    return (int) bufSize;
}

When I run it in Debugger mode, the path arguments shows Path=/.goutputstream-xxx (where xxx is random alphanumeric each time write method is called)

Please guide me how to correctly implement write operation.

1 Answers1

1

Just write to the filename you're given. How do I create a file and write to it in Java?

The reason you're seeing path=/.goutputstream-xxx is because of https://askubuntu.com/a/151124. This isn't a bug in fuse-jna.

Community
  • 1
  • 1
Etienne Perot
  • 4,764
  • 7
  • 40
  • 50
  • if I get `/.goutputstream-xxx` in path argument then how would I know which file to write. for instance, if I edit and save file _abc.txt_. The path argument in write() method show `/.goutputstream-xxx` instead of currently edited file. I know its ubuntu bug. I updated it to ubuntu version 14 from 12, but still the same problem. Which OS should now I try/ or any other solution. – Syed Rahman Mashwani Jun 16 '14 at 18:44
  • 1
    You're getting `/.goutputstream-xxx` because of the lightdm bug which tries to write to those files, independently of the text editor writing `/abc.txt`. If you get `/.goutputstream-xxx`, then write to `/.goutputstream-xxx`. When the text editor saves, you will get `/abc.txt`. The function is called multiple times (once per block to write for each file). – Etienne Perot Jun 21 '14 at 05:52
  • 1
    Thanks. I completed this project and made it public here [Java FUSE Mirror File System](https://github.com/Syed-Rahman-Mashwani/Java-FUSE-Mirror-File-System) – Syed Rahman Mashwani Jul 22 '14 at 15:45