Skip to content

Commit a61a0f8

Browse files
authored
Merge pull request #54 from choco-technologies/copilot/add-dmod-readdir-ex-interface
feat: implement Dmod_ReadDirEx in dmvfs SAL layer
2 parents f03a29b + a9851b2 commit a61a0f8

1 file changed

Lines changed: 49 additions & 5 deletions

File tree

src/dmod_sal.c

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -306,17 +306,19 @@ DMOD_INPUT_API_DECLARATION(Dmod, 1.0, void*, _OpenDir, (const char* Path))
306306
}
307307

308308
/**
309-
* @brief Directory entry storage for ReadDir
309+
* @brief Directory entry storage for ReadDir and ReadDirEx
310310
*
311311
* We need to store the last directory entry because the DMOD SAL API
312-
* returns a const char* which must persist until the next call.
312+
* returns a const char* / const Dmod_DirEntry_t* which must persist until
313+
* the next call.
313314
*
314-
* @note Thread safety: This static buffer is NOT thread-safe. Concurrent
315-
* calls to _ReadDir from different threads may result in race
316-
* conditions. The DMOD SAL API design requires this pattern.
315+
* @note Thread safety: These static buffers are NOT thread-safe. Concurrent
316+
* calls to _ReadDir or _ReadDirEx from different threads may result in
317+
* race conditions. The DMOD SAL API design requires this pattern.
317318
* Callers should ensure thread-safe access if needed.
318319
*/
319320
static char g_last_dir_entry_name[256] = {0};
321+
static Dmod_DirEntry_t g_last_dir_entry = {0};
320322

321323
/**
322324
* @brief Read the next directory entry
@@ -346,6 +348,48 @@ DMOD_INPUT_API_DECLARATION(Dmod, 1.0, const char*, _ReadDir, (void* Dir))
346348
return g_last_dir_entry_name;
347349
}
348350

351+
/**
352+
* @brief Read the next directory entry with extended information
353+
*
354+
* @param Dir Directory handle
355+
* @return const Dmod_DirEntry_t* Pointer to entry info, or NULL if no more entries
356+
*
357+
* @note The returned pointer points to static storage that may be overwritten by subsequent calls.
358+
* Not thread-safe: use separate directory handles per thread.
359+
*/
360+
DMOD_INPUT_API_DECLARATION(Dmod, 1.0, const Dmod_DirEntry_t*, _ReadDirEx, (void* Dir))
361+
{
362+
if (Dir == NULL)
363+
{
364+
return NULL;
365+
}
366+
367+
dmfsi_dir_entry_t entry;
368+
int ret = dmvfs_readdir(Dir, &entry);
369+
370+
if (ret != 0)
371+
{
372+
return NULL;
373+
}
374+
375+
// Copy entry name to persistent storage (reuse the existing buffer)
376+
strncpy(g_last_dir_entry_name, entry.name, sizeof(g_last_dir_entry_name) - 1);
377+
g_last_dir_entry_name[sizeof(g_last_dir_entry_name) - 1] = '\0';
378+
379+
// Map dmfsi attr flags to Dmod_DirEntryType_t
380+
g_last_dir_entry.name = g_last_dir_entry_name;
381+
if (entry.attr & DMFSI_ATTR_DIRECTORY)
382+
{
383+
g_last_dir_entry.type = Dmod_DirEntryType_Dir;
384+
}
385+
else
386+
{
387+
g_last_dir_entry.type = Dmod_DirEntryType_File;
388+
}
389+
390+
return &g_last_dir_entry;
391+
}
392+
349393
/**
350394
* @brief Close a directory
351395
*

0 commit comments

Comments
 (0)