Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "type.h"
- // Creates a file newFileName which has the SAME inode (number) as that of oldFileName.
- int link(char *oldFileName, char *newFileName)
- {
- int inoOld, i;
- char *temp = (char *) malloc(sizeof(char)*(strlen(oldFileName) + 1)), *pn = 0;
- MINODE *mip, *pip;
- strcpy(temp, oldFileName);
- //Get the INODE into memory
- //trivial
- inoOld = getino(dev, temp);
- free(temp);
- mip = iget(dev, inoOld);
- printf("starting link\n");
- //check to see if it is a REG or LNK
- //if it's a DIR //if it's a REG
- if((mip->INODE.i_mode & 100000) == 100000 || (mip->INODE.i_mode & 12000 == 12000))
- {
- pn = (char *) malloc(sizeof(char)*(strlen(newFileName) + 1));
- strcpy(pn, newFileName);
- if(pn[0] == '/') // If the first char is a /, go ahead and get dirname on root
- {
- get_dirname(pn); // Get dirname
- }
- temp = (char *) malloc(sizeof(char)*(strlen(dir) + 1));
- strcpy(temp, dir); // Store dir name in temp
- i = getino(dev, temp); // Get inode of oldfile
- free(temp);
- if(i == -1) // if it failed to get inode
- {
- printf("dir \'%s\' doesn't exist.\n", dir);
- free(pn);
- return -1;
- }
- pip = iget(dev, i); // remember iget ends with iput
- i = getino(dev, pn);
- if(i != -1) // check /x/y exists and is a DIR but 'z' does not yet exist in /x/y/
- {
- printf("file already exists.\n");
- free(pn);
- iput(pip);
- return -1;
- }
- get_basename(pn);
- enter_name(pip, mip->ino, base); // Enter name ENTRY into parent's directory
- mip->INODE.i_links_count++; // Increment count
- mip->dirty = 1; // Make dirty
- iput(pip);
- free(pn);
- }
- //Back to Disk
- iput(mip);
- }
- int symlink(char *oldName, char *newName)
- {
- int i, ino;
- char *oldPn = 0, *newPn = 0;
- MINODE *mip;
- //////////////////////////////////////////////////////////////////////////////////
- // Verify oldname exists
- oldPn = (char *) malloc(sizeof(char)*(strlen(oldName) + 1));
- strcpy(oldPn, oldName);
- // if it begins with a /, so a specfied location...
- if(oldPn[0] == '/')
- {
- i = getino(dev, oldPn);
- }
- if(i == -1)//fails to get the inode
- {
- free(oldPn);
- return -1;
- }
- ///////////////////////////////////////////////////////////////////////////////
- // Check where user wants newname to be created
- //Get the pathname then set it to the correct name for creat
- newPn = (char *) malloc(sizeof(char)*(strlen(newName) + 1));
- strcpy(newPn, newName);
- if(newPn[0] == '/')
- {
- strcpy(pathname, newPn);
- }
- else
- {
- free(newPn);
- //newPn = my_cwd();
- newPn = realloc(newPn, sizeof(char)*(strlen(newPn)+strlen(newName)+1));
- strcat(newPn, newName);
- strcpy(pathname, newPn);
- }
- //Complete
- i = getino(dev, newPn);
- if(i != -1)
- {
- free(newPn);
- return -1;
- }
- /////////////////////////////////////////////////////////////////////////////////
- ino = creat_file();
- mip = iget(dev, ino);
- mip->INODE.i_mode = 0xa1ff;
- mip->INODE.i_size = strlen(oldPn);
- mip->dirty = 1;
- strcpy((char *) mip->INODE.i_block, oldPn);
- //Back to Disc
- iput(mip);
- //proper remove!
- free(newPn);
- free(oldPn);
- return 0;
- }
- int unlink(char* pn)
- {
- if (pn[0]=='\0'){ // Empty pathname
- printf("Syntax: unlink [path]\n");
- return -1;
- }
- char parentdir[64],name[64], *cp, *endcp, *last;
- DIR * dp;
- MINODE * pip,*targetip;
- int parent, target;
- cp = strrchr(pn, '/');
- // 1. Check where the parent is and get the iNode based on cp
- if (!cp){ // In the same directory
- parent = running->cwd->ino; // Set parent to cwd inode
- strcpy(name,pn);
- }
- else{ // In another directory
- *(cp) = '\0';
- strcpy(parentdir, pn);
- parent = getino(fd,parentdir); // Get inode of parentdir, store into parent
- strcpy(name,cp+1);
- }
- target = getino(fd,pn);
- // Check if target file exists
- if ((target==0)||(parent==0)){
- printf("Error: File must exist\n");
- return -1;
- }
- pip = iget(fd,parent);
- targetip = iget(fd,target);
- printf("i_mode: %d",targetip->INODE.i_mode);
- // 2. If the target exists, make sure it's not a dir...
- if((targetip->INODE.i_mode & 00100000) != 00100000){
- iput(pip);
- printf("Error: Cannot unlink NON-REG files\n");
- return -1;
- }
- // 3. Decrement links count
- targetip->INODE.i_links_count--; // I links count--
- // 4. Clear data with truncate
- if (targetip->INODE.i_links_count == 0)
- {
- // 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
- targetip->refCount++;
- iput(targetip);
- idealloc(fd,targetip->ino);
- }
- else
- {
- iput(targetip);
- }
- return deleteChild(pip,name);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement