Advertisement
Guest User

links

a guest
Apr 27th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.13 KB | None | 0 0
  1. #include "type.h"
  2.  
  3. // Creates a file newFileName which has the SAME inode (number) as that of oldFileName.
  4. int link(char *oldFileName, char *newFileName)
  5. {
  6.     int inoOld, i;
  7.     char *temp = (char *) malloc(sizeof(char)*(strlen(oldFileName) + 1)), *pn = 0;
  8.     MINODE *mip, *pip;
  9.  
  10.     strcpy(temp, oldFileName);
  11.     //Get the INODE into memory
  12.     //trivial
  13.     inoOld = getino(dev, temp);
  14.     free(temp);
  15.     mip = iget(dev, inoOld);
  16.     printf("starting link\n");
  17.     //check to see if it is a REG or LNK
  18.  
  19.     //if it's a DIR                               //if it's a REG
  20.     if((mip->INODE.i_mode & 100000) == 100000 || (mip->INODE.i_mode & 12000 == 12000))
  21.     {
  22.        
  23.         pn = (char *) malloc(sizeof(char)*(strlen(newFileName) + 1));
  24.         strcpy(pn, newFileName);
  25.  
  26.         if(pn[0] == '/') // If the first char is a /, go ahead and get dirname on root
  27.         {
  28.             get_dirname(pn);    // Get dirname
  29.         }
  30.  
  31.         temp = (char *) malloc(sizeof(char)*(strlen(dir) + 1));
  32.         strcpy(temp, dir);  // Store dir name in temp
  33.         i = getino(dev, temp);  // Get inode of oldfile
  34.         free(temp);
  35.  
  36.         if(i == -1) // if it failed to get inode
  37.         {
  38.             printf("dir \'%s\' doesn't exist.\n", dir);
  39.             free(pn);
  40.             return -1;
  41.         }
  42.         pip = iget(dev, i); // remember iget ends with iput
  43.         i = getino(dev, pn);
  44.  
  45.         if(i != -1) // check /x/y  exists and is a DIR but 'z' does not yet exist in /x/y/
  46.         {
  47.             printf("file already exists.\n");
  48.             free(pn);
  49.             iput(pip);
  50.             return -1;
  51.         }
  52.         get_basename(pn);
  53.         enter_name(pip, mip->ino, base); // Enter name ENTRY into parent's directory
  54.         mip->INODE.i_links_count++; // Increment count
  55.         mip->dirty = 1; // Make dirty
  56.  
  57.         iput(pip);
  58.         free(pn);
  59.     }
  60.     //Back to Disk
  61.     iput(mip);
  62. }
  63.  
  64. int symlink(char *oldName, char *newName)
  65. {
  66.     int i, ino;
  67.     char *oldPn = 0, *newPn = 0;
  68.     MINODE *mip;
  69. //////////////////////////////////////////////////////////////////////////////////
  70.     // Verify oldname exists
  71.     oldPn = (char *) malloc(sizeof(char)*(strlen(oldName) + 1));
  72.     strcpy(oldPn, oldName);
  73.     // if it begins with a /, so a specfied location...
  74.     if(oldPn[0] == '/')
  75.     {
  76.         i = getino(dev, oldPn);
  77.     }
  78.  
  79.     if(i == -1)//fails to get the inode
  80.     {
  81.         free(oldPn);
  82.         return -1;
  83.     }
  84. ///////////////////////////////////////////////////////////////////////////////
  85.     // Check where user wants newname to be created
  86.     //Get the pathname then set it to the correct name for creat
  87.     newPn = (char *) malloc(sizeof(char)*(strlen(newName) + 1));
  88.     strcpy(newPn, newName);
  89.     if(newPn[0] == '/')
  90.     {
  91.         strcpy(pathname, newPn);
  92.     }
  93.     else
  94.     {
  95.         free(newPn);
  96.         //newPn = my_cwd();
  97.         newPn = realloc(newPn, sizeof(char)*(strlen(newPn)+strlen(newName)+1));
  98.         strcat(newPn, newName);
  99.         strcpy(pathname, newPn);
  100.     }
  101.     //Complete
  102.  
  103.     i = getino(dev, newPn);
  104.  
  105.     if(i != -1)
  106.     {
  107.         free(newPn);
  108.         return -1;
  109.     }
  110. /////////////////////////////////////////////////////////////////////////////////
  111.     ino = creat_file();
  112.     mip = iget(dev, ino);
  113.     mip->INODE.i_mode = 0xa1ff;
  114.     mip->INODE.i_size = strlen(oldPn);
  115.     mip->dirty = 1;
  116.  
  117.     strcpy((char *) mip->INODE.i_block, oldPn);
  118.     //Back to Disc
  119.     iput(mip);
  120.     //proper remove!
  121.     free(newPn);
  122.     free(oldPn);
  123.  
  124.     return 0;
  125. }
  126.  
  127. int unlink(char* pn)
  128. {
  129.     if (pn[0]=='\0'){   //  Empty pathname
  130.         printf("Syntax: unlink [path]\n");
  131.         return -1;
  132.     }
  133.     char parentdir[64],name[64], *cp, *endcp, *last;
  134.     DIR * dp;
  135.     MINODE * pip,*targetip;
  136.     int parent, target;
  137.     cp = strrchr(pn, '/');
  138.  
  139.     // 1. Check where the parent is and get the iNode based on cp
  140.  
  141.     if (!cp){ // In the same directory
  142.         parent = running->cwd->ino; // Set parent to cwd inode
  143.         strcpy(name,pn);
  144.     }
  145.     else{   // In another directory
  146.         *(cp) = '\0';
  147.         strcpy(parentdir, pn);
  148.         parent = getino(fd,parentdir);   // Get inode of parentdir, store into parent
  149.         strcpy(name,cp+1);
  150.     }
  151.  
  152.     target = getino(fd,pn);
  153.  
  154.     // Check if target file exists
  155.     if ((target==0)||(parent==0)){ 
  156.         printf("Error: File must exist\n");
  157.         return -1;
  158.     }
  159.     pip = iget(fd,parent);
  160.     targetip = iget(fd,target);
  161.     printf("i_mode: %d",targetip->INODE.i_mode);
  162.     // 2. If the target exists, make sure it's not a dir...
  163.     if((targetip->INODE.i_mode & 00100000) != 00100000){
  164.         iput(pip);
  165.         printf("Error: Cannot unlink NON-REG files\n");
  166.         return -1;
  167.     }
  168.  
  169.     // 3. Decrement links count
  170.     targetip->INODE.i_links_count--;     // I links count--
  171.  
  172.     // 4. Clear data with truncate
  173.     if (targetip->INODE.i_links_count == 0)
  174.     {
  175.         // Truncate for MINODE
  176.         targetip->INODE.i_size = 0; // Set targetip size to 0
  177.         targetip->INODE.i_atime = targetip->INODE.i_mtime = time(0L);   // Touch time
  178.         targetip->dirty = 1;    // Mark targetip dirty
  179.  
  180.         targetip->refCount++;
  181.         iput(targetip);
  182.         idealloc(fd,targetip->ino);
  183.  
  184.     }
  185.     else
  186.     {
  187.         iput(targetip);
  188.     }
  189.  
  190.     return deleteChild(pip,name);
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement