PatchworkOS
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>
4#include <kernel/fs/inode.h>
5#include <kernel/fs/path.h>
6#include <kernel/sync/mutex.h>
7#include <kernel/utils/ref.h>
8
9#include <errno.h>
10#include <stdlib.h>
11
12static void file_free(file_t* file)
13{
14 if (file == NULL)
15 {
16 return;
17 }
18
19 if (file->ops != NULL && file->ops->close != NULL)
20 {
21 file->ops->close(file);
22 }
23
25 file->inode = NULL;
26 path_put(&file->path);
27
28 free(file);
29}
30
31file_t* file_new(inode_t* inode, const path_t* path, path_flags_t flags)
32{
33 file_t* file = malloc(sizeof(file_t));
34 if (file == NULL)
35 {
36 return NULL;
37 }
38
40 file->pos = 0;
41 file->flags = flags;
42 file->inode = REF(inode);
43 file->path = PATH_EMPTY;
44 path_copy(&file->path, path);
45 file->ops = inode->fileOps;
46 file->private = NULL;
47
48 return file;
49}
50
52{
54
55 uint64_t newPos;
56 switch (origin)
57 {
58 case SEEK_SET:
59 newPos = offset;
60 break;
61 case SEEK_CUR:
62 newPos = file->pos + offset;
63 break;
64 case SEEK_END:
65 newPos = file->inode->size + offset;
66 break;
67 default:
68 errno = EINVAL;
69 return ERR;
70 }
71
72 file->pos = newPos;
73 return newPos;
74}
#define SEEK_SET
Definition SEEK.h:4
#define SEEK_CUR
Definition SEEK.h:5
#define SEEK_END
Definition SEEK.h:6
file_t * file_new(inode_t *inode, const path_t *path, path_flags_t flags)
Create a new file structure.
Definition file.c:31
uint64_t file_generic_seek(file_t *file, int64_t offset, seek_origin_t origin)
Helper function for basic seeking.
Definition file.c:51
void path_put(path_t *path)
Put a path.
Definition path.c:257
path_flags_t
Path flags.
Definition path.h:83
void path_copy(path_t *dest, const path_t *src)
Copy a path.
Definition path.c:232
#define PATH_EMPTY
Helper to create an empty path.
Definition path.h:182
#define MUTEX_SCOPE(mutex)
Acquires a mutex for the reminder of the current scope.
Definition mutex.h:23
static void ref_init(ref_t *ref, void *free)
Initialize a reference counter.
Definition ref.h:92
#define REF(ptr)
Increment reference count.
Definition ref.h:65
#define DEREF(ptr)
Decrement reference count.
Definition ref.h:80
#define EINVAL
Invalid argument.
Definition errno.h:142
#define errno
Error number variable.
Definition errno.h:27
uint8_t seek_origin_t
Type for the seek() origin argument.
Definition io.h:262
#define NULL
Pointer error value.
Definition NULL.h:23
#define ERR
Integer error value.
Definition ERR.h:17
static void file_free(file_t *file)
Definition file.c:12
static dentry_t * file
Definition log_file.c:17
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__INT64_TYPE__ int64_t
Definition stdint.h:16
_PUBLIC void * malloc(size_t size)
Definition malloc.c:5
_PUBLIC void free(void *ptr)
Definition free.c:11
inode_t * inode
Definition dentry.h:87
ref_t ref
Definition dentry.h:84
const dentry_ops_t * ops
Definition dentry.h:93
void * private
Definition dentry.h:94
File structure.
Definition file.h:37
Inode structure.
Definition inode.h:54
mutex_t mutex
Definition inode.h:70
uint64_t size
Definition inode.h:60
const file_ops_t * fileOps
Constant after creation.
Definition inode.h:69
Path structure.
Definition path.h:110