Implementing writing to a file in filesystem using FUSE

旧城冷巷雨未停 提交于 2019-12-13 04:33:10

问题


I'm implementing simple in-memory file system with FUSE using this Java binding https://github.com/dtrott/fuse4j I did reading and creating a file support, but can't get writing to a file working. I always get an error with an attempt to write. Here are some implementations:

   public int truncate(String s, long l) throws FuseException {
        System.out.println("truncate: " + s + " l: " +l);
        Node node = lookup(s);
                        if (node == null)
                               return Errno.ENOENT;

        node.setFileSize(node.getFileSize()+l);

        return 0; 
    }

    public int statfs(FuseStatfsSetter fuseStatfsSetter) throws FuseException {
        fuseStatfsSetter.set(512, 1000, 200, 180, 5, 29, 20);
        return 0;
    }

    public int open(String s, int i, FuseOpenSetter fuseOpenSetter) throws FuseException {
        System.out.println("open: " + s);
        Node node = lookup(s);
                if (node == null)
                       return Errno.ENOENT;

        fuseOpenSetter.setFh(node);
        return 0; 
    }

    public int read(String s, Object o, ByteBuffer byteBuffer, long l) throws FuseException {
        System.out.println("read: " + s);
        Node node = lookup(s);
        if (node == null)
               return Errno.EBADF;
        if (node.getContents() != null)
            byteBuffer.put(node.getContents());
        return 0; 
    }

    public int write(String s, Object o, boolean b, ByteBuffer byteBuffer, long l) throws FuseException {
        System.out.println("write: " + s + " byte: " + byteBuffer.array().length + " l: " + l);
        Node node = lookup(s);
        if (node == null)
            return 0;

        node.setContents(byteBuffer.array());
        node.setFileSize(node.getFileSize()+byteBuffer.array().length);
        return byteBuffer.array().length;
    }

statfs uses some hardcoded values without a specific meaning, flush, fsync and release always return 0.

来源:https://stackoverflow.com/questions/8537066/implementing-writing-to-a-file-in-filesystem-using-fuse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!