ENGR 3950U / CSCI 3020U: Operating Systems Description and C Code of Major Functions in Simulated Unix File System. Instructor: Dr. Kamran Sartipi Faculty of Engineering and Applied Science UOIT Canada
Pathname Parsing int parse_pathname (char *path, int*no_ofcomponents) pathname, such as “/foo/bar/kamra/new”, stored in data_buff_1[1024] Scan from left to right and check the validity of the pathname. - start with “/” and number of characters less than 6. Put valid component in one of the rows of two dimensional array pathname_parse[64][7]. Return the number of pathname components, “no_ofcomponents”.
Errors reported Input : foo/bar/kamra/new Path name should start from root dir, (begin with ‘/’) Input: /foo/bar//kamra/new Zero length component. Input: /foo/bar/kamran/new Components more than 5 characters
C code of parse_pathname int parse_pathname(char *path, int *no_ofcomponents) { int i, j=0, k if(path[0] !='/'){ return (-14); } for(i=1; i<=1023; i++) { if((path[i] == '/') && (k==0)) { return (-15); } if(k<=4) { /****** k <=4 ******/ /* length of pathname is valid. */ if(path[i] == '\0') { /* end of parsing. */
C code of parse_pathname while (k<=5) { pathname_parse[j][k] = '\0'; k++; } *no_ofcomponents = j+1; return (0); } if(path[i] != '/') { /* current pathname is valid. */ pathname_parse[j][k] = path[i]; k++; } else { /*another component is started. */
C code of parse_pathname while(k<=5){ pathname_parse[j][k] = '\0'; k++; } k=0; j++; /* next pathname component. */ } else { /****** k == 5 ******/ if(path[i] == '\0') { /* end of parsing. */ *no_ofcomponents = j+1; return (0); } else { if(path[i] == '/') pathname_parse[j][k] = '\0';
C code of parse_pathname k=0; j++; /* set for next pathname component. */ } else { /* length of pathname is invalid. */ return (-16); }
Directory Manipulation Directory is a file and consists of a number of directory entries. Each entry consists of 10 bytes, as follows: i) 6 bytes for "name" of the pathname component. ii) 2 bytes for the "i_number" of the pathname component. iii) 2 bytes unused. Each block of directory file can hold 12 directory entries. 12 * 10 = 120 < 128 bytes (block size).
Pathname Translation Begin from the "root directory”: Parse the pathname and put its components into the pathname array “pathname_parse[64][7]”. Take the first parsed name from the first row of "pathname_parse[64][7]" Search in the root directory for the “I_number” of the first pathname component. Use the “I_number“ to access the data blocks for this pathname component into buffers. Take the second pathname component from "pathname_parse[64][7]" and searches till last pathname component.
C code of Dir_search int dir_search(char *component, int *i_number) { int i,temp_inumber,block_no, index, type,file_ptr, dir_blocks, flag; /* root directory "*i_number = 0". */ temp_inumber = *i_number; if(get_file_pointer(temp_inumber, &file_ptr)<0) return (-1); } if(get_file(temp_inumber, &type)<0){ return (-1); }
C code of Dir_search if(type == 0) { return (-1); } flag = compare_component_tobuff( component, &index, &temp_inumber); if(flag == 0) { /* match is found. */ *i_number = temp_inumber; return (0); } /* component is not found. */ return (1); }
Pseudo-code for Create Int sfs_create (char *pathname, int type): get the I_node table. parse the pathname. while pathname component is not the last component if(i_number ==0) then Error.(0) if intermediate file is regular file, then Error.(1) get the next component.
Pseudo-code for Create search in the parent directory for the new component: if component is found, get the i_number and go to the loop to search for the next pathname component. else Error.(2) end while if the file already existed Error.(3) **** used for every pathname manipulations ****
Pseudo-code for Create - Search for empty i_node table entry. - Search for free disk block. - Put the "free block number" in the - i_node table. - Make a directory entry - Write the directory entry “(fileName, i_number)” in the parent directory. - Put i_node table back in the disk.
Pseudo-code for sfs_delete Perform the process for the pathname manipulation. if the file is a regular file get the reference counter of the file. if reference counter > 0 then Error. else release all blocks of the regular file. set file pointer to 0
Pseudo-code for sfs_delete if the file is a directory file: get the file if the directory is not empty then Error.. get the reference counter of the file. if reference counter > 0 then Error. else release all blocks of the regular file. set file pointer to 0.
Pseudo-code for sfs_delete - Get the file of the parent directory. - Erase the directory entry of the deleted file. - Put the parent directory file back to the disk.
GOOD LUCK!!