Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. #include "filesystem.h"
  2. #include "lib.h"
  3.  
  4.  
  5. #define ERROR -1
  6. /*
  7. * init_file_system
  8. *
  9. * DESCRIPTION: This function initializes the file system.
  10. *
  11. * INPUT: -mod_start: Pointer to start memory.
  12. * -mod_end: Pointer to end memory.
  13. * OUTPUT: None
  14. * RETURN VALUE: None
  15. * SIDE EFFECT: Points fs_start and fs_end to the start and end of the file system memory respectively.
  16. */
  17. void init_file_system(uint32_t* mod_start,uint32_t* mod_end){
  18. fs_start = mod_start;
  19. fs_end = mod_end;
  20. }
  21.  
  22. /*
  23. * get_index_from_file_name
  24. *
  25. * DESCRIPTION: This function retrieves the index of the file specified by the file's name from the boot_block.
  26. *
  27. * INPUT: -fname: The name of the file to be searched for.
  28. * OUTPUT: None
  29. * RETURN VALUE: Index of file on success, ERROR on failure.
  30. * SIDE EFFECT: None.
  31. */
  32. int32_t get_index_from_file_name(const uint8_t* fname){
  33.  
  34. //get pointer to boot block
  35. boot_block_t* boot_block = (boot_block_t*)fs_start;
  36.  
  37. //get number of directories
  38. int max_dirs = boot_block->dir_entries;
  39.  
  40. //copy string into buffer so we can use strncpy. The buffer is size 33 to account for null terminator char.
  41. uint8_t fname_buff[33];
  42. strncpy((int8_t*)fname_buff,(int8_t*)fname,32); //32 is the max file name size.
  43.  
  44. //looping pointer
  45. dentry_t* temp = (dentry_t*)boot_block->dir;
  46.  
  47. //index to return
  48. int index = 0;
  49.  
  50. //looping variable
  51. int x;
  52.  
  53. //temp pointer
  54. uint8_t *name;
  55.  
  56. //loop through available blocks to find matching file
  57. for(x=0; x< max_dirs; x++){
  58. name = temp[x].file_name;
  59.  
  60. if(strncmp((int8_t*)name,(int8_t*)fname_buff,32) == 0){
  61. return index;
  62. }
  63. index++;
  64. }
  65.  
  66. //file isn't found oops
  67. return ERROR;
  68. }
  69.  
  70.  
  71. /*
  72. * read_dentry_by_name
  73. *
  74. * DESCRIPTION: This function copies entry attributes into a given block specified by an entries' file name.
  75. * Does so by calling get_index_from_file_name and _read_dentry_by_index.
  76. *
  77. * INPUT: -fname: Name of file to retrieve attributes from.
  78. * -dentry: Block to copy data into.
  79. * OUTPUT: Block with file entries' attributes.
  80. * RETURN VALUE: 1 on success, ERROR on failure.
  81. * SIDE EFFECT: None.
  82. */
  83. int32_t read_dentry_by_name(const uint8_t* fname, dentry_t* dentry){
  84. int32_t index = get_index_from_file_name(fname);
  85. return index == ERROR? ERROR:read_dentry_by_index(index,dentry);
  86. }
  87.  
  88. /*
  89. * read_dentry_by_index
  90. *
  91. * DESCRIPTION: This function copies entry attributes into a given block specified by and entries' index.
  92. *
  93. * INPUT: -index: Index of file to retrieve attributes from.
  94. * -dentry: Block to copy data into.
  95. * OUTPUT: Block with file entries' attributes.
  96. * RETURN VALUE: 1 on success, ERROR on failure.
  97. * SIDE EFFECT: None.
  98. */
  99. int32_t read_dentry_by_index(uint8_t index, dentry_t* dentry){
  100. //get pointer to boot block
  101. boot_block_t* boot_block = (boot_block_t*)fs_start;
  102.  
  103. //get number of directories
  104. int max_dirs = boot_block->dir_entries;
  105.  
  106. //make sure index is in bounds
  107. if(index >= max_dirs)
  108. return ERROR;
  109.  
  110. //pointer to file
  111. dentry_t* file = (boot_block->dir)+index;
  112.  
  113. //copy to given buffer
  114. memcpy(dentry, file, sizeof(dentry_t));
  115.  
  116. //1 is success
  117. return 1;
  118.  
  119. }
  120.  
  121. /*
  122. * read_data
  123. *
  124. * DESCRIPTION: This function reads up to a certain amount of bytes starting at an offset in a file and stores them into a buffer.
  125. *
  126. * INPUT: -inode: Index node of file to read.
  127. * -offset: Starting location in file.
  128. * -buf: Buffer to store read bytes.
  129. * -length: Number of bytes to read.
  130. * OUTPUT: Buffer filled with the specified data read from the file.
  131. * RETURN VALUE: 0 on success, ERROR on failure
  132. * SIDE EFFECT: None.
  133. */
  134. int32_t read_data(uint8_t inode, uint32_t offset, uint8_t* buf, uint32_t length){
  135.  
  136.  
  137. //get pointer to boot block
  138. boot_block_t* boot_block = (boot_block_t*)fs_start;
  139.  
  140. //get number of directories
  141. uint32_t max_inodes = boot_block->inode_entries;
  142.  
  143. //make sure inode is within the valid range
  144. if(inode >= max_inodes)
  145. return ERROR;
  146.  
  147. //get pointer to inode block
  148. inode_t *node = (inode_t*)fs_start + SKIP_BOOT_BLOCK + inode;
  149.  
  150. //make sure inode is valid
  151. if(node == NULL)
  152. return ERROR;
  153.  
  154. //end reading positions
  155. uint32_t end = offset + length;
  156.  
  157. //make sure it doesn't go beyond its bounds
  158. if(end > node->length)
  159. end = node->length;
  160.  
  161. //variable to hold how many bytes we copied
  162. uint32_t bytes_copied =0;
  163.  
  164. //block offset
  165. uint32_t data_blocks_offset = offset/DATA_BLOCK_SIZE;
  166.  
  167. // partial block offset
  168. uint32_t partial_block = offset%DATA_BLOCK_SIZE;
  169.  
  170. //data array at position in refrence with offset given
  171. uint32_t data_blocks_id = node->data[data_blocks_offset];
  172.  
  173. //how many partial bytes to copy
  174. uint32_t duration = ( (DATA_BLOCK_SIZE-partial_block) > (end-offset)) ? (end-offset) : (DATA_BLOCK_SIZE-partial_block);
  175. printf("%u \n", duration);
  176. uint32_t start = (uint32_t)fs_start + (SKIP_BOOT_BLOCK + max_inodes + data_blocks_id)*DATA_BLOCK_SIZE;
  177. start+=partial_block;
  178. memcpy(buf, (void*)start, duration);
  179.  
  180. //mark partial block copied
  181. end-=duration;
  182. uint32_t blockes_copied = 1;
  183. bytes_copied+= duration;
  184.  
  185. //read rest of blocks if any
  186. while(end != 0){
  187. data_blocks_id = node->data[data_blocks_offset+blockes_copied];
  188. duration = (DATA_BLOCK_SIZE > end)?end:DATA_BLOCK_SIZE;
  189. start = (uint32_t)fs_start + (SKIP_BOOT_BLOCK + max_inodes + data_blocks_id)*DATA_BLOCK_SIZE;
  190. memcpy( buf+bytes_copied, (void*)start, duration);
  191. blockes_copied++;
  192. end-=duration;
  193. bytes_copied+= duration;
  194. }
  195.  
  196. return end;
  197.  
  198. }
  199.  
  200. /*
  201. * get_size
  202. *
  203. * DESCRIPTION: This function returns the size of a file in bytes.
  204. *
  205. * INPUT: -inode_number: Index node of file.
  206. * OUTPUT: None
  207. * RETURN VALUE: Size of file.
  208. * SIDE EFFECT: None.
  209. */
  210. int32_t get_size(int32_t inode_number){
  211.  
  212. inode_t *node = (inode_t*)fs_start + SKIP_BOOT_BLOCK + inode_number;
  213.  
  214. return node->length;
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement