rgpgfs.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. // fprintf(stderr, "rgpgfs_encrypt('%s', %p)\n", source_path, cache_path);
  33. size_t source_path_len = strnlen(source_path, FUSE_PATH_BUF_LEN);
  34. if (source_path_len >= FUSE_PATH_BUF_LEN) {
  35. errno = ENAMETOOLONG;
  36. perror("rgpgfs_encrypt");
  37. return 1;
  38. }
  39. strcpy(cache_path, cache_dir);
  40. strcat(cache_path, source_path);
  41. struct stat source_stat;
  42. if (lstat(source_path, &source_stat)) {
  43. perror("rgpgfs_encrypt: could not stat source file");
  44. return 1;
  45. }
  46. struct stat cache_stat;
  47. if (lstat(cache_path, &cache_stat) ||
  48. source_stat.st_mtim.tv_sec > cache_stat.st_mtim.tv_sec) {
  49. if (rgpgfs_mkdirs(cache_path)) {
  50. perror("rgpgfs_encrypt: failed to create dirs");
  51. return 1;
  52. }
  53. FILE *cache_file = fopen(cache_path, "w");
  54. if (cache_file == NULL) {
  55. perror("rgpgfs_encrypt: failed to open cache file");
  56. return 1;
  57. }
  58. fprintf(cache_file, "path: %s\n", source_path);
  59. fprintf(cache_file, "size: %lu bytes\n", source_stat.st_size);
  60. fprintf(cache_file, "mod time: %ld\n", source_stat.st_mtim.tv_sec);
  61. fclose(cache_file);
  62. printf("encrypted %s\n", source_path);
  63. }
  64. return 0;
  65. }
  66. static int rgpgfs_getattr(const char *source_path, struct stat *statbuf,
  67. struct fuse_file_info *fi) {
  68. if (lstat(source_path, statbuf))
  69. return -errno;
  70. if (!S_ISDIR(statbuf->st_mode)) {
  71. char cache_path[CACHE_PATH_BUF_LEN];
  72. if (rgpgfs_encrypt(source_path, cache_path))
  73. return -errno;
  74. if (lstat(cache_path, statbuf))
  75. return -errno;
  76. }
  77. return 0;
  78. }
  79. static int rgpgfs_access(const char *path, int mask) {
  80. int res = access(path, mask);
  81. // printf("rgpgfs_access(%s, %d) = %d\n", path, mask, res);
  82. if (res == -1)
  83. return -errno;
  84. return 0;
  85. }
  86. static int rgpgfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
  87. off_t offset, struct fuse_file_info *fi,
  88. enum fuse_readdir_flags flags) {
  89. DIR *dirp = opendir(path);
  90. if (dirp == NULL)
  91. return -errno;
  92. struct dirent *entp;
  93. while ((entp = readdir(dirp)) != NULL) {
  94. struct stat statbf;
  95. memset(&statbf, 0, sizeof(statbf));
  96. statbf.st_ino = entp->d_ino;
  97. statbf.st_mode = entp->d_type << 12;
  98. if (filler(buf, entp->d_name, &statbf, 0, 0))
  99. break;
  100. }
  101. closedir(dirp);
  102. return 0;
  103. }
  104. static int rgpgfs_open(const char *source_path, struct fuse_file_info *fi) {
  105. // fprintf(stderr, "rgpgfs_open('%s', %p)", source_path, fi);
  106. char cache_path[CACHE_PATH_BUF_LEN];
  107. if (rgpgfs_encrypt(source_path, cache_path))
  108. return -errno;
  109. int res = open(cache_path, fi->flags);
  110. if (res == -1)
  111. return -errno;
  112. fi->fh = res;
  113. return 0;
  114. }
  115. static int rgpgfs_read(const char *path, char *buf, size_t count, off_t offset,
  116. struct fuse_file_info *fi) {
  117. if (fi == NULL) {
  118. return ENOTSUP;
  119. }
  120. ssize_t bytes_num = pread(fi->fh, buf, count, offset);
  121. if (bytes_num == -1)
  122. return -errno;
  123. return bytes_num;
  124. }
  125. static int rgpgfs_release(const char *path, struct fuse_file_info *fi) {
  126. close(fi->fh);
  127. return 0;
  128. }
  129. static struct fuse_operations rgpgfs_fuse_operations = {
  130. .getattr = rgpgfs_getattr,
  131. .open = rgpgfs_open,
  132. .read = rgpgfs_read,
  133. .release = rgpgfs_release,
  134. .readdir = rgpgfs_readdir,
  135. .access = rgpgfs_access,
  136. };
  137. int main(int argc, char *argv[]) {
  138. if (mkdtemp(cache_dir) == NULL) {
  139. return 1;
  140. }
  141. printf("cache: %s\n", cache_dir);
  142. // TODO rm -r cache_dir (see man nftw)
  143. return fuse_main(argc, argv, &rgpgfs_fuse_operations, NULL);
  144. }