forked from xerub/img4lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvfs_file.c
132 lines (122 loc) · 2.54 KB
/
vfs_file.c
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <fcntl.h>
#include <stdarg.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include "vfs.h"
#include "vfs_internal.h"
struct file_ops_file {
struct file_ops ops;
int fd;
};
static int
file_fsync(FHANDLE fd_)
{
struct file_ops_file *fd = (struct file_ops_file *)fd_;
if (!fd) {
return -1;
}
return fsync(fd->fd);
}
static int
file_close(FHANDLE fd_)
{
struct file_ops_file *fd = (struct file_ops_file *)fd_;
if (!fd) {
return -1;
}
close(fd->fd);
free(fd);
return 0;
}
static ssize_t
file_read(FHANDLE fd_, void *buf, size_t count)
{
struct file_ops_file *fd = (struct file_ops_file *)fd_;
if (!fd) {
return -1;
}
return read(fd->fd, buf, count);
}
static ssize_t
file_write(FHANDLE fd_, const void *buf, size_t count)
{
struct file_ops_file *fd = (struct file_ops_file *)fd_;
if (!fd) {
return -1;
}
return write(fd->fd, buf, count);
}
static off_t
file_lseek(FHANDLE fd_, off_t offset, int whence)
{
struct file_ops_file *fd = (struct file_ops_file *)fd_;
if (!fd) {
return -1;
}
return lseek(fd->fd, offset, whence);
}
static int
file_ioctl(FHANDLE fd_, unsigned long req, ...)
{
struct file_ops_file *fd = (struct file_ops_file *)fd_;
if (!fd) {
return -1;
}
return -1;
}
static int
file_ftruncate(FHANDLE fd_, off_t length)
{
struct file_ops_file *fd = (struct file_ops_file *)fd_;
if (!fd) {
return -1;
}
return ftruncate(fd->fd, length);
}
static ssize_t
file_length(FHANDLE fd_)
{
struct file_ops_file *fd = (struct file_ops_file *)fd_;
int rv;
struct stat st;
if (!fd) {
return -1;
}
rv = fstat(fd->fd, &st);
if (rv) {
return -1;
}
return st.st_size;
}
FHANDLE
file_open(const char *pathname, int flags, ...)
{
mode_t mode = 0;
struct file_ops_file *ops;
ops = malloc(sizeof(*ops));
if (!ops) {
return NULL;
}
if (flags & O_CREAT) {
va_list ap;
va_start(ap, flags);
mode = va_arg(ap, int);
va_end(ap);
}
ops->fd = open(pathname, flags, mode);
if (ops->fd < 0) {
free(ops);
return NULL;
}
ops->ops.flags = flags & O_ACCMODE;
ops->ops.read = file_read;
ops->ops.write = file_write;
ops->ops.lseek = file_lseek;
ops->ops.ioctl = file_ioctl;
ops->ops.ftruncate = file_ftruncate;
ops->ops.fsync = file_fsync;
ops->ops.close = file_close;
ops->ops.length = file_length;
return (FHANDLE)ops;
}