-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbridge.c
215 lines (170 loc) · 4.73 KB
/
bridge.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#define FUSE_USE_VERSION 26
#include <fuse/fuse.h>
#include "bridge.h"
struct callbacks python_callbacks = {NULL};
/*----------------------------------------------------------------------------*/
void *zalloc(size_t size)
{
return calloc(1, size);
}
void zfree(void *ptr)
{
free(ptr);
}
/*----------------------------------------------------------------------------*/
static void load_file_info(const struct fuse_file_info *in,
struct file_info *out)
{
out->handle = in->fh;
out->flags = in->flags;
out->direct_io = in->direct_io;
}
static void unload_file_info(const struct file_info *in,
struct fuse_file_info *out)
{
out->fh = in->handle;
out->flags = in->flags;
out->direct_io = in->direct_io;
}
static void load_attributes(const struct stat *in,
struct file_attributes *out)
{
out->mode = in->st_mode;
out->uid = in->st_uid;
out->gid = in->st_gid;
out->size = in->st_size;
}
static void unload_attributes(const struct file_attributes *in,
struct stat *out)
{
out->st_mode = in->mode;
out->st_uid = in->uid;
out->st_gid = in->gid;
out->st_size = in->size;
}
/*----------------------------------------------------------------------------*/
static int bridge_open(const char *path, struct fuse_file_info *fi)
{
int retval;
struct file_info info = {0};
if (python_callbacks.open == NULL) {
return -EPERM;
}
load_file_info(fi, &info);
retval = python_callbacks.open(path, &info);
unload_file_info(&info, fi);
return retval;
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
static int bridge_readdir(const char *path, void *buf,
fuse_fill_dir_t filler, off_t offset,
struct fuse_file_info *fi)
{
int retval;
char **entries = NULL;
if (python_callbacks.readdir == NULL) {
return -EPERM;
}
retval = python_callbacks.readdir(path, &entries);
if (entries == NULL) {
return -ENOENT;
}
for (uint32_t x = 0; entries[x] != NULL; x++) {
if (filler(buf, entries[x], NULL, 0) != 0) {
retval = -EIO;
break;
}
free(entries[x]);
}
free(entries);
return retval;
}
#pragma GCC diagnostic pop
static int bridge_getattr(const char *path, struct stat *stbuf)
{
int retval;
struct file_attributes attributes = {0};
if (python_callbacks.getattr == NULL) {
return -EPERM;
}
load_attributes(stbuf, &attributes);
retval = python_callbacks.getattr(path, &attributes);
if (retval != -ENOENT) {
stbuf->st_nlink = 1;
unload_attributes(&attributes, stbuf);
}
return retval;
}
static int bridge_access(const char* path, int mask)
{
if (python_callbacks.access == NULL) {
return -EPERM;
}
return python_callbacks.access(path, mask);
}
static int bridge_read(const char *path, char *buf, size_t size,
off_t offset, struct fuse_file_info *fi)
{
int retval;
struct file_info info = {0};
if (python_callbacks.read == NULL) {
return -EPERM;
}
load_file_info(fi, &info);
retval = python_callbacks.read(path, buf, size, offset, &info);
unload_file_info(&info, fi);
return retval;
}
static int bridge_truncate(const char* path, off_t size)
{
if (python_callbacks.truncate == NULL) {
return -EPERM;
}
return python_callbacks.truncate(path, size);
}
static int bridge_write(const char *path, const char *buf, size_t size,
off_t offset, struct fuse_file_info *fi)
{
int retval;
struct file_info info = {0};
if (python_callbacks.write == NULL) {
return -EPERM;
}
load_file_info(fi, &info);
retval = python_callbacks.write(path, buf, size, offset, &info);
unload_file_info(&info, fi);
return retval;
}
static struct fuse_operations bridge_oper = {
.getattr = bridge_getattr,
.readdir = bridge_readdir,
.truncate = bridge_truncate,
.open = bridge_open,
.read = bridge_read,
.write = bridge_write,
.access = bridge_access
};
int bridge_main(int argc, char *argv[])
{
int result = fuse_main(argc, argv, &bridge_oper, NULL);
for (int x = 0; x < argc; x++) {
zfree(argv[x]);
}
zfree(argv);
return result;
}
/*--------------------------------------------------------------------*/
int debug_write(char *string)
{
int result = strnlen(string, 1024);
printf("debug: [%s]\n", string);
return result;
}