mirror of https://github.com/ArduPilot/ardupilot
AP_Filesystem: support full direcotry listing in ROMFS
This commit is contained in:
parent
e01e697343
commit
93d8de2834
|
@ -64,6 +64,7 @@ const AP_Filesystem::Backend AP_Filesystem::backends[] = {
|
|||
{ nullptr, fs_local },
|
||||
#if AP_FILESYSTEM_ROMFS_ENABLED
|
||||
{ "@ROMFS/", fs_romfs },
|
||||
{ "@ROMFS", fs_romfs },
|
||||
#endif
|
||||
#if AP_FILESYSTEM_PARAM_ENABLED
|
||||
{ "@PARAM/", fs_param },
|
||||
|
|
|
@ -159,6 +159,15 @@ void *AP_Filesystem_ROMFS::opendir(const char *pathname)
|
|||
if (!dir[idx].path) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Take a sneak peek and reset
|
||||
const char *name = AP_ROMFS::dir_list(dir[idx].path, dir[idx].ofs);
|
||||
dir[idx].ofs = 0;
|
||||
if (!name) {
|
||||
// Directory does not exist
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return (void*)&dir[idx];
|
||||
}
|
||||
|
||||
|
@ -174,12 +183,28 @@ struct dirent *AP_Filesystem_ROMFS::readdir(void *dirp)
|
|||
return nullptr;
|
||||
}
|
||||
const uint32_t plen = strlen(dir[idx].path);
|
||||
if (strncmp(name, dir[idx].path, plen) != 0 || name[plen] != '/') {
|
||||
return nullptr;
|
||||
}
|
||||
if (plen > 0) {
|
||||
// Offset to get just file/directory name
|
||||
name += plen + 1;
|
||||
dir[idx].de.d_type = DT_REG;
|
||||
}
|
||||
|
||||
// Copy full name
|
||||
strncpy(dir[idx].de.d_name, name, sizeof(dir[idx].de.d_name));
|
||||
|
||||
const char* slash = strchr(name, '/');
|
||||
if (slash == nullptr) {
|
||||
// File
|
||||
dir[idx].de.d_type = DT_REG;
|
||||
|
||||
} else {
|
||||
// Directory
|
||||
dir[idx].de.d_type = DT_DIR;
|
||||
|
||||
// Add null termination after directory name
|
||||
const size_t index = slash - name;
|
||||
dir[idx].de.d_name[index] = 0;
|
||||
}
|
||||
|
||||
return &dir[idx].de;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue