Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SIMFS_ERROR simfsOpenFile(SIMFS_NAME_TYPE fileName, SIMFS_FILE_HANDLE_TYPE *fileHandle)
- {
- // TODO: implement
- unsigned long hashedIndex = hash(fileName);
- SIMFS_DIR_ENT *conflictResolver = simfsContext->directory[hashedIndex];
- if (conflictResolver == NULL)
- return SIMFS_NOT_FOUND_ERROR;
- while (conflictResolver != NULL){
- if (conflictResolver->uniqueFileIdentifier == simfsVolume->block[conflictResolver->nodeReference].content.fileDescriptor.identifier) {
- break;
- }
- conflictResolver = conflictResolver->next;
- }
- if(conflictResolver->globalOpenFileTableIndex != SIMFS_INVALID_INDEX){
- simfsContext->globalOpenFileTable[conflictResolver->globalOpenFileTableIndex].referenceCount++;
- } else {
- bool isEmptySlot = false;
- SIMFS_OPEN_FILE_GLOBAL_TABLE_TYPE *emptySlot = NULL;
- for (int i = 0; i < SIMFS_MAX_NUMBER_OF_OPEN_FILES && !isEmptySlot; ++i) {
- if (simfsContext->globalOpenFileTable[i].fileDescriptor == SIMFS_INVALID_OPEN_FILE_TABLE_INDEX){
- memcpy(simfsContext->globalOpenFileTable[i].fileDescriptor,
- &(simfsVolume->block[conflictResolver->nodeReference].content.fileDescriptor), sizeof(SIMFS_FILE_DESCRIPTOR_TYPE));
- simfsContext->globalOpenFileTable[i].referenceCount = 1;
- conflictResolver->globalOpenFileTableIndex = i;
- isEmptySlot = true;
- break;
- }
- }
- if (!isEmptySlot)
- return SIMFS_ALLOC_ERROR;
- }
- pid_t pid = simfs_debug_get_context()->pid;
- bool isProcessInProcessControlBlock = false;
- SIMFS_PROCESS_CONTROL_BLOCK_TYPE *currentProcess = simfsContext->processControlBlocks;
- while (!isProcessInProcessControlBlock){
- if (currentProcess->pid == pid)//if found
- isProcessInProcessControlBlock = true;
- if (currentProcess->next != NULL)//if there's more to go
- currentProcess = currentProcess->next;
- else
- break;
- }
- if (!isProcessInProcessControlBlock){
- //create one
- SIMFS_PROCESS_CONTROL_BLOCK_TYPE *newProcessBlock = malloc(sizeof(SIMFS_PROCESS_CONTROL_BLOCK_TYPE));
- if (newProcessBlock == NULL)
- return SIMFS_ALLOC_ERROR;
- newProcessBlock->currentWorkingDirectory = simfsVolume->superblock.attr.rootNodeIndex;
- newProcessBlock->numberOfOpenFiles = 1;
- newProcessBlock->pid = pid;
- newProcessBlock->next = NULL;
- currentProcess->next = newProcessBlock;
- currentProcess = currentProcess->next;
- }
- SIMFS_PER_PROCESS_OPEN_FILE_TYPE currentJElement;
- for (int j = 0; j < SIMFS_MAX_NUMBER_OF_OPEN_FILES_PER_PROCESS; ++j) {
- currentJElement = currentProcess->openFileTable[j];
- if (currentJElement.globalOpenFileTableIndex!= SIMFS_INVALID_OPEN_FILE_TABLE_INDEX &&
- currentJElement.globalOpenFileTableIndex == conflictResolver->globalOpenFileTableIndex){
- *fileHandle = j;
- return SIMFS_DUPLICATE_ERROR;
- }
- }
- bool isFreeSlotFound = false;
- for (int j = 0; j < SIMFS_MAX_NUMBER_OF_OPEN_FILES_PER_PROCESS && !isFreeSlotFound; ++j) {
- currentJElement = currentProcess->openFileTable[j];
- if (currentJElement.globalOpenFileTableIndex == SIMFS_INVALID_OPEN_FILE_TABLE_INDEX){
- currentJElement.globalOpenFileTableIndex = conflictResolver->globalOpenFileTableIndex;
- currentJElement.accessRights = simfs_debug_get_context()->umask;
- fileHandle = j;
- isFreeSlotFound = true;
- }
- }
- if (!isFreeSlotFound)
- return SIMFS_ALLOC_ERROR;
- return SIMFS_NO_ERROR;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement