Advertisement
mbah_bejo

fuse

Jun 3rd, 2021
784
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.97 KB | None | 0 0
  1. #define FUSE_USE_VERSION 28
  2. #include <fuse.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <fcntl.h>
  7. #include <dirent.h>
  8. #include <errno.h>
  9. #include <sys/time.h>
  10.  
  11. char buffer[1000];
  12.  
  13. void encode(const char *filename)
  14. {
  15.     sprintf(buffer, "Jago_%s", filename);
  16. }
  17.  
  18. void decode(const char *filename)
  19. {
  20.     const char *s = "/";
  21.     memset(buffer, '\0', sizeof(buffer));
  22.  
  23.     char *token = strtok((char *)filename, s);
  24.     while (token != NULL)
  25.     {
  26.         printf("token - %s\n", token);
  27.         strcat(buffer, "/");
  28.         if (!strncmp(token, "Jago_", 5))
  29.             strcat(buffer, token + 5);
  30.         else
  31.             strcat(buffer, token);
  32.         token = strtok(NULL, s);
  33.     }
  34.     printf("%s\n", buffer);
  35. }
  36.  
  37. static const char *dirpath = "/home/almond/Downloads";
  38.  
  39. static int xmp_getattr(const char *path, struct stat *stbuf)
  40. {
  41.     int res;
  42.     char fpath[1000];
  43.  
  44.     decode(path);
  45.     sprintf(fpath, "%s%s", dirpath, buffer);
  46.     printf("%s\n", fpath);
  47.  
  48.     res = lstat(fpath, stbuf);
  49.  
  50.     if (res == -1)
  51.         return -errno;
  52.  
  53.     return 0;
  54. }
  55.  
  56. static int xmp_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi)
  57. {
  58.     char fpath[1000];
  59.  
  60.     if (strcmp(path, "/") == 0)
  61.     {
  62.         path = dirpath;
  63.         sprintf(fpath, "%s", path);
  64.     }
  65.     else
  66.     {
  67.         decode(path);
  68.         sprintf(fpath, "%s%s", dirpath, buffer);
  69.     }
  70.  
  71.     int res = 0;
  72.  
  73.     DIR *dp;
  74.     struct dirent *de;
  75.     (void)offset;
  76.     (void)fi;
  77.  
  78.     dp = opendir(fpath);
  79.  
  80.     if (dp == NULL)
  81.         return -errno;
  82.  
  83.     while ((de = readdir(dp)) != NULL)
  84.     {
  85.         struct stat st;
  86.  
  87.         memset(&st, 0, sizeof(st));
  88.  
  89.         st.st_ino = de->d_ino;
  90.         st.st_mode = de->d_type << 12;
  91.         if (strcmp(de->d_name, ".") && strcmp(de->d_name, ".."))
  92.         {
  93.             encode(de->d_name);
  94.             res = (filler(buf, buffer, &st, 0));
  95.         }
  96.         else
  97.             res = (filler(buf, de->d_name, &st, 0));
  98.  
  99.         if (res != 0)
  100.             break;
  101.     }
  102.  
  103.     closedir(dp);
  104.  
  105.     return 0;
  106. }
  107.  
  108. static int xmp_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi)
  109. {
  110.     char fpath[1000];
  111.     if (strcmp(path, "/") == 0)
  112.     {
  113.         path = dirpath;
  114.  
  115.         sprintf(fpath, "%s", path);
  116.     }
  117.     else
  118.     {
  119.         decode(path);
  120.         sprintf(fpath, "%s%s", dirpath, buffer);
  121.     }
  122.  
  123.     printf("%s\n", fpath);
  124.  
  125.     int res = 0;
  126.     int fd = 0;
  127.  
  128.     (void)fi;
  129.  
  130.     fd = open(fpath, O_RDONLY);
  131.  
  132.     if (fd == -1)
  133.         return -errno;
  134.  
  135.     res = pread(fd, buf, size, offset);
  136.  
  137.     if (res == -1)
  138.         res = -errno;
  139.  
  140.     close(fd);
  141.  
  142.     return res;
  143. }
  144.  
  145. static struct fuse_operations xmp_oper = {
  146.     .getattr = xmp_getattr,
  147.     .readdir = xmp_readdir,
  148.     .read = xmp_read,
  149. };
  150.  
  151. int main(int argc, char *argv[])
  152. {
  153.     umask(0);
  154.     return fuse_main(argc, argv, &xmp_oper, NULL);
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement