Advertisement
mbah_bejo

fuse2

Jun 3rd, 2021
751
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.62 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. static const char *dirpath = "/home/almond/Documents";
  12.  
  13. void writelog(const char *path)
  14. {
  15.     char *filename = strrchr(path, '/') + 1;
  16.     char *extension = strrchr(filename, '.');
  17.  
  18.     char text[100];
  19.     time_t now = time(NULL);
  20.     struct tm *t = localtime(&now);
  21.     strftime(text, sizeof(text) - 1, "%H_%M_%S:%d-%m-%Y", t);
  22.  
  23.     FILE *logfile = fopen("log.txt", "a");
  24.  
  25.     if (extension != NULL)
  26.     {
  27.         *extension = '\0';
  28.         fprintf(logfile, "%s.%s.%s\n", filename, text, extension + 1);
  29.     }
  30.     else
  31.         fprintf(logfile, "%s.%s.unknown\n", filename, text);
  32.     fclose(logfile);
  33. }
  34.  
  35. static int xmp_getattr(const char *path, struct stat *stbuf)
  36. {
  37.     int res;
  38.     char fpath[1000];
  39.  
  40.     sprintf(fpath, "%s%s", dirpath, path);
  41.  
  42.     res = lstat(fpath, stbuf);
  43.  
  44.     if (res == -1)
  45.         return -errno;
  46.  
  47.     return 0;
  48. }
  49.  
  50. static int xmp_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi)
  51. {
  52.     char fpath[1000];
  53.  
  54.     if (strcmp(path, "/") == 0)
  55.     {
  56.         path = dirpath;
  57.         sprintf(fpath, "%s", path);
  58.     }
  59.     else
  60.         sprintf(fpath, "%s%s", dirpath, path);
  61.  
  62.     int res = 0;
  63.  
  64.     DIR *dp;
  65.     struct dirent *de;
  66.     (void)offset;
  67.     (void)fi;
  68.  
  69.     dp = opendir(fpath);
  70.  
  71.     if (dp == NULL)
  72.         return -errno;
  73.  
  74.     while ((de = readdir(dp)) != NULL)
  75.     {
  76.         struct stat st;
  77.  
  78.         memset(&st, 0, sizeof(st));
  79.  
  80.         st.st_ino = de->d_ino;
  81.         st.st_mode = de->d_type << 12;
  82.         res = (filler(buf, de->d_name, &st, 0));
  83.  
  84.         if (res != 0)
  85.             break;
  86.     }
  87.  
  88.     closedir(dp);
  89.  
  90.     return 0;
  91. }
  92.  
  93. static int xmp_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi)
  94. {
  95.     char fpath[1000];
  96.     if (strcmp(path, "/") == 0)
  97.     {
  98.         path = dirpath;
  99.  
  100.         sprintf(fpath, "%s", path);
  101.     }
  102.     else
  103.         sprintf(fpath, "%s%s", dirpath, path);
  104.  
  105.     int res = 0;
  106.     int fd = 0;
  107.  
  108.     (void)fi;
  109.  
  110.     fd = open(fpath, O_RDONLY);
  111.  
  112.     if (fd == -1)
  113.         return -errno;
  114.  
  115.     res = pread(fd, buf, size, offset);
  116.  
  117.     if (res == -1)
  118.         res = -errno;
  119.  
  120.     close(fd);
  121.  
  122.     return res;
  123. }
  124.  
  125. static int xmp_mknod(const char *path, mode_t mode, dev_t rdev)
  126. {
  127.  
  128.     char fpath[1000];
  129.     if (strcmp(path, "/") == 0)
  130.     {
  131.         path = dirpath;
  132.  
  133.         sprintf(fpath, "%s", path);
  134.     }
  135.     else
  136.         sprintf(fpath, "%s%s", dirpath, path);
  137.  
  138.     int res;
  139.     if (S_ISREG(mode))
  140.     {
  141.         res = open(fpath, O_CREAT | O_EXCL | O_WRONLY, mode);
  142.         if (res >= 0)
  143.             res = close(res);
  144.     }
  145.     else if (S_ISFIFO(mode))
  146.         res = mkfifo(fpath, mode);
  147.     else
  148.         res = mknod(fpath, mode, rdev);
  149.     if (res == -1)
  150.         return -errno;
  151.  
  152.     return 0;
  153. }
  154.  
  155. static int xmp_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi)
  156. {
  157.     char fpath[1000];
  158.     if (strcmp(path, "/") == 0)
  159.     {
  160.         path = dirpath;
  161.  
  162.         sprintf(fpath, "%s", path);
  163.     }
  164.     else
  165.         sprintf(fpath, "%s%s", dirpath, path);
  166.  
  167.     int fd = 0;
  168.     int res = 0;
  169.  
  170.     (void)fi;
  171.  
  172.     fd = open(fpath, O_WRONLY);
  173.  
  174.     if (fd == -1)
  175.         return -errno;
  176.  
  177.     res = pwrite(fd, buf, size, offset);
  178.     if (res == -1)
  179.         res = -errno;
  180.     else
  181.         writelog(path);
  182.  
  183.     close(fd);
  184.     return res;
  185. }
  186.  
  187. static int xmp_unlink(const char *path)
  188. {
  189.     int res;
  190.  
  191.     char fpath[1000];
  192.     if (strcmp(path, "/") == 0)
  193.     {
  194.         path = dirpath;
  195.  
  196.         sprintf(fpath, "%s", path);
  197.     }
  198.     else
  199.         sprintf(fpath, "%s%s", dirpath, path);
  200.  
  201.     res = unlink(fpath);
  202.     if (res == -1)
  203.         return -errno;
  204.  
  205.     return 0;
  206. }
  207.  
  208. static int xmp_truncate(const char *path, off_t size)
  209. {
  210.     int res;
  211.  
  212.     char fpath[1000];
  213.     if (strcmp(path, "/") == 0)
  214.     {
  215.         path = dirpath;
  216.  
  217.         sprintf(fpath, "%s", path);
  218.     }
  219.     else
  220.         sprintf(fpath, "%s%s", dirpath, path);
  221.  
  222.     res = truncate(fpath, size);
  223.     if (res == -1)
  224.         return -errno;
  225.  
  226.     return 0;
  227. }
  228.  
  229. static struct fuse_operations xmp_oper = {
  230.     .getattr = xmp_getattr,
  231.     .readdir = xmp_readdir,
  232.     .read = xmp_read,
  233.     .write = xmp_write,
  234.     .mknod = xmp_mknod,
  235.     .unlink = xmp_unlink,
  236.     // .truncate = xmp_truncate,
  237. };
  238.  
  239. int main(int argc, char *argv[])
  240. {
  241.     umask(0);
  242.     return fuse_main(argc, argv, &xmp_oper, NULL);
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement