Advertisement
Guest User

qazplm

a guest
Oct 16th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. Node* Node::findNode(const char* name) {
  2. if (!isDirectory()) return nullptr;
  3.  
  4. // read the direct block to try to find the d_entry with the given name
  5. // if not found in direct block, go to indirect block and find the d_entry with the given name
  6.  
  7. uint32_t currentSize = this->getSize();
  8. uint8_t* retrieveDir = (uint8_t*) malloc(currentSize);
  9. this->readAll(0, retrieveDir, currentSize);
  10.  
  11. // find the length of the given name
  12. uint32_t givenLength = 0;
  13. while (name[givenLength] != '\0') {
  14. givenLength++;
  15. }
  16. givenLength--;
  17.  
  18. uint32_t currentByte = 0;
  19. while (currentByte < currentSize) {
  20. bool directBlockEntries = true;
  21.  
  22. uint32_t currentINumber = 0;
  23. for (uint32_t i = 0; i < 4; i++) {
  24. currentINumber = (currentINumber << 8) | ((uint32_t) retrieveDir[currentByte + i]);
  25. }
  26.  
  27. Debug::printf("~~~CURRENT I-number: %d\n", currentByte);
  28.  
  29. uint32_t currentNameLength = 0;
  30. for (uint32_t i = 4; i < 8; i++) {
  31. currentNameLength = (currentNameLength << 8) | ((uint32_t) retrieveDir[currentByte + i]);
  32. }
  33.  
  34. Debug::printf("~~~CURRENT NAME LENGTH: %d\n", currentNameLength);
  35.  
  36. // skip if the lengths of the names are not equal
  37. if (givenLength != currentNameLength) {
  38. currentByte += 8 + currentNameLength;
  39. continue;
  40. }
  41.  
  42. // get the name of the current d_entry
  43. //char* currentName = (char*) malloc(currentNameLength);
  44. for (uint32_t i = currentByte + 8; i < currentByte + 8 + currentNameLength; i++) {
  45. //currentName[i - (currentByte + 8)] = retrieveDir[i];
  46. if (name[i - (currentByte + 8)] != retrieveDir[i]) {
  47. currentByte += 8 + currentNameLength;
  48. //free(currentName);
  49. directBlockEntries = false;
  50. }
  51. }
  52. if (!directBlockEntries) continue;
  53.  
  54. Node* ans = new Node(this->fs, currentINumber);
  55.  
  56. free(retrieveDir);
  57. //free(currentName);
  58.  
  59. return ans;
  60. }
  61.  
  62. return nullptr;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement