Nucleus
Barry FS Object wrapper functions 77a8df8 (3 years, 3 months ago)
/*
* This file implements the File object and associated functions. A file
* represents an opened file, and holds a reference the the underlying inode.
* Most of the functions here are just wrappers around the file operations.
* They perform the necessary validation to ensure the operation can be called
* on the particular file. If no operation is found, they may implement a
* generic version.
*/
#include <errno.h>
#include <nucleus/object.h>
#include <nucleus/vfs.h>
static void file_delete(Object *);
/* File object type */
ObjectType fileType = {
.name = "FILE",
.size = sizeof(File),
.delete = file_delete,
};
/* Destroy a file */
static void
file_delete(Object *obj)
{
File *file = (void *) obj;
if (file->inode)
put(file->inode);
if (file->path)
destroy_list(file->path);
}
/* Open a file */
int
file_open(File *file)
{
if (!file->ops || !file->ops->open)
return -EINVAL;
return file->ops->open(file);
}
/* Read a file */
size_t
file_read(File *file, char *buf, size_t size)
{
size_t count = 0;
if (!file->ops || !file->ops->read)
return count;
count = file->ops->read(file, buf, size, file->pos);
file->pos += count;
return count;
}
/* Write a file */
size_t
file_write(File *file, char *buf, size_t size)
{
size_t count = 0;
if (!file->ops || !file->ops->write)
return count;
count = file->ops->write(file, buf, size, file->pos);
file->pos += count;
return count;
}