/* fs_read * inputs: fd : file descriptor * buf : buffer to write into * nbytes: number of bytes to write * outputs: -1 on failure * otherwise, number of bytes read * notes: Reads nbytes into a buffer given a file * descriptor. */ int32_t fs_read(int32_t fd, void* buf, int32_t nbytes) { dentry_t curr_dentry; int32_t check; int32_t inum; int32_t readpos; int32_t flent; if(!buf){ return -1; } inum = pcb_loc[current_terminal]->file_desc[fd].inode_ptr; // we need to fix this because its a temp fa readpos = pcb_loc[current_terminal]->file_desc[fd].file_pos; flent = inode_length(inum); inode_t* inode_ptr; inode_ptr = (inode_t*)(&(boot[inum + 1])); // verify file existence check = read_dentry_by_index(inum, &curr_dentry); if (check == -1){ return -1; } // read data into buffer check = read_data(inum, readpos, buf, nbytes); if(check == -1){ return -1; } //update file position pcb_loc[current_terminal]->file_desc[fd].file_pos += check; // we need to fix this because its a temp fa //returns number of read bytes return check; }
/* dir_read * inputs: fd : file descriptor * buf : buffer to write into * nbytes: number of bytes to write * outputs: -1 on failure * 0 signfies that directory has been completely read * otherwise, number of bytes read * notes: Same as fs_read, except only file names are read * into buffer */ int32_t dir_read(int32_t fd, void* buf, int32_t nbytes){ dentry_t curr_dentry; int32_t check; int32_t readpos; if(!buf){ return -1; } readpos = pcb_loc[current_terminal]->file_desc[fd].file_pos; check = read_dentry_by_index(readpos, &curr_dentry); /* if either name is null, or file is not found, return 0 to signal that directory has finished reading*/ if(check == -1){ return 0; } if(curr_dentry.name[0] == '\0'){ return 0; } // make sure nbytes is size of string length if(strlen((char *)curr_dentry.name) < nbytes){ nbytes = strlen((char *)curr_dentry.name); } if(nbytes>NAME_SIZE){ nbytes = NAME_SIZE; } memcpy(buf, &(curr_dentry.name), nbytes); //update file position readpos++; //returns number of read bytes pcb_loc[current_terminal]->file_desc[fd].file_pos = readpos; return nbytes; }
/* * dir_read * Description: read directory * INPUTS: int32_t fd -- file descriptor void* buf -- buffer address int32_t nbytes -- number of bytes pcb_t* mypcb -- Process control block * OUTPUTS: None * RETURN: None * SIDE EFFECTS: None */ extern int32_t dir_read(int32_t fd, void* buf, int32_t nbytes, pcb_t* mypcb) { uint32_t offset = mypcb->file_array[fd].file_position; boot_block_t* bb = (boot_block_t*)mods_addrs; uint32_t limit = bb->num_dir_entry; dentry_t dentry; static uint32_t read_dir_falg = 0; if (read_dir_falg == 1) { read_dir_falg = 0; return 0; } read_dentry_by_index(offset, &dentry); offset++; if (offset == limit) { offset %= limit; read_dir_falg = 1; } mypcb->file_array[fd].file_position = offset; uint32_t n = nbytes; if (nbytes > FIVE_BITS) n = FIVE_BITS; // strncpy((int8_t *)buf, dentry.file_name, n); // ((int8_t *)buf)[n+1] = '\n'; strcpy((int8_t *)buf, dentry.file_name); return strlen((int8_t *)buf); return n; }
/* * Function: dir_read * Description: this functions outputs (into buf) the name of the file or directory specified in the * directoryLoc file variable, which is then incremented (or returned to 0 if it passes the end) * the inputs fd and nbytes are ignored. * inputs: fd and nbytes are ignored * outputs: buf is filled with a c string (not null terminated) containing the name of the file * returns: number of bytes written (will be 0 if we've reached the end) */ int32_t dir_read (int32_t fd, void* buf, int32_t nbytes) { dentry_t dentry; int i; if (read_dentry_by_index(directoryLoc, &dentry) == 0) { //clear buffer for (i = 0; i < 33; i++) ((int8_t*)(buf))[i] = '\0'; //copy file name into buffer if directory entry can be read int32_t len = strlen((int8_t*)dentry.fileName); strncpy((int8_t*)buf, (int8_t*)dentry.fileName, len); directoryLoc++; return len; } else { /*TEMP FIX = directoryLoc needs to be changed for checkpoint 3 */ directoryLoc = 0; return 0; } }