diff --git a/libraries/AP_Filesystem/AP_Filesystem.cpp b/libraries/AP_Filesystem/AP_Filesystem.cpp index 564696bb97..cd7e88a88e 100644 --- a/libraries/AP_Filesystem/AP_Filesystem.cpp +++ b/libraries/AP_Filesystem/AP_Filesystem.cpp @@ -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 }, diff --git a/libraries/AP_Filesystem/AP_Filesystem_ROMFS.cpp b/libraries/AP_Filesystem/AP_Filesystem_ROMFS.cpp index db4ddb42da..0175bc19ff 100644 --- a/libraries/AP_Filesystem/AP_Filesystem_ROMFS.cpp +++ b/libraries/AP_Filesystem/AP_Filesystem_ROMFS.cpp @@ -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; } - 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; }