diff --git a/fs/adhoc.c b/fs/adhoc.c index a4fe9486..31e8ed5e 100644 --- a/fs/adhoc.c +++ b/fs/adhoc.c @@ -11,26 +11,25 @@ struct fd *adhoc_fd_create() { if (fd == NULL) return NULL; fd->mount = &adhoc_mount; - fd->stat = malloc(sizeof(struct statbuf)); - *fd->stat = (struct statbuf) {}; + fd->stat = (struct statbuf) {}; return fd; } static int adhoc_fstat(struct fd *fd, struct statbuf *stat) { - *stat = *fd->stat; + *stat = fd->stat; return 0; } static int adhoc_fsetattr(struct fd *fd, struct attr attr) { switch (attr.type) { case attr_uid: - fd->stat->uid = attr.uid; + fd->stat.uid = attr.uid; break; case attr_gid: - fd->stat->gid = attr.gid; + fd->stat.gid = attr.gid; break; case attr_mode: - fd->stat->mode = (fd->stat->mode & S_IFMT) | (attr.mode & ~S_IFMT); + fd->stat.mode = (fd->stat.mode & S_IFMT) | (attr.mode & ~S_IFMT); break; case attr_size: return _EINVAL; diff --git a/fs/fake.c b/fs/fake.c index d5794e25..515b8fea 100644 --- a/fs/fake.c +++ b/fs/fake.c @@ -272,7 +272,7 @@ static int fakefs_stat(struct mount *mount, const char *path, struct statbuf *fa static int fakefs_fstat(struct fd *fd, struct statbuf *fake_stat) { // this is truly sad, but there is no alternative char path[MAX_PATH]; - int err = fd->ops->getpath(fd, path); + int err = fd->mount->fs->getpath(fd, path); if (err < 0) return err; return fakefs_stat(fd->mount, path, fake_stat, false); @@ -306,7 +306,7 @@ static int fakefs_setattr(struct mount *mount, const char *path, struct attr att static int fakefs_fsetattr(struct fd *fd, struct attr attr) { char path[MAX_PATH]; - int err = fd->ops->getpath(fd, path); + int err = fd->mount->fs->getpath(fd, path); if (err < 0) return err; return fakefs_setattr(fd->mount, path, attr); @@ -423,12 +423,13 @@ const struct fs_ops fakefs = { .unlink = fakefs_unlink, .rename = fakefs_rename, .symlink = fakefs_symlink, - + .stat = fakefs_stat, .fstat = fakefs_fstat, .flock = realfs_flock, .setattr = fakefs_setattr, .fsetattr = fakefs_fsetattr, + .getpath = realfs_getpath, .utime = realfs_utime, .mkdir = fakefs_mkdir, diff --git a/fs/fd.h b/fs/fd.h index 93b272c1..214f7b3d 100644 --- a/fs/fd.h +++ b/fs/fd.h @@ -5,6 +5,7 @@ #include "emu/memory.h" #include "util/list.h" #include "util/bits.h" +#include "fs/stat.h" struct fd { atomic_uint refcount; @@ -27,10 +28,8 @@ struct fd { // fs/inode data struct mount *mount; - union { - int real_fd; - struct statbuf *stat; - }; + int real_fd; + struct statbuf stat; // for adhoc fs lock_t lock; }; @@ -70,9 +69,6 @@ struct fd_ops { // if ioctl_size returns non-zero, arg must point to ioctl_size valid bytes int (*ioctl)(struct fd *fd, int cmd, void *arg); - // Returns the path of the file descriptor, buf must be at least MAX_PATH - int (*getpath)(struct fd *fd, char *buf); - int (*fsync)(struct fd *fd); int (*close)(struct fd *fd); }; diff --git a/fs/path.c b/fs/path.c index db9236f2..a8dfc56b 100644 --- a/fs/path.c +++ b/fs/path.c @@ -21,7 +21,7 @@ int path_normalize(struct fd *at, const char *path, char *out, bool follow_links unlock(¤t->fs->lock); if (at != NULL) { char at_path[MAX_PATH]; - int err = at->ops->getpath(at, at_path); + int err = at->mount->fs->getpath(at, at_path); if (err < 0) return err; assert(path_is_normalized(at_path)); diff --git a/fs/real.c b/fs/real.c index ad3da0fc..f92c1ac0 100644 --- a/fs/real.c +++ b/fs/real.c @@ -337,6 +337,7 @@ const struct fs_ops realfs = { .setattr = realfs_setattr, .fsetattr = realfs_fsetattr, .utime = realfs_utime, + .getpath = realfs_getpath, .flock = realfs_flock, .mkdir = realfs_mkdir, @@ -348,7 +349,6 @@ const struct fd_ops realfs_fdops = { .readdir = realfs_readdir, .lseek = realfs_lseek, .mmap = realfs_mmap, - .getpath = realfs_getpath, .fsync = realfs_fsync, .close = realfs_close, }; diff --git a/kernel/fs.c b/kernel/fs.c index 766bcd7d..27a9323c 100644 --- a/kernel/fs.c +++ b/kernel/fs.c @@ -261,7 +261,7 @@ dword_t sys_getcwd(addr_t buf_addr, dword_t size) { lock(¤t->fs->lock); struct fd *wd = current->fs->pwd; char pwd[MAX_PATH]; - int err = wd->ops->getpath(wd, pwd); + int err = wd->mount->fs->getpath(wd, pwd); unlock(¤t->fs->lock); if (err < 0) return err; diff --git a/kernel/fs.h b/kernel/fs.h index 8e23968f..bb6cdee8 100644 --- a/kernel/fs.h +++ b/kernel/fs.h @@ -99,6 +99,8 @@ struct fs_ops { int (*setattr)(struct mount *mount, const char *path, struct attr attr); int (*fsetattr)(struct fd *fd, struct attr attr); int (*utime)(struct mount *mount, const char *path, struct timespec atime, struct timespec mtime); + // Returns the path of the file descriptor, buf must be at least MAX_PATH + int (*getpath)(struct fd *fd, char *buf); int (*mkdir)(struct mount *mount, const char *path, mode_t_ mode); @@ -119,6 +121,7 @@ int realfs_utime(struct mount *mount, const char *path, struct timespec atime, s int realfs_statfs(struct mount *mount, struct statfsbuf *stat); int realfs_flock(struct fd *fd, int operation); +int realfs_getpath(struct fd *fd, char *buf); ssize_t realfs_read(struct fd *fd, void *buf, size_t bufsize); ssize_t realfs_write(struct fd *fd, const void *buf, size_t bufsize); int realfs_close(struct fd *fd); diff --git a/kernel/init.c b/kernel/init.c index ee58cebd..7631d88c 100644 --- a/kernel/init.c +++ b/kernel/init.c @@ -68,8 +68,8 @@ int create_stdio(struct tty_driver driver) { // FIXME use generic_open (or something) to avoid this mess struct fd *fd = adhoc_fd_create(); - fd->stat->rdev = dev_make(4, 0); - fd->stat->mode = S_IFCHR | S_IRUSR; + fd->stat.rdev = dev_make(4, 0); + fd->stat.mode = S_IFCHR | S_IRUSR; int err = dev_open(4, 0, DEV_CHAR, fd); if (err < 0) return err;