/* * This file implements the stat system call and similar system calls. */ #include #include #include #include #include /* 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; }