rgpgfs.c 3.9 KB

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