-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathhl.c
370 lines (314 loc) · 8.61 KB
/
hl.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
* Copyright (c) 2014 Dave Vasilevsky <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "squashfuse.h"
#include "fuseprivate.h"
#include "stat.h"
#include "nonstd.h"
#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
typedef struct sqfs_hl sqfs_hl;
struct sqfs_hl {
sqfs fs;
sqfs_inode root;
};
static sqfs_err sqfs_hl_lookup(sqfs **fs, sqfs_inode *inode,
const char *path) {
bool found;
sqfs_hl *hl = fuse_get_context()->private_data;
*fs = &hl->fs;
if (inode)
*inode = hl->root; /* copy */
if (path) {
sqfs_err err = sqfs_lookup_path(*fs, inode, path, &found);
if (err)
return err;
if (!found)
return SQFS_ERR;
}
return SQFS_OK;
}
static void sqfs_hl_op_destroy(void *user_data) {
sqfs_hl *hl = (sqfs_hl*)user_data;
sqfs_destroy(&hl->fs);
free(hl);
}
static void *sqfs_hl_op_init(struct fuse_conn_info *conn
#if FUSE_USE_VERSION >= 30
,struct fuse_config *cfg
#endif
) {
sqfs_hl *hl = fuse_get_context()->private_data;
notify_mount_ready_async(hl->fs.notify_pipe, NOTIFY_SUCCESS);
return hl;
}
static int sqfs_hl_op_getattr(const char *path, struct stat *st
#if FUSE_USE_VERSION >= 30
, struct fuse_file_info *fi
#endif
) {
sqfs *fs;
sqfs_inode inode;
if (sqfs_hl_lookup(&fs, &inode, path))
return -ENOENT;
if (sqfs_stat(fs, &inode, st))
return -ENOENT;
return 0;
}
static int sqfs_hl_op_opendir(const char *path, struct fuse_file_info *fi) {
sqfs *fs;
sqfs_inode *inode;
inode = malloc(sizeof(*inode));
if (!inode)
return -ENOMEM;
if (sqfs_hl_lookup(&fs, inode, path)) {
free(inode);
return -ENOENT;
}
if (!S_ISDIR(inode->base.mode)) {
free(inode);
return -ENOTDIR;
}
fi->fh = (intptr_t)inode;
return 0;
}
static int sqfs_hl_op_releasedir(const char *path,
struct fuse_file_info *fi) {
free((sqfs_inode*)(intptr_t)fi->fh);
fi->fh = 0;
return 0;
}
static int sqfs_hl_op_readdir(const char *path, void *buf,
fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi
#if FUSE_USE_VERSION >= 30
,enum fuse_readdir_flags flags
#endif
) {
sqfs_err err;
sqfs *fs;
sqfs_inode *inode;
sqfs_dir dir;
sqfs_name namebuf;
sqfs_dir_entry entry;
struct stat st;
sqfs_hl_lookup(&fs, NULL, NULL);
inode = (sqfs_inode*)(intptr_t)fi->fh;
#ifdef SQFS_BROKEN_DIR_OFFSETS
offset = 0;
#endif
if (sqfs_dir_open(fs, inode, &dir, offset))
return -EINVAL;
memset(&st, 0, sizeof(st));
sqfs_dentry_init(&entry, namebuf);
while (sqfs_dir_next(fs, &dir, &entry, &err)) {
#ifdef SQFS_BROKEN_DIR_OFFSETS
sqfs_off_t doff = 0;
#else
sqfs_off_t doff = sqfs_dentry_next_offset(&entry);
#endif
st.st_mode = sqfs_dentry_mode(&entry);
if (filler(buf, sqfs_dentry_name(&entry), &st, doff
#if FUSE_USE_VERSION >= 30
, 0
#endif
)) {
return 0;
}
}
if (err)
return -EIO;
return 0;
}
static int sqfs_hl_op_open(const char *path, struct fuse_file_info *fi) {
sqfs *fs;
sqfs_inode *inode;
if (fi->flags & (O_WRONLY | O_RDWR))
return -EROFS;
inode = malloc(sizeof(*inode));
if (!inode)
return -ENOMEM;
if (sqfs_hl_lookup(&fs, inode, path)) {
free(inode);
return -ENOENT;
}
if (!S_ISREG(inode->base.mode)) {
free(inode);
return -EISDIR;
}
fi->fh = (intptr_t)inode;
fi->keep_cache = 1;
return 0;
}
static int sqfs_hl_op_create(const char* unused_path, mode_t unused_mode,
struct fuse_file_info *unused_fi) {
return -EROFS;
}
static int sqfs_hl_op_release(const char *path, struct fuse_file_info *fi) {
free((sqfs_inode*)(intptr_t)fi->fh);
fi->fh = 0;
return 0;
}
static int sqfs_hl_op_read(const char *path, char *buf, size_t size,
off_t off, struct fuse_file_info *fi) {
sqfs *fs;
sqfs_hl_lookup(&fs, NULL, NULL);
sqfs_inode *inode = (sqfs_inode*)(intptr_t)fi->fh;
off_t osize = size;
if (sqfs_read_range(fs, inode, off, &osize, buf))
return -EIO;
return osize;
}
static int sqfs_hl_op_readlink(const char *path, char *buf, size_t size) {
sqfs *fs;
sqfs_inode inode;
if (sqfs_hl_lookup(&fs, &inode, path))
return -ENOENT;
if (!S_ISLNK(inode.base.mode)) {
return -EINVAL;
} else if (sqfs_readlink(fs, &inode, buf, &size)) {
return -EIO;
}
return 0;
}
static int sqfs_hl_op_listxattr(const char *path, char *buf, size_t size) {
sqfs *fs;
sqfs_inode inode;
int ferr;
if (sqfs_hl_lookup(&fs, &inode, path))
return -ENOENT;
ferr = sqfs_listxattr(fs, &inode, buf, &size);
if (ferr)
return -ferr;
return size;
}
static int sqfs_hl_op_getxattr(const char *path, const char *name,
char *value, size_t size
#ifdef FUSE_XATTR_POSITION
, uint32_t position
#endif
) {
sqfs *fs;
sqfs_inode inode;
size_t real = size;
#ifdef FUSE_XATTR_POSITION
if (position != 0) /* We don't support resource forks */
return -EINVAL;
#endif
if (sqfs_hl_lookup(&fs, &inode, path))
return -ENOENT;
if ((sqfs_xattr_lookup(fs, &inode, name, value, &real)))
return -EIO;
if (real == 0)
return -sqfs_enoattr();
if (size != 0 && size < real)
return -ERANGE;
return real;
}
static int sqfs_hl_op_statfs(const char *path, struct statvfs *st) {
sqfs_hl *hl = fuse_get_context()->private_data;
return sqfs_statfs(&hl->fs, st);
}
static sqfs_hl *sqfs_hl_open(const char *path, size_t offset, const char *subdir) {
sqfs_hl *hl;
hl = malloc(sizeof(*hl));
if (!hl) {
perror("Can't allocate memory");
} else {
memset(hl, 0, sizeof(*hl));
if (sqfs_open_image_with_subdir(&hl->fs, path, offset, subdir) == SQFS_OK) {
if (sqfs_inode_get(&hl->fs, &hl->root, sqfs_inode_root(&hl->fs)))
fprintf(stderr, "Can't find the root of this filesystem!\n");
else
return hl;
sqfs_destroy(&hl->fs);
}
free(hl);
}
return NULL;
}
int main(int argc, char *argv[]) {
struct fuse_args args;
sqfs_opts opts;
sqfs_hl *hl;
int ret;
struct fuse_opt fuse_opts[] = {
{"offset=%zu", offsetof(sqfs_opts, offset), 0},
{"subdir=%s", offsetof(sqfs_opts, subdir), 0},
{"notify_pipe=%s", offsetof(sqfs_opts, notify_pipe), 0},
FUSE_OPT_END
};
struct fuse_operations sqfs_hl_ops;
memset(&sqfs_hl_ops, 0, sizeof(sqfs_hl_ops));
sqfs_hl_ops.init = sqfs_hl_op_init;
sqfs_hl_ops.destroy = sqfs_hl_op_destroy;
sqfs_hl_ops.getattr = sqfs_hl_op_getattr;
sqfs_hl_ops.opendir = sqfs_hl_op_opendir;
sqfs_hl_ops.releasedir = sqfs_hl_op_releasedir;
sqfs_hl_ops.readdir = sqfs_hl_op_readdir;
sqfs_hl_ops.open = sqfs_hl_op_open;
sqfs_hl_ops.create = sqfs_hl_op_create;
sqfs_hl_ops.release = sqfs_hl_op_release;
sqfs_hl_ops.read = sqfs_hl_op_read;
sqfs_hl_ops.readlink = sqfs_hl_op_readlink;
sqfs_hl_ops.listxattr = sqfs_hl_op_listxattr;
sqfs_hl_ops.getxattr = sqfs_hl_op_getxattr;
sqfs_hl_ops.statfs = sqfs_hl_op_statfs;
args.argc = argc;
args.argv = argv;
args.allocated = 0;
opts.progname = argv[0];
opts.image = NULL;
opts.subdir = NULL;
opts.mountpoint = 0;
opts.offset = 0;
opts.notify_pipe = NULL;
if (fuse_opt_parse(&args, &opts, fuse_opts, sqfs_opt_proc) == -1) {
ret = sqfs_usage(argv[0], true, false);
goto out;
}
if (!opts.image) {
ret = sqfs_usage(argv[0], true, false);
goto out;
}
hl = sqfs_hl_open(opts.image, opts.offset, opts.subdir);
if (!hl) {
ret = -1;
goto out;
}
hl->fs.notify_pipe = opts.notify_pipe;
fuse_opt_add_arg(&args, "-s"); /* single threaded */
ret = fuse_main(args.argc, args.argv, &sqfs_hl_ops, hl);
out:
if (ret) {
if (opts.notify_pipe) {
notify_mount_ready(opts.notify_pipe, NOTIFY_FAILURE);
}
}
fuse_opt_free_args(&args);
return ret;
}