Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ROMFS: allow list over FTP #26225

Merged
merged 5 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Tools/ardupilotwaf/boards.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,8 @@ def configure_env(self, cfg, env):
# exclude emacs tmp files
continue
fname = root[len(custom_dir)+1:]+"/"+f
if fname.startswith("/"):
fname = fname[1:]
env.ROMFS_FILES += [(fname,root+"/"+f)]

def pre_build(self, bld):
Expand Down
1 change: 1 addition & 0 deletions libraries/AP_Filesystem/AP_Filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
33 changes: 29 additions & 4 deletions libraries/AP_Filesystem/AP_Filesystem_ROMFS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand All @@ -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;
}

Expand Down
25 changes: 23 additions & 2 deletions libraries/AP_ROMFS/AP_ROMFS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ const uint8_t *AP_ROMFS::find_decompress(const char *name, uint32_t &size)
return nullptr;
}

if (f->decompressed_size == 0) {
// empty file
size = 0;
return decompressed_data;
}

// explicitly null-terminate the data
decompressed_data[f->decompressed_size] = 0;

Expand Down Expand Up @@ -117,8 +123,23 @@ const char *AP_ROMFS::dir_list(const char *dirname, uint16_t &ofs)
{
const size_t dlen = strlen(dirname);
for ( ; ofs < ARRAY_SIZE(files); ofs++) {
if (strncmp(dirname, files[ofs].filename, dlen) == 0 &&
files[ofs].filename[dlen] == '/') {
if (strncmp(dirname, files[ofs].filename, dlen) == 0) {
const char last_char = files[ofs].filename[dlen];
if (dlen != 0 && last_char != '/' && last_char != 0) {
// only a partial match, skip
continue;
}
/*
prevent duplicate directories
*/
const char *start_name = files[ofs].filename + dlen + 1;
const char *slash = strchr(start_name, '/');
if (ofs > 0 && slash != nullptr) {
auto len = slash - start_name;
if (memcmp(files[ofs].filename, files[ofs-1].filename, len+dlen+1) == 0) {
continue;
}
}
// found one
return files[ofs++].filename;
}
Expand Down
6 changes: 6 additions & 0 deletions libraries/GCS_MAVLink/GCS_FTP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,12 @@ void GCS_MAVLINK::ftp_list_dir(struct pending_ftp &request, struct pending_ftp &

request.data[sizeof(request.data) - 1] = 0; // ensure the path is null terminated

// Strip trailing /
const size_t dir_len = strlen((char *)request.data);
if ((dir_len > 1) && (request.data[dir_len - 1] == '/')) {
request.data[dir_len - 1] = 0;
}

// open the dir
auto *dir = AP::FS().opendir((char *)request.data);
if (dir == nullptr) {
Expand Down
Loading