-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
245 lines (218 loc) · 8.15 KB
/
main.cpp
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
////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2008-2014 by Alexander Galanin //
// [email protected] //
// http://galanin.nnov.ru/~al //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU Lesser General Public License as //
// published by the Free Software Foundation; either version 3 of the //
// License, or (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU Lesser General Public //
// License along with this program; if not, write to the //
// Free Software Foundation, Inc., //
// 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA //
////////////////////////////////////////////////////////////////////////////
#define KEY_HELP (0)
#define KEY_VERSION (1)
#define KEY_RO (2)
#include "config.h"
#include <fuse.h>
#include <fuse_opt.h>
#include <limits.h>
#include <syslog.h>
#include <cerrno>
#include "fuse-zip.h"
#include "fuseZipData.h"
/**
* Print usage information
*/
void print_usage() {
fprintf(stderr, "usage: %s [options] <zip-file> <mountpoint>\n\n", PROGRAM);
fprintf(stderr,
"general options:\n"
" -o opt,[opt...] mount options\n"
" -h --help print help\n"
" -V --version print version\n"
" -r -o ro open archive in read-only mode\n"
" -f don't detach from terminal\n"
" -d turn on debugging, also implies -f\n"
"\n");
}
/**
* Print version information (fuse-zip and FUSE library)
*/
void print_version() {
fprintf(stderr, "%s version: %s\n", PROGRAM, VERSION);
}
/**
* Parameters for command-line argument processing function
*/
struct fusezip_param {
// help shown
bool help;
// version information shown
bool version;
// number of string arguments
int strArgCount;
// zip file name
const char *fileName;
// read-only flag
bool readonly;
};
/**
* Function to process arguments (called from fuse_opt_parse).
*
* @param data Pointer to fusezip_param structure
* @param arg is the whole argument or option
* @param key determines why the processing function was called
* @param outargs the current output argument list
* @return -1 on error, 0 if arg is to be discarded, 1 if arg should be kept
*/
static int process_arg(void *data, const char *arg, int key, struct fuse_args *outargs) {
struct fusezip_param *param = (fusezip_param*)data;
(void)outargs;
// 'magic' fuse_opt_proc return codes
const static int KEEP = 1;
const static int DISCARD = 0;
const static int ERROR = -1;
switch (key) {
case KEY_HELP: {
print_usage();
param->help = true;
return DISCARD;
}
case KEY_VERSION: {
print_version();
param->version = true;
return KEEP;
}
case KEY_RO: {
param->readonly = true;
return KEEP;
}
case FUSE_OPT_KEY_NONOPT: {
++param->strArgCount;
switch (param->strArgCount) {
case 1: {
// zip file name
param->fileName = arg;
return DISCARD;
}
case 2: {
// mountpoint
// keep it and then pass to FUSE initializer
return KEEP;
}
default:
fprintf(stderr, "%s: only two arguments allowed: filename and mountpoint\n", PROGRAM);
return ERROR;
}
}
default: {
return KEEP;
}
}
}
static const struct fuse_opt fusezip_opts[] = {
FUSE_OPT_KEY("-h", KEY_HELP),
FUSE_OPT_KEY("--help", KEY_HELP),
FUSE_OPT_KEY("-V", KEY_VERSION),
FUSE_OPT_KEY("--version", KEY_VERSION),
FUSE_OPT_KEY("-r", KEY_RO),
FUSE_OPT_KEY("ro", KEY_RO),
{NULL, 0, 0}
};
int main(int argc, char *argv[]) {
if (sizeof(void*) > sizeof(uint64_t)) {
fprintf(stderr,"%s: This program cannot be run on your system because of FUSE design limitation\n", PROGRAM);
return EXIT_FAILURE;
}
struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
FuseZipData *data = NULL;
struct fusezip_param param;
param.help = false;
param.version = false;
param.readonly = false;
param.strArgCount = 0;
param.fileName = NULL;
if (fuse_opt_parse(&args, ¶m, fusezip_opts, process_arg)) {
fuse_opt_free_args(&args);
return EXIT_FAILURE;
}
// if all work is done inside options parsing...
if (param.help) {
fuse_opt_free_args(&args);
return EXIT_SUCCESS;
}
// pass version switch to HELP library to see it's version
if (!param.version) {
// no file name passed
if (param.fileName == NULL) {
print_usage();
fuse_opt_free_args(&args);
return EXIT_FAILURE;
}
openlog(PROGRAM, LOG_PID, LOG_USER);
if ((data = initFuseZip(PROGRAM, param.fileName, param.readonly))
== NULL) {
fuse_opt_free_args(&args);
return EXIT_FAILURE;
}
}
static struct fuse_operations fusezip_oper;
fusezip_oper.init = fusezip_init;
fusezip_oper.destroy = fusezip_destroy;
fusezip_oper.readdir = fusezip_readdir;
fusezip_oper.getattr = fusezip_getattr;
fusezip_oper.statfs = fusezip_statfs;
fusezip_oper.open = fusezip_open;
fusezip_oper.read = fusezip_read;
fusezip_oper.write = fusezip_write;
fusezip_oper.release = fusezip_release;
fusezip_oper.unlink = fusezip_unlink;
fusezip_oper.rmdir = fusezip_rmdir;
fusezip_oper.mkdir = fusezip_mkdir;
fusezip_oper.rename = fusezip_rename;
fusezip_oper.create = fusezip_create;
fusezip_oper.chmod = fusezip_chmod;
fusezip_oper.chown = fusezip_chown;
fusezip_oper.flush = fusezip_flush;
fusezip_oper.fsync = fusezip_fsync;
fusezip_oper.fsyncdir = fusezip_fsyncdir;
fusezip_oper.opendir = fusezip_opendir;
fusezip_oper.releasedir = fusezip_releasedir;
fusezip_oper.access = fusezip_access;
fusezip_oper.utimens = fusezip_utimens;
fusezip_oper.ftruncate = fusezip_ftruncate;
fusezip_oper.truncate = fusezip_truncate;
fusezip_oper.setxattr = fusezip_setxattr;
fusezip_oper.getxattr = fusezip_getxattr;
fusezip_oper.listxattr = fusezip_listxattr;
fusezip_oper.removexattr= fusezip_removexattr;
fusezip_oper.readlink = fusezip_readlink;
fusezip_oper.symlink = fusezip_symlink;
#if FUSE_VERSION >= 28
// don't allow NULL path
fusezip_oper.flag_nullpath_ok = 0;
#endif
struct fuse *fuse;
char *mountpoint;
// this flag ignored because libzip does not supports multithreading
int multithreaded;
int res;
fuse = fuse_setup(args.argc, args.argv, &fusezip_oper, sizeof(fusezip_oper), &mountpoint, &multithreaded, data);
fuse_opt_free_args(&args);
if (fuse == NULL) {
delete data;
return EXIT_FAILURE;
}
res = fuse_loop(fuse);
fuse_teardown(fuse, mountpoint);
return (res == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}