Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "filesystem.h"
- #include "lib.h"
- #define ERROR -1
- /*
- * init_file_system
- *
- * DESCRIPTION: This function initializes the file system.
- *
- * INPUT: -mod_start: Pointer to start memory.
- * -mod_end: Pointer to end memory.
- * OUTPUT: None
- * RETURN VALUE: None
- * SIDE EFFECT: Points fs_start and fs_end to the start and end of the file system memory respectively.
- */
- void init_file_system(uint32_t* mod_start,uint32_t* mod_end){
- fs_start = mod_start;
- fs_end = mod_end;
- }
- /*
- * get_index_from_file_name
- *
- * DESCRIPTION: This function retrieves the index of the file specified by the file's name from the boot_block.
- *
- * INPUT: -fname: The name of the file to be searched for.
- * OUTPUT: None
- * RETURN VALUE: Index of file on success, ERROR on failure.
- * SIDE EFFECT: None.
- */
- int32_t get_index_from_file_name(const uint8_t* fname){
- //get pointer to boot block
- boot_block_t* boot_block = (boot_block_t*)fs_start;
- //get number of directories
- int max_dirs = boot_block->dir_entries;
- //copy string into buffer so we can use strncpy. The buffer is size 33 to account for null terminator char.
- uint8_t fname_buff[33];
- strncpy((int8_t*)fname_buff,(int8_t*)fname,32); //32 is the max file name size.
- //looping pointer
- dentry_t* temp = (dentry_t*)boot_block->dir;
- //index to return
- int index = 0;
- //looping variable
- int x;
- //temp pointer
- uint8_t *name;
- //loop through available blocks to find matching file
- for(x=0; x< max_dirs; x++){
- name = temp[x].file_name;
- if(strncmp((int8_t*)name,(int8_t*)fname_buff,32) == 0){
- return index;
- }
- index++;
- }
- //file isn't found oops
- return ERROR;
- }
- /*
- * read_dentry_by_name
- *
- * DESCRIPTION: This function copies entry attributes into a given block specified by an entries' file name.
- * Does so by calling get_index_from_file_name and _read_dentry_by_index.
- *
- * INPUT: -fname: Name of file to retrieve attributes from.
- * -dentry: Block to copy data into.
- * OUTPUT: Block with file entries' attributes.
- * RETURN VALUE: 1 on success, ERROR on failure.
- * SIDE EFFECT: None.
- */
- int32_t read_dentry_by_name(const uint8_t* fname, dentry_t* dentry){
- int32_t index = get_index_from_file_name(fname);
- return index == ERROR? ERROR:read_dentry_by_index(index,dentry);
- }
- /*
- * read_dentry_by_index
- *
- * DESCRIPTION: This function copies entry attributes into a given block specified by and entries' index.
- *
- * INPUT: -index: Index of file to retrieve attributes from.
- * -dentry: Block to copy data into.
- * OUTPUT: Block with file entries' attributes.
- * RETURN VALUE: 1 on success, ERROR on failure.
- * SIDE EFFECT: None.
- */
- int32_t read_dentry_by_index(uint8_t index, dentry_t* dentry){
- //get pointer to boot block
- boot_block_t* boot_block = (boot_block_t*)fs_start;
- //get number of directories
- int max_dirs = boot_block->dir_entries;
- //make sure index is in bounds
- if(index >= max_dirs)
- return ERROR;
- //pointer to file
- dentry_t* file = (boot_block->dir)+index;
- //copy to given buffer
- memcpy(dentry, file, sizeof(dentry_t));
- //1 is success
- return 1;
- }
- /*
- * read_data
- *
- * DESCRIPTION: This function reads up to a certain amount of bytes starting at an offset in a file and stores them into a buffer.
- *
- * INPUT: -inode: Index node of file to read.
- * -offset: Starting location in file.
- * -buf: Buffer to store read bytes.
- * -length: Number of bytes to read.
- * OUTPUT: Buffer filled with the specified data read from the file.
- * RETURN VALUE: 0 on success, ERROR on failure
- * SIDE EFFECT: None.
- */
- int32_t read_data(uint8_t inode, uint32_t offset, uint8_t* buf, uint32_t length){
- //get pointer to boot block
- boot_block_t* boot_block = (boot_block_t*)fs_start;
- //get number of directories
- uint32_t max_inodes = boot_block->inode_entries;
- //make sure inode is within the valid range
- if(inode >= max_inodes)
- return ERROR;
- //get pointer to inode block
- inode_t *node = (inode_t*)fs_start + SKIP_BOOT_BLOCK + inode;
- //make sure inode is valid
- if(node == NULL)
- return ERROR;
- //end reading positions
- uint32_t end = offset + length;
- //make sure it doesn't go beyond its bounds
- if(end > node->length)
- end = node->length;
- //variable to hold how many bytes we copied
- uint32_t bytes_copied =0;
- //block offset
- uint32_t data_blocks_offset = offset/DATA_BLOCK_SIZE;
- // partial block offset
- uint32_t partial_block = offset%DATA_BLOCK_SIZE;
- //data array at position in refrence with offset given
- uint32_t data_blocks_id = node->data[data_blocks_offset];
- //how many partial bytes to copy
- uint32_t duration = ( (DATA_BLOCK_SIZE-partial_block) > (end-offset)) ? (end-offset) : (DATA_BLOCK_SIZE-partial_block);
- printf("%u \n", duration);
- uint32_t start = (uint32_t)fs_start + (SKIP_BOOT_BLOCK + max_inodes + data_blocks_id)*DATA_BLOCK_SIZE;
- start+=partial_block;
- memcpy(buf, (void*)start, duration);
- //mark partial block copied
- end-=duration;
- uint32_t blockes_copied = 1;
- bytes_copied+= duration;
- //read rest of blocks if any
- while(end != 0){
- data_blocks_id = node->data[data_blocks_offset+blockes_copied];
- duration = (DATA_BLOCK_SIZE > end)?end:DATA_BLOCK_SIZE;
- start = (uint32_t)fs_start + (SKIP_BOOT_BLOCK + max_inodes + data_blocks_id)*DATA_BLOCK_SIZE;
- memcpy( buf+bytes_copied, (void*)start, duration);
- blockes_copied++;
- end-=duration;
- bytes_copied+= duration;
- }
- return end;
- }
- /*
- * get_size
- *
- * DESCRIPTION: This function returns the size of a file in bytes.
- *
- * INPUT: -inode_number: Index node of file.
- * OUTPUT: None
- * RETURN VALUE: Size of file.
- * SIDE EFFECT: None.
- */
- int32_t get_size(int32_t inode_number){
- inode_t *node = (inode_t*)fs_start + SKIP_BOOT_BLOCK + inode_number;
- return node->length;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement