rgpgfs.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include <errno.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. // posix
  5. #include <dirent.h>
  6. #include <sys/stat.h>
  7. #include <unistd.h>
  8. #define FUSE_USE_VERSION 31
  9. #include <fuse.h>
  10. static int rgpgfs_getattr(const char *path, struct stat *statbuf,
  11. struct fuse_file_info *fi) {
  12. int res = lstat(path, statbuf);
  13. // printf("rgpgfs_getattr(%s, ...) = %d\n", path, res);
  14. if (res == -1)
  15. return -errno;
  16. return 0;
  17. }
  18. static int rgpgfs_access(const char *path, int mask) {
  19. int res = access(path, mask);
  20. // printf("rgpgfs_access(%s, %d) = %d\n", path, mask, res);
  21. if (res == -1)
  22. return -errno;
  23. return 0;
  24. }
  25. static int rgpgfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
  26. off_t offset, struct fuse_file_info *fi,
  27. enum fuse_readdir_flags flags) {
  28. DIR *dirp = opendir(path);
  29. if (dirp == NULL)
  30. return -errno;
  31. struct dirent *entp;
  32. while ((entp = readdir(dirp)) != NULL) {
  33. struct stat statbf;
  34. memset(&statbf, 0, sizeof(statbf));
  35. statbf.st_ino = entp->d_ino;
  36. statbf.st_mode = entp->d_type << 12;
  37. if (filler(buf, entp->d_name, &statbf, 0, 0))
  38. break;
  39. }
  40. closedir(dirp);
  41. return 0;
  42. }
  43. static int rgpgfs_open(const char *path, struct fuse_file_info *fi) {
  44. int res = open(path, fi->flags);
  45. if (res == -1)
  46. return -errno;
  47. fi->fh = res;
  48. return 0;
  49. }
  50. static int rgpgfs_read(const char *path, char *buf, size_t count, off_t offset,
  51. struct fuse_file_info *fi) {
  52. if (fi == NULL) {
  53. return ENOTSUP;
  54. }
  55. ssize_t bytes_num = pread(fi->fh, buf, count, offset);
  56. if (bytes_num == -1)
  57. return -errno;
  58. return bytes_num;
  59. }
  60. static int rgpgfs_release(const char *path, struct fuse_file_info *fi) {
  61. close(fi->fh);
  62. return 0;
  63. }
  64. static struct fuse_operations rgpgfs_fuse_operations = {
  65. .getattr = rgpgfs_getattr,
  66. .open = rgpgfs_open,
  67. .read = rgpgfs_read,
  68. .release = rgpgfs_release,
  69. .readdir = rgpgfs_readdir,
  70. .access = rgpgfs_access,
  71. };
  72. int main(int argc, char *argv[]) {
  73. return fuse_main(argc, argv, &rgpgfs_fuse_operations, NULL);
  74. }