Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ////////////////////////////////////////////////////////////////////
- * Foreword:
- * Unfortunately with this assignment I ran into issues with actually traversing
- * through the paths given. This impacted my implementation of every function as
- * every function did require it. As a result I was not able to pass the tests.
- * Furthermore the functions mostly relied on create, which I was unable to fully
- * implement. As a result my functions are very barebones and not to the quality
- * that I was hoping they would be. I am hoping that I will not be triple penalized
- * for my inability to figure out directory traversal. Regardless, below is my
- * attempted implementation of each of the functions.
- */ ////////////////////////////////////////////////////////////////////
- int fs_create(F19FS_t *fs, const char *path, file_t type)
- {
- if(fs == NULL)
- return -1;
- if(path == NULL)
- return -1;
- if(strlen(path) == 0)
- return -1;
- if(type != FS_REGULAR && type != FS_DIRECTORY)
- return -1;
- directoryFile_t * file = malloc(sizeof(directoryFile_t));
- inode_t * node = malloc(sizeof(inode_t));
- /*
- while(tmpstring != NULL)
- {
- k = 0;
- tmpstring++;
- count++;
- while(tmpstring != NULL && *tmpstring != '/')
- {
- k++;
- tmpstring++;
- }
- if(k > 32)
- return -1;
- }
- // I could not figure out the code implementation for the recursive moving down the folders
- // for this function. Instead I will write out my plan. At a high level what we will
- // need to do is traverse through a directory, examining all entries and seeing if the filename matches
- // the next step in our path. Once that happens, we will need to create a directory if the filename does not
- // exist. If a match is found, we can load the data from the pointer to the next file down, and repeat the process
- // until we hit the file we are planning to add. Following that, the code below will execute.
- // Below is the code for assigning all the relevant information for a specific file or directory, given the filename
- tmpstring = path;
- while(l < count-1)
- {
- k = 0;
- tmpstring++;
- while(tmpstring != NULL && *tmpstring != '/')
- {
- k++;
- tmpstring++;
- }
- if(k > 32)
- return -1;
- l++;
- }
- */
- char * path1 = malloc(sizeof(char) * 1024);
- strcpy(path1, path);
- node->linkCount = 1;
- strncpy(file->filename, path1, 32);
- size_t inodeNumber = block_store_sub_allocate(fs->BlockStore_inode);
- if(inodeNumber == SIZE_MAX)
- return -1;
- node->inodeNumber = inodeNumber;
- file->inodeNumber = inodeNumber;
- inodeNumber = block_store_allocate(fs->BlockStore_whole);
- if(inodeNumber == SIZE_MAX)
- return -1;
- node->directPointer[0] = inodeNumber;
- if(type == FS_DIRECTORY)
- {
- node->fileType = 'd';
- node->vacantFile = 0x00000000;
- }
- else
- {
- node->fileType = 'f';
- }
- size_t size = block_store_inode_write(fs->BlockStore_inode, inodeNumber, node);
- if(size == 0)
- return -1;
- size = block_store_fd_write(fs->BlockStore_fd, inodeNumber, file);
- if(size == 0)
- return -1;
- return 0;
- }
- int fs_open(F19FS_t *fs, const char *path)
- {
- if(fs == NULL)
- return -1;
- if(path == NULL)
- return -1;
- if(strlen(path) == 0)
- return -1;
- // Because open relys on create, I was not able to test it and I got stuck in the same place as the previous
- // function (traversing through the directories). Therefore I will again just pseudocode. At a high level what we will
- // need to do is traverse through a directory, examining all entries and seeing if the filename matches
- // the next step in our path. Once that happens, we will return -1 if the filename does not
- // exist. If a match is found, we can load the data from the pointer to the next file down, and repeat the process
- // until we hit the file we are planning to add. Assuming the file does exist, we would have its' inode information
- // loaded up in a variable, which i will arbitrarily use 0.
- size_t arbitrary_inode_number = 0;
- fileDescriptor_t * file = malloc(sizeof(fileDescriptor_t));
- file->inodeNum = arbitrary_inode_number;
- file->locate_offset = 0;
- block_store_fd_write(fs->BlockStore_fd, arbitrary_inode_number, file);
- return arbitrary_inode_number;
- }
- int fs_close(F19FS_t *fs, int fd)
- {
- if(fs == NULL)
- return -1;
- if(fd < 0)
- return -1;
- if(block_store_sub_test(fs->BlockStore_fd, fd))
- block_store_sub_release(fs->BlockStore_fd, fd);
- return 0;
- }
- dyn_array_t *fs_get_dir(F19FS_t *fs, const char *path)
- {
- if(fs == NULL)
- return NULL;
- if(path == NULL)
- return NULL;
- if(strlen(path) == 0)
- return NULL;
- // This functions relies on the previous ones as before, and also relies on
- // being able to traverse down through the directory. Unfortunately, all of the functions
- // required this and it left my implementations quite barebones as most of the work
- // requires you to actually traverse through everything. This is all the work that I have to
- // show for this function.
- dyn_array_t * dyn = dyn_array_create(15, sizeof(file_record_t), NULL);
- for(int i = 0; i < folder_number_entries; i++)
- {
- file_record_t * file = malloc(sizeof(file_record_t));
- // Here we would go through each of the non-vacant files in the lowest directory
- // and we would set file equal to their information
- dyn_array_push_front(dyn, file);
- free(file);
- }
- return dyn;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement