HOW TO link_unlink_symlink

READ Chapter 11.8.6 - 11.8.9

0. POSTED sample solutions samples/directory: =========== Project Level-1 ========== LAB6 : mount_root; ls, cd, pwd mkdir_creat: mkdir, creat rmdir : rmdir link_unlink: link, unlink, symlink, readlink ====================================== 1. link oldFileName newFileName (link pathname parameter) creates a file newFileName which has the SAME inode (number) as that of oldFileName. Example: link /a/b/c /x/y/z ==> /a/b/ data block /x/y data block ------------------------ ------------------------- .. .|ino rlen nlen c|... ....|ino rlen nlen z| .... ------|----------------- ------|------------------ | | INODE <---------------------------- i_links_count = 1 <== INCrement i_links_count to 2 (1). get the INODE of /a/b/c into memory: mip->minode[ ] INODE of /a/b/c dev,ino ....... mip = path2inode(pathname); (2). check /a/b/c is NOT a DIRectory (link to DIR is NOT allowed). (3). break up paramter into parent (/x/y), child (z) check /x/y exists and is a DIR but 'z' does not yet exist in /x/y/ (4). Add an entry [ino rec_len name_len z] to the data block of /x/y/ This creates /x/y/z, which has the SAME ino as that of /a/b/c (NOTE: both /a/b/c and /x/y/z must be on the SAME device; link can not be across different devices). (5). increment the i_links_count of INODE by 1 (6). write INODE back to disk =========================================================================== 2. HOW TO unlink unlink pathname (1). get pathname's INODE into memory (2). verify it's a FILE (REG or LNK), can not be a DIR; (3). decrement INODE's i_links_count by 1; (4). if i_links_count == 0 ==> rm pathname by deallocate its data blocks by: Write a truncate(INODE) function, which deallocates ALL the data blocks of INODE. This is similar to printing the data blocks of INODE. deallocate its INODE; (5). Remove childName = basename(pathname) from the parent directory by rm_child(parentInodePtr, childName) which is the SAME as that in rmdir or unlink file operations. 3. ======================== HOW TO symlink ================================ symlink oldNAME newNAME e.g. symlink /a/b/c /x/y/z ASSUME: oldNAME has <= 60 chars, inlcuding the ending NULL byte. (1). verify oldNAME exists (either a DIR or a REG file) (2). creat a FILE /x/y/z (3). change /x/y/z's type to LNK (0120000)=(b1010.....)=0xA... (4). write the string oldNAME into the i_block[ ], which has room for 60 chars. set /x/y/z file size = number of chars in oldName (5). write the INODE of /x/y/z back to disk. 4. readlink pathname: return the contents of a symLink file (1). get INODE of pathname into a minode[ ]. (2). check INODE is a symbolic Link file. (3). return its string contents in INODE.i_block[ ].