Store the adhoc stat info in the fd

This commit is contained in:
Theodore Dubois
2018-10-08 11:43:54 -07:00
parent 6cfbe6a15f
commit e170446cc4
8 changed files with 20 additions and 21 deletions
+5 -6
View File
@@ -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;
+4 -3
View File
@@ -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,
+3 -7
View File
@@ -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);
};
+1 -1
View File
@@ -21,7 +21,7 @@ int path_normalize(struct fd *at, const char *path, char *out, bool follow_links
unlock(&current->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));
+1 -1
View File
@@ -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,
};
+1 -1
View File
@@ -261,7 +261,7 @@ dword_t sys_getcwd(addr_t buf_addr, dword_t size) {
lock(&current->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(&current->fs->lock);
if (err < 0)
return err;
+3
View File
@@ -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);
+2 -2
View File
@@ -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;