PatchworkOS  966e257
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>
4#include <kernel/fs/inode.h>
5#include <kernel/fs/mount.h>
6#include <kernel/fs/path.h>
7#include <kernel/sync/mutex.h>
8#include <kernel/utils/ref.h>
9
10#include <errno.h>
11#include <stdlib.h>
12
13static void file_free(file_t* file)
14{
15 if (file == NULL)
16 {
17 return;
18 }
19
20 if (file->ops != NULL && file->ops->close != NULL)
21 {
22 file->ops->close(file);
23 }
24
26 file->inode = NULL;
27 path_put(&file->path);
28
29 free(file);
30}
31
32file_t* file_new(const path_t* path, mode_t mode)
33{
34 if (path == NULL)
35 {
36 errno = EINVAL;
37 return NULL;
38 }
39
40 if (((mode & MODE_ALL_PERMS) & ~path->mount->mode) != 0)
41 {
42 errno = EACCES;
43 return NULL;
44 }
45
46 if (!dentry_is_positive(path->dentry))
47 {
48 errno = ENOENT;
49 return NULL;
50 }
51
52 if (mode & MODE_DIRECTORY)
53 {
54 if (dentry_is_file(path->dentry))
55 {
56 errno = ENOTDIR;
57 return NULL;
58 }
59 }
60 else
61 {
62 if (dentry_is_dir(path->dentry))
63 {
64 errno = EISDIR;
65 return NULL;
66 }
67 }
68
69 file_t* file = malloc(sizeof(file_t));
70 if (file == NULL)
71 {
72 errno = ENOMEM;
73 return NULL;
74 }
75
76 if ((mode & MODE_ALL_PERMS) == MODE_NONE)
77 {
78 mode |= path->mount->mode & MODE_ALL_PERMS;
79 }
80
82 file->pos = 0;
83 file->mode = mode;
84 file->inode = REF(path->dentry->inode);
85 file->path = PATH_CREATE(path->mount, path->dentry);
86 file->ops = path->dentry->inode->fileOps;
87 file->private = NULL;
88
89 return file;
90}
91
93{
95
96 uint64_t newPos;
97 switch (origin)
98 {
99 case SEEK_SET:
100 newPos = offset;
101 break;
102 case SEEK_CUR:
103 newPos = file->pos + offset;
104 break;
105 case SEEK_END:
106 newPos = file->inode->size + offset;
107 break;
108 default:
109 errno = EINVAL;
110 return ERR;
111 }
112
113 file->pos = newPos;
114 return newPos;
115}
#define SEEK_SET
Definition SEEK.h:4
#define SEEK_CUR
Definition SEEK.h:5
#define SEEK_END
Definition SEEK.h:6
bool dentry_is_positive(dentry_t *dentry)
Check if a dentry is positive.
Definition dentry.c:243
bool dentry_is_dir(dentry_t *dentry)
Check if the inode associated with a dentry is a directory.
Definition dentry.c:268
bool dentry_is_file(dentry_t *dentry)
Check if the inode associated with a dentry is a file.
Definition dentry.c:253
uint64_t file_generic_seek(file_t *file, int64_t offset, seek_origin_t origin)
Helper function for basic seeking.
Definition file.c:92
file_t * file_new(const path_t *path, mode_t mode)
Create a new file structure.
Definition file.c:32
mode_t
Path flags and permissions.
Definition path.h:74
void path_put(path_t *path)
Put a path.
Definition path.c:246
#define PATH_CREATE(inMount, inDentry)
Helper to create a path.
Definition path.h:207
@ MODE_ALL_PERMS
Definition path.h:87
@ MODE_NONE
Definition path.h:75
@ MODE_DIRECTORY
Definition path.h:84
#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 UNREF(ptr)
Decrement reference count.
Definition ref.h:80
#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 ENOTDIR
Not a directory.
Definition errno.h:132
#define errno
Error number variable.
Definition errno.h:27
#define EACCES
Permission denied.
Definition errno.h:97
#define EISDIR
Is a directory.
Definition errno.h:137
uint8_t seek_origin_t
Type for the seek() origin argument.
Definition io.h:231
#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:13
static dentry_t * file
Definition log_file.c:22
__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
Will be NULL if the dentry is negative, once positive it will never be NULL.
Definition dentry.h:88
ref_t ref
Definition dentry.h:85
const dentry_ops_t * ops
Definition dentry.h:94
void * private
Definition dentry.h:95
File structure.
Definition file.h:39
mutex_t mutex
Definition inode.h:72
uint64_t size
Definition inode.h:62
const file_ops_t * fileOps
Constant after creation.
Definition inode.h:71
mode_t mode
Specifies the maximum permissions for this mount and if it is a directory or a file.
Definition mount.h:52
Path structure.
Definition path.h:125
mount_t * mount
Definition path.h:126
dentry_t * dentry
Definition path.h:127