MCPcopy Create free account
hub / github.com/LemonOSProject/LemonOS / ListDir

Method ListDir

Kernel/src/fs/ext2.cpp:691–754  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

689 }
690
691 int Ext2Volume::ListDir(Ext2Node* node, List<DirectoryEntry>& entries){
692 if((node->flags & FS_NODE_TYPE) != FS_NODE_DIRECTORY){
693 Log::Warning("[Ext2] ListDir: Not a directory (inode %d)", node->inode);
694 error = MiscError;
695 return -1;
696 }
697
698 if(node->inode < 1){
699 Log::Warning("[Ext2] ReadDir: Invalid inode: %d", node->inode);
700 error = InvalidInodeError;
701 return -1;
702 }
703
704 ext2_inode_t& ino = node->e2inode;
705
706 uint8_t* buffer = (uint8_t*)kmalloc(blocksize);
707 uint32_t currentBlockIndex = 0;
708 uint32_t blockOffset = 0;
709 uint32_t totalOffset = 0;
710
711 ext2_directory_entry_t* e2dirent = (ext2_directory_entry_t*)buffer;
712
713 if(ReadBlock(GetInodeBlock(currentBlockIndex, ino), buffer)){
714 Log::Warning("[Ext2] ListDir: Error reading block %d", ino.blocks[currentBlockIndex]);
715 error = DiskReadError;
716 return -1;
717 }
718
719 for(;;){
720 if(e2dirent->recordLength == 0) break;
721
722 if(e2dirent->inode > 0){ // 0 indicates unused
723 DirectoryEntry dirent;
724 dirent.flags = e2dirent->fileType;
725 strncpy(dirent.name, e2dirent->name, e2dirent->nameLength);
726 dirent.name[e2dirent->nameLength] = 0;
727 dirent.inode = e2dirent->inode;
728
729 entries.add_back(dirent);
730 }
731
732 blockOffset += e2dirent->recordLength;
733 totalOffset += e2dirent->recordLength;
734
735 if(blockOffset >= blocksize){
736 currentBlockIndex++;
737
738 if(currentBlockIndex > ino.blockCount / (blocksize / 512)){
739 // End of dir
740 break;
741 }
742
743 blockOffset = 0;
744 if(ReadBlock(GetInodeBlock(currentBlockIndex, ino), buffer)){
745 error = DiskReadError;
746 return -1;
747 }
748 }

Callers

nothing calls this directly

Calls 4

WarningFunction · 0.85
kmallocFunction · 0.85
strncpyFunction · 0.85
add_backMethod · 0.45

Tested by

no test coverage detected