Advertisement
Guest User

rmdirrm

a guest
Apr 27th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.11 KB | None | 0 0
  1. #include "type.h"
  2.  
  3.  
  4.  
  5. int rm_child(MINODE *parent, char *name)
  6. {
  7.     int i = 0, j;
  8.     int x;
  9.     DIR *tdp;
  10.     char temp[64], tbuf[BLKSIZE], *cp, *tcp;
  11.    
  12.     for (; i < 12; i++)
  13.     {
  14.         if(parent->INODE.i_block[i] == 0)
  15.             break;
  16.         printf("data block: %d\n", i);
  17.         get_block(parent->dev, parent->INODE.i_block[i], tbuf);//every loop , you need to get the block
  18.         dp = (DIR *) tbuf;
  19.         cp = tbuf;
  20.         while (cp < tbuf + BLKSIZE && dp->rec_len != 0)
  21.         {
  22.             for(j = 0; j < dp->name_len; j++)//copy everything to temp, so I can modify temp
  23.             {
  24.                 temp[j] = dp->name[j];
  25.             }
  26.             temp[j] = '\0';
  27.  
  28.             if (!strcmp(name, temp)) //if dp->name is the same as "name" parameter
  29.             {
  30.             // if (first entry in a data block)
  31.                 if (BLKSIZE == dp->rec_len)//if the rec_len is the maximun size the block
  32.                 {
  33.                     printf("deleting block.\n");
  34.                     bdealloc(dev, ip->i_block[i]);//deallocate the data block
  35.                     parent->INODE.i_size -= BLKSIZE;//modify parent's file size
  36.                     //move parent's NONZERO block upward, so that there is no HOLEs in parent's data block numbers
  37.                     for(i; i < 11 && ip->i_block[i+1] != 0; i++)
  38.                     {
  39.                         ip->i_block[i] = ip->i_block[i+1];
  40.                     }
  41.                 }
  42.                 // if NOT first entry in blo
  43.                 else if (cp+dp->rec_len == tbuf+BLKSIZE)
  44.                 {
  45.                 //move all entries AFter this entry Left
  46.                     cp = cp - x;
  47.                     x = dp->rec_len;
  48.                     dp = (DIR *)cp;
  49.                     printf("%d //", dp->rec_len);
  50.                     dp->rec_len += x;//add rec_len to the Last entry of the block
  51.                     printf(" %d\n", dp->rec_len);
  52.                 }
  53.                 else
  54.                 {
  55.                 // write the parent's data block back to disk and mark parent minode Dirty for write-back
  56.                     tdp = dp;
  57.                     tcp = cp;
  58.                     x = dp->rec_len;
  59.                     cp+=dp->rec_len;
  60.                     dp = (DIR *)cp;
  61.  
  62.                     while(cp < tbuf + BLKSIZE && dp->rec_len != 0)
  63.                     {
  64.                         tdp->name_len = dp->name_len;
  65.                         tdp->rec_len = dp->rec_len;
  66.                         tdp->inode = dp->inode;
  67.                         for(j = 0; j < dp->name_len; j++)
  68.                         {
  69.                             tdp->name[j] = dp->name[j];
  70.                         }
  71.                         if(cp+dp->rec_len == tbuf+BLKSIZE)
  72.                         {
  73.                             tdp->rec_len+=x;
  74.                             break;
  75.                         }
  76.                         cp+=dp->rec_len;
  77.                         dp = (DIR *) cp;
  78.  
  79.                         tcp += tdp->rec_len;
  80.                         tdp = (DIR *) tcp;
  81.                     }
  82.                 }
  83.                 break;
  84.             }
  85.             x = dp->rec_len;
  86.             cp += dp->rec_len;
  87.             dp = (DIR *) cp;
  88.         }
  89.         put_block(parent->dev, parent->INODE.i_block[i], tbuf);
  90.     }
  91. }
  92.  
  93. int empty_dir(MINODE *mip)
  94. {
  95.     int block, node, count = 0;
  96.     char tbuf[BLKSIZE], *cp;
  97.     if(mip->INODE.i_links_count > 2)
  98.     {
  99.         printf("Not empty dir links remain\n");
  100.         return 0;
  101.     }
  102.     else
  103.     {
  104.         block = (mip->ino - 1)/8 + inodes_start;
  105.         node = (mip->ino - 1)%8;
  106.  
  107.         get_block(dev, block, tbuf);
  108.         ip = (INODE *) tbuf + node;
  109.  
  110.         get_block(dev, ip->i_block[0], tbuf);
  111.         dp = (DIR *) tbuf;
  112.         cp = tbuf;
  113.         while(cp < tbuf + BLKSIZE && dp->rec_len == 4*((11+dp->name_len)/4))
  114.         {
  115.             count++;
  116.             cp += dp->rec_len;
  117.             dp = (DIR *) cp;
  118.             printf("stepping through a dir entry count: %d\n",count);
  119.         }
  120.         if(count > 2)
  121.         {
  122.             printf("Not empty dir too many entries\n");
  123.             return 0;  
  124.         }
  125.     }
  126.     printf("Dir is empty\n");
  127.     return 1;
  128.    
  129. }
  130.  
  131. int rmdir()
  132. {
  133.     int ino, i, pipo;
  134.     MINODE *mip, *pip;
  135.     char temp[64];
  136.  
  137.     printf("before: %s\n", pathname);
  138.     // Extract cmd, pathname from line and save them as globals.
  139.     get_dirname(pathname);
  140.     get_basename(pathname);
  141.     printf("after: %s\n", pathname);
  142.     ino = getino(dev, pathname); // Get inumber of pathname then...
  143.     mip = iget(dev, ino); // Get its minode pointer
  144.  
  145.  
  146.     printf("base: %s\ndir: %s\nino: %d\n", base, dir, ino);
  147.  
  148.     if (running->uid == 0) // Check ownership, if UID == 0/superuser then OK
  149.     {
  150.         // Check whether a DIR is empty
  151.         if (mip->INODE.i_mode & 00400000 == 00400000 && empty_dir(mip))
  152.         {
  153.             printf("Target is a dir and is empty\n");
  154.             // Deallocate its block and inode GIVEN CODE
  155.             for(i = 0; i < 12; i++)
  156.             {
  157.                 if(mip->INODE.i_block[i] == 0)
  158.                 {
  159.                     continue;
  160.                 }
  161.                 bdealloc(mip->dev, mip->INODE.i_block[i]);
  162.             }
  163.             idealloc(mip->dev, mip->ino);
  164.             iput(mip);
  165.             // End of given code
  166.  
  167.             pipo = getino(dev, dir);
  168.  
  169.             pip = iget(mip->dev, pipo); // Get parent DIR's ino and minode (pointed by pip)
  170.  
  171.             rm_child(pip, base); // Remove child's entry from parent directory
  172.  
  173.             pip->INODE.i_links_count--; // Decrement pip's link count by 1
  174.             pip->INODE.i_atime = pip->INODE.i_mtime = time(0L); // Touch pip's a m time fields
  175.             pip->dirty = 1; // Make dirty
  176.  
  177.             iput(pip);
  178.             return 1; // Success
  179.         }
  180.     }
  181.     printf("rmdir complete\n");
  182.     iput(mip);
  183. }
  184.  
  185. // Deletes the child of a minode with name name
  186. int deleteChild(MINODE* pip,char* name) {
  187.     get_block(fd,pip->INODE.i_block[0],buf);
  188.  
  189.     char* cp = buf;
  190.     DIR* dp = (DIR *) buf;
  191.     int flag, last = 0, tmp, i = 0;
  192.     char* endcp = buf;
  193.    
  194.    
  195.  
  196.     // Traverse to end of buffer
  197.     while(endcp+dp->rec_len < buf +1024) {
  198.         endcp += dp->rec_len;
  199.         dp = (DIR *)endcp;
  200.     }
  201.  
  202.     dp =(DIR *) cp;
  203.  
  204.     while (cp < buf+1024)
  205.     {
  206.         if (dp->name_len == strlen(name))
  207.         {
  208.             // Compares name with dp->name but only up to the name_length
  209.             if (strncmp(name, dp->name, dp->name_len) == 0) // Delete file
  210.             {
  211.                 tmp = dp->rec_len;  // First, store rec_len in tmp
  212.  
  213.                 if (cp == endcp) // If it's at the end
  214.                 {
  215.                     dp = (DIR *) last;
  216.                     dp->rec_len += tmp; // Increment rec_length by tmp
  217.                     break;
  218.                 }
  219.                 else
  220.                 {
  221.                     dp = (DIR *) endcp;
  222.                     dp->rec_len += tmp;
  223.                     memcpy(cp, cp+tmp, 1024-i-tmp);
  224.                 }
  225.                 break;
  226.             }
  227.         }
  228.  
  229.         last = (int)cp;
  230.         i += dp->rec_len;
  231.         cp += dp->rec_len;
  232.         dp = (DIR *)cp;
  233.     }
  234.     put_block(fd,pip->INODE.i_block[0],buf);
  235.  
  236.     return 0;
  237. }
  238.  
  239. int rm_file(char* path)
  240. {
  241.     char parentdir[64], name[64], *cp, *endcp, *last;
  242.     DIR * dp;
  243.     MINODE * pip, *targetip;
  244.     int parent, target;
  245.  
  246.     if (path[0] == '\0')    // If the path is null probably if user forgets
  247.     {
  248.         printf("Syntax: rm [path]\n");
  249.         return -1;
  250.     }
  251.  
  252.     cp = strrchr(path, '/');    // Returns a pointer to the last occurance of character in the string
  253.    
  254.     if (!cp) // No occurance of /
  255.     {  
  256.         parent = running->cwd->ino;
  257.         strcpy(name,path);
  258.     }
  259.     else
  260.     {
  261.         *(cp) = '\0';
  262.         strcpy(parentdir, path);    // Copy path to parent directory
  263.         parent = getino(fd, parentdir); // Set parent to parent directory inode
  264.         strcpy(name, cp+1);
  265.     }
  266.     target = getino(fd,path);
  267.  
  268.     // Check if the file exists
  269.     if (parent == 0 || target == 0)
  270.     {
  271.         printf("rm: file does not exist\n");
  272.         return -1;
  273.     }
  274.  
  275.     pip = iget(fd,parent);
  276.     targetip = iget(fd,target);
  277.  
  278.     // If it's not a regular file
  279.     if ((targetip->INODE.i_mode & 00100000) != 00100000)
  280.     {
  281.         iput(pip);
  282.         printf("rm: cannot remove non-regular file\n");
  283.         return -1;
  284.     }
  285.  
  286.     // Truncate for MINODE
  287.     targetip->INODE.i_size = 0; // Set targetip size to 0
  288.     targetip->INODE.i_atime = targetip->INODE.i_mtime = time(0L);   // Touch time
  289.     targetip->dirty = 1;    // Mark targetip dirty
  290.  
  291.     iput(targetip);
  292.     idealloc(fd,targetip->ino);
  293.     deleteChild(pip,name);
  294.  
  295.     printf("rm: removed file '%s'\n", name);
  296.     return 0;
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement