Advertisement
Guest User

opensimfs

a guest
Apr 20th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. SIMFS_ERROR simfsOpenFile(SIMFS_NAME_TYPE fileName, SIMFS_FILE_HANDLE_TYPE *fileHandle)
  2. {
  3. // TODO: implement
  4.  
  5. unsigned long hashedIndex = hash(fileName);
  6.  
  7. SIMFS_DIR_ENT *conflictResolver = simfsContext->directory[hashedIndex];
  8.  
  9. if (conflictResolver == NULL)
  10. return SIMFS_NOT_FOUND_ERROR;
  11.  
  12. while (conflictResolver != NULL){
  13.  
  14. if (conflictResolver->uniqueFileIdentifier == simfsVolume->block[conflictResolver->nodeReference].content.fileDescriptor.identifier) {
  15. break;
  16. }
  17. conflictResolver = conflictResolver->next;
  18. }
  19.  
  20. if(conflictResolver->globalOpenFileTableIndex != SIMFS_INVALID_INDEX){
  21. simfsContext->globalOpenFileTable[conflictResolver->globalOpenFileTableIndex].referenceCount++;
  22. } else {
  23.  
  24. bool isEmptySlot = false;
  25. SIMFS_OPEN_FILE_GLOBAL_TABLE_TYPE *emptySlot = NULL;
  26. for (int i = 0; i < SIMFS_MAX_NUMBER_OF_OPEN_FILES && !isEmptySlot; ++i) {
  27. if (simfsContext->globalOpenFileTable[i].fileDescriptor == SIMFS_INVALID_OPEN_FILE_TABLE_INDEX){
  28. memcpy(simfsContext->globalOpenFileTable[i].fileDescriptor,
  29. &(simfsVolume->block[conflictResolver->nodeReference].content.fileDescriptor), sizeof(SIMFS_FILE_DESCRIPTOR_TYPE));
  30. simfsContext->globalOpenFileTable[i].referenceCount = 1;
  31. conflictResolver->globalOpenFileTableIndex = i;
  32. isEmptySlot = true;
  33. break;
  34. }
  35. }
  36.  
  37. if (!isEmptySlot)
  38. return SIMFS_ALLOC_ERROR;
  39. }
  40.  
  41. pid_t pid = simfs_debug_get_context()->pid;
  42.  
  43. bool isProcessInProcessControlBlock = false;
  44. SIMFS_PROCESS_CONTROL_BLOCK_TYPE *currentProcess = simfsContext->processControlBlocks;
  45.  
  46. while (!isProcessInProcessControlBlock){
  47. if (currentProcess->pid == pid)//if found
  48. isProcessInProcessControlBlock = true;
  49. if (currentProcess->next != NULL)//if there's more to go
  50. currentProcess = currentProcess->next;
  51. else
  52. break;
  53. }
  54.  
  55. if (!isProcessInProcessControlBlock){
  56. //create one
  57. SIMFS_PROCESS_CONTROL_BLOCK_TYPE *newProcessBlock = malloc(sizeof(SIMFS_PROCESS_CONTROL_BLOCK_TYPE));
  58. if (newProcessBlock == NULL)
  59. return SIMFS_ALLOC_ERROR;
  60. newProcessBlock->currentWorkingDirectory = simfsVolume->superblock.attr.rootNodeIndex;
  61. newProcessBlock->numberOfOpenFiles = 1;
  62. newProcessBlock->pid = pid;
  63. newProcessBlock->next = NULL;
  64. currentProcess->next = newProcessBlock;
  65. currentProcess = currentProcess->next;
  66. }
  67.  
  68. SIMFS_PER_PROCESS_OPEN_FILE_TYPE currentJElement;
  69. for (int j = 0; j < SIMFS_MAX_NUMBER_OF_OPEN_FILES_PER_PROCESS; ++j) {
  70. currentJElement = currentProcess->openFileTable[j];
  71. if (currentJElement.globalOpenFileTableIndex!= SIMFS_INVALID_OPEN_FILE_TABLE_INDEX &&
  72. currentJElement.globalOpenFileTableIndex == conflictResolver->globalOpenFileTableIndex){
  73. *fileHandle = j;
  74. return SIMFS_DUPLICATE_ERROR;
  75. }
  76. }
  77.  
  78. bool isFreeSlotFound = false;
  79. for (int j = 0; j < SIMFS_MAX_NUMBER_OF_OPEN_FILES_PER_PROCESS && !isFreeSlotFound; ++j) {
  80. currentJElement = currentProcess->openFileTable[j];
  81. if (currentJElement.globalOpenFileTableIndex == SIMFS_INVALID_OPEN_FILE_TABLE_INDEX){
  82. currentJElement.globalOpenFileTableIndex = conflictResolver->globalOpenFileTableIndex;
  83. currentJElement.accessRights = simfs_debug_get_context()->umask;
  84. fileHandle = j;
  85. isFreeSlotFound = true;
  86. }
  87. }
  88.  
  89. if (!isFreeSlotFound)
  90. return SIMFS_ALLOC_ERROR;
  91.  
  92.  
  93. return SIMFS_NO_ERROR;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement