rgpgfs.c 2.2 KB

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