/* * Main driver program for hardlink to directory program. * Written by Michael Rywalt - 2569 * Operating System Concepts */ #include "../Common/file.h" int main(int argc, char **argv) { void *pFileSystem; tsSuperBlock *pSuperBlock; tsDirEntry sDirEntry; tsInode *pInode; int iBlockSize; int iFileSystemSize; int iRetVal = 0; /* We must have the correct number of arguments to proceed. */ if(argc < 4) { printf("usage: lnhdir \n"); return EXIT_FAILURE; } pFileSystem = LoadFileSystem(argv[1]); if(pFileSystem == NULL) { printf("Error: Filesystem not found, or too small.\n"); return EXIT_FAILURE; } pSuperBlock = (tsSuperBlock*)((char*)pFileSystem + SUPERBLOCK_OFFSET); /* Verify that we're looking at a real EXT2 filesystem. */ if(!VerifyFSMagicNumber(pSuperBlock)) { FreeFileSystem(pFileSystem); printf("Filesystem is not EXT2, or has been corrupted.\n"); return EXIT_FAILURE; } printf("lnhdir: attempting to hardlink %s to %s...\n", argv[3], argv[2]); /* Get the filesystem size. */ iFileSystemSize = GetFileSize(argv[1]); /* Get our block size. */ iBlockSize = GetFSBlockSize(pSuperBlock); /* We want to get the root directory inode. 0 is our starting inode table entry. */ pInode = GetInode(1, pFileSystem); /* We are now finding an inode based on the file/dir name. This is the second argument to the program. */ pInode = GetMatchingInode(argv[2], pInode, pFileSystem); /* Add a new entry into the current directory's table which points to the current directory's inode. * This is our hard link to the directory. */ iRetVal = InsertHardLink(argv[1], argv[3], pInode, pFileSystem); if(iRetVal == 1) { /* Save the newly modified filesystem to disk. */ SaveModifiedFileSystemData(argv[1], pFileSystem, iFileSystemSize); printf("Directory %s now has hard-link %s inside.\nHere is the new directory listing for %s:\n", argv[2], argv[3], argv[2]); /* Print the directory listing showing our new entry. */ PrintDirectoryListing(pInode, pFileSystem); } else if(iRetVal == -1) { printf("Sorry, %s is not a directory, as it should be! Quitting without modifying filesystem...\n", argv[2]); } else if(iRetVal == -2) { printf("Error: Filename already exists in directory %s.\n", argv[2]); } return 0; }