Advertisement
Guest User

Untitled

a guest
Dec 4th, 2023
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.64 KB | None | 0 0
  1. #define FUSE_USE_VERSION 30
  2. #include <fuse.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <errno.h>
  7. #include <fcntl.h>
  8. #include <dirent.h>
  9. #include <unistd.h>
  10.  
  11. #ifndef PATH_MAX
  12. #define PATH_MAX 4096
  13. #endif
  14.  
  15. static const char *basedir;
  16. static char mountpoint[PATH_MAX];
  17.  
  18. static int simplefs_getattr(const char *path, struct stat *stbuf)
  19. {
  20.     int res = 0;
  21.     char full_path[PATH_MAX];
  22.     snprintf(full_path, sizeof(full_path), "%s%s", basedir, path);
  23.  
  24.     res = lstat(full_path, stbuf);
  25.     if (res == -1)
  26.         return -errno;
  27.  
  28.     return 0;
  29. }
  30.  
  31. static int simplefs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
  32.                             off_t offset, struct fuse_file_info *fi)
  33. {
  34.     DIR *dp;
  35.     struct dirent *de;
  36.  
  37.     (void)offset;
  38.     (void)fi;
  39.  
  40.     char full_path[PATH_MAX];
  41.     snprintf(full_path, sizeof(full_path), "%s%s", basedir, path);
  42.  
  43.     dp = opendir(full_path);
  44.     if (dp == NULL)
  45.         return -errno;
  46.  
  47.     filler(buf, ".", NULL, 0);
  48.     filler(buf, "..", NULL, 0);
  49.  
  50.     while ((de = readdir(dp)) != NULL) {
  51.         filler(buf, de->d_name, NULL, 0);
  52.     }
  53.  
  54.     closedir(dp);
  55.     return 0;
  56. }
  57.  
  58. static int simplefs_read(const char *path, char *buf, size_t size, off_t offset,
  59.                          struct fuse_file_info *fi)
  60. {
  61.     int fd;
  62.     int res;
  63.     char full_path[PATH_MAX];
  64.     snprintf(full_path, sizeof(full_path), "%s%s", basedir, path);
  65.  
  66.     (void)fi;
  67.     fd = open(full_path, O_RDONLY);
  68.     if (fd == -1)
  69.         return -errno;
  70.  
  71.     res = pread(fd, buf, size, offset);
  72.     if (res == -1)
  73.         res = -errno;
  74.  
  75.     close(fd);
  76.     return res;
  77. }
  78.  
  79. static int simplefs_write(const char *path, const char *buf, size_t size,
  80.                           off_t offset, struct fuse_file_info *fi)
  81. {
  82.     int fd;
  83.     int res;
  84.     char full_path[PATH_MAX];
  85.     snprintf(full_path, sizeof(full_path), "%s%s", basedir, path);
  86.  
  87.     (void)fi;
  88.     fd = open(full_path, O_WRONLY);
  89.     if (fd == -1)
  90.         return -errno;
  91.  
  92.     res = pwrite(fd, buf, size, offset);
  93.     if (res == -1)
  94.         res = -errno;
  95.  
  96.     close(fd);
  97.     return res;
  98. }
  99.  
  100. static int simplefs_mkdir(const char *path, mode_t mode)
  101. {
  102.     int res;
  103.     char full_path[PATH_MAX];
  104.     snprintf(full_path, sizeof(full_path), "%s%s", basedir, path);
  105.  
  106.     res = mkdir(full_path, mode);
  107.     if (res == -1)
  108.         return -errno;
  109.  
  110.     return 0;
  111. }
  112.  
  113. static int simplefs_rmdir(const char *path)
  114. {
  115.     int res;
  116.     char full_path[PATH_MAX];
  117.     snprintf(full_path, sizeof(full_path), "%s%s", basedir, path);
  118.  
  119.     res = rmdir(full_path);
  120.     if (res == -1)
  121.         return -errno;
  122.  
  123.     return 0;
  124. }
  125.  
  126. static int simplefs_unlink(const char *path)
  127. {
  128.     int res;
  129.     char full_path[PATH_MAX];
  130.     snprintf(full_path, sizeof(full_path), "%s%s", basedir, path);
  131.  
  132.     res = unlink(full_path);
  133.     if (res == -1)
  134.         return -errno;
  135.  
  136.     return 0;
  137. }
  138.  
  139. static struct fuse_operations simplefs_operations = {
  140.     .getattr = simplefs_getattr,
  141.     .readdir = simplefs_readdir,
  142.     .read = simplefs_read,
  143.     .write = simplefs_write,
  144.     .mkdir = simplefs_mkdir,
  145.     .rmdir = simplefs_rmdir,
  146.     .unlink = simplefs_unlink,
  147. };
  148.  
  149. int main(int argc, char *argv[])
  150. {
  151.     if (argc != 3) {
  152.         fprintf(stderr, "Usage: %s <mountpoint> <basedir>\n", argv[0]);
  153.         return EXIT_FAILURE;
  154.     }
  155.  
  156.     basedir = realpath(argv[2], NULL);
  157.     if (basedir == NULL) {
  158.         perror("Error getting realpath");
  159.         return EXIT_FAILURE;
  160.     }
  161.  
  162.     strcpy(mountpoint, argv[1]);
  163.     strcat(mountpoint, "1");
  164.  
  165.     return fuse_main(argc - 1, argv + 1, &simplefs_operations, NULL);
  166. }
  167.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement