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

148 lines
4.2 KiB
C++

// SPDX-License-Identifier: Apache-2.0
#include <algorithm>
#include <cstring>
#include <tactility/concurrent/recursive_mutex.h>
#include <tactility/device.h>
#include <tactility/filesystem/file_system.h>
#include <tactility/system_event.h>
#include <vector>
// Define the internal FileSystem structure
struct FileSystem {
const FileSystemApi* api;
void* data;
Device* owner;
const char* name;
};
// Global list of file systems and its mutex
struct FileSystemsLedger {
std::vector<FileSystem*> file_systems;
// Use recursive mutex so that file_system_for_each() can lock within the callback
RecursiveMutex mutex {};
FileSystemsLedger() { recursive_mutex_construct(&mutex); }
~FileSystemsLedger() { recursive_mutex_destruct(&mutex); }
void lock() { recursive_mutex_lock(&mutex); }
bool is_locked() { return recursive_mutex_is_locked(&mutex); }
void unlock() { recursive_mutex_unlock(&mutex); }
};
static FileSystemsLedger& get_ledger() {
static FileSystemsLedger ledger;
return ledger;
}
extern "C" {
FileSystem* file_system_add(const char* name, const FileSystemApi* fs_api, void* data) {
auto& ledger = get_ledger();
check(!ledger.is_locked()); // ensure file_system_for_each() doesn't add a filesystem while iterating
ledger.lock();
auto* fs = new(std::nothrow) struct FileSystem();
check(fs != nullptr);
fs->api = fs_api;
fs->data = data;
fs->owner = nullptr;
fs->name = name;
ledger.file_systems.push_back(fs);
ledger.unlock();
return fs;
}
FileSystem* file_system_find_by_name(const char* name) {
auto& ledger = get_ledger();
ledger.lock();
FileSystem* found = nullptr;
for (auto* fs : ledger.file_systems) {
if (fs->name != nullptr && strcmp(fs->name, name) == 0) {
found = fs;
break;
}
}
ledger.unlock();
return found;
}
void file_system_remove(FileSystem* fs) {
check(!file_system_is_mounted(fs));
auto& ledger = get_ledger();
check(!ledger.is_locked()); // ensure file_system_for_each() doesn't remove a filesystem while iterating
ledger.lock();
auto it = std::ranges::find(ledger.file_systems, fs);
if (it != ledger.file_systems.end()) {
ledger.file_systems.erase(it);
delete fs;
}
ledger.unlock();
}
void file_system_for_each(void* callback_context, bool (*callback)(FileSystem* fs, void* context)) {
auto& ledger = get_ledger();
ledger.lock();
for (auto* fs : ledger.file_systems) {
if (!callback(fs, callback_context)) break;
}
ledger.unlock();
}
void file_system_for_each_mounted(void* callback_context, bool (*callback)(FileSystem* fs, void* context)) {
auto& ledger = get_ledger();
ledger.lock();
for (auto* fs : ledger.file_systems) {
if (!file_system_is_mounted(fs)) {
continue;
}
if (!callback(fs, callback_context)) break;
}
ledger.unlock();
}
error_t file_system_mount(FileSystem* fs) {
// Assuming 'device' is accessible or passed via a different mechanism
// as it's required by the FileSystemApi signatures.
auto result = fs->api->mount(fs->data);
if (result == ERROR_NONE) {
FileSystemMountedEvent mounted_event = { .file_system = fs };
system_event_emit(KERNEL_EVENT_FILE_SYSTEM_MOUNTED, &mounted_event, sizeof(mounted_event));
}
return result;
}
error_t file_system_unmount(FileSystem* fs) {
auto result = fs->api->unmount(fs->data);
if (result == ERROR_NONE) {
FileSystemUnmountedEvent unmounted_event = { .file_system = fs };
system_event_emit(KERNEL_EVENT_FILE_SYSTEM_UNMOUNTED, &unmounted_event, sizeof(unmounted_event));
}
return result;
}
bool file_system_is_mounted(FileSystem* fs) {
return fs->api->is_mounted(fs->data);
}
error_t file_system_get_path(FileSystem* fs, char* out_path, size_t out_path_size) {
return fs->api->get_path(fs->data, out_path, out_path_size);
}
const char* file_system_get_name(FileSystem* fs) {
return fs->name;
}
void file_system_set_owner(FileSystem* fs, Device* owner) {
fs->owner = owner;
}
Device* file_system_get_owner(FileSystem* fs) {
return fs->owner;
}
}