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

Method ReadDir

Kernel/src/fs/fat32.cpp:173–236  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

171 }
172
173 int Fat32Volume::ReadDir(Fat32Node* node, DirectoryEntry* dirent, uint32_t index){
174 unsigned lfnCount = 0;
175 unsigned entryCount = 0;
176
177 uint32_t cluster = node->inode;
178 int clusterCount = 0;
179
180 fat_entry_t* dirEntries = (fat_entry_t*)ReadClusterChain(cluster, &clusterCount);
181
182 fat_entry_t* dirEntry;
183 int dirEntryIndex = -1;
184
185 fat_lfn_entry_t** lfnEntries;
186
187 for(unsigned i = 0; i < static_cast<unsigned>(clusterCount) * clusterSizeBytes; i++){
188 if(dirEntries[i].filename[0] == 0) continue; // No Directory Entry at index
189 else if (dirEntries[i].filename[0] == 0xE5) {
190 lfnCount = 0;
191 continue; // Unused Entry
192 }
193 else if (dirEntries[i].attributes == 0x0F) lfnCount++; // Long File Name Entry
194 else if (dirEntries[i].attributes & 0x08 /*Volume ID*/){
195 lfnCount = 0;
196 continue;
197 } else {
198 if(entryCount == index){
199 dirEntry = &dirEntries[i];
200 dirEntryIndex = i;
201 break;
202 }
203
204 entryCount++;
205 lfnCount = 0;
206 }
207 }
208
209 if(dirEntryIndex == -1){
210 return 0;
211 }
212
213 lfnEntries = (fat_lfn_entry_t**)kmalloc(sizeof(fat_lfn_entry_t*) * lfnCount);
214
215 for(unsigned i = 0; i < lfnCount; i++){
216 fat_lfn_entry_t* lfnEntry = (fat_lfn_entry_t*)(&dirEntries[dirEntryIndex - i - 1]);
217
218 lfnEntries[i] = lfnEntry;
219 }
220
221 if(lfnCount){
222 GetLongFilename(dirent->name, lfnEntries, lfnCount);
223 } else {
224 strncpy(dirent->name, (char*)dirEntry->filename, 8);
225 while(strchr(dirent->name, ' ')) *strchr(dirent->name, ' ') = 0; // Remove Spaces
226 if(strchr((char*)dirEntry->ext, ' ') != (char*)dirEntry->ext){
227 strncpy(dirent->name + strlen(dirent->name), ".", 1);
228 strncpy(dirent->name + strlen(dirent->name), (char*)dirEntry->ext, 3);
229 }
230 }

Callers

nothing calls this directly

Calls 5

kmallocFunction · 0.85
GetLongFilenameFunction · 0.85
strncpyFunction · 0.85
strchrFunction · 0.85
strlenFunction · 0.85

Tested by

no test coverage detected