diff options
Diffstat (limited to 'src/fs/devfs')
| -rw-r--r-- | src/fs/devfs/CMakeLists.txt | 20 | ||||
| -rw-r--r-- | src/fs/devfs/include/devfs.h | 24 | ||||
| -rw-r--r-- | src/fs/devfs/private_include/devfs/ops.h | 48 | ||||
| -rw-r--r-- | src/fs/devfs/private_include/devfs/types.h | 44 | ||||
| -rw-r--r-- | src/fs/devfs/src/CMakeLists.txt | 34 | ||||
| -rw-r--r-- | src/fs/devfs/src/close.c | 26 | ||||
| -rw-r--r-- | src/fs/devfs/src/flags.c | 25 | ||||
| -rw-r--r-- | src/fs/devfs/src/mkdir.c | 28 | ||||
| -rw-r--r-- | src/fs/devfs/src/mknod.c | 54 | ||||
| -rw-r--r-- | src/fs/devfs/src/mount.c | 59 | ||||
| -rw-r--r-- | src/fs/devfs/src/open.c | 57 | ||||
| -rw-r--r-- | src/fs/devfs/src/ops.c | 32 | ||||
| -rw-r--r-- | src/fs/devfs/src/read.c | 95 | ||||
| -rw-r--r-- | src/fs/devfs/src/register.c | 44 | ||||
| -rw-r--r-- | src/fs/devfs/src/search.c | 29 | ||||
| -rw-r--r-- | src/fs/devfs/src/seek.c | 98 | ||||
| -rw-r--r-- | src/fs/devfs/src/stat.c | 30 | ||||
| -rw-r--r-- | src/fs/devfs/src/status.c | 30 | ||||
| -rw-r--r-- | src/fs/devfs/src/unlink.c | 26 | ||||
| -rw-r--r-- | src/fs/devfs/src/update.c | 27 | ||||
| -rw-r--r-- | src/fs/devfs/src/write.c | 95 |
21 files changed, 925 insertions, 0 deletions
diff --git a/src/fs/devfs/CMakeLists.txt b/src/fs/devfs/CMakeLists.txt new file mode 100644 index 0000000..7305bdf --- /dev/null +++ b/src/fs/devfs/CMakeLists.txt @@ -0,0 +1,20 @@ +# wanix, 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..d9634fc --- /dev/null +++ b/src/fs/devfs/include/devfs.h @@ -0,0 +1,24 @@ +/* + * wanix, 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..f7c4e5b --- /dev/null +++ b/src/fs/devfs/private_include/devfs/ops.h @@ -0,0 +1,48 @@ +/* + * wanix, 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 <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_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(const struct fs_close *c, struct fs_ret *r); +int devfs_seek(const struct fs_seek *s, struct fs_ret *r); +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, 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..698ccac --- /dev/null +++ b/src/fs/devfs/private_include/devfs/types.h @@ -0,0 +1,44 @@ +/* + * wanix, 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..7db8c9f --- /dev/null +++ b/src/fs/devfs/src/CMakeLists.txt @@ -0,0 +1,34 @@ +# wanix, 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 + register.c + search.c + seek.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..0277b96 --- /dev/null +++ b/src/fs/devfs/src/close.c @@ -0,0 +1,26 @@ +/* + * wanix, 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(const struct fs_close *const c, struct fs_ret *const r) +{ + return -1; +} diff --git a/src/fs/devfs/src/flags.c b/src/fs/devfs/src/flags.c new file mode 100644 index 0000000..b99bb8c --- /dev/null +++ b/src/fs/devfs/src/flags.c @@ -0,0 +1,25 @@ +/* + * wanix, 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..9aa74b3 --- /dev/null +++ b/src/fs/devfs/src/mkdir.c @@ -0,0 +1,28 @@ +/* + * wanix, 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..8d13075 --- /dev/null +++ b/src/fs/devfs/src/mknod.c @@ -0,0 +1,54 @@ +/* + * wanix, 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..cff4779 --- /dev/null +++ b/src/fs/devfs/src/mount.c @@ -0,0 +1,59 @@ +/* + * wanix, 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..b9bd3f7 --- /dev/null +++ b/src/fs/devfs/src/open.c @@ -0,0 +1,57 @@ +/* + * wanix, 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..1cd4a15 --- /dev/null +++ b/src/fs/devfs/src/ops.c @@ -0,0 +1,32 @@ +/* + * wanix, 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..0bc19f0 --- /dev/null +++ b/src/fs/devfs/src/read.c @@ -0,0 +1,95 @@ +/* + * wanix, 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; + 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->pr = re->r; + free(re); + return STATE_AGAIN; +} + +int devfs_read(const struct fs_read *const fr, struct fs_ret *const r) +{ + 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); + struct read *const re = malloc(sizeof *re); + + if (!re) + goto failure; + else if (!o) + { + errno = ENOENT; + goto failure; + } + + *re = (const struct read) + { + .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, &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/register.c b/src/fs/devfs/src/register.c new file mode 100644 index 0000000..46dcdb6 --- /dev/null +++ b/src/fs/devfs/src/register.c @@ -0,0 +1,44 @@ +/* + * wanix, 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, + .write = devfs_write, + .close = devfs_close, + .seek = devfs_seek, + .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..22a4e92 --- /dev/null +++ b/src/fs/devfs/src/search.c @@ -0,0 +1,29 @@ +/* + * wanix, 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/seek.c b/src/fs/devfs/src/seek.c new file mode 100644 index 0000000..d955e70 --- /dev/null +++ b/src/fs/devfs/src/seek.c @@ -0,0 +1,98 @@ +/* + * wanix, 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 <stdlib.h> + +struct seek +{ + bool done; + struct fs_fd *fd; + struct fs_ret *pr, r; +}; + +static int done(int error, void *const args) +{ + struct seek *const s = args; + struct fs_fd *const fd = s->fd; + + if (error) + fd->error = error; + + s->done = true; + return 0; +} + +static enum state wait(void *const args) +{ + struct seek *const s = args; + + if (!s->done) + return STATE_AGAIN; + + *s->pr = s->r; + free(s); + return STATE_AGAIN; +} + +int devfs_seek(const struct fs_seek *const fs, struct fs_ret *const r) +{ + struct fs_fd *const fd = fs->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 seek *const s = malloc(sizeof *s); + + if (!s) + goto failure; + else if (!o) + { + errno = ENOENT; + goto failure; + } + + *s = (const struct seek) + { + .fd = fd, + .pr = r, + .r = *r + }; + + const struct drv_event_done d = + { + .f = done, + .args = s + }; + + const struct drv_event_ops *const ops = &o->ops; + + if (ops->seek(fs->offset, &d, fd)) + goto failure; + + *r = (const struct fs_ret){.f = wait, .args = s}; + return 0; + +failure: + free(s); + return -1; +} diff --git a/src/fs/devfs/src/stat.c b/src/fs/devfs/src/stat.c new file mode 100644 index 0000000..dff4d3c --- /dev/null +++ b/src/fs/devfs/src/stat.c @@ -0,0 +1,30 @@ +/* + * wanix, 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..8a4d1e1 --- /dev/null +++ b/src/fs/devfs/src/status.c @@ -0,0 +1,30 @@ +/* + * wanix, 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 <stdbool.h> + +int devfs_status(const char *const node, const struct drv_event_ops *const ops, + const bool available, void *const args) +{ + struct fs_mp_prv *const mp = args; + + return available ? devfs_mknod(node, 0755, 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..d03fd68 --- /dev/null +++ b/src/fs/devfs/src/unlink.c @@ -0,0 +1,26 @@ +/* + * wanix, 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..2a0a879 --- /dev/null +++ b/src/fs/devfs/src/update.c @@ -0,0 +1,27 @@ +/* + * wanix, 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..759751e --- /dev/null +++ b/src/fs/devfs/src/write.c @@ -0,0 +1,95 @@ +/* + * wanix, 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; +} |
