perfectly accurate fs

turns out, file cost is 0 for loot disks, so there was never an actual
discrepancy
This commit is contained in:
2026-03-31 18:00:18 +02:00
parent 917427c2e1
commit 2a31dc838d
183 changed files with 48 additions and 14007 deletions

View File

@@ -330,7 +330,7 @@ int main(int argc, char **argv) {
bool sandboxMem = getenv("NN_MEMSAND") != NULL;
bool showStats = getenv("NN_STAT") != NULL;
const char *mainDir = "OpenOS";
const char *mainDir = "openos";
if(argc > 1) mainDir = argv[1];
nn_Context ctx;
@@ -378,12 +378,9 @@ int main(int argc, char **argv) {
.isReadonly = false,
};
printf("%zu bytes logically used by OpenOS\n", ncl_spaceUsedIn(ncl_defaultFS, "data/OpenOS"));
printf("%zu bytes physically used by OpenOS\n", ncl_spaceUsedBy(ncl_defaultFS, "data/OpenOS"));
nn_Component *eepromCard = nn_createVEEPROM(u, "eeprom", &veeprom, &nn_defaultEEPROMs[3]);
nn_Component *managedfs = ncl_createFilesystem(u, "mainFS", "data/OpenOS", &nn_defaultFilesystems[3], true);
nn_Component *managedfs = ncl_createFilesystem(u, "mainFS", "data/openos", &nn_defaultFilesystems[3], true);
size_t ramTotal = 0;
ramTotal += nn_ramSizes[5];

View File

@@ -115,7 +115,18 @@ ncl_VFS ncl_defaultFS = (ncl_VFS) {
#else
.pathsep = '/',
#endif
.fileCost = 512,
.fileCost = NCL_FILECOST_DEFAULT,
};
ncl_VFS ncl_installerFS = (ncl_VFS) {
.state = NULL,
.handler = ncl_defaultHandler,
#ifdef NN_WINDOWS
.pathsep = '\\',
#else
.pathsep = '/',
#endif
.fileCost = NCL_FILECOST_INSTALL,
};
void *ncl_openfile(ncl_VFS vfs, const char *path, const char *mode) {
@@ -213,8 +224,8 @@ bool ncl_readdir(ncl_VFS vfs, void *dir, char name[NN_MAX_PATH]) {
size_t ncl_spaceUsedIn(ncl_VFS vfs, const char *path) {
ncl_Stat s;
if(!ncl_stat(vfs, path, &s)) return 0;
if(!s.isDirectory) return vfs.fileCost + s.size;
size_t spaceUsed = vfs.fileCost;
size_t spaceUsed = vfs.fileCost + s.size;
if(!s.isDirectory) return spaceUsed;
void *dir = ncl_opendir(vfs, path);
if(dir == NULL) return spaceUsed;
char name[NN_MAX_PATH];
@@ -289,7 +300,7 @@ bool ncl_mkdirRecursive(ncl_VFS vfs, const char *path) {
char buf[NN_MAX_PATH];
// use snprintf instead of strncpy cuz NULL terminator
snprintf(buf, NN_MAX_PATH, "%s", path);
char *sep = strrchr(buf, '/');
char *sep = strrchr(buf, vfs.pathsep);
if(sep == NULL) {
return ncl_mkdir(vfs, path);
}
@@ -606,6 +617,12 @@ static nn_Exit ncl_fsHandler(nn_FSRequest *req) {
}
char path[NN_MAX_PATH];
ncl_fixPath(state, req->open.path, path);
size_t spaceRemaining = state->conf.spaceTotal - ncl_fsGetUsage(state);
if(mode[0] == 'w' && !ncl_exists(state->vfs, path) && spaceRemaining < state->vfs.fileCost) {
nn_unlock(ctx, state->lock);
nn_setError(C, "out of space");
return NN_EBADCALL;
}
void *file = ncl_openfile(state->vfs, path, mode);
if(file == NULL) {
nn_unlock(ctx, state->lock);

View File

@@ -9,6 +9,17 @@
#define NCL_GPU "ncl-gpu"
#define NCL_SCREEN "ncl-screen"
// Default file cost.
// This is for a normal HDD/floppy.
#define NCL_FILECOST_DEFAULT 512
// File cost on an installer floppy.
// In OC, those are backed by a file called ZipFileInputStream.
// That class has a different implementation for spaceUsed,
// it is almost identical except it does not add the file cost.
// For that reason, it is recommended to set it to 0, for parity.
#define NCL_FILECOST_INSTALL 0
#define NCL_MAX_VRAMBUF 128
#define NCL_MAX_KEYBOARD 64
@@ -124,8 +135,22 @@ typedef struct ncl_VFS {
size_t fileCost;
} ncl_VFS;
// The default FS.
// Uses a basic implementation using POSIX/Windows FS APIs,
// or erroring on all operations if baremetal.
// Has default file cost (512)
extern ncl_VFS ncl_defaultFS;
// The installer FS.
// Same implementation as the default FS,
// but has a file cost of 0.
// This makes it accurate to ZipFileInputStreams in OC,
// which are used for the loot floppy disks.
// ENSURE ALL FILESYSTEMS WITH 0 FILE COST
// ARE READ-ONLY, OR ELSE YOU CAN BE SPAMMED
// ENDLESSLY WITH GIGABYTES OF DISK HOGGING.
extern ncl_VFS ncl_installerFS;
void *ncl_openfile(ncl_VFS vfs, const char *path, const char *mode);
void ncl_closefile(ncl_VFS vfs, void *file);
// returns false on EoF