Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make symbolic links in FUSE?

I'm developing a FUSE app that takes a directory with mp3's and mounts a filesystem in another directory with the following structure (according to their tag's):

    Artist1
       |
       -----> Album11
                 |
                 -----> Track01
                 -----> Track02
                 -----> Track03
                 -----> Track04
                 -----> Track05
       -----> Album12
       ....
    Artist2
       |
       -----> Album21
       -----> Album22
       ....
    Artist3
    .....

I'm using a sqlite3 database to mantain the links to the real files. The artists and albums elements are folders, and the tracks elements are links to the real ones.

I have achieved to create the folders for the artists and the albums. But now I have a problem.

I have this:

  static int getattr(...) {

      ....
      else if ( level == 0  || level == 1 || level == 2 )
      {
          // Estamos en el primer nivel. Son artistas, y por lo tanto, carpetas.
          stbuf->st_mode = S_IFDIR | 0755;
          stbuf->st_nlink = 2;
          lstat(path, stbuf);
      }
      else if (level == 3) {
      // Estamos en el tercer nivel. Son canciones, por lo que son enlaces
      stbuf->st_mode = S_IFLNK | 0755;
      stbuf->st_nlink = 2;
      lstat(path, stbuf);
      }

      .....
  }

And now, When I ls in the tracks directory y get a message that tells me that the function is not implemented (the links functions). Which function do I have to implement to know where the link points? Or where do I have to fill the direction of the pointer?

Thanks!

like image 602
Javier Manzano Avatar asked May 23 '11 10:05

Javier Manzano


1 Answers

This seems to work for me:

  1. Add a int my_readlink(const char *path, char *buf, size_t size) function, which your struct fuse_operations member .readlink should point to. For the symbolic link path, your function should fill the buffer buf with a zero-terminated string which is the path of the link target. Don't write more than size bytes. E.g. do something like:

    strncpy(buf, "/target/path", size);
    
  2. In your my_getattr() function (which struct fuse_operations member .getattr should point to), for the symbolic link path, set in the 2nd parameter (struct stat * stbuf):

    • stbuf->st_mod to S_IFLNK | 0777
    • stbuf->st_nlink to 1
    • stbuf->st_size to the length of the target path (don't include the string's zero terminator in the length)
like image 50
Craig McQueen Avatar answered Sep 28 '22 10:09

Craig McQueen