-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfile.c
More file actions
43 lines (36 loc) · 993 Bytes
/
file.c
File metadata and controls
43 lines (36 loc) · 993 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "file.h"
struct file *file_open(const char *path, int flags, int rights) {
struct file *filp = NULL;
int err = 0;
filp = filp_open(path, flags, rights);
if (IS_ERR(filp)) {
err = PTR_ERR(filp);
return NULL;
}
return filp;
}
void file_close(struct file *file) {
filp_close(file, NULL);
}
int file_read(struct file *file, unsigned long long offset, unsigned char *data, unsigned int size) {
int ret;
ret = kernel_read(file, data, size, &offset);
return ret;
}
int file_write(struct file *file, unsigned long long offset, unsigned char *data, unsigned int size) {
int ret;
ret = kernel_write(file, data, size, &offset);
return ret;
}
int file_sync(struct file *file) {
vfs_fsync(file, 0);
return 0;
}
size_t get_file_size(const char *path){
struct kstat stat;
mm_segment_t old_fs = get_fs();
set_fs(KERNEL_DS);
vfs_stat(path, &stat);
set_fs(old_fs);
return (size_t)stat.size;
}