/
nspire.c
110 lines (89 loc) · 2.89 KB
/
nspire.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
/*
FUSE filesystem for TI-Nspire calculators
Copyright (C) 2013 Daniel Tang
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU 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 General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <stddef.h>
#include "nspire.h"
int std_libnspire_err(int ret) {
switch (ret) {
case NSPIRE_ERR_SUCCESS: return 0;
case -NSPIRE_ERR_TIMEOUT: return -ETIMEDOUT;
case -NSPIRE_ERR_NOMEM: return -ENOMEM;
case -NSPIRE_ERR_INVALID: return -EPERM;
case -NSPIRE_ERR_LIBUSB: return -EIO;
case -NSPIRE_ERR_NODEVICE: return -ENODEV;
case -NSPIRE_ERR_INVALPKT: return -EIO;
case -NSPIRE_ERR_NACK: return -EIO;
case -NSPIRE_ERR_BUSY: return -EBUSY;
case -NSPIRE_ERR_EXISTS: return -EEXIST;
case -NSPIRE_ERR_NONEXIST: return -ENOENT;
case -NSPIRE_ERR_OSFAILED: return -EPERM;
default: return -EINVAL;
}
}
static struct fuse_operations nspire_fs = {
.getattr = nsp_getattr,
.readdir = nsp_readdir,
.mkdir = nsp_mkdir,
.unlink = nsp_delete,
.rmdir = nsp_rmdir,
.rename = nsp_rename,
.statfs = nsp_statfs,
.truncate = nsp_truncate,
.create = nsp_create,
.open = nsp_open,
.read = nsp_read,
.write = nsp_write,
.release = nsp_release,
.fsync = nsp_fsync,
//.access = nsp_access,
};
#define OPTION(t, p, v) { (t), offsetof(struct nsp_ctx, p), (v) }
static struct fuse_opt nsp_opts[] = {
OPTION("allow_bigfile", allow_bigfile, 1),
OPTION("thresh_bigfile=%i", thresh_bigfile, 0),
};
#undef OPTION
#define ERR_EXIT(errstr, errnum) do { \
fprintf(stderr, "Error (%d): %s\n", (errnum), (errstr)); \
exit(errnum); \
} while (0)
int main(int argc, char **argv) {
int ret;
struct nsp_ctx *ctx = malloc(sizeof(*ctx));
struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
if (!ctx)
ERR_EXIT("Out of memory\n", -ENOMEM);
if ((ret = nspire_init(&ctx->handle)))
ERR_EXIT(nspire_strerror(ret), std_libnspire_err(ret));
atomic_init(&ctx->lock);
ctx->allow_bigfile = 0;
ctx->thresh_bigfile = 512 * 1024;
fuse_opt_parse(&args, ctx, nsp_opts, NULL);
if (ctx->allow_bigfile) {
fprintf(stderr,
"Warning: Allowing files larger than %d bytes to be "
"opened. Large files may hang filesystem operations.\n",
ctx->thresh_bigfile);
}
ret = fuse_main(args.argc, args.argv, &nspire_fs, ctx);
nspire_free(ctx->handle);
free(ctx);
return ret;
}
#undef ERR_EXIT