| 859 | } |
| 860 | |
| 861 | int Ext2Volume::ReadDir(Ext2Node* node, DirectoryEntry* dirent, uint32_t index){ |
| 862 | if((node->flags & FS_NODE_TYPE) != FS_NODE_DIRECTORY){ |
| 863 | return -ENOTDIR; |
| 864 | } |
| 865 | |
| 866 | if(node->inode < 1){ |
| 867 | Log::Warning("[Ext2] ReadDir: Invalid inode: %d", node->inode); |
| 868 | return -1; |
| 869 | } |
| 870 | |
| 871 | ext2_inode_t& ino = node->e2inode; |
| 872 | |
| 873 | uint8_t buffer[blocksize]; |
| 874 | uint32_t currentBlockIndex = 0; |
| 875 | uint32_t blockOffset = 0; |
| 876 | uint32_t totalOffset = 0; |
| 877 | |
| 878 | ext2_directory_entry_t* e2dirent = (ext2_directory_entry_t*)buffer; |
| 879 | |
| 880 | if(ReadBlockCached(GetInodeBlock(currentBlockIndex, ino), buffer)){ |
| 881 | Log::Warning("[Ext2] Failed to read block %d", GetInodeBlock(currentBlockIndex, ino)); |
| 882 | error = DiskReadError; |
| 883 | return -1; |
| 884 | } |
| 885 | |
| 886 | for(unsigned i = 0; i < index; i++){ |
| 887 | if(e2dirent->recordLength == 0) return 0; |
| 888 | |
| 889 | blockOffset += e2dirent->recordLength; |
| 890 | totalOffset += e2dirent->recordLength; |
| 891 | |
| 892 | if(e2dirent->recordLength < 8) { |
| 893 | IF_DEBUG(debugLevelExt2 >= DebugLevelNormal, { |
| 894 | Log::Warning("[Ext2] Error (inode: %d) record length of directory entry is invalid (value: %d)!", node->inode, e2dirent->recordLength); |
| 895 | }); |
| 896 | break; |
| 897 | } |
| 898 | |
| 899 | if(e2dirent->inode == 0){ |
| 900 | index--; // Ignore the entry |
| 901 | } |
| 902 | |
| 903 | if(blockOffset >= blocksize){ |
| 904 | currentBlockIndex++; |
| 905 | |
| 906 | if(currentBlockIndex >= ino.blockCount / (blocksize / 512)){ |
| 907 | // End of dir |
| 908 | return 0; |
| 909 | } |
| 910 | |
| 911 | blockOffset = 0; |
| 912 | if(ReadBlockCached(GetInodeBlock(currentBlockIndex, ino), buffer)){ |
| 913 | Log::Warning("[Ext2] Failed to read block"); |
| 914 | return -1; |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | e2dirent = (ext2_directory_entry_t*)(buffer + blockOffset); |
nothing calls this directly
no test coverage detected