int File::unlinkFile(char* file)//, TableManager& tm, FileSystem& fs ) { TableManager* t = TableManager::getInstance(); TableManager& tm = *t; FileSystem* f = FileSystem::getInstance(); FileSystem& fs = *f; PathManager* p = PathManager::getInstance(); PathManager& pm = *p; // ��ü System�� TableManager���� unlink�� file�� open�Ǿ��ִ��� �˻� // ���� ���丮�� ���� ���̳�带 �о�� tmpTm���� �Ķ���ͷ� �Ѿ�� �ý����� TM���� ������ ���µǾ��ִ��� �˻��Ѵ� --> inode���̺� inode�� �˻��ϴ� ��� �߰� // ���̳�� ��ȣ�� ��ü �ý����� �ý������� ���̺� ������ ������ ���µǾ��ִ� ���̹Ƿ� Unlink �Ұ� // ������ ���� ���丮�κ��� ������ ���̳���ȣ�� �ƿ´� // ������(file)�� pathŬ������ �Լ��� ���� ���ʷ� �������ش�. TableManager tmpTM = TableManager::getTmpInstance(); vector<string>* pathFiles = pm.getAllAbsPath(file);// ������ ���ʷ� �������� // ��� ���������Ͽ� ��� ���� ( Mode �����ؾ��ϴϱ� ��� ���ϵ� ���� ) int count = pathFiles->size(); int *fd = new int[count]; for (int i = 0; i < count; i++) { fd[i] = openFile(stringToCharArr((*pathFiles)[i]), tmpTM); } InodeElement* delFileInode = (InodeElement*)tmpTM.getElement(INODET, fd[count - 1]); // ���� �ý����� ���̺� �����Ѵٴ� ���� ���µǾ��ִٴ� ���̹Ƿ� ����! if (tm.isExistInInodeTable(delFileInode->inode_number)) return 1; // ���°�� ���������� ������ unlink����� ��. // ���̳�� ��ȣ�� �ش��ϴ� ���̳�� ������ �о�� // ���ϴ� ���丮���� �ش� ���ϰ� ���̳���ȣ ���� --> ���丮 Ŭ�������� ���ϸ��� ���� ��Ʈ�� �����ϴ� �Լ� �ʿ� DirectoryManager dm = DirectoryManager::getTmpInstance(); Directory dir = dm.Dir_Read(stringToCharArr((*pathFiles)[pathFiles->size() - 2])); dir.rmDirectory(delFileInode->inode_number); /* ���� inode�� ��ũ���� ������ ��ũ���� 0�ΰ� ������������, �� ���������� ��ũ���� ������� �����ϱ� �ٷ� ������ �� */ fs.writeFS(delFileInode->inode_number); /* InodeBitmap�� �ش� ��Ʈ 0���� set BlockBitmap�� dataIdx �� �ش��ϴ� ��Ʈ 0���� set --> blockBitmap�� 0�̸� ��Ȱ���̹Ƿ� ���� �ʱ�ȭ������ ��ġ�� �ʾƵ� �� InodeBlock���� ���̳�� ��ȣ�� �ش��ϴ� ���̳�� ���� --> InodeBitmap�� 0�̸� ��Ȱ���̹Ƿ� ���� �ʱ�ȭ������ ��ġ�� �ʾƵ� �� */ return 0; }
int File::openFile(char* file, TableManager& tm)//, FileSystem& fs ) { FileSystem* f = FileSystem::getInstance(); FileSystem& fs = *f; PathManager* p = PathManager::getInstance(); PathManager& pm = *p; int fd = 0; // InodeTable���� ���� ���丮�� ������ �� idx �ƿ� vector<string>* pathFiles = pm.getAllAbsPath(file);// ������ ���ʷ� �������� DirectoryManager dm = DirectoryManager::getTmpInstance(); Directory dir = dm.Dir_Read(stringToCharArr((*pathFiles)[pathFiles->size() - 2])); // ������ �����ͺ� �о�� Entry* dirFileEntry = dir.findName(stringToCharArr((*pathFiles)[pathFiles->size() - 1])); if (dirFileEntry == nullptr) { cout << endl << "�ش� ���丮�� ������ ����." << endl; fd = -1; return fd; } int inodeNo = dirFileEntry->inodeNum; Inode inode = fs.readFS(inodeNo); /* (FS) InodeBlock���� inodeNo��° �о� �� */ if (tm.isExistInInodeTable(inodeNo)) { cout << endl << "open�Ǿ��ִ� �Լ���" << endl; return fd;// fd = 0 -> �̹� ���µǾ��ִ� ��� } fd = tm.fileOpenEvent(inodeNo, inode); /* InodeTable�� �о�� inode���� ����->�ý����������̺�->���ϵ�ũ���� ������ InodeBlocks���� ������ ���̳�� ������ �о� ���̳�� ���̺��� �ε��� n�� ���� �ý��� ���� ���̺��� �ε��� k�� ���� ������ ���� inode��ȣ�� �ε���n�� �����ϰ� ���� �����ʹ� 0���� �ʱ�ȭ ���� ��ũ���� ���̺��� �ε��� j �� �ý��� ���� ���̺��� �ε��� k ���� �ε��� j ���� */ return fd; //���� ��ũ���� ��ȯ } // openFile
void File::createFile(char* file)//, DirectoryManager& dm ) { DirectoryManager* d = DirectoryManager::getInstance(); DirectoryManager& dm = *d; FileSystem* f = FileSystem::getInstance(); FileSystem& fs = *f; PathManager* p = PathManager::getInstance(); PathManager& pm = *p; Inode inode; char mode[5] = "f666"; char time[13]; getCurrentTime(time); char linkCount[2] = "1"; char blocks[2] = "1"; /* ( ���� ) ���� ���� �� ������ ���� ������ 1 ( �� ������ �����ϹǷ� ), ������ ���� �ε����� FileSystem���� �Ҵ����� */ // datablock[dataIdx] �� ���ϸ� ���� setDatablock( int idx, char* data ) --> ( dataIdx, file ); char size[2] = "0"; // getSizeOfDataBlock( int idx ); // Inode Table�� ���� ���ֱ� ( char* mode ,int size, struct tm lastTime, struct tm creatTime, struct tm modifyTime, int links_count, int blocks, int dataIdx) // Inode Table������ block[]�� dataIdx ������ inode.mode = mode; inode.size = size; // ���ϻ����ÿ��� ��� �����Ƿ� �Ű������� ������ Ÿ�� ��ü�� �ѱ� inode.time = time; // ���� ������ ���� �ð� ���� inode.ctime = time; // ���� ���� �ð� ���� inode.mtime = time; // ������ ���������� ������ �ð� ���� inode.linksCount = linkCount; inode.blocks = blocks; int inodeNo = fs.writeFS(inode); /* (FS) BlockDescriptor Table�� �� �Ҵ�� ��� ��, �� �Ҵ�� inode �� ���� inode�� writeFS�� ���� FS�� ������ ���� ���� �������� FS�� �������ְ�, inodeNo�� �ƿ´�. */ vector<string>* pathFiles = pm.getAllAbsPath(file); Directory dir = dm.Dir_Read(stringToCharArr((*pathFiles)[pathFiles->size() - 2])); Entry fileEntry; fileEntry.inodeNum = inodeNo; vector<string> files; pm.doAnalyzeFolder(file, files);// ������ ���ʷ� �������� strcpy(fileEntry.name, files[files.size() - 1].c_str()); // directory.addDir( inodeNo, filename ); // ���ϸ�� ����InodeNo�� ���丮�� ���� dir.addDirectory(fileEntry, inodeNo); } // createFile
void DirectoryManager::openAllDir(char * path) { FileSystem& fs = *FileSystem::getInstance(); TableManager& tm = *TableManager::getInstance(); PathManager& pm = *PathManager::getInstance(); vector<string> vStr = *pm.getAllAbsPath(path); vector<string> vAllDirec = pm.doAnalyzeFolder(path); Directory dir = *returnDir(0); // root의 dir 객체 가져오기 int fd = tm.fileOpenEvent(0, fs.inodeBlock->getInodeData(0)); openedDir_FDList.push_back(fd); for (int i = 1; i < vStr.size(); i++) { // 상위 디렉토리를 통해 먼저 inodeNum과 Block을 얻는다. Entry* en = dir.findName(stringToCharArr(vAllDirec[i])); if (!en) { cout << "Name : " << vAllDirec[i] << endl; throw "dir의 엔트리에서 name을 찾지 못함."; } int inodeNum = en->inodeNum; Inode inodeBl = fs.inodeBlock->getInodeData(inodeNum); dir = Dir_Read(stringToCharArr(vStr[i])); if (tm.isExistInInodeTable(inodeNum)) { cout << endl << "open되어있는 디렉토리임" << endl; throw "error in DirectoryManager.cpp in allOpen Func";// fd = 0 -> 이미 오픈되어있는 경우 } fd = tm.fileOpenEvent(inodeNum, inodeBl); openedDir_FDList.push_back(fd); openedDirList.push_back(dir); } }
int File::findFile(char* file) // ����� Ȥ�� ������ { PathManager* p = PathManager::getInstance(); PathManager& pm = *p; FileSystem* f = FileSystem::getInstance(); FileSystem& fs = *f; char* absFile = pm.getAbsolutePath(file); TableManager tmpTM = TableManager::getTmpInstance(); vector<string>* pathFiles = pm.getAllAbsPath(file);// ������ ���ʷ� �������� int count = pathFiles->size(); int *fd = new int[count]; for (int i = 0; i < count - 1; i++) { fd[i] = openFile(stringToCharArr((*pathFiles)[i]), tmpTM); } InodeElement* dirInode = (InodeElement*)tmpTM.getElement(INODET, fd[count - 2]); DirectoryManager dm = DirectoryManager::getTmpInstance(); Directory* dir = dm.returnDir(dirInode->inode_number); Entry* fileEntry = dir->findName(file); if (fileEntry == nullptr) { string path = pm.getCurrentPath() + '/'; string filename = file; path = path + filename; createFile((char*)path.c_str()); } TableManager* t = TableManager::getInstance(); TableManager& tm = *t; return openFile(file, tm); }
void DirectoryManager::Dir_Unlink_All(char * direc) { Directory dr = Dir_Read(direc); if (dr.entryCnt != 2) { for (int i = 0; i < dr.entryCnt; i++) { string path = direc; path.append("/"); path.append(dr.entryList[i].name); if (isFile(dr.entryList[i].name, dr) == 'f') { File f; f.removeFile(dr.entryList[i].name); } else Dir_Unlink_All(stringToCharArr(path)); } } Dir_Unlink(direc); }
void DirectoryManager::Dir_Create(char* direc) { try { //Bitmap 갱신 함수를 통해 비트맵 갱신 string path = direc; Inode inode; // inode 설정 //비트맵들 1로 설정 .. inode.mode = "d777"; // int EmptyDataNum = dBlockBitmap.getEmpty(); // int EmptyInodeNum = inodeBitmap.getEmpty(); ppp.push("DC"); // 절대 경로 분석 PathManager& pm = *PathManager::getInstance(); vector<string> arr; pm.doAnalyzeFolder(direc, arr); int n = arr.size(); // n: 경로상의 폴더 개수? string currDir = arr[n - 1]; // 현재디렉토리 이름(내가 만들 디렉토리) string topDir = ""; // 상위디렉토리 이름 if (strcmp(direc, "/") == 0) topDir = arr[n - 1]; // 현재디렉토리 이름(내가 만들 디렉토리) else topDir = arr[n - 2]; //addDirectory int currDirInode = -1; int topDirInode = -1; if(path == "/") topDirInode = returnInodeNum((char*)topDir.c_str()); else topDirInode = returnInodeNum(stringToCharArr((*pm.getAllAbsPath(direc))[n - 2])); FileSystem& fs = *FileSystem::getInstance(); //memcpy(dB.data, (void *)content.c_str, sizeof(content)); Directory curDr; Directory topDr; char time[13]; getCurrentTime(time); char linkCount[2] = "0"; //Inode 정보 설정 inode.blocks = "1"; inode.linksCount = linkCount; inode.mtime = time; inode.size = "0"; inode.time = time; inode.ctime = time; if (strcmp(direc, "/") != 0 && topDr.isExist((char*)currDir.c_str()) == true) { cout << "dir exist" << endl; return; }//디렉토리 중복 검사 ppp.push("2"); currDirInode = fs.writeFS_Dir(inode); ppp.push("3"); if (strcmp(direc, "/") != 0) { topDr = *returnDir(topDirInode); Entry e; ppp.push("4"); e.inodeNum = currDirInode; strcpy(e.name, currDir.c_str()); topDr.addDirectory(e, topDirInode); ppp.push("5"); } curDr.setInodeNum(currDirInode, topDirInode); Entry *enList = curDr.entryList; string content = ".,"; //cout << enList[0].inodeNum << endl; content.append(to_string(enList[0].inodeNum)); content.append(";..," + to_string(enList[1].inodeNum)); content.append(";"); //데이터블록에 데이터 추가(idx는 datablock Index) //할당받은 블록 받아오기 char* tmpBlockList = fs.inodeBlock->getDataBlockList(currDirInode); int idx = atoi(tmpBlockList); //할당받은 블록 리셋 fs.resetDataBlock(&idx, 1); //다시 쓴다 int assignedIdx = fs.writeFS((char*)content.c_str()); char dataBlockList[] = " \0"; itoa(assignedIdx, dataBlockList); char size[4]; memcpy(size, stringToCharArr(to_string(content.length())), strlen(stringToCharArr(to_string(content.length())))+1); //링크 정보 다시 읽어와야 함 inode = fs.readFS(currDirInode); //Inode 정보 설정 getCurrentTime(time); inode.blocks = "1"; inode.mtime = time; inode.size = size; inode.dataBlockList = dataBlockList; ppp.push("6"); //데이터 블록 추가 후 업데이트 fs.updateInode_writeFile(currDirInode, inode); ppp.push("7"); } catch (char* msg) { cerr << "error : " << msg << endl; } }