How to intercept storage size query commands

123 Views Asked by At

I am developing a file system using libfuse and need to find a way to intercept calls for storage size querying, i.e. du and df. But I have been unable to identify how to this and unable to find an example that showcase this.

Looking at the debug output from my filesystem, doesn't give much information either, as I am unsure which call I should intercept.

1

There are 1 best solutions below

0
On BEST ANSWER

For the df you can implement the statfs() operation, something like this:

static int do_statfs(const char *path, struct statvfs *st)
{
        int rv; 

        rv = statvfs("/", st);
        st->f_bavail = 15717083;

        return rv; 
}

In the example above, just to simplify, I query the root filesystem, than modify blocks available, but you can (and should) feel the complete statvfs structure with the information regarding your filesystem.

Now for the du, the man says: "Summarize disk usage of each FILE, recursively for directories", so every file will be queried. For this you need to implement the stat() operation.

static int do_getattr(const char *path, struct stat *st)
{
    st->st_uid = getuid();
    st->st_gid = getgid();
    st->st_atime = time(NULL);
    st->st_mtime = time(NULL);

    // fill the rest of the stat structure

    return 0;
}

Once those are implemented you have to add them do fuse_operations structure:

static struct fuse_operations operations = {
        .open           = do_open,
        .getattr        = do_getattr,
        .readdir        = do_readdir,
        .read           = do_read,
        .statfs         = do_statfs,
        .release        = do_release,
};

and pass it as a parameter to the fuse_main()

int main(int argc, char *argv[])
{
        return fuse_main(argc, argv, &operations, NULL);
}