/
files.js
400 lines (350 loc) · 11.1 KB
/
files.js
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
"use strict";
var child_process = require('child_process');
var fs = require ('fs');
var f4js = require('fuse4js');
var files_xmpp =null;
var fuse = null;
var ERROR = -2;
var mountFile = null;
function load(modules)
{
files_xmpp = modules.files_xmpp;
fuse = modules.fuse;
mountFile = modules.settings.mountFile;
console.log("loaded");
}
function canMount()
{
return fuse.canMount();
}
/*
* Handler for the getattr() system call.
* path: the path to the file
* cb: a callback of the form cb(err, stat), where err is the Posix return code
* and stat is the result in the form of a stat structure (when err === 0)
*/
function getattr(path, cb)
{
// console.log('files.js get attr = '+path);
var stat = {};
files_xmpp.getAttr(path,function(err, attrs){
// console.log ('files.js responding get attr');
if(err == 0)
{
stat.size = attrs.size;
stat.mode = attrs.mode;
}
else
err = ERROR;
cb(err, stat);
});
console.log("got attribute");
};
//---------------------------------------------------------------------------
/*
* Handler for the readdir() system call.
* path: the path to the file
* cb: a callback of the form cb(err, names), where err is the Posix return code
* and names is the result in the form of an array of file names (when err === 0).
*/
function readdir(path, cb)
{
// console.log ('file.js read dir path = '+path);
console.log('readdir');
files_xmpp.readDir(path,function(err, names){
// console.log ('file.js responding read dir');
if(err != 0)
err = ERROR;
// console.log ('names: '+names);
cb(err, names);
});
}
//---------------------------------------------------------------------------
/*
* Handler for the open() system call.
* path: the path to the file
* flags: requested access flags as documented in open(2)
* cb: a callback of the form cb(err, [fh]), where err is the Posix return code
* and fh is an optional numerical file handle, which is passed to subsequent
* read(), write(), and release() calls.
*/
function open(path, flags, cb) {
console.log('open');
// files_xmpp.open(path, function(err){
// cb(err);
// });
cb(0); // we don't return a file handle, so fuse4js will initialize it to 0
}
//---------------------------------------------------------------------------
/*
* Handler for the read() system call.
* path: the path to the file
* offset: the file offset to read from
* len: the number of bytes to read
* buf: the Buffer to write the data to
* fh: the optional file handle originally returned by open(), or 0 if it wasn't
* cb: a callback of the form cb(err), where err is the Posix return code.
* A positive value represents the number of bytes actually read.
*/
function read(path, offset, len, buf, fh, cb) {
console.log('read');
files_xmpp.read(path,offset,len, function(err,data,length){
if(err == 0)
{
// console.log('err = 0');
err = length;
buf.write(data, 0, length, 'ascii');
}
else
{
// console.log('else error');
err = ERROR;
}
cb(err);
});
}
// //---------------------------------------------------------------------------
// /*
// * Handler for the write() system call.
// * path: the path to the file
// * offset: the file offset to write to
// * len: the number of bytes to write
// * buf: the Buffer to read data from
// * fh: the optional file handle originally returned by open(), or 0 if it wasn't
// * cb: a callback of the form cb(err), where err is the Posix return code.
// * A positive value represents the number of bytes actually written.
// */
// function write(path, offset, len, buf, fh, cb) {
// var err = 0; // assume success
// var info = lookup(obj, path);
// var file = info.node;
// var name = info.name;
// var parent = info.parent;
// var beginning, blank = '', data, ending='', numBlankChars;
// switch (typeof file) {
// case 'undefined':
// err = -2; // -ENOENT
// break;
// case 'object': // directory
// err = -1; // -EPERM
// break;
// case 'string': // a string treated as ASCII characters
// data = buf.toString('ascii'); // read the new data
// if (offset < file.length) {
// beginning = file.substring(0, offset);
// if (offset + data.length < file.length) {
// ending = file.substring(offset + data.length, file.length)
// }
// } else {
// beginning = file;
// numBlankChars = offset - file.length;
// while (numBlankChars--) blank += ' ';
// }
// delete parent[name];
// parent[name] = beginning + blank + data + ending;
// err = data.length;
// break;
// default:
// break;
// }
// cb(err);
// }
// //---------------------------------------------------------------------------
// /*
// * Handler for the release() system call.
// * path: the path to the file
// * fh: the optional file handle originally returned by open(), or 0 if it wasn't
// * cb: a callback of the form cb(err), where err is the Posix return code.
// */
// function release(path, fh, cb) {
// cb(0);
// }
// //---------------------------------------------------------------------------
// /*
// * Handler for the create() system call.
// * path: the path of the new file
// * mode: the desired permissions of the new file
// * cb: a callback of the form cb(err, [fh]), where err is the Posix return code
// * and fh is an optional numerical file handle, which is passed to subsequent
// * read(), write(), and release() calls (it's set to 0 if fh is unspecified)
// */
// function create (path, mode, cb) {
// var err = 0; // assume success
// var info = lookup(obj, path);
// switch (typeof info.node) {
// case 'undefined':
// if (info.parent !== null) {
// info.parent[info.name] = '';
// } else {
// err = -2; // -ENOENT
// }
// break;
// case 'string': // existing file
// case 'object': // existing directory
// err = -17; // -EEXIST
// break;
// default:
// break;
// }
// cb(err);
// }
// //---------------------------------------------------------------------------
// /*
// * Handler for the unlink() system call.
// * path: the path to the file
// * cb: a callback of the form cb(err), where err is the Posix return code.
// */
// function unlink(path, cb) {
// var err = 0; // assume success
// var info = lookup(obj, path);
// switch (typeof info.node) {
// case 'undefined':
// err = -2; // -ENOENT
// break;
// case 'object': // existing directory
// err = -1; // -EPERM
// break;
// case 'string': // existing file
// delete info.parent[info.name];
// break;
// default:
// break;
// }
// cb(err);
// }
// //---------------------------------------------------------------------------
// /*
// * Handler for the rename() system call.
// * src: the path of the file or directory to rename
// * dst: the new path
// * cb: a callback of the form cb(err), where err is the Posix return code.
// */
// function rename(src, dst, cb) {
// var err = -2; // -ENOENT assume failure
// var source = lookup(obj, src), dest;
// if (typeof source.node !== 'undefined') { // existing file or directory
// dest = lookup(obj, dst);
// if (typeof dest.node === 'undefined' && dest.parent !== null) {
// dest.parent[dest.name] = source.node;
// delete source.parent[source.name];
// err = 0;
// } else {
// err = -17; // -EEXIST
// }
// }
// cb(err);
// }
// //---------------------------------------------------------------------------
// /*
// * Handler for the mkdir() system call.
// * path: the path of the new directory
// * mode: the desired permissions of the new directory
// * cb: a callback of the form cb(err), where err is the Posix return code.
// */
// function mkdir(path, mode, cb) {
// var err = -2; // -ENOENT assume failure
// var dst = lookup(obj, path), dest;
// if (typeof dst.node === 'undefined' && dst.parent != null) {
// dst.parent[dst.name] = {};
// err = 0;
// }
// cb(err);
// }
// //---------------------------------------------------------------------------
// /*
// * Handler for the rmdir() system call.
// * path: the path of the directory to remove
// * cb: a callback of the form cb(err), where err is the Posix return code.
// */
// function rmdir(path, cb) {
// var err = -2; // -ENOENT assume failure
// var dst = lookup(obj, path), dest;
// if (typeof dst.node === 'object' && dst.parent != null) {
// delete dst.parent[dst.name];
// err = 0;
// }
// cb(err);
// }
// //---------------------------------------------------------------------------
/*
* Handler for the init() FUSE hook. You can initialize your file system here.
* cb: a callback to call when you're done initializing. It takes no arguments.
*/
function init(cb) {
console.log('init');
cb();
}
// //---------------------------------------------------------------------------
/*
* Handler for the setxattr() FUSE hook.
* The arguments differ between different operating systems.
* Darwin(Mac OSX):
* * a = position
* * b = options
* * c = cmd
* Other:
* * a = flags
* * b = cmd
* * c = undefined
*/
function setxattr(path, name, value, size, a, b, c) {
console.log('setxattr');
cb(0);
}
// //---------------------------------------------------------------------------
// /*
// * Handler for the statfs() FUSE hook.
// * cb: a callback of the form cb(err, stat), where err is the Posix return code
// * and stat is the result in the form of a statvfs structure (when err === 0)
// */
function statfs(cb) {
console.log("statfs");
cb(0, {
bsize: 1000000,
frsize: 1000000,
blocks: 1000000,
bfree: 1000000,
bavail: 1000000,
files: 1000000,
ffree: 1000000,
favail: 1000000,
fsid: 1000000,
flag: 1000000,
namemax: 1000000
});
}
//---------------------------------------------------------------------------
/*
* Handler for the destroy() FUSE hook. You can perform clean up tasks here.
* cb: a callback to call when you're done. It takes no arguments.
*/
function destroy(cb) {
console.log('destroy');
cb();
}
//---------------------------------------------------------------------------
var handlers = {
getattr: getattr,
readdir: readdir,
open: open,
read: read,
// write: write,
// release: release,
// create: create,
// unlink: unlink,
// rename: rename,
// mkdir: mkdir,
// rmdir: rmdir,
// init: init,
// destroy: destroy,
// setxattr: setxattr,
statfs: statfs
};
function start()
{
f4js.start(mountFile, handlers, true);
console.log("fuse started");
}
exports.load = load;
exports.canMount = canMount;
exports.start = start;