PatchworkOS  19e446b
A non-POSIX operating system.
Loading...
Searching...
No Matches
file.c
Go to the documentation of this file.
1#include <kernel/fs/file.h>
2
3#include <kernel/fs/dentry.h>
5#include <kernel/fs/mount.h>
6#include <kernel/fs/path.h>
8#include <kernel/fs/vnode.h>
9#include <kernel/io/irp.h>
10#include <kernel/mem/cache.h>
11#include <kernel/mem/mdl.h>
12#include <kernel/proc/process.h>
13#include <kernel/sync/mutex.h>
14#include <kernel/utils/ref.h>
15
16#include <errno.h>
17#include <stdlib.h>
18
19static void file_free(file_t* file)
20{
21 if (file == NULL)
22 {
23 return;
24 }
25
26 if (file->ops != NULL && file->ops->close != NULL)
27 {
28 file->ops->close(file);
29 }
30
31 UNREF(file->vnode);
32 file->vnode = NULL;
33 path_put(&file->path);
34
35 cache_free(file);
36}
37
38static cache_t cache = CACHE_CREATE(cache, "file", sizeof(file_t), CACHE_LINE, NULL, NULL);
39
40file_t* file_new(const path_t* path, mode_t mode)
41{
42 if (path == NULL)
43 {
44 errno = EINVAL;
45 return NULL;
46 }
47
48 if (mode_check(&mode, path->mount->mode) == ERR)
49 {
50 return NULL;
51 }
52
53 if (!DENTRY_IS_POSITIVE(path->dentry))
54 {
55 errno = ENOENT;
56 return NULL;
57 }
58
59 file_t* file = cache_alloc(&cache);
60 if (file == NULL)
61 {
62 errno = ENOMEM;
63 return NULL;
64 }
65
66 ref_init(&file->ref, file_free);
67 file->pos = 0;
68 file->mode = mode;
69 file->vnode = REF(path->dentry->vnode);
70 file->path = PATH_CREATE(path->mount, path->dentry);
71 file->ops = path->dentry->vnode->fileOps;
72 file->data = NULL;
73 return file;
74}
75
77{
78 MUTEX_SCOPE(&file->vnode->mutex);
79
80 size_t newPos;
81 switch (origin)
82 {
83 case SEEK_SET:
84 newPos = offset;
85 break;
86 case SEEK_CUR:
87 newPos = file->pos + offset;
88 break;
89 case SEEK_END:
90 newPos = file->vnode->size + offset;
91 break;
92 default:
93 errno = EINVAL;
94 return ERR;
95 }
96
97 file->pos = newPos;
98 return newPos;
99}
#define SEEK_SET
Definition SEEK.h:4
#define SEEK_CUR
Definition SEEK.h:5
#define SEEK_END
Definition SEEK.h:6
#define DENTRY_IS_POSITIVE(dentry)
Check if a dentry is positive.
Definition dentry.h:67
size_t file_generic_seek(file_t *file, ssize_t offset, seek_origin_t origin)
Helper function for basic seeking.
Definition file.c:76
file_t * file_new(const path_t *path, mode_t mode)
Create a new file structure.
Definition file.c:40
mode_t
Path flags and permissions.
Definition path.h:79
void path_put(path_t *path)
Put a path.
Definition path.c:273
uint64_t mode_check(mode_t *mode, mode_t maxPerms)
Check and adjust mode permissions.
Definition path.c:816
#define PATH_CREATE(inMount, inDentry)
Helper to create a path.
Definition path.h:176
void cache_free(void *obj)
Free an object back to its cache.
Definition cache.c:205
#define CACHE_LINE
Cache line size in bytes.
Definition cache.h:75
void * cache_alloc(cache_t *cache)
Allocate an object from the cache.
Definition cache.c:109
#define CACHE_CREATE(_cache, _name, _size, _alignment, _ctor, _dtor)
Macro to create a cache initializer.
Definition cache.h:148
#define MUTEX_SCOPE(mutex)
Acquires a mutex for the reminder of the current scope.
Definition mutex.h:23
#define REF(ptr)
Increment reference count.
Definition ref.h:82
static void ref_init(ref_t *ref, void *callback)
Initialize a reference counter.
Definition ref.h:130
#define UNREF(ptr)
Decrement reference count.
Definition ref.h:109
#define ENOENT
No such file or directory.
Definition errno.h:42
#define EINVAL
Invalid argument.
Definition errno.h:142
#define ENOMEM
Out of memory.
Definition errno.h:92
#define errno
Error number variable.
Definition errno.h:27
uint8_t seek_origin_t
Type for the seek() origin argument.
Definition fs.h:260
#define NULL
Pointer error value.
Definition NULL.h:25
__INT64_TYPE__ ssize_t
Signed size type.
Definition ssize_t.h:11
#define ERR
Integer error value.
Definition ERR.h:17
static void file_free(file_t *file)
Definition file.c:19
static cache_t cache
Definition file.c:38
static uint64_t offset
Definition screen.c:19
Cache structure.
Definition cache.h:123
vnode_t * vnode
Will be NULL if the dentry is negative, once positive it will never be modified.
Definition dentry.h:159
void(* close)(file_t *file)
Definition file.h:57
File structure.
Definition file.h:39
void * data
Definition file.h:46
const file_ops_t * ops
Definition file.h:45
ref_t ref
Definition file.h:40
mode_t mode
Definition file.h:42
vnode_t * vnode
Definition file.h:43
size_t pos
Definition file.h:41
path_t path
Definition file.h:44
mode_t mode
Specifies the maximum permissions for this mount and if it is a directory or a file.
Definition mount.h:56
Path structure.
Definition path.h:127
mount_t * mount
Definition path.h:128
dentry_t * dentry
Definition path.h:129
const file_ops_t * fileOps
Definition vnode.h:56
mutex_t mutex
Definition vnode.h:59
uint64_t size
Used for convenience by certain filesystems, does not represent the file size.
Definition vnode.h:53