rgpgfs.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include <errno.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. // posix
  6. #include <dirent.h>
  7. #include <sys/stat.h>
  8. #include <sys/types.h>
  9. #include <unistd.h>
  10. // http://libfuse.github.io/doxygen/globals.html
  11. #define FUSE_USE_VERSION 31
  12. #include <fuse.h>
  13. #define FUSE_PATH_BUF_LEN 256
  14. static char cache_dir[] = "/tmp/rgpgfs-cache-XXXXXX";
  15. static const size_t CACHE_PATH_BUF_LEN = sizeof(cache_dir) + FUSE_PATH_BUF_LEN;
  16. static int rgpgfs_mkdirs(char *path) {
  17. char *delimiter = strrchr(path, '/');
  18. if (delimiter == NULL) {
  19. errno = ENOTSUP;
  20. return 1;
  21. }
  22. *delimiter = '\0';
  23. struct stat statbuf;
  24. if (lstat(path, &statbuf) && (rgpgfs_mkdirs(path) || mkdir(path, S_IRWXU))) {
  25. *delimiter = '/';
  26. return 1;
  27. }
  28. *delimiter = '/';
  29. return 0;
  30. }
  31. static int rgpgfs_encrypt(const char *source_path, char *cache_path) {
  32. size_t source_path_len = strnlen(source_path, FUSE_PATH_BUF_LEN);
  33. if (source_path_len >= FUSE_PATH_BUF_LEN) {
  34. errno = ENAMETOOLONG;
  35. perror("rgpgfs_encrypt");
  36. return 1;
  37. }
  38. strcpy(cache_path, cache_dir);
  39. strcat(cache_path, source_path);
  40. struct stat source_stat;
  41. if (lstat(source_path, &source_stat)) {
  42. perror("rgpgfs_encrypt: could not stat source file");
  43. return 1;
  44. }
  45. if (rgpgfs_mkdirs(cache_path)) {
  46. perror("rgpgfs_encrypt: failed to create dirs");
  47. return 1;
  48. }
  49. FILE *cache_file = fopen(cache_path, "w");
  50. if (cache_file == NULL) {
  51. perror("rgpgfs_encrypt: failed to open cache file");
  52. return 1;
  53. }
  54. fprintf(cache_file, "path: %s\n", source_path);
  55. fprintf(cache_file, "size: %lu bytes\n", source_stat.st_size);
  56. fclose(cache_file);
  57. return 0;
  58. }
  59. static int rgpgfs_getattr(const char *path, struct stat *statbuf,
  60. struct fuse_file_info *fi) {
  61. int res = lstat(path, statbuf);
  62. // printf("rgpgfs_getattr(%s, ...) = %d\n", path, res);
  63. if (res == -1)
  64. return -errno;
  65. return 0;
  66. }
  67. static int rgpgfs_access(const char *path, int mask) {
  68. int res = access(path, mask);
  69. // printf("rgpgfs_access(%s, %d) = %d\n", path, mask, res);
  70. if (res == -1)
  71. return -errno;
  72. return 0;
  73. }
  74. static int rgpgfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
  75. off_t offset, struct fuse_file_info *fi,
  76. enum fuse_readdir_flags flags) {
  77. DIR *dirp = opendir(path);
  78. if (dirp == NULL)
  79. return -errno;
  80. struct dirent *entp;
  81. while ((entp = readdir(dirp)) != NULL) {
  82. struct stat statbf;
  83. memset(&statbf, 0, sizeof(statbf));
  84. statbf.st_ino = entp->d_ino;
  85. statbf.st_mode = entp->d_type << 12;
  86. if (filler(buf, entp->d_name, &statbf, 0, 0))
  87. break;
  88. }
  89. closedir(dirp);
  90. return 0;
  91. }
  92. static int rgpgfs_open(const char *source_path, struct fuse_file_info *fi) {
  93. char cache_path[CACHE_PATH_BUF_LEN];
  94. if (rgpgfs_encrypt(source_path, cache_path))
  95. return -errno;
  96. int res = open(cache_path, fi->flags);
  97. if (res == -1)
  98. return -errno;
  99. fi->fh = res;
  100. return 0;
  101. }
  102. static int rgpgfs_read(const char *path, char *buf, size_t count, off_t offset,
  103. struct fuse_file_info *fi) {
  104. if (fi == NULL) {
  105. return ENOTSUP;
  106. }
  107. ssize_t bytes_num = pread(fi->fh, buf, count, offset);
  108. if (bytes_num == -1)
  109. return -errno;
  110. return bytes_num;
  111. }
  112. static int rgpgfs_release(const char *path, struct fuse_file_info *fi) {
  113. close(fi->fh);
  114. return 0;
  115. }
  116. static struct fuse_operations rgpgfs_fuse_operations = {
  117. .getattr = rgpgfs_getattr,
  118. .open = rgpgfs_open,
  119. .read = rgpgfs_read,
  120. .release = rgpgfs_release,
  121. .readdir = rgpgfs_readdir,
  122. .access = rgpgfs_access,
  123. };
  124. int main(int argc, char *argv[]) {
  125. if (mkdtemp(cache_dir) == NULL) {
  126. return 1;
  127. }
  128. printf("cache: %s\n", cache_dir);
  129. // TODO rm -r cache_dir (see man nftw)
  130. return fuse_main(argc, argv, &rgpgfs_fuse_operations, NULL);
  131. }