Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.51 KB | None | 0 0
  1. /* ////////////////////////////////////////////////////////////////////
  2. * Foreword:
  3. * Unfortunately with this assignment I ran into issues with actually traversing
  4. * through the paths given. This impacted my implementation of every function as
  5. * every function did require it. As a result I was not able to pass the tests.
  6. * Furthermore the functions mostly relied on create, which I was unable to fully
  7. * implement. As a result my functions are very barebones and not to the quality
  8. * that I was hoping they would be. I am hoping that I will not be triple penalized
  9. * for my inability to figure out directory traversal. Regardless, below is my
  10. * attempted implementation of each of the functions.
  11. */ ////////////////////////////////////////////////////////////////////
  12. int fs_create(F19FS_t *fs, const char *path, file_t type)
  13. {
  14. if(fs == NULL)
  15. return -1;
  16. if(path == NULL)
  17. return -1;
  18. if(strlen(path) == 0)
  19. return -1;
  20. if(type != FS_REGULAR && type != FS_DIRECTORY)
  21. return -1;
  22. directoryFile_t * file = malloc(sizeof(directoryFile_t));
  23. inode_t * node = malloc(sizeof(inode_t));
  24. /*
  25. while(tmpstring != NULL)
  26. {
  27. k = 0;
  28. tmpstring++;
  29. count++;
  30. while(tmpstring != NULL && *tmpstring != '/')
  31. {
  32. k++;
  33. tmpstring++;
  34. }
  35. if(k > 32)
  36. return -1;
  37.  
  38. }
  39. // I could not figure out the code implementation for the recursive moving down the folders
  40. // for this function. Instead I will write out my plan. At a high level what we will
  41. // need to do is traverse through a directory, examining all entries and seeing if the filename matches
  42. // the next step in our path. Once that happens, we will need to create a directory if the filename does not
  43. // exist. If a match is found, we can load the data from the pointer to the next file down, and repeat the process
  44. // until we hit the file we are planning to add. Following that, the code below will execute.
  45. // Below is the code for assigning all the relevant information for a specific file or directory, given the filename
  46. tmpstring = path;
  47. while(l < count-1)
  48. {
  49. k = 0;
  50. tmpstring++;
  51. while(tmpstring != NULL && *tmpstring != '/')
  52. {
  53. k++;
  54. tmpstring++;
  55. }
  56. if(k > 32)
  57. return -1;
  58. l++;
  59. }
  60. */
  61. char * path1 = malloc(sizeof(char) * 1024);
  62. strcpy(path1, path);
  63. node->linkCount = 1;
  64. strncpy(file->filename, path1, 32);
  65. size_t inodeNumber = block_store_sub_allocate(fs->BlockStore_inode);
  66. if(inodeNumber == SIZE_MAX)
  67. return -1;
  68. node->inodeNumber = inodeNumber;
  69. file->inodeNumber = inodeNumber;
  70. inodeNumber = block_store_allocate(fs->BlockStore_whole);
  71. if(inodeNumber == SIZE_MAX)
  72. return -1;
  73. node->directPointer[0] = inodeNumber;
  74. if(type == FS_DIRECTORY)
  75. {
  76. node->fileType = 'd';
  77. node->vacantFile = 0x00000000;
  78. }
  79. else
  80. {
  81. node->fileType = 'f';
  82. }
  83.  
  84. size_t size = block_store_inode_write(fs->BlockStore_inode, inodeNumber, node);
  85. if(size == 0)
  86. return -1;
  87. size = block_store_fd_write(fs->BlockStore_fd, inodeNumber, file);
  88. if(size == 0)
  89. return -1;
  90. return 0;
  91.  
  92. }
  93. int fs_open(F19FS_t *fs, const char *path)
  94. {
  95. if(fs == NULL)
  96. return -1;
  97. if(path == NULL)
  98. return -1;
  99. if(strlen(path) == 0)
  100. return -1;
  101. // Because open relys on create, I was not able to test it and I got stuck in the same place as the previous
  102. // function (traversing through the directories). Therefore I will again just pseudocode. At a high level what we will
  103. // need to do is traverse through a directory, examining all entries and seeing if the filename matches
  104. // the next step in our path. Once that happens, we will return -1 if the filename does not
  105. // exist. If a match is found, we can load the data from the pointer to the next file down, and repeat the process
  106. // until we hit the file we are planning to add. Assuming the file does exist, we would have its' inode information
  107. // loaded up in a variable, which i will arbitrarily use 0.
  108. size_t arbitrary_inode_number = 0;
  109. fileDescriptor_t * file = malloc(sizeof(fileDescriptor_t));
  110. file->inodeNum = arbitrary_inode_number;
  111. file->locate_offset = 0;
  112. block_store_fd_write(fs->BlockStore_fd, arbitrary_inode_number, file);
  113.  
  114. return arbitrary_inode_number;
  115. }
  116. int fs_close(F19FS_t *fs, int fd)
  117. {
  118. if(fs == NULL)
  119. return -1;
  120. if(fd < 0)
  121. return -1;
  122. if(block_store_sub_test(fs->BlockStore_fd, fd))
  123. block_store_sub_release(fs->BlockStore_fd, fd);
  124. return 0;
  125. }
  126. dyn_array_t *fs_get_dir(F19FS_t *fs, const char *path)
  127. {
  128. if(fs == NULL)
  129. return NULL;
  130. if(path == NULL)
  131. return NULL;
  132. if(strlen(path) == 0)
  133. return NULL;
  134.  
  135. // This functions relies on the previous ones as before, and also relies on
  136. // being able to traverse down through the directory. Unfortunately, all of the functions
  137. // required this and it left my implementations quite barebones as most of the work
  138. // requires you to actually traverse through everything. This is all the work that I have to
  139. // show for this function.
  140.  
  141.  
  142. dyn_array_t * dyn = dyn_array_create(15, sizeof(file_record_t), NULL);
  143. for(int i = 0; i < folder_number_entries; i++)
  144. {
  145. file_record_t * file = malloc(sizeof(file_record_t));
  146.  
  147. // Here we would go through each of the non-vacant files in the lowest directory
  148. // and we would set file equal to their information
  149.  
  150. dyn_array_push_front(dyn, file);
  151. free(file);
  152. }
  153. return dyn;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement