4

In the implemention of xxx_readdir() in FUSE, I use the codes below:

static int hello_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
                         off_t offset, struct fuse_file_info *fi)
{
    DIR *dp;
    struct dirent *de;

    (void) offset;
    (void) fi;

    dp = opendir(path);
    if (dp == NULL)
        return -errno;

    while ((de = readdir(dp)) != NULL) {
        struct stat st;
        memset(&st, 0, sizeof(st));
        st.st_ino = de->d_ino;
        st.st_mode = de->d_type << 12;
        if (filler(buf, de->d_name, &st, 0))
            break;
    }

    closedir(dp);
    return 0;
}

Then, compile and execute on a foler:

./hello /tmp/hello/

When I use ls /tmp/hello/ command, the results are as below:

bin   dev  home  lib64       media  opt   root  sbin  sys  usr
boot  etc  lib   lost+found  mnt    proc  run   srv   tmp  var

However, I have not created any file or directory in /tmp/hello/. So, why are these direcoties reside on it when I use the ls command?

injoy
  • 3,993
  • 10
  • 40
  • 69

1 Answers1

5

You call:

dp = opendir(path);

to begin your readdir implementation. When you call that path is relative to the root of your filesystem, not an absolute path on your system.

So in /tmp/hello/ the value of path will be '/' because it doesn't make sense for filesystems to need to know the details of where they're mounted. There's a deliberate abstraction there that makes it such that each filesystem is only concerned with things it stores.

Flexo
  • 87,323
  • 22
  • 191
  • 272
  • Then, what if I want `readdir` could display the files and directories that really in the `/tmp/hello` folder? What shall I do? – injoy Jul 31 '12 at 07:00
  • @injoy - that's tricky because you have to make sure that your `opendir` call doesn't get passed back to fuse. A better design would be to have the mount point and the directory you show as different directories, i.e. `/tmp/hello.real` which your FUSE program mounts in `/tmp/hello`. You can pass the 'real' path to your program at run time and then just record that. – Flexo Jul 31 '12 at 07:02
  • thanks and btw, could you recommend some good and simple (more complex than `hello.c`) FS implementation using FUSE? – injoy Jul 31 '12 at 07:07
  • @injoy - there's a list on the FUSE site. I'd suggest [localfs](http://sourceforge.net/apps/mediawiki/fuse/index.php?title=FileSystems#localfs) since it sounds similar to what you're trying to do. The network ones usually make good examples, I think I read httpfs last time I was looking for one. – Flexo Jul 31 '12 at 07:11