-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·441 lines (412 loc) · 14.6 KB
/
index.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#!/usr/bin/env node
var debug = require('debug')('gcs-fuse'),
fuse = require('fuse-bindings'),
fs = require('fs'),
mime = require('mime-types'),
stat = require('./fixtures/stat'),
Storage = require('@google-cloud/storage'),
streamToBuffer = require('stream-to-buffer'),
tmp = require('tmp'),
Promise = require('bluebird'),
File = Storage.File;
var storage = false,
bucket = false,
path = '',
fileDescriptors = [],
unlinked = []; // List of recently unlinked files due to cache
function flagDecode( flags ) {
switch( flags & 3 ) {
// case 0b00000000000000001000000000000000: return 'r';
// case 0b00000000000000001000000000000010: return 'r+';
// case 0b00000000000000001001000000000000: return 'rs';
// case 0b00000000000000001001000000000010: return 'rs+';
// case 0b00000000000000001000000000000001: return 'w';
// case 0b00000000000000001000000000000010: return 'w+';
// case 0b00000000000000001000010000000001: return 'a';
// case 0b00000000000000001000010000000010: return 'a+';
case 0: return 'r';
case 1: return 'w';
default: return 'r+';
}
}
function f_bucket_getfiles(prefix) {
return new Promise(function(resolve, reject) {
var tstamp = new Date().getTime(),
key = 'f_bucket_getfiles',
opts = undefined;
if (prefix) opts = { prefix: prefix };
bucket.getFiles(opts)
.then(function(result) {
result = result.shift();
resolve(result);
})
.catch(reject);
});
}
require('yargs')
.demand(1)
.command('daemon <fname> <bucket> <path>', 'start the server', (yargs) => {
yargs
.positional('fname', {
describe: 'Authentication key file'
})
.positional('bucket', {
describe: 'Name of the bucket to mount'
})
.positional('path', {
describe: 'Path to mount the bucket on'
})
}, (argv) => {
// Setup & connect
storage = new Storage({ keyFilename: argv.fname });
bucket = storage.bucket(argv.bucket);
path = argv.path;
var ops;
fuse.mount( path, ops = {
options: argv.options.split(','),
force : true,
getattr: function f_getattr( path, cb, objMode, tries ) {
tries = tries || 0;
if(tries>10)return cb(fuse.EIO);
debug('GETATTR',tries,path);
if ( path === '/' ) return cb( null, stat({ mode: 'dir', size: 4096 }));
if ( path.substr(0,1) === '/' ) path = path.substr(1);
var dirname = path.split('/');
dirname.pop();
dirname = '/' + dirname.join('/');
ops.readdir( dirname, function( err, dirEntries ) {
var fileObject = dirEntries
.filter(function(dirEntry) {
if ('string' === typeof dirEntry) return false;
var name = dirEntry.name;
if ( name.slice(-1) === '/' ) name = name.slice(0,-1);
return name === path;
})
.shift();
if (!fileObject) {
debug('- ENOENT');
return cb(fuse.ENOENT);
}
var attr = { mode : 'file',
size : parseInt(fileObject.metadata.size),
mtime: Date.parse( fileObject.metadata.updated ),
ctime: Date.parse( fileObject.metadata.updated ) };
if ( fileObject.metadata.metadata && fileObject.metadata.metadata.gcsfuse_symlink_target ) {
attr.mode = 'link';
}
if ( fileObject.name.slice(-1) === '/' ) {
attr.mode = 'dir';
attr.size = 4096;
}
debug('-',attr.mode,attr.size);
if ( objMode ) {
attr.fileObject = fileObject;
return cb( null, attr );
}
cb(null,stat(attr));
}, true, 0);
},
readdir: function f_readdir( path, cb, objMode, tries ) {
tries = tries || 0;
objMode = objMode || false;
if(tries>10)return cb(fuse.EIO);
debug('READDIR',tries,objMode,path);
if ( path.slice(-1) != '/' ) path += '/';
while ( path.substr(0,1) === '/' ) path = path.substr(1);
f_bucket_getfiles( path )
.then(function(results) {
var list = results
.filter(function(fileObject) {
if ( fileObject.name.substr(0,path.length) !== path ) return false;
if ( fileObject.name.substr(path.length).split('/').filter((n)=>n).length > 1 ) return false;
return true;
})
.map(function(fileObject) {
if ( objMode ) return fileObject;
if ( fileObject.name === path ) return false;
var basename = fileObject.name.substr(path.length);
if ( basename.slice(-1) === '/' ) basename = basename.slice(0,-1);
return basename;
})
.filter(function(basename) {
return !!basename;
})
cb(null,['.','..'].concat(list));
})
.catch(function(err) {
debug(err);
f_readdir( path, cb, objMode, tries + 1 );
})
},
open: function f_open( path, flags, cb, tries ) {
tries = tries || 0;
if(tries>10)return cb(fuse.EIO);
flags = ( 'string' === typeof flags ) ? flags : flagDecode( flags );
debug('OPEN',tries,path,flags);
ops.getattr( path, function( err, attr ) {
if ( err ) {
switch(err) {
case fuse.ENOENT:
return cb(err);
case fuse.EIO:
return f_open(path,flags,cb,tries+1);
default:
return f_open(path,flags,cb,tries+1);
}
}
if ( attr.mode === 'dir' ) return cb(fuse.EISDIR);
var fd = { id: 5, mode: flags, attr: attr, fo: attr.fileObject };
fileDescriptors.forEach(function(lfd) {
fd.id = Math.max( fd.id, lfd.id + 1 );
});
fileDescriptors.push(fd);
return cb(0,fd.id);
}, true, 0 );
},
release: function f_release( path, fd, cb, tries ) {
tries = tries || 0;
if(tries>10)return cb(fuse.EIO);
debug('RELEASE',tries,fd,path);
fileDescriptors = fileDescriptors.filter(function(lfd) {
return lfd.id != fd;
});
cb(0);
},
read: function f_read( path, fd, buf, len, pos, cb, tries ) {
tries = tries || 0;
if(tries>10)return cb(fuse.EIO);
debug('READ',tries,fd,pos,len,path);
var lfd = fileDescriptors.filter(function(lfdo) {
return lfdo.id == fd;
}).shift();
if(!lfd)return cb(fuse.ENOENT);
if(lfd.mode.substr(0,1)!='r') return cb(fuse.EBADF);
if ( ( lfd.fo.metadata.size === 0 ) && ( len > 0 ) ) {
return cb(0);
}
streamToBuffer(
lfd.fo.createReadStream({ start: pos, end: Math.min( pos+len, parseInt(lfd.fo.metadata.size) - pos ) }),
function( err, buffer ) {
if(err && (err.code===416)) return cb(0);
if(err)return f_read(path,fd,buf,len,pos,cb,tries+1);
buf.write(buffer.toString());
cb(buffer.length);
}
);
},
create: function f_create( path, flags, cb, tries ) {
tries = tries || 0;
if(tries>10)return cb(fuse.EIO);
debug('CREATE', tries, path, flags);
if ( path.substr(0,1) === '/' ) path = path.substr(1);
var file = new File( bucket, path ),
tmpFile = tmp.tmpNameSync();
fs.writeFileSync(tmpFile,'');
bucket.upload( tmpFile, {
destination: file,
metadata : {
contentType: mime.lookup( path ) || 'application/octet-stream',
}
}, function(err, fileObject) {
fs.unlinkSync(tmpFile);
if (err) return f_create( path, flags, cb, tries + 1 );
ops.open( path, flags, cb );
})
},
write: function f_write( path, fd, buf, len, pos, cb, tries ) {
tries = tries || 0;
if(tries>10)return cb(fuse.EIO);
debug('WRITE', tries, path, fd, len, pos );
if ( path.substr(0,1) === '/' ) path = path.substr(1);
var lfd = fileDescriptors.filter(function(lfdo) {
return lfdo.id == fd;
}).shift();
if(!lfd)return cb(fuse.ENOENT);
streamToBuffer(
lfd.fo.createReadStream(),
function(err, oldContents) {
if (err) return f_write( path, fd, buf, len, pos, cb, tries + 1 );
var ws = lfd.fo.createWriteStream();
ws.on('error', function(err) {
debug('-',err);
cb(fuse.EIO);
});
ws.on('finish', function(err) {cb(len)});
ws.write(oldContents.slice(0,pos));
ws.write(buf.slice(0,len));
ws.end(oldContents.slice(pos+len));
}
)
},
truncate: function f_truncate( path, size, cb, tries ) {
tries = tries || 0;
if(tries>10)return cb(fuse.EIO);
debug('TRUNCATE',tries,path,size);
ops.getattr ( path, function(err,attr) {
streamToBuffer(
attr.fileObject.createReadStream({ start: 0, end: size }),
function( err, buffer ) {
attr.fileObject.createWriteStream()
.on('error', function(err) {
f_truncate( path, size, cb, tries + 1 );
})
.on('finish', function(err) {
cb(0);
})
.end(buffer);
}
);
}, true );
},
unlink: function f_unlink(path, cb, tries) {
tries = tries || 0;
if(tries>10)return cb(fuse.EIO);
debug('UNLINK',tries,path);
if ( path.substr(0,1) === '/' ) path = path.substr(1);
var fileObject = bucket.file(path);
if(!fileObject) return cb(fuse.ENOENT);
fileObject
.delete()
.then(function() {
cb(0);
})
.catch(function(err) {
f_unlink(path,cb,tries+1);
});
},
mkdir: function f_mkdir( path, mode, cb, tries ) {
tries = tries || 0;
if(tries>10)return cb(fuse.EIO);
debug('MKDIR',tries,path,mode);
if ( path.substr(0,1) === '/' ) path = path.substr(1);
if ( path.slice(-1) != '/') path += '/';
var file = new File( bucket, path ),
tmpFile = tmp.tmpNameSync();
fs.writeFileSync(tmpFile,'');
bucket.upload( tmpFile, {
destination: file
}, function(err, fileObject) {
fs.unlinkSync(tmpFile);
if (err) return f_mkdir( path, mode, cb, tries + 1 );
cb(0);
});
},
rmdir: function f_rmdir( path, cb, tries ) {
tries = tries || 0;
if(tries>10)return cb(fuse.EIO);
debug('RMDIR',tries,path);
if ( path.substr(0,1) === '/' ) path = path.substr(1);
if ( path.slice(-1) != '/') path += '/';
ops.unlink(path,function(err) {
if(err)return f_rmdir( path, cb, tries + 1 );
cb(0);
});
},
symlink: function f_symlink( target, path, cb, tries ) {
tries = tries || 0;
if(tries>10)return cb(fuse.EIO);
debug('SYMLINK',tries,path,'=>',target);
if ( path.substr(0,1) === '/' ) path = path.substr(1);
var file = new File( bucket, path ),
tmpFile = tmp.tmpNameSync();
fs.writeFileSync(tmpFile,'');
bucket.upload( tmpFile, {
destination: file,
metadata: {
metadata: {
gcsfuse_symlink_target: target
}
}
}, function(err, fileObject) {
if(err)return f_symlink( target, path, cb, tries + 1 );
cb(0);
});
},
readlink: function f_readlink( path, cb, tries ) {
tries = tries || 0;
if(tries>10)return cb(fuse.EIO);
debug('READLINK',tries,path);
ops.getattr(path,function(err, attr) {
if(err)return f_readlink(path,cb,tries+1);
if(attr.mode !== 'link') return cb(fuse.ENOENT);
cb(0,attr.fileObject.metadata.metadata.gcsfuse_symlink_target);
}, true);
},
fgetattr: function( path, fd, cb ) {
debug('FGETATTR',fd,path);
if ( path.substr(0,1) === '/' ) path = path.substr(1);
var lfd = fileDescriptors.filter(function(lfdo) {
return lfdo.id == fd;
}).shift();
if(!lfd)return cb(fuse.ENOENT);
var fileObject = lfd.fo,
attr = { mode : 'file',
size : parseInt(fileObject.metadata.size),
mtime: Date.parse( fileObject.metadata.updated ),
ctime: Date.parse( fileObject.metadata.updated ) };
if ( fileObject.metadata.metadata && fileObject.metadata.metadata.gcsfuse_symlink_target ) {
attr.mode = 'link';
}
if ( fileObject.name.slice(-1) === '/' ) {
attr.mode = 'dir';
attr.size = 4096;
}
cb(0,stat(attr));
},
fsync: function( path, fd, datasync, cb ) {
debug('FSYNC',fd,path);
cb(0);
},
rename: function f_rename( src, dest, cb, tries ) {
tries = tries || 0;
if(tries>10)return cb(fuse.EIO);
debug('RENAME',tries,src,dest);
if ( dest.substr(0,1) === '/' ) dest = dest.substr(1);
ops.getattr(src,function(err,attr) {
if(err)f_rename(src,dest,cb,tries+1);
if( ( attr.mode === 'dir' ) && ( dest.slice(-1) !== '/' ) ) dest += '/';
attr.fileObject
.move(dest)
.then(function() {
cb(0);
})
.catch(function(err) {
f_rename(src,dest,cb,tries+1);
})
},true);
},
chmod: function( path, mode, cb ) {
debug('CHMOD',path,mode);
cb(0);
},
chown: function( path, uid, gid, cb ) {
debug('CHOWN',path,uid,gid);
cb(0);
},
utimens: function( path, atime, mtime, cb ) {
debug('UTIMENS',path,atime,mtime);
cb(0);
},
}, function(err) {
if(err) throw err;
debug( bucket.id + ' mounted on ' + path );
})
})
.option('verbose', {
alias: 'v',
default: false
})
.option('options', {
alias: 'o',
default: ''
})
.argv
process.on('SIGINT', function() {
fuse.unmount(path,function(err) {
if(err) {
console.error('Couldn\'t unmount ' + path);
} else {
debug( path + ' unmounted');
}
})
});