Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "type.h"
- int rm_child(MINODE *parent, char *name)
- {
- int i = 0, j;
- int x;
- DIR *tdp;
- char temp[64], tbuf[BLKSIZE], *cp, *tcp;
- for (; i < 12; i++)
- {
- if(parent->INODE.i_block[i] == 0)
- break;
- printf("data block: %d\n", i);
- get_block(parent->dev, parent->INODE.i_block[i], tbuf);//every loop , you need to get the block
- dp = (DIR *) tbuf;
- cp = tbuf;
- while (cp < tbuf + BLKSIZE && dp->rec_len != 0)
- {
- for(j = 0; j < dp->name_len; j++)//copy everything to temp, so I can modify temp
- {
- temp[j] = dp->name[j];
- }
- temp[j] = '\0';
- if (!strcmp(name, temp)) //if dp->name is the same as "name" parameter
- {
- // if (first entry in a data block)
- if (BLKSIZE == dp->rec_len)//if the rec_len is the maximun size the block
- {
- printf("deleting block.\n");
- bdealloc(dev, ip->i_block[i]);//deallocate the data block
- parent->INODE.i_size -= BLKSIZE;//modify parent's file size
- //move parent's NONZERO block upward, so that there is no HOLEs in parent's data block numbers
- for(i; i < 11 && ip->i_block[i+1] != 0; i++)
- {
- ip->i_block[i] = ip->i_block[i+1];
- }
- }
- // if NOT first entry in blo
- else if (cp+dp->rec_len == tbuf+BLKSIZE)
- {
- //move all entries AFter this entry Left
- cp = cp - x;
- x = dp->rec_len;
- dp = (DIR *)cp;
- printf("%d //", dp->rec_len);
- dp->rec_len += x;//add rec_len to the Last entry of the block
- printf(" %d\n", dp->rec_len);
- }
- else
- {
- // write the parent's data block back to disk and mark parent minode Dirty for write-back
- tdp = dp;
- tcp = cp;
- x = dp->rec_len;
- cp+=dp->rec_len;
- dp = (DIR *)cp;
- while(cp < tbuf + BLKSIZE && dp->rec_len != 0)
- {
- tdp->name_len = dp->name_len;
- tdp->rec_len = dp->rec_len;
- tdp->inode = dp->inode;
- for(j = 0; j < dp->name_len; j++)
- {
- tdp->name[j] = dp->name[j];
- }
- if(cp+dp->rec_len == tbuf+BLKSIZE)
- {
- tdp->rec_len+=x;
- break;
- }
- cp+=dp->rec_len;
- dp = (DIR *) cp;
- tcp += tdp->rec_len;
- tdp = (DIR *) tcp;
- }
- }
- break;
- }
- x = dp->rec_len;
- cp += dp->rec_len;
- dp = (DIR *) cp;
- }
- put_block(parent->dev, parent->INODE.i_block[i], tbuf);
- }
- }
- int empty_dir(MINODE *mip)
- {
- int block, node, count = 0;
- char tbuf[BLKSIZE], *cp;
- if(mip->INODE.i_links_count > 2)
- {
- printf("Not empty dir links remain\n");
- return 0;
- }
- else
- {
- block = (mip->ino - 1)/8 + inodes_start;
- node = (mip->ino - 1)%8;
- get_block(dev, block, tbuf);
- ip = (INODE *) tbuf + node;
- get_block(dev, ip->i_block[0], tbuf);
- dp = (DIR *) tbuf;
- cp = tbuf;
- while(cp < tbuf + BLKSIZE && dp->rec_len == 4*((11+dp->name_len)/4))
- {
- count++;
- cp += dp->rec_len;
- dp = (DIR *) cp;
- printf("stepping through a dir entry count: %d\n",count);
- }
- if(count > 2)
- {
- printf("Not empty dir too many entries\n");
- return 0;
- }
- }
- printf("Dir is empty\n");
- return 1;
- }
- int rmdir()
- {
- int ino, i, pipo;
- MINODE *mip, *pip;
- char temp[64];
- printf("before: %s\n", pathname);
- // Extract cmd, pathname from line and save them as globals.
- get_dirname(pathname);
- get_basename(pathname);
- printf("after: %s\n", pathname);
- ino = getino(dev, pathname); // Get inumber of pathname then...
- mip = iget(dev, ino); // Get its minode pointer
- printf("base: %s\ndir: %s\nino: %d\n", base, dir, ino);
- if (running->uid == 0) // Check ownership, if UID == 0/superuser then OK
- {
- // Check whether a DIR is empty
- if (mip->INODE.i_mode & 00400000 == 00400000 && empty_dir(mip))
- {
- printf("Target is a dir and is empty\n");
- // Deallocate its block and inode GIVEN CODE
- for(i = 0; i < 12; i++)
- {
- if(mip->INODE.i_block[i] == 0)
- {
- continue;
- }
- bdealloc(mip->dev, mip->INODE.i_block[i]);
- }
- idealloc(mip->dev, mip->ino);
- iput(mip);
- // End of given code
- pipo = getino(dev, dir);
- pip = iget(mip->dev, pipo); // Get parent DIR's ino and minode (pointed by pip)
- rm_child(pip, base); // Remove child's entry from parent directory
- pip->INODE.i_links_count--; // Decrement pip's link count by 1
- pip->INODE.i_atime = pip->INODE.i_mtime = time(0L); // Touch pip's a m time fields
- pip->dirty = 1; // Make dirty
- iput(pip);
- return 1; // Success
- }
- }
- printf("rmdir complete\n");
- iput(mip);
- }
- // Deletes the child of a minode with name name
- int deleteChild(MINODE* pip,char* name) {
- get_block(fd,pip->INODE.i_block[0],buf);
- char* cp = buf;
- DIR* dp = (DIR *) buf;
- int flag, last = 0, tmp, i = 0;
- char* endcp = buf;
- // Traverse to end of buffer
- while(endcp+dp->rec_len < buf +1024) {
- endcp += dp->rec_len;
- dp = (DIR *)endcp;
- }
- dp =(DIR *) cp;
- while (cp < buf+1024)
- {
- if (dp->name_len == strlen(name))
- {
- // Compares name with dp->name but only up to the name_length
- if (strncmp(name, dp->name, dp->name_len) == 0) // Delete file
- {
- tmp = dp->rec_len; // First, store rec_len in tmp
- if (cp == endcp) // If it's at the end
- {
- dp = (DIR *) last;
- dp->rec_len += tmp; // Increment rec_length by tmp
- break;
- }
- else
- {
- dp = (DIR *) endcp;
- dp->rec_len += tmp;
- memcpy(cp, cp+tmp, 1024-i-tmp);
- }
- break;
- }
- }
- last = (int)cp;
- i += dp->rec_len;
- cp += dp->rec_len;
- dp = (DIR *)cp;
- }
- put_block(fd,pip->INODE.i_block[0],buf);
- return 0;
- }
- int rm_file(char* path)
- {
- char parentdir[64], name[64], *cp, *endcp, *last;
- DIR * dp;
- MINODE * pip, *targetip;
- int parent, target;
- if (path[0] == '\0') // If the path is null probably if user forgets
- {
- printf("Syntax: rm [path]\n");
- return -1;
- }
- cp = strrchr(path, '/'); // Returns a pointer to the last occurance of character in the string
- if (!cp) // No occurance of /
- {
- parent = running->cwd->ino;
- strcpy(name,path);
- }
- else
- {
- *(cp) = '\0';
- strcpy(parentdir, path); // Copy path to parent directory
- parent = getino(fd, parentdir); // Set parent to parent directory inode
- strcpy(name, cp+1);
- }
- target = getino(fd,path);
- // Check if the file exists
- if (parent == 0 || target == 0)
- {
- printf("rm: file does not exist\n");
- return -1;
- }
- pip = iget(fd,parent);
- targetip = iget(fd,target);
- // If it's not a regular file
- if ((targetip->INODE.i_mode & 00100000) != 00100000)
- {
- iput(pip);
- printf("rm: cannot remove non-regular file\n");
- return -1;
- }
- // Truncate for MINODE
- targetip->INODE.i_size = 0; // Set targetip size to 0
- targetip->INODE.i_atime = targetip->INODE.i_mtime = time(0L); // Touch time
- targetip->dirty = 1; // Mark targetip dirty
- iput(targetip);
- idealloc(fd,targetip->ino);
- deleteChild(pip,name);
- printf("rm: removed file '%s'\n", name);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement