Advertisement
Guest User

mkdircreat

a guest
Apr 27th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.29 KB | None | 0 0
  1. #include "type.h"
  2.  
  3. void make_dir()
  4. {
  5.     //MINODE *mip;
  6.     MINODE *pip;
  7.     int pino;
  8.     char temp[64];
  9.     int x;
  10.  
  11.     strcpy(temp, pathname);
  12.     get_dirname(temp);
  13.     strcpy(temp, pathname);
  14.     get_basename(temp);
  15.  
  16.     // Get the IN_MEMORY minode of parent:
  17.     pino = getino(dev, dir);
  18.     pip = iget(dev, pino);
  19.  
  20.     // Verify parent INODE is a DIR by checking file type
  21.     if(pip->INODE.i_mode & 00400000 == 00400000)
  22.     {
  23.         // Verify child does NOT exist in the parent directory
  24.         x = search(dev, &(pip->INODE), base);
  25.         if(x == -1)
  26.         {
  27.             printf("Making directory \'%s\'\n", base);
  28.             mymkdir(pip, base);
  29.             pip->INODE.i_links_count++; // Increment parents inode's link count by 1
  30.             pip->INODE.i_atime = time(0L); // Touch its atime
  31.             pip->dirty = 1; // Make it DIRTY
  32.         }
  33.         else
  34.             printf("Directory \'%s\' already exists.\n", base);
  35.     }
  36.  
  37.     iput(pip);
  38. }
  39.  
  40. int mymkdir(MINODE *pip, char *name)
  41. {
  42.      //allocate an inode and a disk block for the new directory
  43.     int ino = ialloc(dev);
  44.     int bno = balloc(dev), i;
  45.  
  46.     char mbuf[BLKSIZE];
  47.  
  48.     MINODE *mip = iget(dev, ino);     // To load the inode to minode
  49.  
  50.     INODE *cip = &mip->INODE;
  51.     DIR *mdp;
  52.     char *mcp;
  53.  
  54.     //writing contents to the INODE in memory
  55.     //---------given code--------
  56.     cip->i_mode = 0x41ed;                          
  57.     cip->i_uid = running->uid;                                      //owner UID
  58.     cip->i_gid = running->gid;                  //group id
  59.     cip->i_size = BLKSIZE;                      //size if bytes
  60.     cip->i_links_count = 2;                     //link count=2 because of . and ..
  61.     cip->i_atime = cip->i_ctime = cip->i_mtime = time(0L);      //set to current time
  62.     cip->i_blocks = 2;                      //LINUX: Block cout in 512-byte chucks
  63.     cip->i_block[0] = bno;
  64.     for(i = 1; i < 15; i++)// for the last i_block array attribute
  65.     {
  66.         cip->i_block[i] = 0;
  67.     }
  68.  
  69.     mip->dirty = 1; // Mark minode dirty
  70.     mip->dev = dev;
  71.     iput(mip);  // Write INODE to disk
  72.     //----end-------
  73.     mdp = (DIR *) mbuf;
  74.     mcp = mbuf;
  75.  
  76.     // Write . entry into a buf[ ]
  77.     mdp->inode = root->ino;
  78.     mdp->rec_len = 12;
  79.     mdp->name_len = 1;
  80.     mdp->name[0] = '.';
  81.  
  82.     // Write .. entry into a buf[ ]
  83.     mcp += mdp->rec_len;
  84.     mdp = (DIR *) mcp;
  85.     mdp->inode = pip->ino;
  86.     mdp->rec_len = 1012;
  87.     mdp->name_len = 2;
  88.     mdp->name[0] = '.';
  89.     mdp->name[1] = '.';
  90.  
  91.     // Write buf to the disk block bno
  92.     put_block(dev, bno, mbuf);
  93.  
  94.     // Enter name ENTRY into parent's directory
  95.     enter_name(pip, ino, name);
  96. }
  97.  
  98. int creat_file()
  99. {
  100.     /*   (1). its inode's mode field is set to a REGULAR file,
  101.                 permission bits to (default) rw-r--r--,
  102.         (2). No data block, so size = 0
  103.         (3). links_count = 1;
  104.         (4). Do NOT increment parent's links_count
  105.     */
  106.     MINODE *pip;
  107.     int pino, ino;
  108.     char temp[64];
  109.     int x;
  110.  
  111.     strcpy(temp, pathname);
  112.  
  113.     get_dirname(temp);
  114.     strcpy(temp, pathname);
  115.     get_basename(temp);
  116.     printf("basename: %s\n", base);
  117.     printf("dirname: ");
  118.  
  119.     putchar('\n');
  120.     pino = getino(dev, dir);;
  121.     pip = iget(dev, pino);
  122.  
  123.     // Check file type
  124.     if(pip->INODE.i_mode & 00400000 == 00400000)
  125.     {
  126.         x = search(dev, &(pip->INODE), base);
  127.         if(x == -1)
  128.         {
  129.             printf("Making file \'%s\'\n", base);
  130.             ino = mycreat(pip, base);
  131.             pip->INODE.i_atime = time(0L);
  132.             pip->dirty = 1;
  133.         }
  134.         else
  135.             printf("File \'%s\' already exists.\n", base);
  136.     }
  137.  
  138.     iput(pip);
  139.     return ino;
  140. }
  141.  
  142. int mycreat(MINODE *pip, char *name)
  143. {
  144.     /* Differences b/w mycreat and mymkdir
  145.     - INODE's file type = 0x81A4 OR 0100644
  146.     - links_count = 1
  147.     - NO data block, so size = 0
  148.     - do NOT inc parent's link count.
  149.     */
  150.  
  151.     int ino = ialloc(dev), i;
  152.     char mbuf[BLKSIZE];
  153.     MINODE *mip = iget(dev, ino);
  154.     INODE *cip = &mip->INODE;
  155.     DIR *mdp;
  156.     char *mcp;
  157.     //printf("enter mycreat\n");
  158.     //---------given code--------
  159.     cip->i_mode = 0x81a4;
  160.     cip->i_uid = running->uid;
  161.     cip->i_gid = running->gid;
  162.     cip->i_size = 0; // NO data block, so size = 0
  163.     cip->i_links_count = 1; // links_count = 1
  164.     cip->i_atime = cip->i_ctime = cip->i_mtime = time(0L);
  165.     cip->i_blocks = 0;
  166.     cip->i_block[0] = 0;
  167.     for(i = 1; i < 15; i++)
  168.     {
  169.         cip->i_block[i] = 0;
  170.     }
  171.  
  172.     mip->dirty = 1;
  173.     mip->dev = dev;
  174.     iput(mip);
  175.  
  176.     enter_name(pip, ino, name);
  177.  
  178.     return ino;
  179. }
  180.  
  181. int enter_name(MINODE *pip, int myino, char *myname)
  182. {
  183.     int lastRecLen = 0, remain, currNode, currBlock, ideal_len = 4*((11+strlen(myname))/4), i, j;
  184.     char mbuf[BLKSIZE];
  185.     INODE *mip;
  186.     DIR *mdp;
  187.     char *mcp;
  188.     // 12 direct block, for each....
  189.     for(i = 0; i < 12; i++)
  190.     {
  191.         if(pip->INODE.i_block[i] == 0)
  192.         {
  193.             printf("Data block %d was null\n", i);
  194.             break;
  195.         }
  196.         else
  197.         {
  198.             // Get parent's ith data block into a buf
  199.             get_block(dev, pip->INODE.i_block[i], mbuf);
  200.             mdp = (DIR *) mbuf;
  201.             mcp = mbuf;
  202.  
  203.             // Step to LAST entry in block: int blk = parent->INODE.i_block[i]
  204.             while(mcp < mbuf + BLKSIZE && mdp->rec_len != 0)
  205.             {
  206.                 if(mcp + mdp->rec_len >= mbuf + BLKSIZE)
  207.                     break;
  208.                 mcp += mdp->rec_len; // increment
  209.                 mdp = (DIR *) mcp; //keep changing this as mcp goes up. haha
  210.             } //dp or (mdp) now points at last entry in block
  211.  
  212.             lastRecLen = mdp->rec_len;
  213.             remain = lastRecLen - 4*((11+mdp->name_len)/4); // Each entry's ideal length is 4
  214.             // let remain = Last entry's rec_len - it's IDEAL_LENGTH
  215.  
  216.             if(remain >= ideal_len)
  217.             {
  218.                 //enter the new entry as the last entry and trim the previous entry to its IDEAL_LENGTH
  219.                 mdp->rec_len = 4*((11+mdp->name_len)/4);//update new length for entry
  220.                 mcp += mdp->rec_len;
  221.                 mdp = (DIR *) mcp;
  222. //
  223. //
  224. //                                 |LAST entry
  225. //  --|-4---2----2--|----|---------|--------- rlen ->------------------------|
  226. //    |ino rlen nlen NAME|.........|ino rlen nlen|NAME                       |
  227. //  --------------------------------------------------------------------------
  228. //                                                    |     NEW entry
  229. //  --|-4---2----2--|----|---------|----ideal_len-----|--- rlen=remain ------|
  230. //    |ino rlen nlen NAME|.........|ino rlen nlen|NAME|myino rlen nlen myname|
  231. //
  232.  
  233.                 mdp->inode = myino;
  234.                 mdp->rec_len = remain;
  235.                 mdp->name_len = strlen(myname);
  236.                 for(j = 0; j < strlen(myname); j++){
  237.                     mdp->name[j] = myname[j];
  238.                 }
  239.                 put_block(dev, pip->INODE.i_block[i], mbuf);
  240.                 return 0;
  241.             }
  242.         }
  243.     }
  244.  
  245.     // Reach here mean: NO space in existing data blocks
  246.     // Allocate a new data block
  247.     pip->INODE.i_block[i] = balloc(dev);
  248.     strcpy(mbuf, "\0");
  249.     mdp = (DIR *) mbuf;
  250.  
  251.     mdp->inode = myino;
  252.     mdp->rec_len = 1024;
  253.     mdp->name_len = strlen(myname);
  254.     for(j = 0; j < strlen(myname); j++)
  255.     {
  256.         mdp->name[j] = myname[j];
  257.     }
  258.     pip->INODE.i_size += 1024; // INC parent's isze by 1024;
  259.  
  260.     // enter new entry as the first entry in the new data block with rec_len= BLKSIZE
  261.     put_block(dev, pip->INODE.i_block[i], mbuf);
  262.     return 1;
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement