void FileSystem::loadFS( char* FS_File ) { initFS(); initBlocks(); // 클래스 블럭들 포인터 초기화 int fd = open( FS_File, O_RDONLY ); if ( fd == -1 ) // 파일 열기 실패 { /* 블럭의 데이터 값을 초기화 함수 불러준다. */ superBlock.inputSuperBlockData(); // SuperBlock 값 초기화 blockDescriptorTable.inputBlockDescriptorTableData(); // BDT 값 초기화 for ( int i = 0; i <= 5; i++ ) // BlockBitmap 값 초기화 blockBitmap[i] = '1'; /* 디렉토리 초기화 */ DirectoryManager& dm = *DirectoryManager::getInstance(); dm.makeDefaultDirectory(); for (int i = 0; i < 7; i++) // InodeBitmap 값 초기화 inodeBitmap[i] = '1'; } else // 파일 열기 성공 { read( fd, blockList, BLOCK_SIZE * NUM_BLOCKS ); // blockList[409600]에 FS_File을 채워 넣어준다 close( fd ); } }
/* * linkFileKFS() * * Creates a hard link from the old path to the new path, creating * any requested directory structure along the way. */ sval linkFileKFS(char *prog, Disk *disk, char *oldPath, char *newPath) { LSOBasicDir *lsoDir; char *oldfile; SysStatus rc; ObjTokenID oldfileID, newfileID; // initialize the in-memory structures if (sb == NULL) initFS(disk); // find oldPath rc = parseName(0, sb, g, oldPath, lsoDir, oldfile); if (_FAILURE(rc)) { tassertMsg(_SGENCD(rc) == ENOENT, "? rc 0x%lx\n", rc); return rc; } // check if the oldfile exists, and it's a regular file rc = lsoDir->matchDir(oldfile, strlen(oldfile), &oldfileID); if (_FAILURE(rc)) { return _SERROR(2611, 0, ENOENT); } // get the stat information for the old file KFSStat stat; LSOBasic *oldlso = (LSOBasic *)lsoDir->getLocalRecordMap()->getObj(&oldfileID); tassertMsg(oldlso != NULL, "?"); oldlso->getAttribute(&stat); if (!S_ISREG(stat.st_mode)) { // FIXME for now we're only dealing with files and directories. // Maybe we'll need to deal with symlinks at some point ... return _SERROR(2612, 0, EPERM); } // find newPath char *newfile; rc = parseName(1, sb, g, newPath, lsoDir, newfile); tassertMsg(_SUCCESS(rc), "? rc 0x%lx\n", rc); // check if the file already exists rc = lsoDir->matchDir(newfile, strlen(newfile), &newfileID); if (_FAILURE(rc)) { // doesn't exist, so create the new file // FIXME: uid, gid ? lsoDir->createEntry(newfile, strlen(newfile), &oldfileID, 0, 0); lsoDir->flush(); } else { // just update the existing entry lsoDir->updateEntry(newfile, strlen(newfile), &oldfileID); } // sync the directory lsoDir->flush(); // sync the disk metadata g->recordMap->flush(); sb->sync(); return 0; }
/* * formatKFS() * * Creates a fresh superblock on the disk. Also initializes the * RecordMap entries and the block allocation bitmap. Then it creates * the root directory with entries . & .. */ SysStatus formatKFS(Disk *disk) { SysStatus rc; // initialize the memory structures for KFS if (sb == NULL) initFS(disk, 1); // format the superblock, and bitmaps rc = sb->format("disk"); passertMsg(_SUCCESS(rc), "superblock format() failed with rc 0x%lx\n", rc); // Get record oriented PSO for storing obj data KFSFactory factory; rc = factory.allocRecordMap(g->recordMap, g->super->getRecMap(), g); tassertMsg(_SUCCESS(rc), "factory.allocRecordMap problem? rc 0x%lx\n", rc); // create root directory sb->createRootDirectory(); // sync the disk metadata g->recordMap->flush(); sb->sync(); return 0; }
JNIEXPORT jint JNICALL Java_csh_cryptonite_Cryptonite_jniCreate(JNIEnv* env, jobject thiz, jstring srcdir, jstring password, jint config) { int pw_len = (int)env->GetStringLength(password); if (pw_len == 0) { return EXIT_FAILURE; } jniStringManager msrcdir(env, srcdir); jniStringManager mpassword(env, password); RootPtr result; shared_ptr<EncFS_Opts> opts( new EncFS_Opts() ); opts->createIfNotFound = true; opts->checkKey = true; opts->password.assign(mpassword.str()); opts->rootDir.assign(msrcdir.str()); switch ((int)config) { case 0: opts->configMode = Config_Paranoia; break; case 1: opts->configMode = Config_Standard; break; case 2: opts->configMode = Config_Compatible; break; case 3: opts->configMode = Config_Quick; break; default: opts->configMode = Config_Standard; } if(checkDir( opts->rootDir )) { LOGI((std::string("Initialising file system with root ") + msrcdir.str()).c_str()); result = initFS( NULL, opts ); } // clear buffer opts->password.assign(opts->password.length(), '\0'); if(!result) { LOGE("Unable to initialize encrypted filesystem - check path."); return EXIT_FAILURE; } /* clear password copy */ mpassword.release(); return EXIT_SUCCESS; }
/** * Tests the initFS functionality. */ int initFSTest() { int hr = SUCCESS; FAIL_BRK4(initAndLoadDisk()); FAIL_BRK4(initFS()); Fail: saveAndCloseDisk(); PRINT_RESULTS("Init FS Test"); return hr; }
/** * Tests having multiple files openned and written at the same time */ int multipleOpenFilesTest() { int hr = SUCCESS; int fd, fd2, fd3, fsize = SD_SECTORSIZE; char *fname1 = "simult1"; char *fname2 = "simult2"; char *fname3 = "simult3"; char *buffer = malloc(fsize); initBuffer(buffer, fsize); FAIL_BRK4(initAndLoadDisk()); FAIL_BRK4(initFS()); // Openning the files fd = sfs_fopen(fname1); FAIL_BRK3((fd == -1), stdout, "Error: fopen for (%s) failed\n", fname1); fd2 = sfs_fopen(fname2); FAIL_BRK3((fd2 == -1), stdout, "Error: fopen for (%s) failed\n", fname2); fd3 = sfs_fopen(fname3); FAIL_BRK3((fd3 == -1), stdout, "Error: fopen for (%s) failed\n", fname3); // Writing in a different order FAIL_BRK3((sfs_fwrite(fd2, buffer, fsize) != fsize), stdout, "Error: Write failed\n"); FAIL_BRK3((sfs_fwrite(fd, buffer, fsize) != fsize), stdout, "Error: Write failed\n"); FAIL_BRK3((sfs_fwrite(fd3, buffer, fsize) != fsize), stdout, "Error: Write failed\n"); // Closing the files in different order FAIL_BRK3(sfs_fclose(fd3), stdout, "Error: Closing the file failed\n"); FAIL_BRK3(sfs_fclose(fd2), stdout, "Error: Closing the file failed\n"); FAIL_BRK3(sfs_fclose(fd), stdout, "Error: Closing the file failed\n"); FAIL_BRK3(sfs_ls(f_ls), stdout, "Error: ls failed\n"); // reload the disk and verify FAIL_BRK4(saveAndCloseDisk()); FAIL_BRK4(initAndLoadDisk()); FAIL_BRK4(verifyFile(fname1, buffer, fsize)); FAIL_BRK4(verifyFile(fname2, buffer, fsize)); FAIL_BRK4(verifyFile(fname3, buffer, fsize)); Fail: SAFE_FREE(buffer); saveAndCloseDisk(); PRINT_RESULTS("Multiple Open Files Test"); return hr; }
void initFS(Disk *disk, uval format=0) { if (sb != NULL) { printf("initFS has already been called\n"); return; } tassertMsg(g == NULL, "how come?"); g = new KFSGlobals(); sb = initFS(disk, g, format); }
/** * Tests the creation of a number of folders in the root */ int multipleFoldersTest() { int hr = SUCCESS; FAIL_BRK4(initAndLoadDisk()); FAIL_BRK4(initFS()); FAIL_BRK4(createFolders("folder", 500)); Fail: saveAndCloseDisk(); PRINT_RESULTS("Multiple Folders Test"); return hr; }
/** * Tests the creation of a simple folder. */ int createFolderTest() { int hr = SUCCESS; FAIL_BRK4(initAndLoadDisk()); FAIL_BRK4(initFS()); FAIL_BRK4(createFolder("bar")); Fail: saveAndCloseDisk(); PRINT_RESULTS("Create Folder Test"); return hr; }
/** * Tests the ability to add more data to a existing file. */ int appendFileTest() { int hr = SUCCESS; FAIL_BRK4(initAndLoadDisk()); FAIL_BRK4(initFS()); FAIL_BRK4(appendFile()); Fail: saveAndCloseDisk(); PRINT_RESULTS("Append File Test"); return hr; }
/** * Tests the nesting of folders */ int nestedFoldersTest() { int hr = SUCCESS; int numNestedFolders = 50; FAIL_BRK4(initAndLoadDisk()); FAIL_BRK4(initFS()); FAIL_BRK4(nestedFolders("dir", numNestedFolders)); Fail: saveAndCloseDisk(); PRINT_RESULTS("Nested Folders Test"); return hr; }
/** * Tests a number of files in the root folder. */ int multipleFilesTest() { int hr = SUCCESS; int numFiles = 50; int maxFileSize = 10 * SD_SECTORSIZE; FAIL_BRK4(initAndLoadDisk()); FAIL_BRK4(initFS()); FAIL_BRK4(multipleFiles("file", numFiles, maxFileSize)); Fail: saveAndCloseDisk(); PRINT_RESULTS("Multiple Files Test"); return hr; }
/** * Tests a big file write and then read with various techniques. */ int singleBigFileTest() { int hr = SUCCESS; int fsize = SD_SECTORSIZE * 16; char *fileName = "foo"; FAIL_BRK4(initAndLoadDisk()); FAIL_BRK4(initFS()); FAIL_BRK4(singleBigFile(fileName, fsize)); Fail: saveAndCloseDisk(); PRINT_RESULTS("Single Big File Test"); return hr; }
/** * Tests the creation of a small file without attempting to read it afterwards. */ int createSimpleFileTest() { int hr = SUCCESS; int fsize = 250; char fdata[fsize]; initBuffer(fdata, fsize); FAIL_BRK4(initAndLoadDisk()); FAIL_BRK4(initFS()); FAIL_BRK4(createSmallFile("foo", fdata, fsize)); Fail: saveAndCloseDisk(); PRINT_RESULTS("Create Simple File Test"); return hr; }
/* * validateDiskKFS * * A simple listing of all the files on the disk to help validate * the current disk. */ sval validateDiskKFS(Disk *disk) { LSOBasicDir *root; // initialize the in-memory structures if (sb == NULL) initFS(disk); ObjTokenID rootTokID = sb->getRootLSO(); // locate '/' root = (LSOBasicDir *)g->recordMap->getObj(&rootTokID); tassertMsg(root != NULL, "?"); // now recursivly list every file return recursiveList(root, "", g); }
/* * createDirKFS */ SysStatus createDirKFS(char *prog, Disk *disk, char *dir, uval uid, uval gid) { LSOBasicDir *lsoDir, *lsoNewDir; ObjTokenID fileID; char *dirname; SysStatus rc; // initialize the in-memory structures if (sb == NULL) initFS(disk); rc = parseName(1, sb, g, dir, lsoDir, dirname); tassertMsg(_SUCCESS(rc), "? rc 0x%lx\n", rc); // check if the file already exists rc = lsoDir->matchDir(dirname, strlen(dirname), &fileID); if (rc < 0) { // doesn't exist, so create the new directory #ifdef KFS_USE_GLOBAL_RECORDMAP rc = lsoDir->createRecord(&fileID, OT_LSO_BASIC_DIR); #else rc = lsoDir->createRecord(&fileID, OT_LSO_DIR_EMB); #endif _IF_FAILURE_RET(rc); lsoNewDir = (LSOBasicDir *)lsoDir->getLocalRecordMap()->getObj(&fileID); lsoNewDir->initAttribute(S_IFDIR | (0755 & ~S_IFMT), uid, gid); lsoDir->createDir(dirname, strlen(dirname), 0755, uid, &fileID); lsoDir->flush(); lsoNewDir->flush(); } else { err_printf("%s: directory %s already exists, no action taken by " "createDirKFS\n", prog, dir); return 0; } // sync the disk metadata g->recordMap->flush(); sb->sync(); return 0; }
/** * Tests some error messages that we expect when we pass bogus arguments the sfs API */ int errorTest() { int hr = SUCCESS; char buf[100]; // test setup FAIL_BRK4(initAndLoadDisk()); FAIL_BRK4(initFS()); // make sure we are not allowed to read if the files are not open FAIL_BRK3((sfs_fread(0, buf, 50) != -1), stdout, "Error: Allowing read from unopened file\n"); FAIL_BRK3((sfs_fread(rand(), buf, 50) != -1), stdout, "Error: Allowing read from unopened file\n"); FAIL_BRK3((sfs_fread(-5, buf, 50) != -1), stdout, "Error: Allowing read from unopened file\n"); // test if fcd does not allow going to places that does not exist FAIL_BRK3((sfs_fcd("bla") != -1), stdout, "Error: Allowing cd to folder that does not exist\n"); FAIL_BRK3((sfs_fcd("x") != -1), stdout, "Error: Allowing cd to folder that does not exist\n"); FAIL_BRK3((sfs_fcd("x/y/x/z") != -1), stdout, "Error: Allowing cd to folder that does not exist\n"); // testing if lseek does not allow going pass the end of the file int fsize = 10; FAIL_BRK3(createSmallFile("foo", "aaaaaaaaaaaaaaaaaaaaaaaa", fsize), stdout, "Error: Creating file failed\n"); int fd = sfs_fopen("foo"); FAIL_BRK3((fd == -1), stdout, "Error: reopening the file failed\n"); FAIL_BRK3((sfs_lseek(fd, fsize) != -1), stdout, "Error: Allowing seek pass the end of the file\n"); Fail: saveAndCloseDisk(); PRINT_RESULTS("Error Test"); return hr; }
int main(int argc, char **argv) { pvr_init_defaults(); initTXT(TEXT_ENC); initBG("/rd/bg2.png"); if (initHDD()) dbglog(DBG_DEBUG, "* Failed to Initialize HDD!\n"); if (initFS("/hd")) dbglog(DBG_DEBUG, "* Failed to Initialize EXT2 FS!\n"); while (1) { update(); gfx(); } return 0; }
int main() { HANDLE myHandle; char* result = (char*)malloc(1); //TCHAR VirtualDisc[]=TEXT("Carmi.carmi"); initFS("Carmi.Carmi1",100000); myHandle = create("dima.txt", 10); myHandle = open("dima.txt", READWRITE); write((int)myHandle, 0, "Dima is the best", 17); read((int)myHandle, 0, result , 17); puts(result); close((int)myHandle); // UnmapViewOfFile(pDisk); // CloseHandle(hMapFile); return 0; }
static RootPtr initRootInfo(const std::string& rootDir, const std::string& password, bool useAnyKey, const std::string& configOverride) { RootPtr result; shared_ptr<EncFS_Opts> opts( new EncFS_Opts() ); opts->createIfNotFound = false; opts->checkKey = !useAnyKey; opts->password.assign(password); opts->configOverride.assign(configOverride); opts->rootDir.assign(rootDir); if(checkDir( opts->rootDir )) { LOGI((std::string("Initialising file system with root ") + rootDir).c_str()); result = initFS( NULL, opts ); } // clear buffer opts->password.assign(opts->password.length(), '\0'); if(!result) { LOGE("Unable to initialize encrypted filesystem - check path."); } return result; }
/* * createFileKFS() * * Creates a new file on the disk, creating any requested directory * structure along the way. If the file already exists, then it is * overwritten. */ SysStatus createFileKFS(char *prog, Disk *disk, int fd, char *newPath, uval mode, uval uid, uval gid) { LSOBasicDir *lsoDir; LSOBasic *lso; ObjTokenID fileID; char *file; SysStatus rc; // initialize the in-memory structures if (sb == NULL) initFS(disk); rc = parseName(1, sb, g, newPath, lsoDir, file); tassertMsg(_SUCCESS(rc), "? rc 0x%lx\n", rc); // check if the file already exists rc = lsoDir->matchDir(file, strlen(file), &fileID); if (rc < 0) { // doesn't exist, so create the new file rc = lsoDir->createRecord(&fileID, OT_LSO_BASIC); _IF_FAILURE_RET(rc); lso = (LSOBasic *)lsoDir->getLocalRecordMap()->getObj(&fileID); lso->initAttribute(S_IFREG | (mode & ~S_IFMT), uid, gid); lsoDir->createEntry(file, strlen(file), &fileID, S_IFREG | (mode & ~S_IFMT), uid); lsoDir->flush(); } else { lso = (LSOBasic *)lsoDir->getLocalRecordMap()->getObj(&fileID); } // now write the contents of the file tassertMsg(lso != NULL, "?"); KFSStat stat; // ensure that this is a file lso->getAttribute(&stat); if (!S_ISREG(stat.st_mode)) { // error! if (S_ISDIR(stat.st_mode)) { printf("%s: Error for newfile %s: Can't overwrite a directory " "with a file!\n", prog, file); // FIXME: for now let's allow things proceed return 0; } else { printf("%s: Error for newfile %s: stat.st_mode is %lo\n", prog, file, (uval) stat.st_mode); // FIXME: for now let's allow things proceed return 0; } } // [over]write the file (the program fscp using this method has already // verified fd represents an existing, regular file uval64 offset; sval64 count; char buf[OS_BLOCK_SIZE]; offset = 0; while ((count = read(fd, buf, OS_BLOCK_SIZE)) > 0) { lso->writeBlock(offset + count, offset / OS_BLOCK_SIZE, buf, PSO_EXTERN); offset += count; } if (count < 0) { printf("Error while reading from file input file (returned <0)\n"); tassertMsg(0, "?"); return -1; } // sync the disk metadata lso->flush(); lsoDir->flush(); g->recordMap->flush(); sb->sync(); return 0; }
/* * createSymLinkKFS() * * Creates a new symlink on the disk, creating any requested directory * structure along the way. */ SysStatus createSymLinkKFS(char *prog, Disk *disk, char *oldPath, char *newPath, uval uid, uval gid) { LSOBasicDir *lsoDir; ObjTokenID fileID; char *file; SysStatus rc; // initialize the in-memory structures if (sb == NULL) initFS(disk); rc = parseName(1, sb, g, newPath, lsoDir, file); tassertMsg(_SUCCESS(rc), "? rc 0x%lx\n", rc); // check if the file already exists rc = lsoDir->matchDir(file, strlen(file), &fileID); if (rc < 0) { // doesn't exist, so create the new file char linkValue[512]; int sz; if ((sz = readlink(oldPath, linkValue, sizeof(linkValue))) == -1) { passertMsg(0, "readlink for %s failed\n", oldPath); // FIXME: implement cleanup after error } passertMsg((uval)sz < sizeof(linkValue), "sz %ld, size of buffer %ld", (uval)sz, (uval)(sizeof(linkValue))); linkValue[sz] = '\0'; //err_printf("linkValue is %s\n", linkValue); rc = lsoDir->createRecord(&fileID, OT_LSO_BASIC_LNK); if (_FAILURE(rc)) { passertMsg(0, "createSymLinkKFS() problem with allocRecord\n"); // FIXME: kfs tools stuff should have proper SysStatus ... // "rc is (%ld, %ld, %ld)\n", _SERRCD(rc), _SCLSCD(rc), // _SGENCD(rc) } LSOBasicSymlink *lsoSymlink; lsoSymlink = (LSOBasicSymlink *)lsoDir->getLocalRecordMap()->getObj(&fileID); tassertMsg(lsoSymlink != NULL, "?"); rc = lsoSymlink->initAttribute(linkValue, S_IFLNK | (0777 & ~S_IFMT), uid, gid); if (_FAILURE(rc)) { passertMsg(0, "creareSymLinkKFS(): problem in initAttribute\n"); } rc = lsoDir->createEntry(file, strlen(file), &fileID, S_IFLNK, 0); if (_FAILURE(rc)) { passertMsg(0, "creareSymLinkKFS(): problem in createEntry\n"); } lsoSymlink->flush(); lsoDir->flush(); } else { passertMsg(0, "NIY\n"); } // sync the disk metadata g->recordMap->flush(); sb->sync(); return 0; }
if (mode == MODE_SINGLE) { aptOpenSession(); APT_GetProgramID(&PID); aptCloseSession(); printf("Title Id: %08X%08X\n", (unsigned int) (PID >> 32), (unsigned int) (PID & 0xFFFFFFFF)); } gfxFlushBuffers(); gfxSwapBuffers(); u8* u8file; u64 size; Result ret = initFS(); if (ret != 0) { printf("Error Initializing: %li\n", ret); debug = true; } else { ret = getFile(&u8file, &size); if (ret != 0) { printf("Error Reading: %li\n", ret); debug = true; } else if (size == 0) { puts("File already cleared"); debug = true; } else { if (mode != MODE_ALL) {
/** * Tests the algorithm for performance */ int perfTest() { int hr = SUCCESS; int i; char *fileName = malloc(32); char *dirName = malloc(32); printf("###########################\n"); printf("Let the competition begin!\n"); FAIL_BRK4(initAndLoadDisk()); FAIL_BRK4(initFS()); // lots of files FAIL_BRK4(multipleFiles("file", 900, SD_SECTORSIZE)); // deleting them for (i = 0; i < 900; i++) { sprintf(fileName, "file%05d", i); FAIL_BRK4(sfs_rm(fileName)); } // lots of files again FAIL_BRK4(multipleFiles("file", 900, SD_SECTORSIZE)); // now deleting them in reverse for (i = 900 - 1; i >= 0; i--) { sprintf(fileName, "file%05d", i); FAIL_BRK4(sfs_rm(fileName)); } // one very big file FAIL_BRK4(singleBigFile("huge.txt", (SD_NUMSECTORS - 100) * SD_SECTORSIZE)); // and now it is gone FAIL_BRK4(sfs_rm("huge.txt")); // lots of flat folders FAIL_BRK4(createFolders("dir", 900)); for (i = 0; i < 900; i++) { sprintf(dirName, "dir%04d", i); FAIL_BRK4(sfs_rm(dirName)); } // lots of nested folders FAIL_BRK4(nestedFolders("dir", 900)); // going to the bottom FAIL_BRK3(sfs_fcd("/"), stdout, "Error: cd / failed\n"); for (i = 0; i < 900; i++) { sprintf(dirName, "dir%04d", i); FAIL_BRK3(sfs_fcd(dirName), stdout , "Error: fcd to (%s) failed\n", dirName); } // deleting them on the way out for (i = 900 - 1; i >= 0; i--) { sprintf(dirName, "dir%04d", i); FAIL_BRK3(sfs_fcd(".."), stdout, "Error: cd .. failed\n"); FAIL_BRK3(sfs_rm(dirName), stdout, "Error: rm (%s) failed\n", dirName); } Fail: SAFE_FREE(fileName); SAFE_FREE(dirName); printf("COMPETITION RESULTS: "); saveAndCloseDisk(); PRINT_RESULTS("Performance Test"); return hr; }
/** * Tests sfs_rm functionality. */ int removeTest() { int hr = SUCCESS; char dirName[16], fileName[16]; int i, numDirs = 100, numFiles = 100, fsize = SD_SECTORSIZE * 1.8; char *buffer = malloc(fsize * sizeof(char)); // test setup FAIL_BRK4(initAndLoadDisk()); FAIL_BRK4(initFS()); FAIL_BRK3(sfs_ls(f_ls), stdout, "Error: sfs_ls() failed\n"); // make some files for (i = 0; i < numFiles; i++) { sprintf(fileName, "file%04d", i); initBuffer(buffer, fsize); FAIL_BRK4(createSmallFile(fileName, buffer, fsize)); } // make some folders for (i = 0; i < numDirs; i++) { sprintf(dirName, "dir%04d", i); FAIL_BRK4(createFolder(dirName)); } FAIL_BRK3(sfs_ls(f_ls), stdout, "Error: sfs_ls() failed\n"); // delete the even folders and files for (i = 0; i < numFiles; i += 2) { sprintf(fileName, "file%04d", i); FAIL_BRK3(sfs_rm(fileName), stdout, "Error: deleting file (%s) failed\n", fileName); sprintf(dirName, "dir%04d", i); FAIL_BRK3(sfs_rm(dirName), stdout, "Error: deleting folder (%s) failed\n", dirName); } FAIL_BRK3(sfs_ls(f_ls), stdout, "Error: sfs_ls() failed\n"); // delete the rest of the files and folders for (i = 1; i < numFiles; i += 2) { sprintf(fileName, "file%04d", i); FAIL_BRK3(sfs_rm(fileName), stdout, "Error: deleting file (%s) failed\n", fileName); sprintf(dirName, "dir%04d", i); FAIL_BRK3(sfs_rm(dirName), stdout, "Error: deleting folder (%s) failed\n", dirName); } FAIL_BRK3(sfs_ls(f_ls), stdout, "Error: sfs_ls() failed\n"); Fail: SAFE_FREE(buffer); saveAndCloseDisk(); PRINT_RESULTS("Remove Test"); return hr; }
int main(){ FileSystem fs; size_type size = 0x7FFFFFF; printf("system size set to: %ld\n", size); initFS(size, 20, &fs); loadFS(&fs); printFileSystem(&fs); // printDisk(&(fs.disk_emulator), 0); Inode inode; size_type inode_id = ROOT_INODE_ID; if(getInode(&fs, &inode_id, &inode)!=Success){ printf("[Potato mount] Error: root directory not exist.\n"); //return Err_GetInode; return -1; } //allocate a block to root directory size_type block_id; allocBlock(&fs, &block_id); inode.fileType = Directory; size_type i = 0; for(i=0; i<DIRECT_BLOCK_NUM; i++){ inode.directBlock[i] = -1; } inode.singleBlock = -1; inode.doubleBlock = -1; inode.tripleBlock = -1; inode.directBlock[0] = block_id; inode.used = true; strcpy(inode.fileOwner, "NULL"); inode.fileModifiedTime = time(NULL); inode.fileAccessTime = time(NULL); inode.inodeModifiedTime = time(NULL); inode.Permission = S_IFDIR | 0755; DirEntry dir_entry[2]; strcpy(dir_entry[0].key, "."); dir_entry[0].inodeId = ROOT_INODE_ID; strcpy(dir_entry[1].key, ".."); dir_entry[1].inodeId = ROOT_INODE_ID; BYTE* buf = malloc(BLOCK_SIZE); memcpy(buf, dir_entry, sizeof(dir_entry)); put(&fs, block_id + fs.super_block.firstDataBlockId, buf); inode.fileSize = sizeof(dir_entry); //for . and .. inode.numOfLinks = 2; ErrorCode err = putInode(&fs, &inode_id, &inode); if(err != Success){ printf("put root failed: Error %d", err); } Inode cur_inode; getInode(&fs, &inode_id, &cur_inode); printf("inode links: %ld\n", cur_inode.numOfLinks); printDisk(&(fs.disk_emulator), 1); //put free list buf into disk put(&fs, fs.super_block.pDataFreeListHead + fs.super_block.firstDataBlockId, &(fs.dataBlockFreeListHeadBuf)); put(&fs, fs.super_block.pDataFreeListTail + fs.super_block.firstDataBlockId, &(fs.dataBlockFreeListTailBuf)); SuperBlockonDisk super_block_on_disk; mapSuperBlockonDisk(&(fs.super_block), &(super_block_on_disk)); memcpy(buf, &super_block_on_disk, sizeof(SuperBlockonDisk)); put(&fs, SUPER_BLOCK_OFFSET, buf); //set root acess time free(buf); closefs(&fs); FileSystem new_fs; loadFS(&new_fs); // printFileSystem(&new_fs); getInode(&new_fs, &inode_id, &cur_inode); printf("inode links: %ld\n", cur_inode.numOfLinks); closefs(&fs); return 0; }
/** * This is a place where you need to put your test. Please insert your test code here and * change the name of the test to be representative of what you are testing. You can use this test * during the development period to test your code. When you do the final submission here you have to * implement a test that tests some interesting functionality of the sfs. * @return SUCCESS if the test is passed successfully FAIL otherwise. */ int customTest() { int hr = SUCCESS; int i, j, fd, fd1, fd2, lseek2; char *randomBuf;//buffer contain junk data, size: SD_SECTORSIZE randomBuf = (char *) malloc(sizeof(char) * SD_SECTORSIZE); // For later tests char* asciidata = (char*)malloc(257 * sizeof(char)); // 2 char* morealphabet = (char*)malloc(7*sizeof(char)); morealphabet[0] = 't'; morealphabet[1] = 'u'; morealphabet[2] = 'v'; morealphabet[3] = 'w'; morealphabet[4] = 'x'; morealphabet[5] = 'y'; morealphabet[6] = 'z'; char alphabet[26] = "abcdefghijklmnopqrstuvwxyz"; char* fd1read = (char*)malloc(26*sizeof(char)); // normal test from testfs // initialize disk FAIL_BRK4(SD_initDisk()); for (i = 0; i < SD_NUMSECTORS; i++) { for (j = 0; j < SD_SECTORSIZE; j++) { randomBuf[j] = (char) rand(); } while (SD_write(i, (void*) randomBuf)); } FAIL_BRK4(SD_loadDisk(gsDiskFName)); refreshDisk(); // initialize fs, sfs_mkfs FAIL_BRK4(initFS()); refreshDisk(); // initialize test sfs_mkfs, when mkfs, nothing should appear again. FAIL_BRK4(initFS()); FAIL_BRK4(sfs_mkdir("foo")); refreshDisk(); FAIL_BRK4(sfs_fcd("foo")); FAIL_BRK4(sfs_mkdir("bar")); FAIL_BRK4(initFS()); refreshDisk(); FAIL_BRK4((sfs_fcd("foo") != -1)); FAIL_BRK4((sfs_fcd("bar") != -1)); FAIL_BRK4(sfs_fcd("/")); FAIL_BRK4((sfs_fcd("foo") != -1)); FAIL_BRK4((sfs_fcd("bar") != -1)); FAIL_BRK4(sfs_mkdir("foo")); refreshDisk(); FAIL_BRK4(sfs_fcd("foo")); FAIL_BRK4((sfs_fcd("bar") != -1)); //normal test . and .. FAIL_BRK4(initFS()); refreshDisk(); FAIL_BRK4((sfs_mkdir(".") != -1)); FAIL_BRK4(sfs_mkdir(".foo.")); FAIL_BRK4(sfs_mkdir(".foo")); FAIL_BRK4((sfs_mkdir("..") != -1)); FAIL_BRK4(sfs_mkdir("..foo")); FAIL_BRK4(sfs_mkdir("..foo..")); FAIL_BRK4((sfs_mkdir("/") != -1)); FAIL_BRK4(sfs_mkdir("...")); FAIL_BRK4(sfs_mkdir("....")); FAIL_BRK4((sfs_mkdir("./") != -1)); FAIL_BRK4((sfs_mkdir("/.") != -1)); FAIL_BRK4((sfs_mkdir("./.") != -1)); // ascii code test, make a file containing all ascii code chars to make sure implementation does not use an EOF char for size FAIL_BRK4(initFS()); refreshDisk(); //257 chars, possible ascii 0 -> 255 + 255 at the end to make sure we test // create data for (i = 0; i < 256; i++) { asciidata[i] = i; // table sequentially } // now put 255 in again at the end asciidata[256] = 255; // now fwrite into the a file and fread and compare FAIL_BRK4(createSmallFile("asciitable", asciidata, 257)); refreshDisk(); FAIL_BRK4(verifyFile("asciitable", asciidata, 257)); // This test will open the same file twice, write more onto it using one of the file descriptors to lengthen it, and read both to make sure they still match (should test to make sure size is with inodes and not the table FAIL_BRK4(initFS()); refreshDisk(); FAIL_BRK4(createSmallFile("alphabet", "abcdefghijklmnopqrst", 20)); // Open twice FAIL_BRK4((fd1 = sfs_fopen("alphabet")) == -1); FAIL_BRK4((fd2 = sfs_fopen("alphabet")) == -1); // lseek to end and write to second one FAIL_BRK4((lseek2 = sfs_lseek(fd2, 19)) == -1); FAIL_BRK4((sfs_fwrite(fd2, morealphabet, 7)) == -1); // Now verify we can read from fd1 and it should be the full alphabet since we wrote to fd2 which is the same file FAIL_BRK4((sfs_fread(fd1, fd1read, 26)) == -1); FAIL_BRK4((strncmp(alphabet, fd1read, 26)) != 0); // here is the comparison of strings FAIL_BRK4((sfs_fclose(fd1)) == -1); FAIL_BRK4((sfs_fclose(fd2)) == -1); //test dir takes more that one sector //test for file FAIL_BRK4(initFS()); refreshDisk(); FAIL_BRK4((fd = sfs_fopen("foo.bin")) == -1); FAIL_BRK4(sfs_fclose(fd)); FAIL_BRK4((sfs_fcd("foo.bin") != -1)); //test same name FAIL_BRK4(initFS()); refreshDisk(); FAIL_BRK4(sfs_mkdir("foo.bin")); FAIL_BRK4((fd = sfs_fopen("foo.bin")) != -1); FAIL_BRK4((fd = sfs_fopen("bar.bin")) == -1); FAIL_BRK4((sfs_mkdir("bar.bin") != -1)); //test initFS will erase the file descriptor FAIL_BRK4(initFS()); refreshDisk(); FAIL_BRK4((fd = sfs_fopen("foo.bin")) == -1); FAIL_BRK4(initFS()); FAIL_BRK4((sfs_fclose(fd) != -1)); //test create nothing FAIL_BRK4(initFS()); refreshDisk(); FAIL_BRK4((sfs_mkdir("") != -1)); FAIL_BRK4((fd = sfs_fopen("")) != -1); //test open . and .. FAIL_BRK4(initFS()); refreshDisk(); FAIL_BRK4((fd = sfs_fopen("/")) != -1); FAIL_BRK4((fd = sfs_fopen(".")) != -1); FAIL_BRK4((fd = sfs_fopen("..")) != -1); FAIL_BRK4(sfs_mkdir("foo")); FAIL_BRK4((fd = sfs_fopen("foo")) != -1); FAIL_BRK4(sfs_fcd("foo")); FAIL_BRK4((fd = sfs_fopen("/")) != -1); FAIL_BRK4((fd = sfs_fopen(".")) != -1); FAIL_BRK4((fd = sfs_fopen("..")) != -1); FAIL_BRK4(sfs_fcd("/")); FAIL_BRK4((fd = sfs_fopen("/.")) != -1); FAIL_BRK4((fd = sfs_fopen("./")) != -1); FAIL_BRK4((fd = sfs_fopen("./.")) != -1); FAIL_BRK4((fd = sfs_fopen("/..")) != -1); FAIL_BRK4((fd = sfs_fopen("../")) != -1); FAIL_BRK4((fd = sfs_fopen("../..")) != -1); FAIL_BRK4((fd = sfs_fopen("//")) != -1); FAIL_BRK4((fd = sfs_fopen(".foo")) == -1); FAIL_BRK4((fd = sfs_fopen(".foo.")) == -1); FAIL_BRK4((fd = sfs_fopen("..foo")) == -1); FAIL_BRK4((fd = sfs_fopen("..foo..")) == -1); FAIL_BRK4((fd = sfs_fopen("...")) == -1); FAIL_BRK4((fd = sfs_fopen(".....")) == -1); // test the malloc not been released FAIL_BRK4(sfs_mkfs()); refreshDisk(); int thememmax; struct rusage ru; getrusage(RUSAGE_SELF, &ru); thememmax=ru.ru_maxrss; long int k; for(k = 0; k < 100000; ++k) { FAIL_BRK4(sfs_mkfs()); FAIL_BRK4(sfs_mkdir("foo")); FAIL_BRK4(sfs_fcd("foo")); FAIL_BRK4(sfs_ls(f_ls)); FAIL_BRK4((fd = sfs_fopen("bar")) == -1); FAIL_BRK4((sfs_fwrite(fd, randomBuf, SD_SECTORSIZE)) == -1); FAIL_BRK4((sfs_lseek(fd, 1)) == -1); FAIL_BRK4((sfs_fread(fd, randomBuf, SD_SECTORSIZE - 1)) == -1); FAIL_BRK4(sfs_fclose(fd)); } getrusage(RUSAGE_SELF, &ru); FAIL_BRK4(100 * thememmax <= ru.ru_maxrss); /* FAIL_BRK4(createFolder("bar")); FAIL_BRK4(sfs_fcd("bar")); FAIL_BRK4(createFolder("foo")); FAIL_BRK4(sfs_fcd("foo")); FAIL_BRK4(sfs_fcd("/bar/foo")); // FAIL_BRK3((sfs_fcd("//bar/foo") != -1), stdout, "Error: sfs_fcd() failed\n"); FAIL_BRK4(sfs_fcd("/../bar/foo")); FAIL_BRK4(sfs_fcd("/../bar/foo/")); FAIL_BRK4(sfs_fcd("/")); FAIL_BRK3(sfs_ls(f_ls), stdout, "Error: sfs_ls() failed\n"); FAIL_BRK4(sfs_fcd("/bar")); FAIL_BRK3(sfs_ls(f_ls), stdout, "Error: sfs_ls() failed\n"); //int fopentmp; //FAIL_BRK3((fopentmp = sfs_fopen("test.txt") != -1), stdout, "Error: Allowing read from unopened file\n"); //FAIL_BRK3((sfs_fread(fopentmp, buf, 50) != -1), stdout, "Error: Allowing read from unopened file\n"); // test if fcd does not allow going to places that does not exist FAIL_BRK3((sfs_fcd("bla") != -1), stdout, "Error: Allowing cd to folder that does not exist\n"); FAIL_BRK3((sfs_fcd("x") != -1), stdout, "Error: Allowing cd to folder that does not exist\n"); FAIL_BRK3((sfs_fcd("x/y/x/z") != -1), stdout, "Error: Allowing cd to folder that does not exist\n"); */ Fail: //clean up code goes here SAFE_FREE(asciidata); SAFE_FREE(morealphabet); SAFE_FREE(fd1read); SAFE_FREE(randomBuf); saveAndCloseDisk(); PRINT_RESULTS("customTest!"); return hr; }