//rd_open, will require a pid, really only accessible via the kernel int rd_open(char* pathname){ char *dir_path; char *filename; int pid = 5; //for now its five, fix this later when we aren't in userspace split_dir_file(pathname, &dir_path, &filename); int root_check = strcmp(dir_path, "/"); if(filename != NULL && root_check == 0){ int new_fd = allocate_fd(pid, 0); printf("rd_open success\n"); return new_fd; } int find_inode = get_dir_inode(dir_path, 0); //search if such a directory exists if(find_inode > -1){ find_inode = get_dir_entry(find_inode, filename, 0); if(find_inode > -1){ int new_fd = allocate_fd(pid, find_inode); printf("rd_open success\n"); return new_fd; } } printf("rd_open failed\n"); return -1; }
int rd_creat(char* pathname){ char *dir_path; char *filename; split_dir_file(pathname, &dir_path, &filename); int dir_inode_num = get_dir_inode(dir_path, 1); //check if dir_path exists if(dir_inode_num == -1){ return -1; } int tmp = get_dir_entry(dir_inode_num, filename, 1); if(tmp != -1){ //directory already exists printf("error: dir already exists\n"); return -1; } //get ready to allocate inode int index = get_free_inode(); if(index == -1){ printf("no free inodes\n"); return -1; } s_block->free_inodes = s_block->free_inodes - 1; init_inode(index, 2); //type = 2, regular file inode_array[dir_inode_num].size = inode_array[dir_inode_num].size + sizeof(dir_entry); dir_entry* new_dir = get_free_dir_entry(dir_inode_num); new_dir->inode_number = index; strcpy(new_dir->filename, filename); printf("rd_creat created a file\n"); return 1; }
static void follow_chain(struct fat32_fs *fs, unsigned int start_cluster) { int next = start_cluster; while (next != -1) { if (next == 0) { kprintf("unused\n"); } else if (next == -2) { kprintf("bad cluster\n"); } else { struct fat_dir_entry *dir = get_dir_entry(fs, next); if (dir == NULL) { kprintf("not on disk\n"); } else { kprintf("on disk: `%s`\n", dir->dirname); } } next = next_cluster(fs, next); } kprintf("EOC\n"); }
void list_dirs(struct fat32_fs *fs) { int next, entry = 0; /* Grab the root dir first */ next = fs->bs->fat32.root_cluster; struct fat_dir_entry *root = get_dir_entry(fs, next); if (root == NULL) { kprintf("no root?\n"); return; } else { kprintf("found root at rel %d\n", next); kprintf("/\n"); } entry = 1; while (entry < 256) { follow_chain(fs, entry); entry++; } #if 0 next = 0; while (next < 256) { unsigned int n = *(unsigned int *)&fs->table[next] & 0x0FFFFFFF; if (n >= 0x0FFFFFF8) { kprintf("next %d: n %X (EOC)\n", next, n); next++; } else if (n == 0) { kprintf("empty\n"); break; } else { kprintf("next %d: n %X ", next, n); struct fat_dir_entry *dir = get_dir_entry(fs, next); if (dir == NULL) { kprintf("(not on disk)"); } else { kprintf("(on disk `%s`)", dir->dirname); } kprintf("\n"); } } next = next_cluster(fs, next); if (next != -1) { kprintf("root isn't EOC?\n"); return; } next++; /* Iterate over until there are no more to read */ //while ((next = next_cluster(fs, next)) != -1) { while (1) { struct fat_dir_entry *dir = get_dir_entry(fs, next); if (dir == NULL) { kprintf("no entry for rel %d?\n", next); next = next_cluster(fs, next); } else { kprintf("`%s`\n", dir->dirname); next = next_cluster(fs, next); if (next == -2) { /* Bad cluster */ } else if (next == -1) { /* EOC */ next++; continue; } else if (next == 0) { /* Unused */ } } } #endif }
/** * @brief * Function to open a specific file mentioned in the path * based on the incoming modes * @param file_path * Pointer to the location of the file path string * @param flags * Flags indicating the modes in which the file is to be opened * * @return Non-zero:File descriptor of the opened file Zero :File open is unsuccessful */ int file_open(const char *file_path,int flags) { const char *path = file_path; const char *temp_path,*delim_strt; char shrt_file_name[SHRT_FILE_NAME_LEN]; char long_file_name[LONG_FILE_NAME_LEN]; int len = 0,fl_des = 0,crt_flag,i; int delim_cnt = 0; int mode; int extn_len_cnt = 0; int seq_num = 1; bool is_file_found; dir_entry *entry = NULL; file_info *info; u8 *pwd = root_directory; u32 strt_cluster = rt_dir_strt_clus; bool is_long_file_name = false; sw_memset(long_file_name,SPACE_VAL,LONG_FILE_NAME_LEN); delim_cnt = find_depth(file_path); path = file_path; for(i=0;i<delim_cnt;i++){ if(*path == DELIMITER){ delim_strt = path; path++; } while((*path != EXTN_DELIMITER) && (*path != '\0') && (*path != DELIMITER) && (len < LONG_FILE_NAME_LEN)){ long_file_name[len] = *path; path++; len++; } temp_path = path; if(*temp_path == EXTN_DELIMITER){ temp_path++; while(*temp_path != DELIMITER && *temp_path != '\0'){ extn_len_cnt++; temp_path++; } } if(len > FILE_NAME_SHRT_LEN || extn_len_cnt > FILE_NAME_EXTN_LEN) is_long_file_name = true; if(is_long_file_name){ path = delim_strt; len = 0; if(*path == DELIMITER) path++; while(len < LONG_FILE_NAME_LEN && *path != '\0' && *path != DELIMITER){ long_file_name[len] = *path; path++; len++; } long_file_name[len] = '\0'; if(entry){ sw_free(entry); entry = NULL; } is_file_found = get_dir_entry(long_file_name,&entry, pwd,strt_cluster,true); } else{ len = FILE_NAME_SHRT_LEN; while(len < SHRT_FILE_NAME_LEN && *path != '\0' && *path != DELIMITER){ if(*path == EXTN_DELIMITER) path++; long_file_name[len] = *path; path++; len++; } convert_to_uppercase(long_file_name); if(entry){ sw_free(entry); entry = NULL; } is_file_found = get_dir_entry(long_file_name,&entry, pwd,strt_cluster,false); } if((is_file_found) & (i != delim_cnt - 1)){ strt_cluster = (entry->strt_clus_hword)<<16 | (entry->strt_clus_lword); pwd = cluster_to_memory_addr(strt_cluster); len = 0; extn_len_cnt = 0; sw_memset(shrt_file_name,SPACE_VAL,SHRT_FILE_NAME_LEN); sw_memset(long_file_name,SPACE_VAL,LONG_FILE_NAME_LEN); is_long_file_name = false; } } if(is_file_found){ if(flags & FILE_WRITE){ if(chk_file_lock(file_path) == -1) flags = FILE_READ; if(entry->attr & ATTR_READ){ sw_printf("Cannot open the file in write mode\n"); return -1; } } info = (file_info*)sw_malloc(sizeof(file_info)); fl_des = retrieve_file_info(info,entry,flags, dir_file_offset,file_path); } else{ if((flags & FILE_CREATE_NEW) || (flags & FILE_CREATE_ALWAYS) || (flags & FILE_WRITE)){ if(is_long_file_name){ get_short_file_name(long_file_name,shrt_file_name, (char)seq_num); if(get_dir_entry(shrt_file_name,NULL, pwd,strt_cluster,false) == true){ while(get_dir_entry(shrt_file_name,NULL, pwd,strt_cluster,false)){ seq_num++; get_short_file_name(long_file_name, shrt_file_name,'seq_num'); } } convert_to_uppercase(shrt_file_name); crt_flag = create_file(long_file_name, shrt_file_name,strt_cluster,&entry); } else crt_flag = create_file(NULL,long_file_name,strt_cluster,&entry); if(crt_flag == 0) sw_printf("File creation success\n"); info = (file_info*)sw_malloc(sizeof(file_info)); fl_des = retrieve_file_info(info,entry,flags, dir_file_offset,file_path); } else return -1; } return fl_des; }
int rd_unlink(char* pathname) { int pid = 5; char* dir; char* file; char* file_type; int parent_dir; int file_inode; int i, direct_count; int rem_inode, bp_count; pnode_fd_table* fd_unlink; single_indirect* single_block; double_indirect* double_block; // (4) error if root if (strcmp(pathname, "/") == 0) { return -1; } split_dir_file(pathname, &dir, &file); parent_dir = parent_dir_exists(dir); // (1) error if pathname/file doesn't exist if (parent_dir == -1) { //vfree(dir); //vfree(file); return -1; } file_inode = get_dir_entry(parent_dir, file, 2); if (file_inode == -1) { // vfree(dir); //vfree(file); return -1; } // (2) error if dir is non-empty file_type = inode_array[file_inode].type; if ((file_type == 1) && (inode_array[file_inode].size != 0)) { return -1; } // (3) error if trying to unlink open file fd_unlink = find_fd_list(fd_table, pid); if (fd_unlink->table[file_inode].inode_pointer == NULL) { //vfree(dir); //vfree(file); return -1; } rem_inode = unlinker(parent_dir, file, inode_array[file_inode].type); inode_array[parent_dir].size -= 16; s_block->free_inodes += 1; inode_array[rem_inode].id = rem_inode; inode_array[rem_inode].allocated = 0; inode_array[rem_inode].size = 0; inode_array[rem_inode].type = 0; bp_count = inode_array[rem_inode].bp_count; if (bp_count > 8){ direct_count = 8; } else {direct_count = bp_count;} for(i = 0; i < direct_count; i++) { if (inode_array[rem_inode].block_pointer[i] == NULL){ break; } memset(inode_array[rem_inode].block_pointer[i], 0, RAMDISK_BLOCK_SIZE); } // single indirect if (bp_count > 8) { single_block = (single_indirect*) inode_array[file_inode].block_pointer[8]; for (i = 0; i < 64; i++) { if (!single_block->block_pointer[i]) { break; } memset(single_block->block_pointer[i], 0, RAMDISK_BLOCK_SIZE); } } //double indirect if (bp_count == 10) { double_block = (double_indirect*) inode_array[file_inode].block_pointer[9]; for (i = 0; i < 64; i++) { if (!double_block->indirect_block_pointer[i]) { break; } for (i = 0; i < 64; i++) { if (!single_block->block_pointer[i]) { break; } memset(single_block->block_pointer[i], 0, RAMDISK_BLOCK_SIZE); } } } inode_array[rem_inode].bp_count = 0; printf("rd_unlink %s is now unlinked\n", pathname); return 0; }