The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
file.h File Reference
#include "common/stdint.h"
#include "common/error.h"
#include "arch/arch.h"
#include "kernel/vfs/vfs.h"
#include "kernel/vfs/stat.h"
#include "kernel/syscall/pipe.h"

Go to the source code of this file.

Data Structures

struct  file_ops_t
 what a kind of file can do. More...
struct  file
 an open-file description. More...

Macros

#define O_RDONLY   0x0000
 open-file descriptions.
#define O_WRONLY   0x0001
#define O_RDWR   0x0002
#define O_ACCMODE   0x0003
#define O_CREAT   0x0040
#define O_EXCL   0x0080
#define O_TRUNC   0x0200
#define O_APPEND   0x0400
#define O_NONBLOCK   0x0800
#define O_DIRECTORY   0x10000
#define O_CLOEXEC   0x80000

Typedefs

typedef struct file file_t

Functions

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.
file_t * file_console (void)
 the console (keyboard in, screen out).
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_is_vfs (const file_t *file)
 true if the description is a filesystem file (not a pipe or the console).
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)
int32_t file_stat (file_t *file, struct kstat *st)
uint32_t file_poll (file_t *file)
 the readiness of a file (kernel/vfs/poll.h).
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)

Macro Definition Documentation

◆ O_RDONLY

#define O_RDONLY   0x0000

open-file descriptions.

open(), pipe() and the console each make a file_t. a file descriptor is a pointer to one, so dup, dup2, fork and spawn share the description: the same file offset, the same status flags. the description is released (the file closed, the pipe end shut) when its last descriptor closes.

each kind of file (a filesystem file, a pipe end, the console) supplies a file_ops_t, so the syscalls never switch on the kind.

locking: the reference count is guarded by one global spinlock. the operations do their own locking (pipes their spinlock, ext2 its mutex).

◆ O_WRONLY

#define O_WRONLY   0x0001

◆ O_RDWR

#define O_RDWR   0x0002

◆ O_ACCMODE

#define O_ACCMODE   0x0003

◆ O_CREAT

#define O_CREAT   0x0040

◆ O_EXCL

#define O_EXCL   0x0080

◆ O_TRUNC

#define O_TRUNC   0x0200

◆ O_APPEND

#define O_APPEND   0x0400

◆ O_NONBLOCK

#define O_NONBLOCK   0x0800

◆ O_DIRECTORY

#define O_DIRECTORY   0x10000

◆ O_CLOEXEC

#define O_CLOEXEC   0x80000

Typedef Documentation

◆ file_t

typedef struct file file_t

Function Documentation

◆ 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.

◆ file_console()

file_t * file_console ( void )

the console (keyboard in, screen out).

there is one console description; every process starts with it on fds 0, 1 and 2.

Returns
the console description, with a new reference.

◆ 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_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_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_stat()

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

◆ 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_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 )