Nucleus
Barry Directory listing in VFS 2d05318 (3 years, 2 months ago)
diff --git a/vfs/stat.c b/vfs/stat.c
new file mode 100644
index 0000000..037a77f
--- /dev/null
+++ b/vfs/stat.c
@@ -0,0 +1,60 @@
+/*
+ * This file implements the stat system call and similar system calls.
+ */
+
+#include <string.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <nucleus/memory.h>
+#include <nucleus/vfs.h>
+
+/* Get file status */
+int
+stat(const char *pathname, struct stat *statbuf)
+{
+ if (!pathname)
+ return -EFAULT;
+ if (!verify_access(pathname, strnlen(pathname, PATH_MAX), PROT_READ))
+ return -EFAULT;
+ Inode *inode = lookup(pathname, NULL);
+ if (!inode)
+ return -ENOENT;
+ if (!statbuf)
+ goto end;
+ if (!verify_access(statbuf, sizeof(struct stat), PROT_WRITE))
+ return -EFAULT;
+ statbuf->st_dev = inode->dev;
+ statbuf->st_ino = inode->ino;
+ statbuf->st_mode = inode->mode;
+ statbuf->st_nlink = inode->nlink;
+ statbuf->st_uid = inode->uid;
+ statbuf->st_gid = inode->gid;
+ statbuf->st_size = inode->size;
+end:
+ put(inode);
+ return 0;
+}
+
+/* Get file status by file descriptor */
+int
+fstat(int fd, struct stat *statbuf)
+{
+ File *file = get_file_by_fd(fd);
+ if (!file)
+ return -EBADF;
+ Inode *inode = get(file->inode);
+ if (!statbuf)
+ goto end;
+ if (!verify_access(statbuf, sizeof(struct stat), PROT_WRITE))
+ return -EFAULT;
+ statbuf->st_dev = inode->dev;
+ statbuf->st_ino = inode->ino;
+ statbuf->st_mode = inode->mode;
+ statbuf->st_nlink = inode->nlink;
+ statbuf->st_uid = inode->uid;
+ statbuf->st_gid = inode->gid;
+ statbuf->st_size = inode->size;
+end:
+ put(inode);
+ return 0;
+}