-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphFuse.js
More file actions
291 lines (270 loc) · 9.02 KB
/
Copy pathGraphFuse.js
File metadata and controls
291 lines (270 loc) · 9.02 KB
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
var fuse = require('node-fuse-bindings')
var onedrive=require('./onedrive-c')
const fs=require('fs')
const sqlite3 = require('sqlite3')
const getConfig = require("./config.js");
require('console-stamp')(console, 'HH:MM:ss.l');
let db = new sqlite3.Database("cache/livedrivecloud.db");
db.run("CREATE TABLE IF NOT EXISTS toupload( path TEXT NOT NULL primary key, tmpfile TEXT NOT NULL, superseeded INT DEFAULT 0)")
_fileToUpload={}
onedrive.ODInterface(onedrive.buildTreeDelta,
{nextURI: "", extra: "", cache: true}, function(v){
onedrive._lastChecked=new Date();
startMount()
}
)
function refreshTree(cb)
{
console.log("Refresh Tree")
onedrive.ODInterface(onedrive.buildTreeDelta,
{nextURI: "", extra: onedrive._lastChecked.toISOString()}, (err)=>{
onedrive._lastChecked=new Date();
cb(0)
}
)
}
function startMount()
{
_dir=onedrive._structure
_waiting=false
db.all( "SELECT * FROM toupload",[],(err,rows)=>{
if( err) console.log( err);
else{
rows.forEach( (row)=>{
//id,path, tmpfile
console.log( row );
var stats=fs.statSync(row.tmpfile)
onedrive.ODInterface(onedrive.msUploadFile, {path:row.path.substring(1), tpmName: row.tmpfile, size: stats.size} ,function (msg) {
console.log( "Msg Uploaded",msg)
db.run('DELETE FROM toupload WHERE path=?',[row.path], function(err) {
if( err)
console.log("Cannot delete ", err.message)
else {
fs.unlink(row.tmpfile,(err=>{
if( err) console.log( err)
else {
_fileToUpload={}
}
}))
}
})
})
})
}
})
fuse.mount(getConfig.mountPath, {
// Force Umount
force: true,
readdir: async function (path, cb) {
console.log('readdir(%s)', path);
_dir=onedrive.findDir(path, onedrive._structure, true);
console.log( _dir );
var _files=[];
for( var b in _dir.folders)
_files.push(_dir.folders[b]['name']);
for( var b in _dir.files)
_files.push(_dir.files[b]['name']);
return cb(0, _files);
},
getattr: function (path, cb) {
console.log('getattr(%s)', path);
var _dt=new Date();
if( (_dt.getTime()-onedrive._lastChecked.getTime())>15000 && !_waiting)
{
_waiting=true;
refreshTree((msg)=>{});
}
var _dir=onedrive.findDir( path, onedrive._structure, true);
// console.log( _dir )
var _file=path.split('/').pop();
if (path === '/') {
cb(0, new onedrive.Folder(''))
return
}
else if( _file in _dir.files)
return cb(null, _dir.files[_file]);
else if( _file in _dir.folders)
return cb(0, _dir.folders[_file]);
else if( _dir.name==_file )
return cb(0, _dir);
cb(fuse.ENOENT)
},
open: function (path, flags, cb) {
// 1 Write, 2 Read, 3 W/R
console.log('open(%s, %d)', path, flags);
cb(0, 42); // 42 is an fd
},
truncate: function (path, size, cb) {
console.log("Truncate ",path, size);
var _dir=onedrive.findDir( path, onedrive._structure, true);
var _file=path.split('/').pop();
if (_file in _dir.files)
_dir.files[_file].size=0;
// SET THE FILE TO 0 SIZE or remove it to decide
cb(0)
},
// chmod: function(path, mode, cb)
// {
// console.log('Chmod %s Mode %s', path, mode);
// cb(0);
// },
create: function (path, flags, cb) {
// created = true
console.log('Create %s Flag %s', path, flags);
var _dir=onedrive.findDir( path, onedrive._structure, true);
if( flags=33188)
{
var _name=path.split('/').pop();
// console.log( _name );
_dir.files[_name]=new onedrive.Folder(_name);
}
_fileToUpload={size: 0, fileRef:null, buffer:[], tmpName:null, db: null};
created = true;
cb(0, 42);
},
setxattr: function(path, name, buffer, length, offset, flags, cb){
console.log("SetXattr Path %s Name %s Len %s offset %s Flags %s",path, name, length, offset, flags);
cb(0);
},
release: async function (path, fd, cb) {
console.log( "Release ",path);
// Called agter read or write finish
// var buf=Buffer.concat(_fileToUpload.buffer)
if( 'fileRef' in _fileToUpload )
{
_fileToUpload.fileRef.end();
if( "startSaving" in _fileToUpload && _fileToUpload.startSaving==0){
_fileToUpload.startSaving=1;
console.log("Saving on cloud ");
await onedrive.ODInterface(onedrive.msUploadFile, {path:path.substring(1), tpmName: _fileToUpload.tmpName, size: _fileToUpload.size} ,function (msg) {
console.log( "Msg Uploaded",msg);
refreshTree( (msg) => {});
db.run('DELETE FROM toupload WHERE path=?',[path], function(err) {
if( err)
console.log("Cannot delete ", err.message);
else {
console.log("Unlink ",_fileToUpload.tmpName)
fs.unlink(_fileToUpload.tmpName,(err=>{
if( err) console.log( err);
else
_fileToUpload={};
}))
}
})
// remove for the db and delete tmp file
} )}
}
cb(0)
},
unlink: async function(path, cb) {
console.log('Unlink '+path);
_dir=onedrive.findDir(path, onedrive._structure, true);
_file=path.split('/').pop();
if( _file in _dir.files)
{
console.log( _dir.files[_file]['id']);
onedrive.ODInterface(onedrive.msUnlink, {itemId: _dir.files[_file]['id'] } , (info, response) =>{
console.log(r);
delete _dir.files[_file];
refreshTree( (msg) => {});
cb(0);
})
}
},
fsync: function(path, fd, datasync, cb)
{
console.log('Fsync', path, datasync);
_dir=onedrive.findDir(path, onedrive._structure,true);
_file=path.split('/').pop();
// msUnlink
if( _file in _dir.files)
_dir.files[_file].mtime= new Date();
cb(0);
},
rename: function(src, dest, cb){
console.log("Rename %s => %s",src,dest);
cb(0);
},
write: async function(path, fd, buf, len, pos, cb){
console.log('writing %s Len %s Pos %s ', path, len, pos);
if(pos==0)
{
console.log("Open Stream")
let tmpName=getConfig.cacheDir+((Math.random() + 1)*99999).toString(16).substring(4);
// You should check if there is a previous path, if it is the case remove it
let stmt=db.run("INSERT INTO toupload (path, tmpfile ) VALUES (?,?)",[path, tmpName], function(err){
if(err) {
console.log("Cannot save ", err.message);
}
})
var stream = await fs.createWriteStream(`${tmpName}`);
_fileToUpload.tmpName=tmpName;
_fileToUpload.fileRef=stream;
_fileToUpload.startSaving=0;
}
else {
_fileToUpload.fileRef.write(Buffer.from(buf));
console.log("Confirm",len);
cb(len);
}
_fileToUpload.fileRef.on('open', async () => {
_fileToUpload.fileRef.write(Buffer.from(buf));
console.log("Confirm",len);
cb(len);
});
_fileToUpload.size+=len;
console.log("END Write");
},
read: async function (path, fd, buf, len, pos, cb) {
console.log('read(%s Pos %d Len %d)', path, pos,len);
var _dir=onedrive.findDir( path, onedrive._structure, true);
_file=path.split('/').pop();
if( _file in _dir.files && 'new' in _dir.files[_file])
{
console.log("New file");
return cb(0);
}
var _response='';
var _link='';
var _info=0;
await onedrive.ODInterface(onedrive.msDownload, {path:path} ,function (info, response) {
console.log( 'Info ', info);
_info=info;
if( info==200 && response!='')
{
var _pos=pos+'-'+(len+pos-1)
console.log( _pos)
onedrive.ODInterface(onedrive.msDownloadPartial, {uri:response, range: _pos } ,function (info, response) {
if( info==200)
{
var part=response.slice( 0, len);
part.copy(buf);
return cb(part.length);
}
else
return cb(0);
})
}
// console.log('AB')
});
}
}, function (err) {
if (err) throw err;
console.log('filesystem mounted on ' + getConfig.mountPath);
})
process.on('SIGINT', function () {
db.close((err) => {
if (err)
return console.error(err.message);
console.log('Close the database connection.');
});
fuse.unmount(getConfig.mountPath, function (err) {
if (err) {
console.log('filesystem at ' + getConfig.mountPath + ' not unmounted', err)
} else {
console.log('filesystem at ' + getConfig.mountPath + ' unmounted')
}
});
})
}
// refreshTree( (err)=> {startMount()})