Giorgos_Xou

Attempted Inode-based VFS (Basic)

Sep 17th, 2024 (edited)
214
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.48 KB | Source Code | 0 0
  1. // https://stackoverflow.com/questions/78986903/fuse3-inability-to-create-inode-based-virtual-file-system
  2.  
  3. #define FUSE_USE_VERSION 31
  4. #include <fuse3/fuse.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <dirent.h>
  8. #include <errno.h>
  9.  
  10. static void *xmp_init(struct fuse_conn_info *conn, struct fuse_config *cfg) {
  11.     // From passthrough example
  12.     cfg->use_ino          = 1;
  13.     cfg->entry_timeout    = 0;
  14.     cfg->attr_timeout     = 0;
  15.     cfg->negative_timeout = 0;
  16.  
  17.     // // From hello.c example
  18.     // cfg->kernel_cache = 1;
  19.  
  20.     // cfg->readdir_ino =  1;
  21.     // cfg->remember    = -1;
  22.  
  23.  
  24.     return NULL;
  25. }
  26.  
  27. static int xmp_getattr(const char *path, struct stat *stbuf, struct fuse_file_info *fi) {
  28.     (void) fi;
  29.     int res;
  30.  
  31.     // NOTE: Why it doesn't read the inode of "/hello.txt" ???
  32.     // (it prints ERROR for that file too if you `ls`)
  33.     // the only thing I want, is to read the inode, nothing more.
  34.     res = lstat(path, stbuf);
  35.     if (res == -1){
  36.         printf("\033[91mERROR: getattr %lu %s\033[0m\n", stbuf->st_ino, path); // stbuf->st_ino just in case it returns -1 but fills the inode
  37.         return -errno;
  38.     }
  39.  
  40.     printf("getattr %lu = %s\n", stbuf->st_ino, path);
  41.  
  42.     return 0;
  43. }
  44.  
  45. static int xmp_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi, enum fuse_readdir_flags flags) {
  46.     if (strcmp(path, "/") != 0)
  47.         return -ENOENT;
  48.  
  49.     printf("readdir: %s\n", path );
  50.     filler(buf, ".", NULL, 0, 0);
  51.     filler(buf, "..", NULL, 0, 0);
  52.  
  53.     struct stat st;
  54.     memset(&st, 0, sizeof(st));
  55.     st.st_ino = 5; // NOTE: WHY INODE IS NOT SET ???????
  56.     st.st_mode = S_IFREG | 0444;
  57.     filler(buf, "hello.txt", &st, 0, 0); // maybe use "..., FUSE_FILL_DIR_PLUS);"
  58.  
  59.  
  60.     // // passthrough.c
  61.     // DIR *dp;
  62.     // struct dirent *de;
  63.  
  64.     // (void) offset;
  65.     // (void) fi;
  66.     // (void) flags;
  67.  
  68.     // dp = opendir(path);
  69.     // if (dp == NULL)
  70.     //  return -errno;
  71.     //
  72.     // while ((de = readdir(dp)) != NULL) {
  73.     //  b++;
  74.     //  struct stat st;
  75.     //  memset(&st, 0, sizeof(st));
  76.     //  st.st_ino = de->d_ino;
  77.     //  st.st_mode = de->d_type << 12;
  78.     //  // printf("%lu : %lu = %s\n", de->d_ino, b, de->d_name );
  79.     //  char buf[100];
  80.     //  snprintf(buf, 100, "name_%lu", b);
  81.     //  if (filler(buf, buf, &st, 0, 0))
  82.     //      break;
  83.     // }
  84.  
  85.     // closedir(dp);
  86.  
  87.     return 0;
  88. }
  89.  
  90. static struct fuse_operations xmp_oper = {
  91.     .init     = xmp_init,
  92.     .getattr    = xmp_getattr,
  93.     .readdir    = xmp_readdir,
  94. };
  95.  
  96. int main(int argc, char *argv[]) {
  97.     // umask(0);
  98.     return fuse_main(argc, argv, &xmp_oper, NULL);
  99. }
  100.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment