diff options
| author | Xavier Del Campo Romero <xavi92@disroot.org> | 2025-07-07 13:22:53 +0200 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi92@disroot.org> | 2025-11-11 00:08:15 +0100 |
| commit | 7861a52adf92a083bb2aed4c35f98d8035dce032 (patch) | |
| tree | 28cd3c40e4c878f730f5df3c1d93bdf91af490c3 /src/fs | |
| parent | 7fc48e9216ff809da5f8055a50b0be17628ef1df (diff) | |
| download | wnix-7861a52adf92a083bb2aed4c35f98d8035dce032.tar.gz | |
Setup project skeleton
Diffstat (limited to 'src/fs')
93 files changed, 4721 insertions, 0 deletions
diff --git a/src/fs/CMakeLists.txt b/src/fs/CMakeLists.txt new file mode 100644 index 0000000..6f4aaea --- /dev/null +++ b/src/fs/CMakeLists.txt @@ -0,0 +1,24 @@ +# wnix, a Unix-like operating system for WebAssembly applications. +# Copyright (C) 2025 Xavier Del Campo Romero +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +add_library(fs) +add_subdirectory(src) +target_include_directories(fs PUBLIC include PRIVATE private_include) +target_link_libraries(fs PUBLIC state) +add_subdirectory(devfs) +add_subdirectory(iso9660) +add_subdirectory(ramfs) +add_subdirectory(rootfs) diff --git a/src/fs/devfs/CMakeLists.txt b/src/fs/devfs/CMakeLists.txt new file mode 100644 index 0000000..c3bbcf8 --- /dev/null +++ b/src/fs/devfs/CMakeLists.txt @@ -0,0 +1,20 @@ +# wnix, a Unix-like operating system for WebAssembly applications. +# Copyright (C) 2025 Xavier Del Campo Romero +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +add_library(devfs) +add_subdirectory(src) +target_include_directories(devfs PUBLIC include PRIVATE private_include) +target_link_libraries(devfs PUBLIC state c PRIVATE fs drv ramfs) diff --git a/src/fs/devfs/include/devfs.h b/src/fs/devfs/include/devfs.h new file mode 100644 index 0000000..ccc82ac --- /dev/null +++ b/src/fs/devfs/include/devfs.h @@ -0,0 +1,24 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#ifndef DEVFS_H +#define DEVFS_H + +int devfs_register(void); + +#endif diff --git a/src/fs/devfs/private_include/devfs/ops.h b/src/fs/devfs/private_include/devfs/ops.h new file mode 100644 index 0000000..0c09776 --- /dev/null +++ b/src/fs/devfs/private_include/devfs/ops.h @@ -0,0 +1,49 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#ifndef DEVFS_OPS_H +#define DEVFS_OPS_H + +#include <fs/fs.h> +#include <drv/event.h> +#include <sys/types.h> +#include <stdbool.h> + +int devfs_mount(const struct fs_mount *m, struct fs_ret *r); +int devfs_mkdir(const struct fs_mkdir *m, const struct fs_mp *mp, + const union inode_result *i, struct fs_ret *r); +int devfs_open(const struct fs_open *o, const struct fs_mp *mp, + const union inode_result *i, struct fs_ret *r); +int devfs_read(const struct fs_read *r, struct fs_ret *ret); +int devfs_read_nb(const struct fs_read *r); +int devfs_write(const struct fs_write *w, struct fs_ret *r); +int devfs_stat(const struct fs_stat *s, const struct fs_mp *mp, + const union inode_result *i, struct fs_ret *r); +int devfs_close(struct fs_fd *fd); +int devfs_search(const char *path, const struct fs_mp *mp, + union inode_result *inode, struct fs_ret *r); +int devfs_status(const char *node, const struct drv_event_ops *const ops, + bool available, mode_t mode, void *args); +int devfs_flags(const union inode_result *i); +int devfs_update(struct fs_mp_prv *pr); +int devfs_mknod(const char *path, mode_t mode, dev_t dev, + const struct drv_event_ops *const ops, struct fs_mp_prv *mp); +int devfs_unlink(const char *path, struct fs_mp_prv *mp); +const struct devfs_ops *devfs_ops(const struct fs_mp_prv *pr, const char *node); + +#endif diff --git a/src/fs/devfs/private_include/devfs/types.h b/src/fs/devfs/private_include/devfs/types.h new file mode 100644 index 0000000..da6b50b --- /dev/null +++ b/src/fs/devfs/private_include/devfs/types.h @@ -0,0 +1,44 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#ifndef DEVFS_TYPES_H +#define DEVFS_TYPES_H + +#include <fs/fs.h> +#include <fs/inode.h> +#include <drv/drv.h> +#include <drv/event.h> +#include <ramfs.h> + +struct devfs_ops +{ + char *node; + struct drv_event_ops ops; + struct devfs_ops *prev, *next; +}; + +struct fs_mp_prv +{ + struct ramfs *rfs; + struct drv *drv; + struct devfs_ops *head, *tail; +}; + +extern const struct fs devfs; + +#endif diff --git a/src/fs/devfs/src/CMakeLists.txt b/src/fs/devfs/src/CMakeLists.txt new file mode 100644 index 0000000..2a12700 --- /dev/null +++ b/src/fs/devfs/src/CMakeLists.txt @@ -0,0 +1,34 @@ +# wnix, a Unix-like operating system for WebAssembly applications. +# Copyright (C) 2025 Xavier Del Campo Romero +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +target_sources(devfs PRIVATE + close.c + flags.c + mount.c + mkdir.c + mknod.c + open.c + ops.c + stat.c + read.c + read_nb.c + register.c + search.c + status.c + unlink.c + update.c + write.c +) diff --git a/src/fs/devfs/src/close.c b/src/fs/devfs/src/close.c new file mode 100644 index 0000000..db236d1 --- /dev/null +++ b/src/fs/devfs/src/close.c @@ -0,0 +1,26 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <devfs.h> +#include <devfs/ops.h> +#include <fs/fs.h> + +int devfs_close(struct fs_fd *const fd) +{ + return -1; +} diff --git a/src/fs/devfs/src/flags.c b/src/fs/devfs/src/flags.c new file mode 100644 index 0000000..082c5ad --- /dev/null +++ b/src/fs/devfs/src/flags.c @@ -0,0 +1,25 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <devfs.h> +#include <devfs/ops.h> + +int devfs_flags(const union inode_result *const i) +{ + return i->memi->flags; +} diff --git a/src/fs/devfs/src/mkdir.c b/src/fs/devfs/src/mkdir.c new file mode 100644 index 0000000..cc091e0 --- /dev/null +++ b/src/fs/devfs/src/mkdir.c @@ -0,0 +1,28 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <devfs.h> +#include <devfs/ops.h> +#include <fs/fs.h> +#include <fs/inode.h> + +int devfs_mkdir(const struct fs_mkdir *m, const struct fs_mp *const mp, + const union inode_result *const i, struct fs_ret *const r) +{ + return -1; +} diff --git a/src/fs/devfs/src/mknod.c b/src/fs/devfs/src/mknod.c new file mode 100644 index 0000000..f9a369e --- /dev/null +++ b/src/fs/devfs/src/mknod.c @@ -0,0 +1,54 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <devfs/ops.h> +#include <devfs/types.h> +#include <fs/inode.h> +#include <ramfs.h> +#include <stddef.h> +#include <stdlib.h> +#include <string.h> + +int devfs_mknod(const char *const node, const mode_t mode, const dev_t dev, + const struct drv_event_ops *const ops, struct fs_mp_prv *const pr) +{ + struct devfs_ops *dops = NULL; + char *const nodedup = strdup(node); + + if (!nodedup + || !(dops = malloc(sizeof *dops)) + || ramfs_mknod(node, mode, dev, pr->rfs)) + goto failure; + + *dops = (const struct devfs_ops){.node = nodedup, .ops = *ops}; + + if (!pr->head) + pr->head = dops; + else + { + dops->prev = pr->tail; + pr->tail->next = dops; + } + + pr->tail = dops; + return 0; + +failure: + free(dops); + return -1; +} diff --git a/src/fs/devfs/src/mount.c b/src/fs/devfs/src/mount.c new file mode 100644 index 0000000..2165971 --- /dev/null +++ b/src/fs/devfs/src/mount.c @@ -0,0 +1,59 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <devfs.h> +#include <devfs/ops.h> +#include <devfs/types.h> +#include <drv/drv.h> +#include <fs/fs.h> +#include <ramfs.h> +#include <stdlib.h> + +int devfs_mount(const struct fs_mount *const m, struct fs_ret *const r) +{ + struct drv *drv = NULL; + struct fs_mp_prv *p = NULL; + struct ramfs *const rfs = ramfs_mount(m, r); + + if (!rfs || !(p = malloc(sizeof *p))) + goto failure; + + const struct drv_event ev = + { + .status = devfs_status, + .args = p + }; + + if (!(drv = drv_init(&ev)) + || fs_mountpoint(m->src, m->tgt, &devfs, devfs_update, p)) + goto failure; + + *p = (const struct fs_mp_prv) + { + .drv = drv, + .rfs = rfs + }; + + return 0; + +failure: + drv_free(drv); + free(p); + ramfs_free(rfs); + return -1; +} diff --git a/src/fs/devfs/src/open.c b/src/fs/devfs/src/open.c new file mode 100644 index 0000000..551061f --- /dev/null +++ b/src/fs/devfs/src/open.c @@ -0,0 +1,57 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <devfs.h> +#include <devfs/types.h> +#include <devfs/ops.h> +#include <fs/fs.h> +#include <state.h> +#include <stdlib.h> + +struct open +{ + struct fs_ret *pr, r; +}; + +static enum state done(void *const args) +{ + struct open *const op = args; + + *op->pr = op->r; + free(op); + return STATE_AGAIN; +} + +int devfs_open(const struct fs_open *const o, const struct fs_mp *const mp, + const union inode_result *const inode, struct fs_ret *const r) +{ + struct open *const op = malloc(sizeof *op); + + if (!op) + return -1; + + *op = (const struct open){.pr = r, .r = *r}; + *o->fd = (const struct fs_fd) + { + .inode = *inode, + .mp = mp + }; + + *r = (const struct fs_ret){.f = done, .args = op}; + return 0; +} diff --git a/src/fs/devfs/src/ops.c b/src/fs/devfs/src/ops.c new file mode 100644 index 0000000..c51cfa7 --- /dev/null +++ b/src/fs/devfs/src/ops.c @@ -0,0 +1,32 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <devfs.h> +#include <devfs/ops.h> +#include <devfs/types.h> +#include <string.h> + +const struct devfs_ops *devfs_ops(const struct fs_mp_prv *const pr, + const char *const node) +{ + for (struct devfs_ops *o = pr->head; o; o = o->next) + if (!strcmp(o->node, node)) + return o; + + return NULL; +} diff --git a/src/fs/devfs/src/read.c b/src/fs/devfs/src/read.c new file mode 100644 index 0000000..e968eb2 --- /dev/null +++ b/src/fs/devfs/src/read.c @@ -0,0 +1,98 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <devfs.h> +#include <devfs/ops.h> +#include <devfs/types.h> +#include <fs/fs.h> +#include <state.h> +#include <errno.h> +#include <stdbool.h> +#include <stdlib.h> + +struct read +{ + bool done; + size_t n; + struct fs_fd *fd; + struct fs_ret *pr, r; +}; + +static int done(const int error, void *const args) +{ + struct read *const re = args; + + re->done = true; + return 0; +} + +static enum state wait(void *const args) +{ + struct read *const re = args; + + if (!re->done) + return STATE_AGAIN; + + re->fd->offset += re->n; + *re->pr = re->r; + free(re); + return STATE_AGAIN; +} + +int devfs_read(const struct fs_read *const fr, struct fs_ret *const r) +{ + struct read *re = NULL; + struct fs_fd *const fd = fr->fd; + const struct fs_mp *const mp = fd->mp; + const struct inode *const inode = fd->inode.memi; + const struct devfs_ops *const o = devfs_ops(mp->prv, inode->name); + + if (!o) + { + errno = ENOENT; + goto failure; + } + else if (!(re = malloc(sizeof *re))) + goto failure; + + *re = (const struct read) + { + .n = fr->n, + .fd = fd, + .pr = r, + .r = *r + }; + + const struct drv_event_done d = + { + .f = done, + .args = re + }; + + const struct drv_event_ops *const ops = &o->ops; + + if (ops->read(fr->buf, fr->n, fd->start + fd->offset, &d, ops->args)) + goto failure; + + *r = (const struct fs_ret){.f = wait, .args = re}; + return 0; + +failure: + free(re); + return -1; +} diff --git a/src/fs/devfs/src/read_nb.c b/src/fs/devfs/src/read_nb.c new file mode 100644 index 0000000..2f30d5c --- /dev/null +++ b/src/fs/devfs/src/read_nb.c @@ -0,0 +1,41 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <devfs.h> +#include <devfs/ops.h> +#include <devfs/types.h> +#include <fs/fs.h> +#include <errno.h> + +int devfs_read_nb(const struct fs_read *const fr) +{ + struct fs_fd *const fd = fr->fd; + const struct fs_mp *const mp = fd->mp; + const struct inode *const inode = fd->inode.memi; + const struct devfs_ops *const o = devfs_ops(mp->prv, inode->name); + + if (!o) + { + errno = ENOENT; + return -1; + } + + const struct drv_event_ops *const ops = &o->ops; + + return ops->read_nb(fr->buf, fr->n, ops->args); +} diff --git a/src/fs/devfs/src/register.c b/src/fs/devfs/src/register.c new file mode 100644 index 0000000..5ff7b33 --- /dev/null +++ b/src/fs/devfs/src/register.c @@ -0,0 +1,44 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <devfs.h> +#include <devfs/ops.h> +#include <fs/fs.h> + +const struct fs devfs = +{ + .type = "devfs", + .mount = devfs_mount, + .stat = devfs_stat, + .mkdir = devfs_mkdir, + .open = devfs_open, + .read = devfs_read, + .read_nb = devfs_read_nb, + .write = devfs_write, + .close = devfs_close, + .iops = + { + .search = devfs_search, + .flags = devfs_flags + } +}; + +int devfs_register(void) +{ + return fs_register(&devfs); +} diff --git a/src/fs/devfs/src/search.c b/src/fs/devfs/src/search.c new file mode 100644 index 0000000..4360a9f --- /dev/null +++ b/src/fs/devfs/src/search.c @@ -0,0 +1,29 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <devfs.h> +#include <devfs/ops.h> +#include <devfs/types.h> +#include <fs/fs.h> +#include <ramfs.h> + +int devfs_search(const char *const path, const struct fs_mp *const mp, + union inode_result *const inode, struct fs_ret *const r) +{ + return ramfs_search(path, mp->prv->rfs, inode, r); +} diff --git a/src/fs/devfs/src/stat.c b/src/fs/devfs/src/stat.c new file mode 100644 index 0000000..50af3ee --- /dev/null +++ b/src/fs/devfs/src/stat.c @@ -0,0 +1,30 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <devfs.h> +#include <devfs/ops.h> +#include <devfs/types.h> +#include <fs/fs.h> +#include <fs/inode.h> +#include <ramfs.h> + +int devfs_stat(const struct fs_stat *const s, const struct fs_mp *const mp, + const union inode_result *const i, struct fs_ret *const r) +{ + return ramfs_stat(s, mp->prv->rfs, i, r); +} diff --git a/src/fs/devfs/src/status.c b/src/fs/devfs/src/status.c new file mode 100644 index 0000000..fb73c94 --- /dev/null +++ b/src/fs/devfs/src/status.c @@ -0,0 +1,31 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <devfs/ops.h> +#include <drv/event.h> +#include <sys/types.h> +#include <stdbool.h> + +int devfs_status(const char *const node, const struct drv_event_ops *const ops, + const bool available, const mode_t mode, void *const args) +{ + struct fs_mp_prv *const mp = args; + + return available ? devfs_mknod(node, mode, 0, ops, mp) + : devfs_unlink(node, mp); +} diff --git a/src/fs/devfs/src/unlink.c b/src/fs/devfs/src/unlink.c new file mode 100644 index 0000000..3cd87b4 --- /dev/null +++ b/src/fs/devfs/src/unlink.c @@ -0,0 +1,26 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <devfs/ops.h> +#include <sys/types.h> +#include <errno.h> + +int devfs_unlink(const char *const node, struct fs_mp_prv *const mp) +{ + return -1; +} diff --git a/src/fs/devfs/src/update.c b/src/fs/devfs/src/update.c new file mode 100644 index 0000000..b437044 --- /dev/null +++ b/src/fs/devfs/src/update.c @@ -0,0 +1,27 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <devfs.h> +#include <devfs/types.h> +#include <devfs/ops.h> +#include <drv/drv.h> + +int devfs_update(struct fs_mp_prv *const pr) +{ + return drv_update(pr->drv); +} diff --git a/src/fs/devfs/src/write.c b/src/fs/devfs/src/write.c new file mode 100644 index 0000000..392666c --- /dev/null +++ b/src/fs/devfs/src/write.c @@ -0,0 +1,95 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <devfs.h> +#include <devfs/ops.h> +#include <devfs/types.h> +#include <fs/fs.h> +#include <state.h> +#include <errno.h> +#include <stdbool.h> +#include <stdlib.h> + +struct write +{ + bool done; + struct fs_fd *fd; + struct fs_ret *pr, r; +}; + +static int done(const int error, void *const args) +{ + struct write *const w = args; + + w->done = true; + return 0; +} + +static enum state wait(void *const args) +{ + struct write *const w = args; + + if (!w->done) + return STATE_AGAIN; + + *w->pr = w->r; + free(w); + return STATE_AGAIN; +} + +int devfs_write(const struct fs_write *const fw, struct fs_ret *const r) +{ + struct fs_fd *const fd = fw->fd; + const struct fs_mp *const mp = fd->mp; + const struct inode *const inode = fd->inode.memi; + const struct devfs_ops *const o = devfs_ops(mp->prv, inode->name); + struct write *const w = malloc(sizeof *w); + + if (!w) + goto failure; + else if (!o) + { + errno = ENOENT; + goto failure; + } + + *w = (const struct write) + { + .fd = fd, + .pr = r, + .r = *r + }; + + const struct drv_event_done d = + { + .f = done, + .args = w + }; + + const struct drv_event_ops *const ops = &o->ops; + + if (ops->write(fw->buf, fw->n, &d, ops->args)) + goto failure; + + *r = (const struct fs_ret){.f = wait, .args = w}; + return 0; + +failure: + free(w); + return -1; +} diff --git a/src/fs/include/fs/fs.h b/src/fs/include/fs/fs.h new file mode 100644 index 0000000..aebe1db --- /dev/null +++ b/src/fs/include/fs/fs.h @@ -0,0 +1,146 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#ifndef FS_H +#define FS_H + +#include <fs/inode.h> +#include <state.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <stdbool.h> +#include <stddef.h> + +struct fs_mountpoint; +struct fs_mp_prv; +struct fs_fd_prv; + +struct fs_stat +{ + const char *path; + struct stat *sb; + uid_t uid; + gid_t gid; +}; + +struct fs_mkdir +{ + const char *path; + mode_t mode; + uid_t uid; + gid_t gid; +}; + +struct fs_mount +{ + const char *src, *tgt; + mode_t mode; + uid_t uid; + gid_t gid; +}; + +struct fs_umount +{ + const char *tgt; + mode_t mode; + uid_t uid; + gid_t gid; +}; + +struct fs_open +{ + const char *path; + struct fs_fd *fd; + int flags; + mode_t mode; + uid_t uid; + gid_t gid; +}; + +struct fs_read +{ + struct fs_fd *fd; + void *buf; + size_t n; +}; + +struct fs_write +{ + struct fs_fd *fd; + const void *buf; + size_t n; +}; + +struct fs_ret +{ + enum state (*f)(void *args); + void *args; +}; + +enum {FS_DEV_REQUIRED = 1}; + +struct fs +{ + const char *type; + int flags; + int (*mount)(const struct fs_mount *, struct fs_ret *); + int (*umount)(const struct fs_umount *, struct fs_ret *); + int (*stat)(const struct fs_stat *, const struct fs_mp *, + const union inode_result *, struct fs_ret *); + int (*mkdir)(const struct fs_mkdir *, const struct fs_mp *, + const union inode_result *, struct fs_ret *); + int (*open)(const struct fs_open *, const struct fs_mp *, + const union inode_result *, struct fs_ret *); + int (*close)(struct fs_fd *); + int (*read)(const struct fs_read *, struct fs_ret *); + int (*read_nb)(const struct fs_read *); + int (*write)(const struct fs_write *, struct fs_ret *); + int (*eof)(const struct fs_fd *); + struct inode_ops iops; +}; + +struct fs_mp +{ + const char *src, *tgt; + const struct fs *fs; + struct fs_mp_prv *prv; +}; + +struct fs_fd +{ + int error; + off_t start, offset, size; + struct fs_fd_prv *prv; + const struct fs_mp *mp, *tgt_mp; + union inode_result inode, tgt_inode; +}; + +typedef int (*fs_update_fn)(struct fs_mp_prv *); + +int fs_register(const struct fs *fs); +int fs_update(void); +int fs_mountpoint(const char *src, const char *tgt, const struct fs *fs, + fs_update_fn fn, struct fs_mp_prv *pr); +const struct fs *fs_from_type(const char *type); +int fs_mp_from_path(const char *path, struct fs_mp *mp); +struct fs_mp *fs_mps_from_path(const char *path, size_t *n); +const char *fs_relpath(const char *path); +char *fs_parent(const char *path); +int fs_next(const char *path, char **next, size_t *i); + +#endif diff --git a/src/fs/include/fs/inode.h b/src/fs/include/fs/inode.h new file mode 100644 index 0000000..fc24d74 --- /dev/null +++ b/src/fs/include/fs/inode.h @@ -0,0 +1,81 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#ifndef FS_INODE_H +#define FS_INODE_H + +#include <state.h> +#include <sys/types.h> +#include <time.h> + +struct fs; +struct fs_mp; +struct fs_ret; +struct inode_prv; + +enum +{ + INODE_DIR = 1, + INODE_REGFILE = 1 << 1, + INODE_BLOCKDEV = 1 << 2, + INODE_MOUNTPOINT = 1 << 3, +}; + +struct inode +{ + char *name; + int flags; + ino_t ino; + mode_t mode; + uid_t uid; + gid_t gid; + off_t size; + struct timespec atim, mtim, ctim; + struct inode *parent, *child, *left, *right; + struct inode_prv *prv; + void (*free)(void *); +}; + +union inode_result +{ + struct inode cachei, *memi; +}; + +typedef int (*inode_search_done)(enum state state, const char *relpath, + const struct fs_mp *mp, const union inode_result *inode, void *args); + +struct inode_search +{ + const char *path; + inode_search_done done; + void *args; +}; + +struct inode_ops +{ + int (*search)(const char *path, const struct fs_mp *mp, + union inode_result *inode, struct fs_ret *r); + int (*reserve)(const struct inode *i, void *args, struct fs_ret *r); + int (*flags)(const union inode_result *i); +}; + +int inode_search(const struct inode_search *s, struct fs_ret *r); +void inode_search_free(struct inode_search *s); +void inode_free(struct inode *i); + +#endif diff --git a/src/fs/iso9660/CMakeLists.txt b/src/fs/iso9660/CMakeLists.txt new file mode 100644 index 0000000..736fc4e --- /dev/null +++ b/src/fs/iso9660/CMakeLists.txt @@ -0,0 +1,20 @@ +# wnix, a Unix-like operating system for WebAssembly applications. +# Copyright (C) 2025 Xavier Del Campo Romero +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +add_library(iso9660) +add_subdirectory(src) +target_include_directories(iso9660 PUBLIC include PRIVATE private_include) +target_link_libraries(iso9660 PUBLIC c PRIVATE fs kprintf state) diff --git a/src/fs/iso9660/include/iso9660.h b/src/fs/iso9660/include/iso9660.h new file mode 100644 index 0000000..e7d1864 --- /dev/null +++ b/src/fs/iso9660/include/iso9660.h @@ -0,0 +1,24 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#ifndef ISO9660_H +#define ISO9660_H + +int iso9660_register(void); + +#endif diff --git a/src/fs/iso9660/private_include/iso9660/ops.h b/src/fs/iso9660/private_include/iso9660/ops.h new file mode 100644 index 0000000..b85d025 --- /dev/null +++ b/src/fs/iso9660/private_include/iso9660/ops.h @@ -0,0 +1,39 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#ifndef ISO9660_OPS_H +#define ISO9660_OPS_H + +#include <fs/fs.h> +#include <fs/inode.h> + +int iso9660_mount(const struct fs_mount *m, struct fs_ret *r); +int iso9660_mkdir(const struct fs_mkdir *m, const struct fs_mp *mp, + const union inode_result *i, struct fs_ret *r); +int iso9660_open(const struct fs_open *o, const struct fs_mp *mp, + const union inode_result *i, struct fs_ret *r); +int iso9660_read(const struct fs_read *r, struct fs_ret *ret); +int iso9660_write(const struct fs_write *w, struct fs_ret *r); +int iso9660_stat(const struct fs_stat *s, const struct fs_mp *mp, + const union inode_result *inode, struct fs_ret *r); +int iso9660_close(struct fs_fd *fd); +int iso9660_eof(const struct fs_fd *fd); +int iso9660_search(const char *path, const struct fs_mp *prv, + union inode_result *inode, struct fs_ret *r); + +#endif diff --git a/src/fs/iso9660/private_include/iso9660/routines.h b/src/fs/iso9660/private_include/iso9660/routines.h new file mode 100644 index 0000000..467cce2 --- /dev/null +++ b/src/fs/iso9660/private_include/iso9660/routines.h @@ -0,0 +1,35 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#ifndef ISO9660_ROUTINES_H +#define ISO9660_ROUTINES_H + +#include <iso9660/types.h> +#include <stddef.h> +#include <stdint.h> +#include <time.h> + +int iso9660_check_header(const struct iso9660_magic *m); +uint16_t iso9660_lmsb16(const struct iso9660_lmsb16 *p); +uint32_t iso9660_lmsb32(const struct iso9660_lmsb32 *p); +int iso9660_totm(const struct iso9660_dt *dt, struct tm *tm); +int iso9660_totimespec(const struct iso9660_dt *dt, struct timespec *ts); +int iso9660_etotimespec(const struct iso9660_entry_dt *dt, struct timespec *ts); +struct tm iso9660_gmtime(const struct iso9660_entry_dt *dt); + +#endif diff --git a/src/fs/iso9660/private_include/iso9660/types.h b/src/fs/iso9660/private_include/iso9660/types.h new file mode 100644 index 0000000..55544fb --- /dev/null +++ b/src/fs/iso9660/private_include/iso9660/types.h @@ -0,0 +1,148 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#ifndef ISO9660_TYPES_H +#define ISO9660_TYPES_H + +#include <fs/fs.h> +#include <fs/inode.h> +#include <stdint.h> + +#define ISO9660_LE ((const union {int a; char c;}){.a = 1}.c) + +struct fs_mp_prv +{ + int dummy; +}; + +enum +{ + ISO9660_TYPE_BOOT_RECORD, + ISO9660_TYPE_PRIMARY_VD, + ISO9660_TYPE_SUPPLEMENTARY_VD, + ISO9660_TYPE_VOLUME_PARTITION, + ISO9660_TYPE_TERMINATOR = 0xff, +}; + +struct inode_prv +{ + long offset; +}; + +struct iso9660_magic +{ + uint8_t id[sizeof "CD001" - 1]; +}; + +struct iso9660_header +{ + uint8_t type; + struct iso9660_magic magic; + uint8_t version; +}; + +struct iso9660_lmsb16 +{ + uint8_t le[sizeof (uint16_t)], be[sizeof (uint16_t)]; +}; + +struct iso9660_lmsb32 +{ + uint8_t le[sizeof (uint32_t)], be[sizeof (uint32_t)]; +}; + +struct iso9660_dt +{ + char year[sizeof "9999" - 1], + month[sizeof "12" - 1], + day[sizeof "31" - 1], + hour[sizeof "23" - 1], + min[sizeof "59" - 1], + sec[sizeof "59" - 1], + hsec[sizeof "99" - 1]; + int8_t zone; +}; + +struct iso9660_lsb32 +{ + uint8_t data[sizeof (uint32_t)]; +}; + +struct iso9660_msb32 +{ + uint8_t data[sizeof (uint32_t)]; +}; + +struct iso9660_entry_dt +{ + uint8_t year, month, day, hour, min, sec, offset; +}; + +enum +{ + ISO9660_FLAGS_HIDDEN = 1, + ISO9660_FLAGS_DIR = 1 << 1, + ISO9660_FLAGS_ASSOC = 1 << 2, + ISO9660_FLAGS_HAS_FORMAT = 1 << 3, + ISO9660_FLAGS_HAS_UID_GID = 1 << 4, + ISO9660_FLAGS_NOT_FINAL = 1 << 7 +}; + +struct iso9660_entry +{ + uint8_t len, attr; + struct iso9660_lmsb32 extent_loc, data_len; + struct iso9660_entry_dt dt; + uint8_t flags; + uint8_t funit_sz, interleave_sz; + struct iso9660_lmsb16 seq_num; + uint8_t id_len; +}; + +struct iso9660_pvd +{ + uint8_t :8, sysid[32], vid[32], unused[8]; + struct iso9660_lmsb32 space_size; + uint8_t unused_2[32]; + struct iso9660_lmsb16 set_size, seq_num, block_size; + struct iso9660_lmsb32 pathtable_size; + struct iso9660_lsb32 lpath_loc, optlpath_loc; + struct iso9660_msb32 mpath_loc, optmpath_loc; + struct iso9660_entry root_dir; + uint8_t padding, set_id[128], pub_id[128], dprep_id[128], app_id[128], + copyright_id[37], abstract_id[37], bibl_id[37]; + struct iso9660_dt creat, mod, exp, eff; + uint8_t fs_version, :8, reserved[512 + 653]; +}; + + +struct iso9660_vd +{ + struct iso9660_header header; + + union + { + struct iso9660_pvd pvd; + } u; +}; + +enum {ISO9660_SECTOR_SZ = 2048}; + +extern const struct fs iso9660; + +#endif diff --git a/src/fs/iso9660/src/CMakeLists.txt b/src/fs/iso9660/src/CMakeLists.txt new file mode 100644 index 0000000..740e404 --- /dev/null +++ b/src/fs/iso9660/src/CMakeLists.txt @@ -0,0 +1,35 @@ +# wnix, a Unix-like operating system for WebAssembly applications. +# Copyright (C) 2025 Xavier Del Campo Romero +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +target_sources(iso9660 PRIVATE + check_header.c + close.c + eof.c + etotimespec.c + gmtime.c + lmsb16.c + lmsb32.c + mount.c + mkdir.c + open.c + stat.c + read.c + register.c + search.c + totimespec.c + totm.c + write.c +) diff --git a/src/fs/iso9660/src/check_header.c b/src/fs/iso9660/src/check_header.c new file mode 100644 index 0000000..f504187 --- /dev/null +++ b/src/fs/iso9660/src/check_header.c @@ -0,0 +1,39 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <iso9660.h> +#include <iso9660/routines.h> +#include <kprintf.h> +#include <stdint.h> +#include <string.h> + +int iso9660_check_header(const struct iso9660_magic *const m) +{ + static const struct iso9660_magic em = {{'C', 'D', '0', '0', '1'}}; + + if (memcmp(m, &em, sizeof *m)) + { + const uint8_t *const b = m->id; + + kprintf("Invalid ISO9660 header: " + "{%hhx, %hhx, %hhx, %hhx, %hhx}\n", b[0], b[1], b[2], b[3], b[4]); + return -1; + } + + return 0; +} diff --git a/src/fs/iso9660/src/close.c b/src/fs/iso9660/src/close.c new file mode 100644 index 0000000..d43da29 --- /dev/null +++ b/src/fs/iso9660/src/close.c @@ -0,0 +1,26 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <iso9660.h> +#include <iso9660/ops.h> +#include <fs/fs.h> + +int iso9660_close(struct fs_fd *const fd) +{ + return -1; +} diff --git a/src/fs/iso9660/src/eof.c b/src/fs/iso9660/src/eof.c new file mode 100644 index 0000000..1c36b63 --- /dev/null +++ b/src/fs/iso9660/src/eof.c @@ -0,0 +1,33 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <iso9660.h> +#include <iso9660/ops.h> +#include <iso9660/types.h> +#include <fs/inode.h> + +int iso9660_eof(const struct fs_fd *const fd) +{ + const union inode_result *const i = &fd->tgt_inode; + const off_t sz = i->cachei.size; + + if (!sz) + return 1; + + return fd->offset >= sz - 1; +} diff --git a/src/fs/iso9660/src/etotimespec.c b/src/fs/iso9660/src/etotimespec.c new file mode 100644 index 0000000..21debf7 --- /dev/null +++ b/src/fs/iso9660/src/etotimespec.c @@ -0,0 +1,40 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <iso9660.h> +#include <iso9660/routines.h> +#include <iso9660/types.h> +#include <time.h> + +int iso9660_etotimespec(const struct iso9660_entry_dt *const dt, + struct timespec *const ts) +{ + struct tm tm = iso9660_gmtime(dt); + const time_t t = mktime(&tm); + + if (t == (time_t)-1) + return -1; + + *ts = (const struct timespec) + { + .tv_sec = t, + .tv_nsec = 0 + }; + + return 0; +} diff --git a/src/fs/iso9660/src/gmtime.c b/src/fs/iso9660/src/gmtime.c new file mode 100644 index 0000000..2649688 --- /dev/null +++ b/src/fs/iso9660/src/gmtime.c @@ -0,0 +1,35 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <iso9660.h> +#include <iso9660/routines.h> +#include <iso9660/types.h> +#include <time.h> + +struct tm iso9660_gmtime(const struct iso9660_entry_dt *const dt) +{ + return (const struct tm) + { + .tm_year = dt->year + 1900, + .tm_mon = dt->month, + .tm_mday = dt->day, + .tm_hour = dt->hour, + .tm_min = dt->min, + .tm_sec = dt->sec + }; +} diff --git a/src/fs/iso9660/src/lmsb16.c b/src/fs/iso9660/src/lmsb16.c new file mode 100644 index 0000000..d404b2c --- /dev/null +++ b/src/fs/iso9660/src/lmsb16.c @@ -0,0 +1,31 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <iso9660/routines.h> +#include <iso9660/types.h> +#include <stdint.h> +#include <string.h> + +uint16_t iso9660_lmsb16(const struct iso9660_lmsb16 *const p) +{ + const uint16_t *const d = (const void *)(ISO9660_LE ? p->le : p->be); + uint16_t ret; + + memcpy(&ret, d, sizeof ret); + return ret; +} diff --git a/src/fs/iso9660/src/lmsb32.c b/src/fs/iso9660/src/lmsb32.c new file mode 100644 index 0000000..2811226 --- /dev/null +++ b/src/fs/iso9660/src/lmsb32.c @@ -0,0 +1,31 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <iso9660/routines.h> +#include <iso9660/types.h> +#include <stdint.h> +#include <string.h> + +uint32_t iso9660_lmsb32(const struct iso9660_lmsb32 *const p) +{ + const uint32_t *const d = (const void *)(ISO9660_LE ? p->le : p->be); + uint32_t ret; + + memcpy(&ret, d, sizeof ret); + return ret; +} diff --git a/src/fs/iso9660/src/mkdir.c b/src/fs/iso9660/src/mkdir.c new file mode 100644 index 0000000..9d30659 --- /dev/null +++ b/src/fs/iso9660/src/mkdir.c @@ -0,0 +1,28 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <iso9660.h> +#include <iso9660/ops.h> +#include <fs/fs.h> +#include <fs/inode.h> + +int iso9660_mkdir(const struct fs_mkdir *const m, const struct fs_mp *const mp, + const union inode_result *const inode, struct fs_ret *const r) +{ + return -1; +} diff --git a/src/fs/iso9660/src/mount.c b/src/fs/iso9660/src/mount.c new file mode 100644 index 0000000..83e7a6d --- /dev/null +++ b/src/fs/iso9660/src/mount.c @@ -0,0 +1,180 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <iso9660.h> +#include <iso9660/ops.h> +#include <iso9660/routines.h> +#include <iso9660/types.h> +#include <fs/fs.h> +#include <kprintf.h> +#include <state.h> +#include <errno.h> +#include <stddef.h> +#include <stdlib.h> +#include <string.h> + +struct mount +{ + char *src, *tgt; + struct fs_fd fd; + struct fs_ret *r; + struct fs_mount mount; + struct iso9660_header header; + const struct fs_mp *mp; +}; + +static void free_mount(struct mount *const m) +{ + if (!m) + return; + + m->mp->fs->close(&m->fd); + free(m->src); + free(m->tgt); + free(m); +} + +static enum state check_header(void *const args) +{ + enum state ret = STATE_OK; + struct mount *const m = args; + + if (iso9660_check_header(&m->header.magic)) + ret = STATE_FATAL; + else if (fs_mountpoint(m->src, m->tgt, &iso9660, NULL, NULL)) + { + kprintf("Failed to allocate mountpoint {source=%s, target=%s}\n", + m->src, m->tgt); + ret = STATE_FATAL; + } + + free_mount(m); + return ret; +} + +static enum state read_header(void *const args) +{ + struct mount *const m = args; + struct fs_fd *const fd = &m->fd; + const struct fs *const fs = m->mp->fs; + const struct fs_read r = + { + .buf = &m->header, + .n = sizeof m->header, + .fd = fd + }; + + if (fd->error) + { + errno = fd->error; + goto failure; + } + + *m->r = (const struct fs_ret) + { + .f = check_header, + .args = m + }; + + fd->offset = 16 * ISO9660_SECTOR_SZ; + + if (fs->read(&r, m->r)) + goto failure; + + return STATE_AGAIN; + +failure: + free_mount(m); + return STATE_FATAL; +} + +static int search_done(const enum state state, const char *const relpath, + const struct fs_mp *const mp, const union inode_result *const inode, + void *const args) +{ + struct mount *const m = args; + const struct fs_mount *const fm = &m->mount; + + if (!inode) + { + errno = ENOENT; + goto failure; + } + + const struct fs_open o = + { + .fd = &m->fd, + .gid = fm->gid, + .uid = fm->uid, + .mode = fm->mode, + .path = relpath + }; + + *m->r = (const struct fs_ret) + { + .f = read_header, + .args = m + }; + + if (mp->fs->open(&o, mp, inode, m->r)) + goto failure; + + m->mp = mp; + return 0; + +failure: + free_mount(m); + return -1; +} + +int iso9660_mount(const struct fs_mount *const fm, struct fs_ret *const r) +{ + char *srcdup = NULL, *tgtdup = NULL; + struct mount *const m = malloc(sizeof *m); + + if (!m + || !(srcdup = strdup(fm->src)) + || !(tgtdup = strdup(fm->tgt))) + goto failure; + + *m = (const struct mount) + { + .src = srcdup, + .tgt = tgtdup, + .mount = *fm, + .r = r + }; + + const struct inode_search s = + { + .args = m, + .path = fm->src, + .done = search_done + }; + + if (inode_search(&s, r)) + goto failure; + + return 0; + +failure: + free(srcdup); + free(tgtdup); + free(m); + return -1; +} diff --git a/src/fs/iso9660/src/open.c b/src/fs/iso9660/src/open.c new file mode 100644 index 0000000..1ea6ad7 --- /dev/null +++ b/src/fs/iso9660/src/open.c @@ -0,0 +1,107 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <iso9660.h> +#include <iso9660/ops.h> +#include <iso9660/routines.h> +#include <iso9660/types.h> +#include <fs/fs.h> +#include <fs/inode.h> +#include <errno.h> +#include <stdlib.h> + +struct open +{ + struct inode inode; + struct fs_fd *fd; + struct fs_ret *pr, r; + const struct fs_mp *mp; +}; + +static int search_done(const enum state state, const char *const relpath, + const struct fs_mp *const mp, const union inode_result *const inode, + void *const args) +{ + int ret = 0; + struct open *const op = args; + const struct inode *const mpinode = &op->inode; + struct fs_fd *const fd = op->fd; + + if (mpinode->flags & INODE_DIR) + { + fd->error = EISDIR; + ret = -1; + } + else if (state) + { + fd->error = ENODEV; + ret = -1; + } + else + { + *fd = (const struct fs_fd) + { + .start = mpinode->prv->offset, + .tgt_inode.cachei = op->inode, + .size = mpinode->size, + .tgt_mp = op->mp, + .inode = *inode, + .mp = mp + }; + + *op->pr = op->r; + } + + free(op); + return ret; +} + +int iso9660_open(const struct fs_open *const o, const struct fs_mp *const mp, + const union inode_result *const inode, struct fs_ret *const r) +{ + const struct inode *const i = &inode->cachei; + struct open *const op = malloc(sizeof *op); + + if (!op) + return -1; + + const struct inode_search s = + { + .args = op, + .path = mp->src, + .done = search_done + }; + + *op = (const struct open) + { + .fd = o->fd, + .inode = *i, + .mp = mp, + .pr = r, + .r = *r + }; + + if (inode_search(&s, r)) + goto failure; + + return 0; + +failure: + free(op); + return -1; +} diff --git a/src/fs/iso9660/src/read.c b/src/fs/iso9660/src/read.c new file mode 100644 index 0000000..6e14845 --- /dev/null +++ b/src/fs/iso9660/src/read.c @@ -0,0 +1,27 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <iso9660.h> +#include <iso9660/ops.h> +#include <fs/fs.h> +#include <stddef.h> + +int iso9660_read(const struct fs_read *const fr, struct fs_ret *const ret) +{ + return -1; +} diff --git a/src/fs/iso9660/src/register.c b/src/fs/iso9660/src/register.c new file mode 100644 index 0000000..70a33f9 --- /dev/null +++ b/src/fs/iso9660/src/register.c @@ -0,0 +1,44 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <iso9660.h> +#include <iso9660/ops.h> +#include <fs/fs.h> + +const struct fs iso9660 = +{ + .type = "iso9660", + .flags = FS_DEV_REQUIRED, + .mount = iso9660_mount, + .stat = iso9660_stat, + .mkdir = iso9660_mkdir, + .open = iso9660_open, + .read = iso9660_read, + .write = iso9660_write, + .close = iso9660_close, + .eof = iso9660_eof, + .iops = + { + .search = iso9660_search + } +}; + +int iso9660_register(void) +{ + return fs_register(&iso9660); +} diff --git a/src/fs/iso9660/src/search.c b/src/fs/iso9660/src/search.c new file mode 100644 index 0000000..a7a5150 --- /dev/null +++ b/src/fs/iso9660/src/search.c @@ -0,0 +1,557 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <iso9660.h> +#include <iso9660/ops.h> +#include <iso9660/routines.h> +#include <iso9660/types.h> +#include <fs/fs.h> +#include <kprintf.h> +#include <fcntl.h> +#include <ctype.h> +#include <errno.h> +#include <inttypes.h> +#include <limits.h> +#include <stddef.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> + +struct id +{ + char buf[UCHAR_MAX]; +}; + +struct search +{ + union inode_result *inode; + const struct fs_mp *src_mp; + struct iso9660_entry entry; + const char *src, *path; + struct fs_ret r, *pr; + struct fs_fd fd; + off_t offset; + size_t token_i; + char *token; + void *buf; + struct id id; +}; + +static int prepare_entry(struct search *); +static int next_entry(struct search *); + +static void free_search(struct search *const s) +{ + if (!s) + return; + else if (s->src_mp) + s->src_mp->fs->close(&s->fd); + + free(s->buf); + free(s->token); + free(s); +} + +static int set_inode(struct search *const s, + const struct iso9660_entry *const e, const struct id *const id) +{ + struct inode *const inode = &s->inode->cachei; + struct inode_prv *p = NULL; + char *namedup = NULL; + struct timespec ts, atim; + + if (clock_gettime(CLOCK_REALTIME, &atim)) + goto failure; + else if (iso9660_etotimespec(&e->dt, &ts)) + { + kprintf("Invalid datetime, path=%s\n", s->path); + goto failure; + } + else if (!(namedup = strdup(id->buf)) || !(p = malloc(sizeof *p))) + goto failure; + + *p = (const struct inode_prv) + { + .offset = iso9660_lmsb32(&e->extent_loc) * ISO9660_SECTOR_SZ + }; + + *inode = (const struct inode) + { + .size = iso9660_lmsb32(&e->data_len), + .flags = INODE_REGFILE, + .name = namedup, + .mode = 0555, + .atim = atim, + .free = free, + .ctim = ts, + .mtim = ts, + .prv = p + }; + + return 0; + +failure: + free(p); + free(namedup); + return -1; +} + +static int next_token(struct search *const s) +{ + free(s->token); + s->token = NULL; + + if (fs_next(s->path, &s->token, &s->token_i)) + { + kprintf("Failed to extract next token, path=%s, i=%zu\n", s->path, + s->token_i); + return -1; + } + + return 0; +} + +static int check_version(const char *const version) +{ + char *end; + + errno = 0; + strtoul(version, &end, 10); + + return errno || *end; +} + +static void extract_id(const char *id, struct id *const out, + const char *const ext, const char *const version) +{ + char *p = out->buf; + + if (ext) + { + const ptrdiff_t n = ext - id; + const char *const e = ext + 1; + + memcpy(p, id, n); + p += n; + id += n; + + if (*e) + { + const ptrdiff_t n = version ? version - e : strlen(e); + + memcpy(p, id, n); + p += n; + } + } + else if (version) + { + memcpy(p, id, version - id); + p += version - id; + } + else + { + const ptrdiff_t n = strlen(id); + + memcpy(p, id, n); + p += n; + } + + *p = '\0'; + p = out->buf; + + while (*p) + { + *p = tolower(*p); + p++; + } +} + +static int parse_id(const char *const id, struct id *const out) +{ + static const char accept[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"; + const char *const ext = strchr(id, '.'), *const version = strchr(id, ';'); + const size_t n = strspn(id, accept), len = strlen(id); + + /* Ensure only valid characters are met if no version or extension. */ + if (!version && !ext && n != len) + return -1; + /* Ensure file version appears only after file extension, + * if both are present. */ + else if (version && ext && ext > version) + return -1; + /* Ensure one and only one file version and/or extension appear. */ + else if ((version && strchr(version + 1, ';')) + || (ext && strchr(ext + 1, '.'))) + return -1; + else if (version && check_version(version + 1)) + return -1; + /* When present, ensure valid characters only until the file extension + * is found. */ + else if (ext && n != ext - id) + return -1; + + extract_id(id, out, ext, version); + return 0; +} + +static enum state id_read(void *const args) +{ + struct search *volatile const s = args; + const struct iso9660_entry *const e = &s->entry; + const char *const id = s->id.buf; + struct id outid; + + if (!*id || !strcmp(id, "\1")) + { + if (next_entry(s)) + goto failure; + } + else if (parse_id(id, &outid)) + { + kprintf("Invalid file or directory identifier=%s\n", id); + goto failure; + } + else if (!strcmp(outid.buf, s->token)) + { + char *token; + + if (fs_next(s->path, &token, &s->token_i)) + { + kprintf("Failed to extract next token, path=%s, i=%zu\n", + s->path, s->token_i); + goto failure; + } + else if (!token) + { + if (set_inode(s, e, &outid)) + goto failure; + + *s->pr = s->r; + free_search(s); + return STATE_AGAIN; + } + else if (!(e->flags & ISO9660_FLAGS_DIR)) + { + kprintf("Intermediate node %s not a directory, path=%s\n", + id, s->path); + goto failure; + } + else + { + s->fd.offset = iso9660_lmsb32(&e->extent_loc) * ISO9660_SECTOR_SZ; + + if (prepare_entry(s)) + goto failure; + } + + free(s->token); + free(s->buf); + s->token = token; + s->buf = NULL; + } + else if (next_entry(s)) + goto failure; + + return STATE_AGAIN; + +failure: + free_search(s); + return STATE_FATAL; +} + +static enum state entry_read(void *const args) +{ + struct search *const s = args; + const struct iso9660_entry *const e = &s->entry; + + if (e->len < sizeof *e) + { + kprintf("Unexpected entry length %hhu, expected %zu\n", e->len, + sizeof *e); + goto failure; + } + + char *const buf = s->id.buf; + const struct fs_mp *const mp = s->fd.mp; + const struct fs_read r = + { + .buf = buf, + .n = e->id_len, + .fd = &s->fd + }; + + *(buf + e->id_len) = '\0'; + *s->pr = (const struct fs_ret) + { + .f = id_read, + .args = s + }; + + if (mp->fs->read(&r, s->pr)) + goto failure; + + return STATE_AGAIN; + +failure: + free_search(s); + return STATE_FATAL; +} + +static int prepare_entry(struct search *const s) +{ + const struct fs_read r = + { + .buf = &s->entry, + .fd = &s->fd, + .n = sizeof s->entry + }; + + *s->pr = (const struct fs_ret) + { + .f = entry_read, + .args = s + }; + + s->offset = s->fd.offset; + return s->src_mp->fs->read(&r, s->pr); +} + +static int next_entry(struct search *const s) +{ + const struct iso9660_entry *const e = &s->entry; + struct fs_fd *const fd = &s->fd; + + fd->offset = s->offset + e->len; + return prepare_entry(s); +} + +static int set_root_inode(const struct iso9660_pvd *const pvd, + struct search *const s) +{ + struct inode *const inode = &s->inode->cachei; + struct inode_prv *p = NULL; + char *namedup = NULL; + + if (clock_gettime(CLOCK_REALTIME, &inode->atim)) + goto failure; + else if (iso9660_totimespec(&pvd->creat, &inode->ctim)) + { + kprintf("Invalid volume creation time\n"); + goto failure; + } + else if (iso9660_totimespec(&pvd->mod, &inode->mtim)) + { + kprintf("Invalid volume modification time\n"); + goto failure; + } + else if (!(namedup = strdup("/")) || !(p = malloc(sizeof *p))) + goto failure; + + *p = (const struct inode_prv) + { + .offset = offsetof(struct iso9660_pvd, root_dir) + }; + + inode->flags = INODE_DIR; + inode->mode = 0555; + inode->name = namedup; + inode->prv = p; + *s->pr = s->r; + free_search(s); + return 0; + +failure: + free(p); + free(namedup); + return -1; +} + +static int check_pvd(const struct iso9660_vd *const vd) +{ + const struct iso9660_pvd *const pvd = &vd->u.pvd; + const struct iso9660_entry *const e = &pvd->root_dir; + const struct iso9660_header *const h = &vd->header; + const uint16_t block_size = iso9660_lmsb16(&pvd->block_size); + const size_t exp_sz = sizeof (struct iso9660_entry) + sizeof pvd->padding; + + if (iso9660_check_header(&h->magic)) + return -1; + else if (h->version != 1) + { + kprintf("Unsupported ISO9660 version %#hhx, expected 1\n", h->version); + return -1; + } + else if (h->type != ISO9660_TYPE_PRIMARY_VD) + { + kprintf("Unexpected volume descriptor type %#hhx, expected=%#x\n", + h->type, ISO9660_TYPE_PRIMARY_VD); + return -1; + } + else if (pvd->fs_version != 1) + { + kprintf("Unexpected file structure version %#hhx, expected 1\n", + pvd->fs_version); + return -1; + } + else if (block_size != ISO9660_SECTOR_SZ) + { + kprintf("Unsupported block size %" PRIu16 ", expected %d\n", + block_size, ISO9660_SECTOR_SZ); + return -1; + } + else if (e->len != exp_sz) + { + kprintf("Unexpected root directory length %hhd, expected %zu\n", + e->len, exp_sz); + return -1; + } + else if (!(e->flags & ISO9660_FLAGS_DIR)) + { + kprintf("Directory flag (%#hx) not found in file flags (%#hhx)\n", + ISO9660_FLAGS_DIR, e->flags); + return -1; + } + + return 0; +} + +static enum state read_pvd_done(void *const args) +{ + struct search *const s = args; + struct fs_fd *const fd = &s->fd; + const struct iso9660_vd *const vd = s->buf; + const struct iso9660_pvd *const pvd = &vd->u.pvd; + const struct iso9660_entry *const e = &pvd->root_dir; + const uint32_t lba = iso9660_lmsb32(&e->extent_loc); + + fd->offset = lba * ISO9660_SECTOR_SZ; + + if (check_pvd(vd)) + goto failure; + else if (!strcmp(s->src, "/")) + { + if (set_root_inode(pvd, s)) + goto failure; + + return STATE_AGAIN; + } + else if (next_token(s) || prepare_entry(s)) + goto failure; + + return STATE_AGAIN; + +failure: + free_search(s); + return STATE_FATAL; +} + +static enum state read_pvd(void *const args) +{ + struct search *const s = args; + struct fs_fd *const fd = &s->fd; + + if (!(s->buf = malloc(ISO9660_SECTOR_SZ)) || next_token(s)) + goto failure; + + const struct fs_read r = + { + .buf = s->buf, + .fd = fd, + .n = ISO9660_SECTOR_SZ + }; + + *s->pr = (const struct fs_ret) + { + .f = read_pvd_done, + .args = s + }; + + fd->offset = 16 * ISO9660_SECTOR_SZ; + + if (s->src_mp->fs->read(&r, s->pr)) + goto failure; + + return STATE_AGAIN; + +failure: + free_search(s); + return STATE_FATAL; +} + +static int src_done(const enum state state, const char *const relpath, + const struct fs_mp *const mp, const union inode_result *const inode, + void *const args) +{ + struct search *const s = args; + + if (state) + { + kprintf("Failed to find source inode for %s\n", s->src); + return -1; + } + + s->src_mp = mp; + *s->pr = (const struct fs_ret) + { + .f = read_pvd, + .args = s + }; + + const struct fs_open o = + { + .fd = &s->fd, + .path = mp->src, + .flags = O_RDONLY + }; + + return mp->fs->open(&o, mp, inode, s->pr); +} + +int iso9660_search(const char *const path, const struct fs_mp *const mp, + union inode_result *const inode, struct fs_ret *const r) +{ + struct search *const s = malloc(sizeof *s); + + if (!s) + goto failure; + + *s = (const struct search) + { + .inode = inode, + .src = mp->src, + .path = path, + .r = *r, + .pr = r + }; + + const struct inode_search is = + { + .path = mp->src, + .done = src_done, + .args = s, + }; + + if (inode_search(&is, r)) + goto failure; + + return 0; + +failure: + free(s); + return -1; +} diff --git a/src/fs/iso9660/src/stat.c b/src/fs/iso9660/src/stat.c new file mode 100644 index 0000000..53732fe --- /dev/null +++ b/src/fs/iso9660/src/stat.c @@ -0,0 +1,28 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <iso9660.h> +#include <iso9660/ops.h> +#include <fs/fs.h> +#include <sys/stat.h> + +int iso9660_stat(const struct fs_stat *const s, const struct fs_mp *const mp, + const union inode_result *const inode, struct fs_ret *const r) +{ + return -1; +} diff --git a/src/fs/iso9660/src/totimespec.c b/src/fs/iso9660/src/totimespec.c new file mode 100644 index 0000000..735deb7 --- /dev/null +++ b/src/fs/iso9660/src/totimespec.c @@ -0,0 +1,54 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <iso9660.h> +#include <iso9660/routines.h> +#include <iso9660/types.h> +#include <errno.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> + +int iso9660_totimespec(const struct iso9660_dt *const dt, + struct timespec *const ts) +{ + char hsec[sizeof dt->hsec + 1], *end; + unsigned long v; + struct tm tm; + time_t t; + + if (iso9660_totm(dt, &tm) + || ((t = mktime(&tm)) == (time_t)-1)) + return -1; + + errno = 0; + memcpy(hsec, dt->hsec, sizeof dt->hsec); + hsec[sizeof dt->hsec] = '\0'; + v = strtoul(hsec, &end, 10); + + if (errno || *end || v >= 100) + return -1; + + *ts = (const struct timespec) + { + .tv_sec = t, + .tv_nsec = v * 100000ul + }; + + return 0; +} diff --git a/src/fs/iso9660/src/totm.c b/src/fs/iso9660/src/totm.c new file mode 100644 index 0000000..1e94359 --- /dev/null +++ b/src/fs/iso9660/src/totm.c @@ -0,0 +1,72 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <iso9660.h> +#include <iso9660/routines.h> +#include <iso9660/types.h> +#include <errno.h> +#include <limits.h> +#include <stddef.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> + +int iso9660_totm(const struct iso9660_dt *const dt, struct tm *const tm) +{ + char year[sizeof dt->year + 1], + month[sizeof dt->month + 1], + day[sizeof dt->day + 1], + hour[sizeof dt->hour + 1], + min[sizeof dt->min + 1], + sec[sizeof dt->sec + 1]; + + struct buf + { + const char *src; + char *s; + int *v; + size_t n; + } bufs[] = + { + {.src = dt->year, .s = year, .n = sizeof dt->year, .v = &tm->tm_year}, + {.src = dt->month, .s = month, .n = sizeof dt->month, .v = &tm->tm_mon}, + {.src = dt->day, .s = day, .n = sizeof dt->day, .v = &tm->tm_yday}, + {.src = dt->hour, .s = hour, .n = sizeof dt->hour, .v = &tm->tm_hour}, + {.src = dt->min, .s = min, .n = sizeof dt->min, .v = &tm->tm_min}, + {.src = dt->sec, .s = sec, .n = sizeof dt->sec, .v = &tm->tm_sec} + }; + + for (size_t i = 0; i < sizeof bufs / sizeof *bufs; i++) + { + struct buf *const b = &bufs[i]; + unsigned long v; + char *end; + + memcpy(b->s, b->src, b->n - 1); + b->s[b->n] = '\0'; + errno = 0; + v = strtoul(b->s, &end, 10); + + if (errno || *end || v > INT_MAX) + return -1; + + *b->v = v; + } + + return 0; +} diff --git a/src/fs/iso9660/src/write.c b/src/fs/iso9660/src/write.c new file mode 100644 index 0000000..2ce3823 --- /dev/null +++ b/src/fs/iso9660/src/write.c @@ -0,0 +1,28 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <iso9660.h> +#include <iso9660/ops.h> +#include <fs/fs.h> +#include <errno.h> + +int iso9660_write(const struct fs_write *const w, struct fs_ret *const r) +{ + errno = EROFS; + return -1; +} diff --git a/src/fs/private_include/fs/private.h b/src/fs/private_include/fs/private.h new file mode 100644 index 0000000..9ccebaf --- /dev/null +++ b/src/fs/private_include/fs/private.h @@ -0,0 +1,63 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#ifndef FS_TYPES_H +#define FS_TYPES_H + +#include <fs/fs.h> +#include <fs/inode.h> +#include <state.h> +#include <sys/types.h> + +struct fs_fd_prv; + +struct fs_register +{ + const struct fs *fs; + struct fs_register *next; +}; + +struct fs_mountpoint +{ + char *src, *tgt; + const struct fs *fs; + struct fs_mp_prv *prv; + struct fs_mountpoint *next; + fs_update_fn update; +}; + +struct fs_add +{ + char *src, *tgt; + struct inode_search *search; + struct inode *inode; + mode_t mode; + const struct fs *fs; + enum state (*next)(struct fs_add *); +}; + +struct fs_fd +{ + long off; + struct fs_fd_prv *prv; +}; + +extern struct fs_register *fs_rhead, *fs_rtail; +extern struct fs_mountpoint *fs_mphead, *fs_mptail; + +#endif diff --git a/src/fs/private_include/fs/types.h b/src/fs/private_include/fs/types.h new file mode 100644 index 0000000..e860d77 --- /dev/null +++ b/src/fs/private_include/fs/types.h @@ -0,0 +1,55 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#ifndef FS_TYPES_H +#define FS_TYPES_H + +#include <fs/fs.h> +#include <fs/inode.h> +#include <state.h> +#include <sys/types.h> + +struct fs_register +{ + const struct fs *fs; + struct fs_register *next; +}; + +struct fs_mountpoint +{ + char *src, *tgt; + const struct fs *fs; + struct fs_mp_prv *prv; + struct fs_mountpoint *next; + fs_update_fn update; +}; + +struct fs_add +{ + char *src, *tgt; + struct inode_search *search; + struct inode *inode; + mode_t mode; + const struct fs *fs; + enum state (*next)(struct fs_add *); +}; + +extern struct fs_register *fs_rhead, *fs_rtail; +extern struct fs_mountpoint *fs_mphead, *fs_mptail; + +#endif diff --git a/src/fs/ramfs/CMakeLists.txt b/src/fs/ramfs/CMakeLists.txt new file mode 100644 index 0000000..477e8a7 --- /dev/null +++ b/src/fs/ramfs/CMakeLists.txt @@ -0,0 +1,20 @@ +# wnix, a Unix-like operating system for WebAssembly applications. +# Copyright (C) 2025 Xavier Del Campo Romero +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +add_library(ramfs) +add_subdirectory(src) +target_include_directories(ramfs PUBLIC include PRIVATE private_include) +target_link_libraries(ramfs PUBLIC state c PRIVATE fs drv) diff --git a/src/fs/ramfs/include/ramfs.h b/src/fs/ramfs/include/ramfs.h new file mode 100644 index 0000000..74160e1 --- /dev/null +++ b/src/fs/ramfs/include/ramfs.h @@ -0,0 +1,43 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#ifndef RAMFS_H +#define RAMFS_H + +#include <fs/fs.h> +#include <fs/inode.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <stddef.h> + +struct ramfs *ramfs_mount(const struct fs_mount *m, struct fs_ret *r); +int ramfs_mkdir(const struct fs_mkdir *m, struct ramfs *rfs, + const union inode_result *i, struct fs_ret *r); +int ramfs_open(const struct fs_open *o, struct inode *i, struct fs_ret *r); +int ramfs_read(const struct fs_read *r, struct fs_ret *ret); +int ramfs_write(const struct fs_write *w, struct fs_ret *r); +int ramfs_stat(const struct fs_stat *s, const struct ramfs *rfs, + const union inode_result *inode, struct fs_ret *r); +int ramfs_close(struct fs_fd *fd); +int ramfs_search(const char *path, const struct ramfs *rfs, + union inode_result *inode, struct fs_ret *r); +int ramfs_flags(const union inode_result *i); +int ramfs_mknod(const char *node, mode_t mode, dev_t dev, struct ramfs *rfs); +void ramfs_free(struct ramfs *rfs); + +#endif diff --git a/src/fs/ramfs/private_include/ramfs/types.h b/src/fs/ramfs/private_include/ramfs/types.h new file mode 100644 index 0000000..9be5899 --- /dev/null +++ b/src/fs/ramfs/private_include/ramfs/types.h @@ -0,0 +1,31 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#ifndef RAMFS_TYPES_H +#define RAMFS_TYPES_H + +#include <fs/fs.h> +#include <fs/inode.h> + +struct ramfs +{ + struct inode *root; + ino_t ino; +}; + +#endif diff --git a/src/fs/ramfs/src/CMakeLists.txt b/src/fs/ramfs/src/CMakeLists.txt new file mode 100644 index 0000000..a35e23e --- /dev/null +++ b/src/fs/ramfs/src/CMakeLists.txt @@ -0,0 +1,29 @@ +# wnix, a Unix-like operating system for WebAssembly applications. +# Copyright (C) 2025 Xavier Del Campo Romero +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +target_sources(ramfs PRIVATE + close.c + free.c + flags.c + mount.c + mkdir.c + mknod.c + open.c + stat.c + read.c + search.c + write.c +) diff --git a/src/fs/ramfs/src/close.c b/src/fs/ramfs/src/close.c new file mode 100644 index 0000000..5243a77 --- /dev/null +++ b/src/fs/ramfs/src/close.c @@ -0,0 +1,25 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <ramfs.h> +#include <fs/fs.h> + +int ramfs_close(struct fs_fd *const fd) +{ + return -1; +} diff --git a/src/fs/ramfs/src/flags.c b/src/fs/ramfs/src/flags.c new file mode 100644 index 0000000..7f51b81 --- /dev/null +++ b/src/fs/ramfs/src/flags.c @@ -0,0 +1,25 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <ramfs.h> +#include <fs/inode.h> + +int ramfs_flags(const union inode_result *const i) +{ + return i->memi->flags; +} diff --git a/src/fs/ramfs/src/free.c b/src/fs/ramfs/src/free.c new file mode 100644 index 0000000..10fcbd6 --- /dev/null +++ b/src/fs/ramfs/src/free.c @@ -0,0 +1,31 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <ramfs.h> +#include <ramfs/types.h> +#include <stddef.h> +#include <stdlib.h> + +void ramfs_free(struct ramfs *const r) +{ + if (!r) + return; + + inode_free(r->root); + free(r); +} diff --git a/src/fs/ramfs/src/mkdir.c b/src/fs/ramfs/src/mkdir.c new file mode 100644 index 0000000..f62984d --- /dev/null +++ b/src/fs/ramfs/src/mkdir.c @@ -0,0 +1,79 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <ramfs.h> +#include <ramfs/types.h> +#include <fs/fs.h> +#include <fs/inode.h> +#include <state.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> + +static enum state done(void *const args) +{ + return STATE_OK; +} + +int ramfs_mkdir(const struct fs_mkdir *const m, struct ramfs *const rfs, + const union inode_result *const inode, struct fs_ret *const r) +{ + struct timespec ts; + struct inode *ni = NULL; + struct inode *const parent = inode->memi; + char *const name = strdup(m->path); + + if (!name + || clock_gettime(CLOCK_REALTIME, &ts) + || !(ni = malloc(sizeof *ni))) + goto failure; + + *ni = (const struct inode) + { + .name = name, + .mode = m->mode, + .uid = m->uid, + .gid = m->gid, + .flags = INODE_DIR, + .atim = ts, + .ctim = ts, + .mtim = ts, + .ino = rfs->ino++, + .parent = parent + }; + + if (!parent->child) + parent->child = ni; + else + for (struct inode *i = parent->child; i; i = i->right) + if (!i->right) + { + i->right = ni; + ni->left = i; + break; + } + + parent->atim = parent->mtim = ts; + *r = (const struct fs_ret){.f = done}; + return 0; + +failure: + free(ni); + free(name); + return -1; +} diff --git a/src/fs/ramfs/src/mknod.c b/src/fs/ramfs/src/mknod.c new file mode 100644 index 0000000..d142ab1 --- /dev/null +++ b/src/fs/ramfs/src/mknod.c @@ -0,0 +1,87 @@ + +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <ramfs.h> +#include <ramfs/types.h> +#include <fs/inode.h> +#include <sys/types.h> +#include <errno.h> +#include <stddef.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> + +static struct inode *find(const char *const node, struct inode *const root) +{ + for (struct inode *c = root->child; c; c = c->right) + if (!strcmp(c->name, node)) + return c; + + return NULL; +} + +int ramfs_mknod(const char *const node, const mode_t mode, const dev_t dev, + struct ramfs *const rfs) +{ + struct inode *const r = rfs->root, + *const inode = find(node, r), + *newinode = NULL; + char *nodedup = NULL; + struct timespec ts; + + if (inode) + { + errno = EEXIST; + goto failure; + } + else if (clock_gettime(CLOCK_REALTIME, &ts) + || !(nodedup = strdup(node)) + || !(newinode = malloc(sizeof *newinode))) + goto failure; + + *newinode = (const struct inode) + { + .atim = ts, + .ctim = ts, + .mtim = ts, + .flags = INODE_BLOCKDEV, + .mode = mode, + .parent = rfs->root, + .name = nodedup + }; + + if (!r->child) + r->child = newinode; + else + for (struct inode *c = r->child; c; c = c->right) + if (!c->right) + { + c->right = newinode; + newinode->left = c; + break; + } + + r->atim = r->mtim = ts; + return 0; + +failure: + free(nodedup); + free(newinode); + return -1; +} diff --git a/src/fs/ramfs/src/mount.c b/src/fs/ramfs/src/mount.c new file mode 100644 index 0000000..567c2da --- /dev/null +++ b/src/fs/ramfs/src/mount.c @@ -0,0 +1,69 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <ramfs.h> +#include <ramfs/types.h> +#include <fs/fs.h> +#include <state.h> +#include <sys/types.h> +#include <stddef.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> + +static enum state mount(void *const args) +{ + return STATE_OK; +} + +struct ramfs *ramfs_mount(const struct fs_mount *const m, + struct fs_ret *const r) +{ + struct ramfs *ret = NULL; + struct timespec ts; + char *const name = strdup("/"); + struct inode *root = NULL; + + if (!name + || clock_gettime(CLOCK_REALTIME, &ts) + || !(root = malloc(sizeof *root)) + || !(ret = malloc(sizeof *ret))) + goto failure; + + *root = (const struct inode) + { + .name = name, + .mode = m->mode, + .uid = m->uid, + .gid = m->gid, + .atim = ts, + .mtim = ts, + .ctim = ts + }; + + *ret = (const struct ramfs){.root = root}; + *r = (const struct fs_ret){.f = mount}; + ret->ino++; + return ret; + +failure: + free(root); + free(ret); + free(name); + return NULL; +} diff --git a/src/fs/ramfs/src/open.c b/src/fs/ramfs/src/open.c new file mode 100644 index 0000000..894b160 --- /dev/null +++ b/src/fs/ramfs/src/open.c @@ -0,0 +1,26 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <ramfs.h> +#include <fs/fs.h> + +int ramfs_open(const struct fs_open *const o,struct inode *const i, + struct fs_ret *const r) +{ + return -1; +} diff --git a/src/fs/ramfs/src/read.c b/src/fs/ramfs/src/read.c new file mode 100644 index 0000000..cc96744 --- /dev/null +++ b/src/fs/ramfs/src/read.c @@ -0,0 +1,26 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <ramfs.h> +#include <fs/fs.h> +#include <stddef.h> + +int ramfs_read(const struct fs_read *const r, struct fs_ret *const ret) +{ + return -1; +} diff --git a/src/fs/ramfs/src/search.c b/src/fs/ramfs/src/search.c new file mode 100644 index 0000000..24e2a98 --- /dev/null +++ b/src/fs/ramfs/src/search.c @@ -0,0 +1,143 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <ramfs.h> +#include <ramfs/types.h> +#include <fs/fs.h> +#include <state.h> +#include <stdbool.h> +#include <stdlib.h> +#include <string.h> + +struct search +{ + union inode_result *inode; + const struct ramfs *rfs; + struct fs_ret r, *pr; + const char *path; +}; + +static void free_search(struct search *const s) +{ + free(s); +} + +static int find_node(const char *const path, struct inode **const out, + size_t *const i) +{ + int ret = -1; + char *node; + struct inode *volatile const inode = *out; + + if (fs_next(path, &node, i)) + return -1; + else if (!strcmp(inode->name, node)) + { + if (*i >= strlen(path)) + { + free(node); + return 1; + } + + *out = inode->child; + } + else + { + bool found = false; + + for (struct inode *in = inode->right; in; in = in->right) + if (!strcmp(in->name, node)) + { + if (*i >= strlen(path)) + { + *out = in; + free(node); + return 1; + } + + *out = in->child; + found = true; + break; + } + + if (!found) + goto end; + } + + ret = 0; + +end: + free(node); + return ret; +} + +static enum state search(void *const args) +{ + enum state ret = STATE_FATAL; + struct search *const s = args; + struct inode *inode = s->rfs->root; + size_t i = 0; + + for (;;) + { + const int n = find_node(s->path, &inode, &i); + + if (n < 0) + goto end; + else if (n > 0) + break; + } + + s->inode->memi = inode; + *s->pr = s->r; + ret = STATE_AGAIN; + +end: + free_search(s); + return ret; +} + +int ramfs_search(const char *const path, const struct ramfs *const rfs, + union inode_result *const inode, struct fs_ret *const r) +{ + struct search *const s = malloc(sizeof *s); + + if (!s) + goto failure; + + *s = (const struct search) + { + .inode = inode, + .path = path, + .rfs = rfs, + .r = *r, + .pr = r + }; + + *r = (const struct fs_ret) + { + .f = search, + .args = s + }; + + return 0; + +failure: + free(s); + return -1; +} diff --git a/src/fs/ramfs/src/stat.c b/src/fs/ramfs/src/stat.c new file mode 100644 index 0000000..6bb1b23 --- /dev/null +++ b/src/fs/ramfs/src/stat.c @@ -0,0 +1,53 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <ramfs.h> +#include <fs/fs.h> +#include <state.h> +#include <sys/stat.h> + +static enum state done(void *const args) +{ + return STATE_OK; +} + +int ramfs_stat(const struct fs_stat *const s, const struct ramfs *const rfs, + const union inode_result *const inode, struct fs_ret *const r) +{ + const struct inode *const i = inode->memi; + + *s->sb = (const struct stat) + { + .st_atim = i->atim, + .st_ctim = i->ctim, + .st_mtim = i->mtim, + .st_size = i->size, + .st_blksize = 1, + .st_ino = i->ino, + .st_uid = i->uid, + .st_gid = i->gid, + .st_mode = i->mode + }; + + *r = (const struct fs_ret) + { + .f = done + }; + + return 0; +} diff --git a/src/fs/ramfs/src/write.c b/src/fs/ramfs/src/write.c new file mode 100644 index 0000000..ca49c22 --- /dev/null +++ b/src/fs/ramfs/src/write.c @@ -0,0 +1,26 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <ramfs.h> +#include <fs/fs.h> +#include <stddef.h> + +int ramfs_write(const struct fs_write *const w, struct fs_ret *const r) +{ + return -1; +} diff --git a/src/fs/rootfs/CMakeLists.txt b/src/fs/rootfs/CMakeLists.txt new file mode 100644 index 0000000..588d14b --- /dev/null +++ b/src/fs/rootfs/CMakeLists.txt @@ -0,0 +1,20 @@ +# wnix, a Unix-like operating system for WebAssembly applications. +# Copyright (C) 2025 Xavier Del Campo Romero +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +add_library(rootfs) +add_subdirectory(src) +target_include_directories(rootfs PUBLIC include PRIVATE private_include) +target_link_libraries(rootfs PUBLIC state c PRIVATE fs ramfs) diff --git a/src/fs/rootfs/include/rootfs.h b/src/fs/rootfs/include/rootfs.h new file mode 100644 index 0000000..2327079 --- /dev/null +++ b/src/fs/rootfs/include/rootfs.h @@ -0,0 +1,24 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#ifndef ROOTFS_H +#define ROOTFS_H + +int rootfs_register(void); + +#endif diff --git a/src/fs/rootfs/private_include/rootfs/ops.h b/src/fs/rootfs/private_include/rootfs/ops.h new file mode 100644 index 0000000..decdf8b --- /dev/null +++ b/src/fs/rootfs/private_include/rootfs/ops.h @@ -0,0 +1,42 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#ifndef ROOTFS_OPS_H +#define ROOTFS_OPS_H + +#include <fs/fs.h> +#include <fs/inode.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <stddef.h> + +int rootfs_mount(const struct fs_mount *m, struct fs_ret *r); +int rootfs_mkdir(const struct fs_mkdir *m, const struct fs_mp *mp, + const union inode_result *i, struct fs_ret *r); +int rootfs_open(const struct fs_open *o, const struct fs_mp *mp, + const union inode_result *i, struct fs_ret *r); +int rootfs_read(const struct fs_read *r, struct fs_ret *ret); +int rootfs_write(const struct fs_write *w, struct fs_ret *r); +int rootfs_stat(const struct fs_stat *s, const struct fs_mp *mp, + const union inode_result *inode, struct fs_ret *r); +int rootfs_close(struct fs_fd *fd); +int rootfs_search(const char *path, const struct fs_mp *mp, + union inode_result *inode, struct fs_ret *r); +int rootfs_flags(const union inode_result *i); + +#endif diff --git a/src/fs/rootfs/private_include/rootfs/types.h b/src/fs/rootfs/private_include/rootfs/types.h new file mode 100644 index 0000000..471d9d3 --- /dev/null +++ b/src/fs/rootfs/private_include/rootfs/types.h @@ -0,0 +1,32 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#ifndef ROOTFS_TYPES_H +#define ROOTFS_TYPES_H + +#include <fs/fs.h> +#include <ramfs.h> + +struct fs_mp_prv +{ + struct ramfs *rfs; +}; + +extern const struct fs rootfs; + +#endif diff --git a/src/fs/rootfs/src/CMakeLists.txt b/src/fs/rootfs/src/CMakeLists.txt new file mode 100644 index 0000000..8e295e1 --- /dev/null +++ b/src/fs/rootfs/src/CMakeLists.txt @@ -0,0 +1,28 @@ +# wnix, a Unix-like operating system for WebAssembly applications. +# Copyright (C) 2025 Xavier Del Campo Romero +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +target_sources(rootfs PRIVATE + close.c + flags.c + mount.c + mkdir.c + open.c + stat.c + read.c + register.c + search.c + write.c +) diff --git a/src/fs/rootfs/src/close.c b/src/fs/rootfs/src/close.c new file mode 100644 index 0000000..1704e48 --- /dev/null +++ b/src/fs/rootfs/src/close.c @@ -0,0 +1,25 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <rootfs.h> +#include <fs/fs.h> + +int rootfs_close(struct fs_fd *const fd) +{ + return -1; +} diff --git a/src/fs/rootfs/src/flags.c b/src/fs/rootfs/src/flags.c new file mode 100644 index 0000000..5a0d5a6 --- /dev/null +++ b/src/fs/rootfs/src/flags.c @@ -0,0 +1,26 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <rootfs.h> +#include <rootfs/ops.h> +#include <fs/inode.h> + +int rootfs_flags(const union inode_result *const i) +{ + return i->memi->flags; +} diff --git a/src/fs/rootfs/src/mkdir.c b/src/fs/rootfs/src/mkdir.c new file mode 100644 index 0000000..5f4118f --- /dev/null +++ b/src/fs/rootfs/src/mkdir.c @@ -0,0 +1,30 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <rootfs.h> +#include <rootfs/ops.h> +#include <rootfs/types.h> +#include <fs/fs.h> +#include <fs/inode.h> +#include <ramfs.h> + +int rootfs_mkdir(const struct fs_mkdir *const m, const struct fs_mp *const mp, + const union inode_result *const inode, struct fs_ret *const r) +{ + return ramfs_mkdir(m, mp->prv->rfs, inode, r); +} diff --git a/src/fs/rootfs/src/mount.c b/src/fs/rootfs/src/mount.c new file mode 100644 index 0000000..4ef6258 --- /dev/null +++ b/src/fs/rootfs/src/mount.c @@ -0,0 +1,42 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <rootfs.h> +#include <rootfs/types.h> +#include <ramfs.h> +#include <fs/fs.h> +#include <stdlib.h> + +int rootfs_mount(const struct fs_mount *const m, struct fs_ret *const r) +{ + struct ramfs *rfs = NULL; + struct fs_mp_prv *const p = malloc(sizeof *p); + + if (!p + || !(rfs = ramfs_mount(m, r)) + || fs_mountpoint(m->src, m->tgt, &rootfs, NULL, p)) + goto failure; + + *p = (const struct fs_mp_prv){.rfs = rfs}; + return 0; + +failure: + ramfs_free(rfs); + free(p); + return -1; +} diff --git a/src/fs/rootfs/src/open.c b/src/fs/rootfs/src/open.c new file mode 100644 index 0000000..4a88f41 --- /dev/null +++ b/src/fs/rootfs/src/open.c @@ -0,0 +1,27 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <rootfs.h> +#include <rootfs/ops.h> +#include <fs/fs.h> + +int rootfs_open(const struct fs_open *const o, const struct fs_mp *const mp, + const union inode_result *const i, struct fs_ret *const r) +{ + return -1; +} diff --git a/src/fs/rootfs/src/read.c b/src/fs/rootfs/src/read.c new file mode 100644 index 0000000..50fe963 --- /dev/null +++ b/src/fs/rootfs/src/read.c @@ -0,0 +1,26 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <rootfs.h> +#include <fs/fs.h> +#include <stddef.h> + +int rootfs_read(const struct fs_read *const r, struct fs_ret *const ret) +{ + return -1; +} diff --git a/src/fs/rootfs/src/register.c b/src/fs/rootfs/src/register.c new file mode 100644 index 0000000..02032d8 --- /dev/null +++ b/src/fs/rootfs/src/register.c @@ -0,0 +1,43 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <rootfs.h> +#include <rootfs/ops.h> +#include <fs/fs.h> + +const struct fs rootfs = +{ + .type = "rootfs", + .mount = rootfs_mount, + .stat = rootfs_stat, + .mkdir = rootfs_mkdir, + .open = rootfs_open, + .read = rootfs_read, + .write = rootfs_write, + .close = rootfs_close, + .iops = + { + .search = rootfs_search, + .flags = rootfs_flags + } +}; + +int rootfs_register(void) +{ + return fs_register(&rootfs); +} diff --git a/src/fs/rootfs/src/search.c b/src/fs/rootfs/src/search.c new file mode 100644 index 0000000..269f5d0 --- /dev/null +++ b/src/fs/rootfs/src/search.c @@ -0,0 +1,29 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <rootfs.h> +#include <rootfs/ops.h> +#include <rootfs/types.h> +#include <fs/fs.h> +#include <ramfs.h> + +int rootfs_search(const char *const path, const struct fs_mp *const mp, + union inode_result *const inode, struct fs_ret *const r) +{ + return ramfs_search(path, mp->prv->rfs, inode, r); +} diff --git a/src/fs/rootfs/src/stat.c b/src/fs/rootfs/src/stat.c new file mode 100644 index 0000000..1561190 --- /dev/null +++ b/src/fs/rootfs/src/stat.c @@ -0,0 +1,28 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <rootfs.h> +#include <rootfs/ops.h> +#include <fs/fs.h> +#include <sys/stat.h> + +int rootfs_stat(const struct fs_stat *const s, const struct fs_mp *const mp, + const union inode_result *const inode, struct fs_ret *const r) +{ + return -1; +} diff --git a/src/fs/rootfs/src/write.c b/src/fs/rootfs/src/write.c new file mode 100644 index 0000000..874e40c --- /dev/null +++ b/src/fs/rootfs/src/write.c @@ -0,0 +1,26 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <rootfs.h> +#include <fs/fs.h> +#include <stddef.h> + +int rootfs_write(const struct fs_write *const w, struct fs_ret *const r) +{ + return -1; +} diff --git a/src/fs/src/CMakeLists.txt b/src/fs/src/CMakeLists.txt new file mode 100644 index 0000000..efeb0f1 --- /dev/null +++ b/src/fs/src/CMakeLists.txt @@ -0,0 +1,30 @@ +# wnix, a Unix-like operating system for WebAssembly applications. +# Copyright (C) 2025 Xavier Del Campo Romero +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +target_sources(fs PRIVATE + from_type.c + headtail.c + mountpoint.c + mp_from_path.c + mps_from_path.c + parent.c + register.c + relpath.c + update.c + next.c +) + +add_subdirectory(inode) diff --git a/src/fs/src/from_type.c b/src/fs/src/from_type.c new file mode 100644 index 0000000..360384b --- /dev/null +++ b/src/fs/src/from_type.c @@ -0,0 +1,36 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <fs/fs.h> +#include <fs/types.h> +#include <stddef.h> +#include <stdlib.h> +#include <string.h> + +const struct fs *fs_from_type(const char *const type) +{ + for (const struct fs_register *r = fs_rhead; r; r = r->next) + { + const struct fs *const fs = r->fs; + + if (!strcmp(type, fs->type)) + return fs; + } + + return NULL; +} diff --git a/src/fs/src/headtail.c b/src/fs/src/headtail.c new file mode 100644 index 0000000..9df580f --- /dev/null +++ b/src/fs/src/headtail.c @@ -0,0 +1,22 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <fs/types.h> + +struct fs_register *fs_rhead, *fs_rtail; +struct fs_mountpoint *fs_mphead, *fs_mptail; diff --git a/src/fs/src/inode/CMakeLists.txt b/src/fs/src/inode/CMakeLists.txt new file mode 100644 index 0000000..350dd71 --- /dev/null +++ b/src/fs/src/inode/CMakeLists.txt @@ -0,0 +1,20 @@ +# wnix, a Unix-like operating system for WebAssembly applications. +# Copyright (C) 2025 Xavier Del Campo Romero +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. + +target_sources(fs PRIVATE + free.c + search.c +) diff --git a/src/fs/src/inode/free.c b/src/fs/src/inode/free.c new file mode 100644 index 0000000..3eca47d --- /dev/null +++ b/src/fs/src/inode/free.c @@ -0,0 +1,34 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <fs/inode.h> +#include <stdlib.h> + +void inode_free(struct inode *const i) +{ + if (i) + { + inode_free(i->child); + free(i->name); + + if (i->free) + i->free(i->prv); + } + + free(i); +} diff --git a/src/fs/src/inode/search.c b/src/fs/src/inode/search.c new file mode 100644 index 0000000..4715dd2 --- /dev/null +++ b/src/fs/src/inode/search.c @@ -0,0 +1,121 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <fs/inode.h> +#include <fs/fs.h> +#include <fs/types.h> +#include <state.h> +#include <stddef.h> +#include <stdlib.h> +#include <string.h> + +struct search +{ + struct fs_mp *mp; + size_t n, mp_i; + struct fs_ret *r; + union inode_result inode; + struct inode_search search; +}; + +static void free_search(struct search *const s) +{ + free(s); +} + +static enum state search_mp(struct search *const s) +{ + const struct inode_search *const is = &s->search; + const struct fs_mp *const cur = &s->mp[s->mp_i], + *const next = s->mp_i + 1 < s->n ? + &s->mp[s->mp_i + 1] : NULL; + const struct fs *const fs = cur->fs; + const char *relpath = next ? next->tgt : is->path; + + for (size_t i = 0; i < s->mp_i; i++) + relpath += strlen(s->mp[s->mp_i].tgt); + + if (fs && fs->iops.search(relpath, cur, &s->inode, s->r)) + return STATE_FATAL; + + s->mp_i++; + return STATE_AGAIN; +} + +static enum state search(void *const args) +{ + struct search *const s = args; + const struct inode_search *const is = &s->search; + + if (!s->mp) + { + const int ret = is->done(STATE_FATAL, NULL, NULL, NULL, is->args); + + free_search(s); + return ret ? STATE_FATAL : STATE_AGAIN; + } + else if (s->mp_i < s->n) + return search_mp(s); + else + { + const char *relpath = is->path; + + for (size_t i = 0; i < s->n; i++) + relpath += strlen(s->mp[i].tgt); + + const int ret = is->done(STATE_OK, relpath, &s->mp[s->n - 1], + &s->inode, is->args); + + free_search(s); + return ret ? STATE_FATAL : STATE_AGAIN; + } + + return STATE_FATAL; +} + +int inode_search(const struct inode_search *const is, struct fs_ret *const r) +{ + struct search *s = NULL; + size_t n; + /* TODO: do not check for errors yet. */ + struct fs_mp *const mp = fs_mps_from_path(is->path, &n); + + if (!(s = malloc(sizeof *s))) + goto failure; + + *s = (const struct search) + { + .search = *is, + .mp = mp, + .n = n, + .r = r + }; + + *r = (const struct fs_ret) + { + .f = search, + .args = s + }; + + return 0; + +failure: + free(s); + free(mp); + return -1; +} diff --git a/src/fs/src/mountpoint.c b/src/fs/src/mountpoint.c new file mode 100644 index 0000000..32b6ea9 --- /dev/null +++ b/src/fs/src/mountpoint.c @@ -0,0 +1,60 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <fs/fs.h> +#include <fs/types.h> +#include <stdlib.h> +#include <string.h> + +int fs_mountpoint(const char *const src, const char *const tgt, + const struct fs *const fs, const fs_update_fn fn, + struct fs_mp_prv *const pr) +{ + struct fs_mountpoint *m = NULL; + char *srcdup = NULL, *const tgtdup = strdup(tgt); + + if (!tgtdup) + goto failure; + else if (src && !(srcdup = strdup(src))) + goto failure; + else if (!(m = malloc(sizeof *m))) + goto failure; + + *m = (const struct fs_mountpoint) + { + .src = srcdup, + .tgt = tgtdup, + .update = fn, + .fs = fs, + .prv = pr + }; + + if (!fs_mphead) + fs_mphead = m; + else if (fs_mptail) + fs_mptail->next = m; + + fs_mptail = m; + return 0; + +failure: + free(srcdup); + free(tgtdup); + free(m); + return -1; +} diff --git a/src/fs/src/mp_from_path.c b/src/fs/src/mp_from_path.c new file mode 100644 index 0000000..4a963d3 --- /dev/null +++ b/src/fs/src/mp_from_path.c @@ -0,0 +1,40 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <fs/fs.h> +#include <fs/types.h> +#include <string.h> + +int fs_mp_from_path(const char *const path, struct fs_mp *const mp) +{ + for (const struct fs_mountpoint *p = fs_mphead; p; p = p->next) + if (!strcmp(path, p->tgt)) + { + *mp = (const struct fs_mp) + { + .fs = p->fs, + .prv = p->prv, + .src = p->src, + .tgt= p->tgt + }; + + return 0; + } + + return -1; +} diff --git a/src/fs/src/mps_from_path.c b/src/fs/src/mps_from_path.c new file mode 100644 index 0000000..4825ccc --- /dev/null +++ b/src/fs/src/mps_from_path.c @@ -0,0 +1,89 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <fs/fs.h> +#include <fs/types.h> +#include <stddef.h> +#include <stdlib.h> +#include <string.h> + +static struct fs_mp *find(const char *const path, size_t *const out) +{ + struct fs_mp *ret = NULL; + size_t n = 0; + + for (const struct fs_mountpoint *p = fs_mphead; p; p = p->next) + { + const size_t len = strlen(p->tgt); + + if (!strncmp(p->tgt, path, len)) + { + const size_t sz = n + 1; + struct fs_mp *const m = realloc(ret, sz * sizeof *m); + + if (!m) + goto failure; + + m[n++] = (const struct fs_mp) + { + .fs = p->fs, + .prv = p->prv, + .src = p->src, + .tgt = p->tgt + }; + + ret = m; + } + } + + *out = n; + return ret; + +failure: + free(ret); + return NULL; +} + +static void sort(struct fs_mp *const m, const size_t n) +{ + for (size_t i = 1; i < n; i++) + { + struct fs_mp am = m[i]; + const size_t len = strlen(am.tgt); + size_t j = i; + + while (j && len < strlen(m[j - 1].tgt)) + { + m[j] = m [j - 1]; + j--; + } + + m[j] = am; + } +} + +struct fs_mp *fs_mps_from_path(const char *const path, size_t *const n) +{ + struct fs_mp *const ret = find(path, n); + + if (!ret) + return NULL; + + sort(ret, *n); + return ret; +} diff --git a/src/fs/src/next.c b/src/fs/src/next.c new file mode 100644 index 0000000..62fdd80 --- /dev/null +++ b/src/fs/src/next.c @@ -0,0 +1,62 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <fs/fs.h> +#include <stddef.h> +#include <stdlib.h> +#include <string.h> + +int fs_next(const char *const path, char **const out, size_t *const i) +{ + char *next = NULL; + + if (*i >= strlen(path)) + { + *out = NULL; + return 0; + } + + const char *const s = &path[*i]; + + if (*s == '/') + { + if (!(next = strdup("/"))) + goto failure; + + (*i)++; + *out = next; + return 0; + } + + const char *sep = strchr(s, '/'); + next = sep ? strndup(s, sep - s) : strdup(s); + + if (!next) + goto failure; + else if (sep) + while (*sep && *sep == '/') + sep++; + + *i += sep ? sep - s : strlen(s); + *out = next; + return 0; + +failure: + free(next); + return -1; +} diff --git a/src/fs/src/parent.c b/src/fs/src/parent.c new file mode 100644 index 0000000..9cf51f6 --- /dev/null +++ b/src/fs/src/parent.c @@ -0,0 +1,33 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <fs/fs.h> +#include <stddef.h> +#include <stdlib.h> +#include <string.h> + +char *fs_parent(const char *const path) +{ + char *ret = NULL; + const char *const sep = strrchr(path, '/'); + + if (!sep || !(ret = strndup(path, sep - path + 1))) + return NULL; + + return ret; +} diff --git a/src/fs/src/register.c b/src/fs/src/register.c new file mode 100644 index 0000000..d28e47a --- /dev/null +++ b/src/fs/src/register.c @@ -0,0 +1,37 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <fs/fs.h> +#include <fs/types.h> +#include <stdlib.h> + +int fs_register(const struct fs *const fs) +{ + struct fs_register *const r = malloc(sizeof *r); + + if (!r) + return -1; + else if (!fs_rhead) + fs_rhead = r; + else if (fs_rtail) + fs_rtail->next = r; + + fs_rtail = r; + *r = (const struct fs_register){.fs = fs}; + return 0; +} diff --git a/src/fs/src/relpath.c b/src/fs/src/relpath.c new file mode 100644 index 0000000..565ea53 --- /dev/null +++ b/src/fs/src/relpath.c @@ -0,0 +1,43 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <fs/fs.h> +#include <fs/types.h> +#include <string.h> + +const char *fs_relpath(const char *const path) +{ + size_t lastn = 0; + const struct fs_mountpoint *mp = NULL; + + for (const struct fs_mountpoint *m = fs_mphead; m; m = m->next) + { + const size_t n = strlen(m->tgt); + + if (!strncmp(m->tgt, path, n) && lastn < n) + { + lastn = n; + mp = m; + } + } + + if (!mp) + return NULL; + + return path + strlen(mp->tgt); +} diff --git a/src/fs/src/update.c b/src/fs/src/update.c new file mode 100644 index 0000000..1e6d9ea --- /dev/null +++ b/src/fs/src/update.c @@ -0,0 +1,30 @@ +/* + * wnix, a Unix-like operating system for WebAssembly applications. + * Copyright (C) 2025 Xavier Del Campo Romero + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include <fs/fs.h> +#include <fs/types.h> +#include <stddef.h> + +int fs_update(void) +{ + for (const struct fs_mountpoint *m = fs_mphead; m; m = m->next) + if (m->update && m->update(m->prv)) + return -1; + + return 0; +} |
