filesystem seek

This commit is contained in:
IonutParau 2025-07-06 00:01:33 +02:00
parent dee0e4b0ab
commit 03c5cb09b5
2 changed files with 17 additions and 2 deletions

View File

@ -396,12 +396,13 @@ void nn_fs_seek(nn_filesystem *fs, void *_, nn_component *component, nn_computer
// size_t capacity = fs->spaceTotal(component, fs->userdata); // size_t capacity = fs->spaceTotal(component, fs->userdata);
int moved = 0; int moved = 0;
/* size_t pos = */ fs->seek(component, fs->userdata, fd, whence, off, &moved); size_t pos = fs->seek(component, fs->userdata, fd, whence, off, &moved);
if(moved < 0) moved = -moved; if(moved < 0) moved = -moved;
// do not ask where it comes from, balance is hard // do not ask where it comes from, balance is hard
nn_fs_readCost(fs, 1, component, computer); nn_fs_readCost(fs, 1, component, computer);
nn_fs_seekCost(fs, nn_fs_countChunks(fs, moved, component), component, computer); nn_fs_seekCost(fs, nn_fs_countChunks(fs, moved, component), component, computer);
nn_return_integer(computer, pos);
} }
void nn_loadFilesystemTable(nn_universe *universe) { void nn_loadFilesystemTable(nn_universe *universe) {

View File

@ -181,6 +181,20 @@ size_t ne_fs_read(nn_component *component, ne_fs *fs, int fd, char *buf, size_t
return fread(buf, sizeof(char), required, f); return fread(buf, sizeof(char), required, f);
} }
size_t ne_fs_seek(nn_component *component, ne_fs *fs, int fd, const char *whence, int off, int *moved) {
FILE *f = fs->files[fd];
*moved = 0;
int w = SEEK_SET;
if(strcmp(whence, "cur") == 0) {
w = SEEK_CUR;
}
if(strcmp(whence, "end") == 0) {
w = SEEK_END;
}
fseek(f, w, off);
return ftell(f);
}
char **ne_fs_list(nn_Alloc *alloc, nn_component *component, ne_fs *fs, const char *path, size_t *len) { char **ne_fs_list(nn_Alloc *alloc, nn_component *component, ne_fs *fs, const char *path, size_t *len) {
const char *p = ne_fs_diskPath(component, path); const char *p = ne_fs_diskPath(component, path);
if(p[0] == '/') p++; if(p[0] == '/') p++;
@ -628,7 +642,7 @@ int main() {
.close = (void *)ne_fs_close, .close = (void *)ne_fs_close,
.write = (void *)ne_fs_write, .write = (void *)ne_fs_write,
.read = (void *)ne_fs_read, .read = (void *)ne_fs_read,
.seek = NULL, .seek = (void *)ne_fs_seek,
}; };
nn_addFileSystem(computer, "OpenOS", 1, &genericFS); nn_addFileSystem(computer, "OpenOS", 1, &genericFS);