Ken Van Hoeylandt 79d3dd4fdb
Improvements for apps, filesystems, terminal and shell (#659)
app-module

- Apps now have support for:
  - environment variables (inherit from parent app)
  - current workdir
  - window size (when running in terminal)
- Add type aliases like `AppId` and `PackageId`
- Remove data duplication between ledger and app instance records: create `AppManifestSet`
- `AppManifest` usage safety improved: pass by value where needed, or use `AppId` instead of a manifest.

Terminal and shell

- Moved shell commands into coreutils-module as proper apps.
- Refine stack size for terminal and shell because apps now run with their own task and stack (including in-memory apps)

Platform

- Fix for listing root dir on ESP32: wrap posix directory functions and wire in file systems.
- Moved all platform-specific file system init code to platform subprojects

Other

- Fix for screenshot capture (issue with paths)
- Fixes for coreutils app correctness
- Memory optimizations (use more PSRAM where possible) in various locations such as app-module, shell and terminal.
- `ls` and `help` commands now use terminal width and show columns of data by default
- Created fs helpers at `TactilityKernel/include/tactility/filesystem/fs.h` (partially ported from `ShellFs`)
2026-09-25 08:29:27 +02:00

115 lines
3.5 KiB
C++

#ifdef ESP_PLATFORM
// There is no real filesystem at "/" on ESP32 and esp_vfs_register() rejects "/" as a mount prefix.
// Every real mount lives at "/data", "/sdcard", etc.), so opendir("/")/readdir()/ closedir() are wrapped
// synthesize a listing of registered filesystems
//
// DIR's own layout (components/vfs's sys/dirent.h) is deliberately minimal:
// Just a `dd_vfs_idx` (which registered VFS owns this handle) and a reserved `dd_rsv`,
// with "remaining fields defined by VFS implementation".
// So a real DIR* and this file's own RootDir agree on the first 4 bytes regardless of who allocated it.
// `dd_vfs_idx` is never a real VFS's table offset here and `dd_rsv` is left untouched (0) by every real VFS implementation,
// so tagging it with a nonzero magic value reliably tells our handles apart from real ones.
#include <tactility/filesystem/file_system.h>
#include <dirent.h>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
namespace {
constexpr uint16_t ROOT_DIR_VFS_IDX = 0xFFFF;
constexpr uint16_t ROOT_DIR_MAGIC = 0x5A11;
constexpr int ROOT_DIR_MAX_ENTRIES = 16;
struct RootDir {
uint16_t dd_vfs_idx;
uint16_t dd_rsv;
int count;
int position;
char names[ROOT_DIR_MAX_ENTRIES][64];
struct dirent entry;
};
bool isRootDir(DIR* pdir) {
auto* dir = reinterpret_cast<RootDir*>(pdir);
return dir->dd_vfs_idx == ROOT_DIR_VFS_IDX && dir->dd_rsv == ROOT_DIR_MAGIC;
}
// Copies the mount path's top-level segment rather than using file_system_get_name(): the
// registered name isn't always the mount point, and isn't guaranteed to be a string literal
// that outlives this snapshot.
bool collectMountName(FileSystem* fs, void* context) {
auto* dir = static_cast<RootDir*>(context);
if (dir->count >= ROOT_DIR_MAX_ENTRIES) {
return false;
}
char path[64];
if (file_system_get_path(fs, path, sizeof(path)) != ERROR_NONE) {
return true;
}
const char* top = (path[0] == '/') ? path + 1 : path;
if (top[0] == '\0' || strchr(top, '/') != nullptr) {
return true; // not a top-level mount point
}
snprintf(dir->names[dir->count], sizeof(dir->names[0]), "%s", top);
dir->count++;
return true;
}
} // namespace
extern "C" DIR* __real_opendir(const char* name);
extern "C" struct dirent* __real_readdir(DIR* pdir);
extern "C" int __real_closedir(DIR* pdir);
extern "C" {
DIR* __wrap_opendir(const char* name) {
if (name == nullptr || strcmp(name, "/") != 0) {
return __real_opendir(name);
}
auto* dir = static_cast<RootDir*>(calloc(1, sizeof(RootDir)));
if (dir == nullptr) {
return nullptr; // matches opendir()'s own ENOMEM contract; errno is left to the caller
}
dir->dd_vfs_idx = ROOT_DIR_VFS_IDX;
dir->dd_rsv = ROOT_DIR_MAGIC;
file_system_for_each_mounted(dir, collectMountName);
return reinterpret_cast<DIR*>(dir);
}
struct dirent* __wrap_readdir(DIR* pdir) {
if (pdir == nullptr || !isRootDir(pdir)) {
return __real_readdir(pdir);
}
auto* dir = reinterpret_cast<RootDir*>(pdir);
if (dir->position >= dir->count) {
return nullptr; // end of directory; errno intentionally left unchanged
}
dir->entry.d_ino = 0;
dir->entry.d_type = DT_DIR;
snprintf(dir->entry.d_name, sizeof(dir->entry.d_name), "%s", dir->names[dir->position]);
dir->position++;
return &dir->entry;
}
int __wrap_closedir(DIR* pdir) {
if (pdir == nullptr || !isRootDir(pdir)) {
return __real_closedir(pdir);
}
free(pdir);
return 0;
}
}
#endif // ESP_PLATFORM