The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
file.c File Reference
#include "file.h"
#include "kernel/errno.h"
#include "kernel/vfs/poll.h"
#include "kernel/device/device.h"
#include "kernel/fs/devfs.h"
#include "kernel/vfs/flock.h"
#include "kernel/vfs/fifo.h"
#include "kernel/memory/heap.h"
#include "kernel/sync/spinlock.h"
#include "kernel/assert.h"
#include "common/memory.h"
#include "common/string.h"

Functions

file_t * file_new (const file_ops_t *ops, uint32_t flags)
 a new description of another kind (a device stream), with one reference.
void file_ref (file_t *file)
 add a reference to a description.
void file_unref (file_t *file)
 drop a reference; the last one releases the description.
bool file_readable (const file_t *file)
 true if the description was opened for reading (or writing).
bool file_writable (const file_t *file)
int32_t file_read (file_t *file, void *buf, uint32_t count)
 the operations, with an errno for the unsupported ones: read and write EBADF (wrong access mode, or no op), seek ESPIPE, ioctl ENOTTY, mmap ENODEV, read_entry ENOTDIR, truncate EINVAL.
int32_t file_write (file_t *file, const void *buf, uint32_t count)
int64_t file_seek (file_t *file, int64_t offset, int whence)
int32_t file_ioctl (file_t *file, uint32_t request, void *arg)
int32_t file_mmap (file_t *file, addr_space_t as, uintptr_t virt, size_t length, int prot, uint64_t offset)
int32_t file_read_entry (file_t *file, fs_dirent_t *entry)
int32_t file_unread_entry (file_t *file, const fs_dirent_t *entry)
 give back the entry file_read_entry returned last, so the next call returns it again (getdents, when it does not fit the caller's buffer).
int32_t file_truncate (file_t *file, uint64_t length)
uint32_t file_poll (file_t *file)
 the readiness of a file (kernel/vfs/poll.h).
int32_t file_stat (file_t *file, struct kstat *st)
int32_t file_pread (file_t *file, void *buf, uint32_t count, int64_t offset)
 read or write at an offset without moving the description's offset (pread, pwrite).
int32_t file_pwrite (file_t *file, const void *buf, uint32_t count, int64_t offset)
bool file_is_vfs (const file_t *file)
 true if the description is a filesystem file (not a pipe or the console).
kerr_t file_open (const char *path, uint32_t flags, file_t **out)
 open a file or directory through the VFS.
file_t * file_new_fifo_end (uint32_t flags)
 a new end of a named pipe, not yet joined to one (kernel/vfs/fifo.c joins it).
kerr_t file_open_pipe (file_t **read_end, file_t **write_end)
 make the two ends of a new pipe.

Function Documentation

◆ file_new()

file_t * file_new ( const file_ops_t * ops,
uint32_t flags )

a new description of another kind (a device stream), with one reference.

its ops release it, and free it with kfree.

Returns
the description, or NULL if the heap is full.

◆ file_ref()

void file_ref ( file_t * file)

add a reference to a description.

Parameters
filethe description.

◆ file_unref()

void file_unref ( file_t * file)

drop a reference; the last one releases the description.

may sleep (closing a filesystem file takes its mutex).

Parameters
filethe description (NULL is ignored).

◆ file_readable()

bool file_readable ( const file_t * file)

true if the description was opened for reading (or writing).

◆ file_writable()

bool file_writable ( const file_t * file)

◆ file_read()

int32_t file_read ( file_t * file,
void * buf,
uint32_t count )

the operations, with an errno for the unsupported ones: read and write EBADF (wrong access mode, or no op), seek ESPIPE, ioctl ENOTTY, mmap ENODEV, read_entry ENOTDIR, truncate EINVAL.

the buffers are kernel pointers or user pointers the caller has checked.

◆ file_write()

int32_t file_write ( file_t * file,
const void * buf,
uint32_t count )

◆ file_seek()

int64_t file_seek ( file_t * file,
int64_t offset,
int whence )

◆ file_ioctl()

int32_t file_ioctl ( file_t * file,
uint32_t request,
void * arg )

◆ file_mmap()

int32_t file_mmap ( file_t * file,
addr_space_t as,
uintptr_t virt,
size_t length,
int prot,
uint64_t offset )

◆ file_read_entry()

int32_t file_read_entry ( file_t * file,
fs_dirent_t * entry )

◆ file_unread_entry()

int32_t file_unread_entry ( file_t * file,
const fs_dirent_t * entry )

give back the entry file_read_entry returned last, so the next call returns it again (getdents, when it does not fit the caller's buffer).

Returns
0, or -ENOMEM.

◆ file_truncate()

int32_t file_truncate ( file_t * file,
uint64_t length )

◆ file_poll()

uint32_t file_poll ( file_t * file)

the readiness of a file (kernel/vfs/poll.h).

a file without a poll op (a regular file) is always ready to read and write.

Returns
POLL* events.

◆ file_stat()

int32_t file_stat ( file_t * file,
struct kstat * st )

◆ file_pread()

int32_t file_pread ( file_t * file,
void * buf,
uint32_t count,
int64_t offset )

read or write at an offset without moving the description's offset (pread, pwrite).

only for files that can seek (ESPIPE otherwise). not atomic against another process using the same description.

◆ file_pwrite()

int32_t file_pwrite ( file_t * file,
const void * buf,
uint32_t count,
int64_t offset )

◆ file_is_vfs()

bool file_is_vfs ( const file_t * file)

true if the description is a filesystem file (not a pipe or the console).

◆ file_open()

kerr_t file_open ( const char * path,
uint32_t flags,
file_t ** out )

open a file or directory through the VFS.

Parameters
pathabsolute path.
flagsO_ access mode and status flags to keep in the description.
outreceives the new description (one reference).
Returns
KERR_OK, KERR_NOMEM, or the VFS's error.

◆ file_new_fifo_end()

file_t * file_new_fifo_end ( uint32_t flags)

a new end of a named pipe, not yet joined to one (kernel/vfs/fifo.c joins it).

its release leaves the named pipe.

Parameters
flagsthe open flags.
Returns
the file, or NULL (no memory).

◆ file_open_pipe()

kerr_t file_open_pipe ( file_t ** read_end,
file_t ** write_end )

make the two ends of a new pipe.

Parameters
read_endreceives the read end (one reference).
write_endreceives the write end (one reference).
Returns
KERR_OK or KERR_NOMEM.