You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

236 line
6.0 KiB

5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
5 年之前
  1. #include <fuse.h>
  2. #include <unistd.h>
  3. #include <errno.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include "source_lsfs/lsfs_disk_controller.h"
  8. int lsfs_getattr( const char *, struct stat * );
  9. int lsfs_mkdir(const char *path, mode_t mode);
  10. int lsfs_mknod(const char *path, mode_t mode, dev_t device);
  11. int lsfs_open( const char *, struct fuse_file_info * );
  12. int lsfs_read( const char *, char *, size_t, off_t, struct fuse_file_info * );
  13. int lsfs_readdir( const char *, void *, fuse_fill_dir_t, off_t, struct fuse_file_info * );
  14. int lsfs_release(const char *path, struct fuse_file_info *fi);
  15. int lsfs_rename(const char *from, const char *to);
  16. int lsfs_rmdir(const char *path);
  17. int lsfs_truncate(const char *path, off_t offset);
  18. int lsfs_unlink(const char *);
  19. int lsfs_write(const char *, const char *, size_t, off_t, struct fuse_file_info *);
  20. static inline int lsfs_utime_STUB(const char *path, struct utimbuf *buf) {
  21. (void)path;
  22. (void)buf;
  23. return 0;
  24. }
  25. static struct fuse_operations lsfs_oper = {
  26. .getattr = lsfs_getattr,
  27. .readdir = lsfs_readdir,
  28. .mknod = lsfs_mknod,
  29. .mkdir = lsfs_mkdir,
  30. .link = NULL,
  31. .unlink = lsfs_unlink,
  32. .rmdir = lsfs_rmdir,
  33. .truncate = lsfs_truncate,
  34. .open = lsfs_open,
  35. .read = lsfs_read,
  36. .release = lsfs_release,
  37. .write = lsfs_write,
  38. .rename = lsfs_rename,
  39. .utime = lsfs_utime_STUB,
  40. };
  41. int lsfs_readdir( const char *path, void *buf, fuse_fill_dir_t filler, off_t offset_to_next_entry, struct fuse_file_info *fi )
  42. {
  43. //(void) offset;
  44. (void) fi;
  45. //printf("readdir: (path=%s)\n", path);
  46. filler(buf, ".", NULL, 0);
  47. filler(buf, "..", NULL, 0);
  48. Directory_Table *directory_table;
  49. directory_table = lsfs_find_directory(path, false);
  50. if (directory_table != NULL)
  51. {
  52. for (int i = 0; i < DEFAULT_TABLE_SIZE; ++i)
  53. {
  54. if (strcmp( "", directory_table->entries[i].filename ) != 0)
  55. {
  56. filler(buf, directory_table->entries[i].filename, NULL, 0);
  57. }
  58. }
  59. }
  60. if (strcmp(path, "/") != 0)
  61. {
  62. //free(directory_table);
  63. }
  64. return 0;
  65. }
  66. int lsfs_getattr( const char *path, struct stat *stbuf )
  67. {
  68. int res = 0;
  69. lsfs_file *found_file = calloc(1, sizeof(lsfs_file));
  70. //printf("getattr: (path=%s)\n", path);
  71. memset(stbuf, 0, sizeof(struct stat));
  72. if( strcmp( path, "/" ) == 0 )
  73. {
  74. stbuf->st_mode = S_IFDIR | 0755;
  75. stbuf->st_nlink = 2;
  76. }
  77. else
  78. {
  79. if(lsfs_disk_getattr(found_file, path))
  80. {
  81. if (found_file->entry_kind == ENTRY_FILE)
  82. {
  83. stbuf->st_mode = S_IFREG | 0777;
  84. }
  85. else if (found_file->entry_kind == ENTRY_DIRECTORY)
  86. {
  87. stbuf->st_mode = S_IFDIR | 0755;
  88. stbuf->st_nlink = 2;
  89. }
  90. else
  91. {
  92. res = -ENOENT;
  93. }
  94. stbuf->st_nlink = 1; // @Hardcode
  95. stbuf->st_size = found_file->size;
  96. stbuf->st_uid = found_file->owner_id;
  97. stbuf->st_gid = found_file->owner_id;
  98. stbuf->st_atime = found_file->access_time;
  99. stbuf->st_mtime = found_file->modification_time;
  100. //free(found_file);
  101. }
  102. else
  103. {
  104. res = -ENOENT;
  105. }
  106. }
  107. return res;
  108. }
  109. int lsfs_mkdir(const char *path, mode_t mode) {
  110. (void)mode;
  111. return lsfs_disk_create_entry(path, ENTRY_DIRECTORY);
  112. }
  113. int lsfs_rmdir(const char *path) {
  114. // call to the disk controller to remove a dir
  115. if (lsfs_disk_delete_directory(path))
  116. {
  117. // error case:
  118. // directory is not empty
  119. return -EINVAL;
  120. }
  121. return 0;
  122. }
  123. int lsfs_unlink(const char *path) {
  124. lsfs_file *found_file = calloc(1, sizeof(lsfs_file));
  125. lsfs_disk_getattr(found_file, path);
  126. lsfs_disk_delete_entry(found_file);
  127. free(found_file);
  128. return 0;
  129. }
  130. int lsfs_truncate(const char *path, off_t offset) {
  131. (void)offset; // Truncate to this byte?
  132. lsfs_file *found_file = calloc(1, sizeof(lsfs_file));
  133. lsfs_disk_getattr(found_file, path);
  134. lsfs_disk_truncate_file(found_file, offset);
  135. free(found_file);
  136. return 0;
  137. }
  138. int lsfs_rename(const char *path, const char *to) {
  139. return lsfs_disk_rename_file(path, to);
  140. }
  141. int lsfs_write(const char *path, const char *content, size_t content_length, off_t offset_to_next_entry, struct fuse_file_info *file_info)
  142. {
  143. int res = lsfs_disk_write_data_to_file(
  144. ((lsfs_file*) file_info->fh),
  145. content_length,
  146. (void*) content, offset_to_next_entry);
  147. return res;
  148. }
  149. //Permission
  150. int lsfs_open( const char *path, struct fuse_file_info *fi ) {
  151. // printf("open: (path=%s)\n", path);
  152. // We can store a pinter in the *fi, this pointer can be used in both read and write.
  153. // https://libfuse.github.io/doxygen/structfuse__operations.html
  154. // printf("read: (path=%s)\n", path);
  155. lsfs_file *found_file = calloc(1, sizeof(lsfs_file));
  156. if (lsfs_disk_getattr(found_file, path)) {
  157. fi->fh = (uint64_t) found_file;
  158. }
  159. return 0;
  160. }
  161. int lsfs_read( const char *path, char *buf, size_t size, off_t offset_to_next_entry, struct fuse_file_info *fi ) {
  162. // printf("read: (path=%s)\n", path);
  163. int res = lsfs_disk_read_data_from_file(
  164. ((lsfs_file*) fi->fh),
  165. size,
  166. buf,
  167. offset_to_next_entry);
  168. return res;
  169. }
  170. int lsfs_release(const char *path, struct fuse_file_info *fi) {
  171. return 0;
  172. }
  173. int lsfs_mknod(const char *path, mode_t mode, dev_t device) {
  174. return lsfs_disk_create_entry(path, ENTRY_FILE);
  175. }
  176. int main( int argc, char *argv[] )
  177. {
  178. if (argc != 3)
  179. {
  180. printf("Wrong number of arguments, format should be ./lsfs_fuse mountpoint diskname\n");
  181. }
  182. if (lsfs_disk_load_disk(argv[2]))
  183. {
  184. return fuse_main( argc - 1 , argv, &lsfs_oper );
  185. }
  186. else
  187. {
  188. printf("[ERROR] - Correct file format not found \n");
  189. return 0;
  190. }
  191. }