Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "type.h"
- void make_dir()
- {
- //MINODE *mip;
- MINODE *pip;
- int pino;
- char temp[64];
- int x;
- strcpy(temp, pathname);
- get_dirname(temp);
- strcpy(temp, pathname);
- get_basename(temp);
- // Get the IN_MEMORY minode of parent:
- pino = getino(dev, dir);
- pip = iget(dev, pino);
- // Verify parent INODE is a DIR by checking file type
- if(pip->INODE.i_mode & 00400000 == 00400000)
- {
- // Verify child does NOT exist in the parent directory
- x = search(dev, &(pip->INODE), base);
- if(x == -1)
- {
- printf("Making directory \'%s\'\n", base);
- mymkdir(pip, base);
- pip->INODE.i_links_count++; // Increment parents inode's link count by 1
- pip->INODE.i_atime = time(0L); // Touch its atime
- pip->dirty = 1; // Make it DIRTY
- }
- else
- printf("Directory \'%s\' already exists.\n", base);
- }
- iput(pip);
- }
- int mymkdir(MINODE *pip, char *name)
- {
- //allocate an inode and a disk block for the new directory
- int ino = ialloc(dev);
- int bno = balloc(dev), i;
- char mbuf[BLKSIZE];
- MINODE *mip = iget(dev, ino); // To load the inode to minode
- INODE *cip = &mip->INODE;
- DIR *mdp;
- char *mcp;
- //writing contents to the INODE in memory
- //---------given code--------
- cip->i_mode = 0x41ed;
- cip->i_uid = running->uid; //owner UID
- cip->i_gid = running->gid; //group id
- cip->i_size = BLKSIZE; //size if bytes
- cip->i_links_count = 2; //link count=2 because of . and ..
- cip->i_atime = cip->i_ctime = cip->i_mtime = time(0L); //set to current time
- cip->i_blocks = 2; //LINUX: Block cout in 512-byte chucks
- cip->i_block[0] = bno;
- for(i = 1; i < 15; i++)// for the last i_block array attribute
- {
- cip->i_block[i] = 0;
- }
- mip->dirty = 1; // Mark minode dirty
- mip->dev = dev;
- iput(mip); // Write INODE to disk
- //----end-------
- mdp = (DIR *) mbuf;
- mcp = mbuf;
- // Write . entry into a buf[ ]
- mdp->inode = root->ino;
- mdp->rec_len = 12;
- mdp->name_len = 1;
- mdp->name[0] = '.';
- // Write .. entry into a buf[ ]
- mcp += mdp->rec_len;
- mdp = (DIR *) mcp;
- mdp->inode = pip->ino;
- mdp->rec_len = 1012;
- mdp->name_len = 2;
- mdp->name[0] = '.';
- mdp->name[1] = '.';
- // Write buf to the disk block bno
- put_block(dev, bno, mbuf);
- // Enter name ENTRY into parent's directory
- enter_name(pip, ino, name);
- }
- int creat_file()
- {
- /* (1). its inode's mode field is set to a REGULAR file,
- permission bits to (default) rw-r--r--,
- (2). No data block, so size = 0
- (3). links_count = 1;
- (4). Do NOT increment parent's links_count
- */
- MINODE *pip;
- int pino, ino;
- char temp[64];
- int x;
- strcpy(temp, pathname);
- get_dirname(temp);
- strcpy(temp, pathname);
- get_basename(temp);
- printf("basename: %s\n", base);
- printf("dirname: ");
- putchar('\n');
- pino = getino(dev, dir);;
- pip = iget(dev, pino);
- // Check file type
- if(pip->INODE.i_mode & 00400000 == 00400000)
- {
- x = search(dev, &(pip->INODE), base);
- if(x == -1)
- {
- printf("Making file \'%s\'\n", base);
- ino = mycreat(pip, base);
- pip->INODE.i_atime = time(0L);
- pip->dirty = 1;
- }
- else
- printf("File \'%s\' already exists.\n", base);
- }
- iput(pip);
- return ino;
- }
- int mycreat(MINODE *pip, char *name)
- {
- /* Differences b/w mycreat and mymkdir
- - INODE's file type = 0x81A4 OR 0100644
- - links_count = 1
- - NO data block, so size = 0
- - do NOT inc parent's link count.
- */
- int ino = ialloc(dev), i;
- char mbuf[BLKSIZE];
- MINODE *mip = iget(dev, ino);
- INODE *cip = &mip->INODE;
- DIR *mdp;
- char *mcp;
- //printf("enter mycreat\n");
- //---------given code--------
- cip->i_mode = 0x81a4;
- cip->i_uid = running->uid;
- cip->i_gid = running->gid;
- cip->i_size = 0; // NO data block, so size = 0
- cip->i_links_count = 1; // links_count = 1
- cip->i_atime = cip->i_ctime = cip->i_mtime = time(0L);
- cip->i_blocks = 0;
- cip->i_block[0] = 0;
- for(i = 1; i < 15; i++)
- {
- cip->i_block[i] = 0;
- }
- mip->dirty = 1;
- mip->dev = dev;
- iput(mip);
- enter_name(pip, ino, name);
- return ino;
- }
- int enter_name(MINODE *pip, int myino, char *myname)
- {
- int lastRecLen = 0, remain, currNode, currBlock, ideal_len = 4*((11+strlen(myname))/4), i, j;
- char mbuf[BLKSIZE];
- INODE *mip;
- DIR *mdp;
- char *mcp;
- // 12 direct block, for each....
- for(i = 0; i < 12; i++)
- {
- if(pip->INODE.i_block[i] == 0)
- {
- printf("Data block %d was null\n", i);
- break;
- }
- else
- {
- // Get parent's ith data block into a buf
- get_block(dev, pip->INODE.i_block[i], mbuf);
- mdp = (DIR *) mbuf;
- mcp = mbuf;
- // Step to LAST entry in block: int blk = parent->INODE.i_block[i]
- while(mcp < mbuf + BLKSIZE && mdp->rec_len != 0)
- {
- if(mcp + mdp->rec_len >= mbuf + BLKSIZE)
- break;
- mcp += mdp->rec_len; // increment
- mdp = (DIR *) mcp; //keep changing this as mcp goes up. haha
- } //dp or (mdp) now points at last entry in block
- lastRecLen = mdp->rec_len;
- remain = lastRecLen - 4*((11+mdp->name_len)/4); // Each entry's ideal length is 4
- // let remain = Last entry's rec_len - it's IDEAL_LENGTH
- if(remain >= ideal_len)
- {
- //enter the new entry as the last entry and trim the previous entry to its IDEAL_LENGTH
- mdp->rec_len = 4*((11+mdp->name_len)/4);//update new length for entry
- mcp += mdp->rec_len;
- mdp = (DIR *) mcp;
- //
- //
- // |LAST entry
- // --|-4---2----2--|----|---------|--------- rlen ->------------------------|
- // |ino rlen nlen NAME|.........|ino rlen nlen|NAME |
- // --------------------------------------------------------------------------
- // | NEW entry
- // --|-4---2----2--|----|---------|----ideal_len-----|--- rlen=remain ------|
- // |ino rlen nlen NAME|.........|ino rlen nlen|NAME|myino rlen nlen myname|
- //
- mdp->inode = myino;
- mdp->rec_len = remain;
- mdp->name_len = strlen(myname);
- for(j = 0; j < strlen(myname); j++){
- mdp->name[j] = myname[j];
- }
- put_block(dev, pip->INODE.i_block[i], mbuf);
- return 0;
- }
- }
- }
- // Reach here mean: NO space in existing data blocks
- // Allocate a new data block
- pip->INODE.i_block[i] = balloc(dev);
- strcpy(mbuf, "\0");
- mdp = (DIR *) mbuf;
- mdp->inode = myino;
- mdp->rec_len = 1024;
- mdp->name_len = strlen(myname);
- for(j = 0; j < strlen(myname); j++)
- {
- mdp->name[j] = myname[j];
- }
- pip->INODE.i_size += 1024; // INC parent's isze by 1024;
- // enter new entry as the first entry in the new data block with rec_len= BLKSIZE
- put_block(dev, pip->INODE.i_block[i], mbuf);
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement