BarryServer : Git

All the code for all my projects
// BarryServer : Git / Nucleus / blob / master / vfs / stat.c

// Related

Nucleus

Barry System headers (remove libc dependency) 18495cf (3 years, 2 months ago)
/*
 * This file implements the stat system call and similar system calls.
 */

#include <sys/errno.h>
#include <sys/stat.h>
#include <nucleus/lib.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;
}