-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLocalFilesystem.cpp
More file actions
629 lines (572 loc) · 15.5 KB
/
LocalFilesystem.cpp
File metadata and controls
629 lines (572 loc) · 15.5 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
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
#include "LocalFilesystem.h"
#include <Application.h>
#include <Message.h>
#include <FindDirectory.h>
#include <NodeMonitor.h>
#include <String.h>
#include "DropboxSupport.h"
#include "Globals.h"
BList LocalFilesystem::sTrackedEntries = BList();
BList LocalFilesystem::sIgnoredEntries = BList();
BLocker* LocalFilesystem::sIgnoredEntriesLocker = new BLocker(true);
void LocalFilesystem::CheckOrCreateRootFolder()
{
BPath userpath;
BDirectory directory;
if (find_directory(B_USER_DIRECTORY, &userpath) == B_OK)
{
userpath.Append(fCloudRootPath);
directory.CreateDirectory(userpath.Path(), NULL);
}
}
void LocalFilesystem::RecursiveDelete(const char *path)
{
BPath userpath;
BDirectory directory;
BEntry fsentry;
node_ref ref;
directory.SetTo(path);
while (directory.GetNextEntry(&fsentry) == B_OK)
{
fsentry.GetNodeRef(&ref);
StopWatchingNodeRef(&ref);
if (fsentry.IsDirectory()) {
fsentry.GetPath(&userpath);
RecursiveDelete(userpath.Path());
}
fsentry.Remove();
}
}
bool LocalFilesystem::TestLocation(BMessage * dbMessage)
{
BPath userpath;
BDirectory directory;
BEntry fsentry;
bool needsUpdate = false;
BString entryPath = NULL;
BString entryType = NULL;
if (find_directory(B_USER_DIRECTORY, &userpath) == B_OK)
{
userpath.Append(fCloudRootPath);
directory.SetTo(userpath.Path());
entryType=dbMessage->GetString(".tag");
entryPath=dbMessage->GetString("path_display");
entryPath.RemoveFirst("/");
if (entryType=="file") {
//look for file on filesystem
fsentry = BEntry(&directory, entryPath.String(), true);
if (fsentry.Exists()) {
time_t modtime, sModified;
off_t size;
off_t sSize = 0;
//compare size, modified date
BNode node (&fsentry);
node.GetModificationTime(&modtime);
node.GetSize(&size);
sSize=(off_t)dbMessage->GetDouble("size",0); //this shouldn't be a double?
//TODO: eww
sModified = DropboxSupport::ConvertTimestampToSystem(dbMessage->GetString("client_modified"));
needsUpdate = (sSize != size || sModified > modtime);
} else {
needsUpdate = true;
}
}
else if (entryType=="folder")
{
//can just create a folder now if it's missing
fsentry = BEntry(&directory, entryPath.String(), true);
if (!fsentry.Exists()) {
fsentry.GetPath(&userpath);
directory.CreateDirectory(userpath.Path(), NULL);
// flag that we can potentially pull down this entire directory
// in a zip file
//also want to watch it after downloads are complete, so need this on
needsUpdate = true;
} else {
needsUpdate = false;
}
}
else if (entryType=="deleted")
{
//attempt to remove entry if it's present
fsentry = BEntry(&directory, entryPath.String(), true);
if (fsentry.Exists()) {
if (fsentry.IsDirectory()) {
node_ref ref;
//directory requires recursive delete...
fsentry.GetPath(&userpath);
fsentry.GetNodeRef(&ref);
StopWatchingNodeRef(&ref);
RecursiveDelete(userpath.Path());
}
fsentry.Remove();
}
needsUpdate = false;
}
else {
gGlobals.SendNotification("Filesystem","Unhandled entryType encountered", true);
}
}
return needsUpdate;
}
//
bool LocalFilesystem::ResolveUnreferencedLocals(const char * leaf, BList & remote, BList & local, bool forceFull)
{
BPath userpath;
BDirectory directory;
BEntry fsentry;
if (find_directory(B_USER_DIRECTORY, &userpath) == B_OK)
{
userpath.Append(fCloudRootPath);
BString hardRoot = userpath.Path();
userpath.Append(leaf);
BString fullRoot = userpath.Path();
directory.SetTo(userpath.Path());
while (directory.GetNextEntry(&fsentry) == B_OK)
{
time_t localModified;
time_t lastSync = gSettings.lastLocalSync;
fsentry.GetPath(&userpath);
fsentry.GetModificationTime(&localModified);
BString strippedPath = BString(userpath.Path());
strippedPath.RemoveFirst(hardRoot);
if ((!IsInRemoteList(strippedPath.String(), localModified, remote) && (lastSync> 0 && localModified> lastSync)) || lastSync == 0 || forceFull)
{
// add pending upload
BMessage * pending = new BMessage();
pending->AddString(".tag", fsentry.IsDirectory()? "folder" : "file");
pending->AddString("path_display", strippedPath);
local.AddItem(pending);
}
if (fsentry.IsDirectory())
{
strippedPath.RemoveFirst("/");
//recurse
ResolveUnreferencedLocals(strippedPath.String(), remote, local, forceFull);
}
}
return true;
}
return false;
}
bool LocalFilesystem::SendMissing(BList & items)
{
bool result = true;
BPath userpath;
if (find_directory(B_USER_DIRECTORY, &userpath) == B_OK)
{
userpath.Append(fCloudRootPath);
for(int i=0; i < items.CountItems(); i++)
{
BEntry fsentry;
time_t sModified;
off_t sSize = 0;
BMessage * item = (BMessage*)items.ItemAtFast(i);
BString entryPath = item->GetString("path_display");
BString entryType = item->GetString(".tag");
BString fullPath = BString(userpath.Path());
fsentry = BEntry(fullPath.String());
fsentry.GetModificationTime(&sModified);
fsentry.GetSize(&sSize);
fullPath.Append(entryPath);
if (entryType=="file") {
fManager->QueueUpload(fullPath.String(), entryPath.String(), sModified, sSize);
} else if (entryType=="folder") {
fManager->QueueCreate(entryPath.String());
}
// we don't support DIRs by Zip yet
}
//update the lastLocalSync time
gSettings.Lock();
gSettings.lastLocalSync = time(NULL);
gSettings.SaveSettings();
gSettings.Unlock();
}
return result;
}
bool LocalFilesystem::IsInRemoteList(const char * path, time_t localModified, BList & remote)
{
for(int i=0; i < remote.CountItems(); i++)
{
BMessage * ref = (BMessage*)remote.ItemAtFast(i);
BString remotePath = ref->GetString("path_display");
if (remotePath==path)
{
BString timestamp = ref->GetString("client_modified");
if (timestamp == "")
return true;
time_t sModified = DropboxSupport::ConvertTimestampToSystem(timestamp);
return sModified <= localModified;
}
}
return false;
}
void LocalFilesystem::StopWatchingNodeRef(node_ref *nref)
{
watch_node(nref, B_STOP_WATCHING, BMessenger(this));
LocalFilesystem::RemoveTrackedEntry(nref);
}
void LocalFilesystem::WatchEntry(BEntry *entry, uint32 flags)
{
node_ref nref;
entry->GetNodeRef(&nref);
if (flags == B_STOP_WATCHING)
{
RemoveTrackedEntry(&nref);
}
else
{
trackeddata * td = new trackeddata();
td->nref = nref;
entry->GetPath(td->path);
sTrackedEntries.AddItem(td);
}
watch_node(&nref, flags, BMessenger(this));
}
void LocalFilesystem::RecursivelyWatchDirectory(const char * fullPath, uint32 flags)
{
BDirectory directory;
BEntry entry;
directory.SetTo(fullPath);
//add node watcher to this dir
directory.GetEntry(&entry);
WatchEntry(&entry, flags);
//add node watchers to any child dirs
while (directory.GetNextEntry(&entry) == B_OK)
{
if (entry.IsDirectory())
{
BPath userpath;
entry.GetPath(&userpath);
RecursivelyWatchDirectory(userpath.Path(), flags);
} else {
WatchEntry(&entry, flags);
}
}
}
void LocalFilesystem::RecursiveAddToCloud(const char *fullPath)
{
BDirectory directory;
BEntry entry;
BString dbpath;
directory.SetTo(fullPath);
directory.GetEntry(&entry);
while (directory.GetNextEntry(&entry) == B_OK)
{
BPath userpath;
entry.GetPath(&userpath);
dbpath = BString(userpath.Path());
ConvertFullPathToCloudRelativePath(dbpath);
if (entry.IsDirectory())
{
fManager->QueueCreate(dbpath);
RecursiveAddToCloud(userpath.Path());
}
else
{
off_t size;
time_t modified;
entry.GetModificationTime(&modified);
entry.GetSize(&size);
fManager->QueueUpload(userpath.Path(), dbpath, modified, size);
}
WatchEntry(&entry, WATCH_FLAGS);
}
}
void LocalFilesystem::ConvertFullPathToCloudRelativePath(BString &full)
{
BString dbpath = BString(fCloudRootPath);
ApplyFullPathToRelativeBasePath(dbpath);
full.RemoveFirst(dbpath);
}
void LocalFilesystem::ApplyFullPathToRelativeBasePath(BString &relative)
{
BPath userpath;
if (find_directory(B_USER_DIRECTORY, &userpath) == B_OK)
{
userpath.Append(relative.String());
relative = BString(userpath.Path());
}
}
void LocalFilesystem::WatchDirectories()
{
BString path = BString(fCloudRootPath);
LocalFilesystem::ApplyFullPathToRelativeBasePath(path);
LocalFilesystem::RecursivelyWatchDirectory(path, WATCH_FLAGS);
}
LocalFilesystem::trackeddata * LocalFilesystem::FindTrackedEntry(node_ref find)
{
for (int i=0; i<sTrackedEntries.CountItems(); i++)
{
trackeddata * file = (trackeddata *)sTrackedEntries.ItemAt(i);
if (find.node == file->nref.node && find.device == file->nref.device)
return file;
}
return NULL;
}
void LocalFilesystem::RemoveTrackedEntriesForPath(const char *fullPath)
{
for (int i=0; i<sTrackedEntries.CountItems(); i++)
{
trackeddata *file = (trackeddata *)sTrackedEntries.ItemAt(i);
BString path = BString(file->path->Path());
if (path.StartsWith(fullPath))
{
watch_node(&file->nref, B_STOP_WATCHING, BMessenger(this));
sTrackedEntries.RemoveItem(i);
delete file;
}
}
}
void LocalFilesystem::RemoveTrackedEntry(node_ref * find)
{
for (int i=0; i<sTrackedEntries.CountItems(); i++)
{
trackeddata *file = (trackeddata *)sTrackedEntries.ItemAt(i);
if (find->node == file->nref.node && find->device == file->nref.device)
{
sTrackedEntries.RemoveItem(i);
delete file;
}
}
}
void LocalFilesystem::AddToIgnoreList(const char * fullPath)
{
BString * ignoring = new BString(fullPath);
sIgnoredEntriesLocker->Lock();
sIgnoredEntries.AddItem(ignoring);
sIgnoredEntriesLocker->Unlock();
}
void LocalFilesystem::RemoveFromIgnoreList(const char * fullPath)
{
for (int i=0; i<sIgnoredEntries.CountItems(); i++)
{
BString *ignoring = (BString *)sIgnoredEntries.ItemAt(i);
if (strcmp(ignoring->String(), fullPath) == 0)
{
sIgnoredEntriesLocker->Lock();
sIgnoredEntries.RemoveItem(i);
sIgnoredEntriesLocker->Unlock();
delete ignoring;
}
}
}
bool LocalFilesystem::IsInIgnoredList(const char *fullPath)
{
for (int i=0; i<sIgnoredEntries.CountItems(); i++)
{
BString *ignoring = (BString *)sIgnoredEntries.ItemAt(i);
if (strcmp(ignoring->String(), fullPath) == 0)
{
return true;
}
}
return false;
}
void LocalFilesystem::HandleCreated(BMessage * msg)
{
entry_ref ref;
BPath path;
const char * name;
msg->FindInt32("device", &ref.device);
msg->FindInt64("directory", &ref.directory);
msg->FindString("name", &name);
ref.set_name(name);
// doesn't support symlinks currently, so no "copying" folders from Desktop
BEntry new_file = BEntry(&ref);
new_file.GetPath(&path);
BString dbpath = BString(path.Path());
BString fullpath = BString(path.Path());
ConvertFullPathToCloudRelativePath(dbpath);
if (new_file.IsDirectory())
{
if (!IsInIgnoredList(path.Path())) {
fManager->QueueCreate(dbpath);
//need to recursively upload folder contents
RecursiveAddToCloud(path.Path());
WatchEntry(&new_file, WATCH_FLAGS);
}
}
else
{
off_t size;
time_t modified;
new_file.GetModificationTime(&modified);
new_file.GetSize(&size);
if (!IsInIgnoredList(path.Path()) && new_file.Exists()) {
fManager->QueueUpload(fullpath, dbpath, modified, size);
}
WatchEntry(&new_file, WATCH_FLAGS);
}
}
void LocalFilesystem::HandleMoved(BMessage * msg)
{
entry_ref from_ref, to_ref;
node_ref nref;
BEntry from_entry, to_entry;
BPath path;
const char * name;
BDirectory dbdirectory;
BString dbpath = BString(fCloudRootPath);
trackeddata * tracked_file;
ApplyFullPathToRelativeBasePath(dbpath);
dbdirectory = BDirectory(dbpath);
//can't move from one device to another, so all the same
msg->FindInt32("device", &from_ref.device);
msg->FindInt32("device", &to_ref.device);
msg->FindInt32("device", &nref.device);
msg->FindInt64("from directory", &from_ref.directory);
msg->FindInt64("to directory", &to_ref.directory);
msg->FindString("name", &name);
msg->FindInt64("node", &nref.node);
to_ref.set_name(name);
to_entry = BEntry(&to_ref);
to_entry.GetPath(&path);
tracked_file = FindTrackedEntry(nref);
if (dbdirectory.Contains(&to_entry))
{
if (tracked_file == NULL) {
off_t size;
time_t modified;
// moving into DropBox
to_entry.GetPath(&path);
BString fullpath = BString(path.Path());
BString topath = BString(path.Path());
ConvertFullPathToCloudRelativePath(topath);
to_entry.GetModificationTime(&modified);
to_entry.GetSize(&size);
if (!IsInIgnoredList(fullpath)) {
if (to_entry.IsDirectory())
{
//upload contents of directory also as it's not being tracked yet
RecursiveAddToCloud(fullpath);
} else {
fManager->QueueUpload(fullpath, topath, modified, size);
}
WatchEntry(&to_entry, WATCH_FLAGS);
}
}
else {
// moving within DropBox
from_entry = BEntry(tracked_file->path->Path());
BString frompath = BString(tracked_file->path->Path());
to_entry.GetPath(&path);
BString topath = BString(path.Path());
ConvertFullPathToCloudRelativePath(frompath);
ConvertFullPathToCloudRelativePath(topath);
StopWatchingNodeRef(&tracked_file->nref);
if (!IsInIgnoredList(path.Path())) {
fManager->QueueMove(frompath, topath);
WatchEntry(&to_entry, WATCH_FLAGS);
}
}
} else {
// deleted from DropBox
if (tracked_file != NULL) {
from_entry = BEntry(tracked_file->path->Path());
BString frompath = BString(tracked_file->path->Path());
ConvertFullPathToCloudRelativePath(frompath);
if (!IsInIgnoredList(tracked_file->path->Path())) {
if (from_entry.IsDirectory())
{
RemoveTrackedEntriesForPath(tracked_file->path->Path());
}
else
{
StopWatchingNodeRef(&tracked_file->nref);
}
fManager->QueueDelete(frompath);
}
}
}
}
void LocalFilesystem::HandleRemoved(BMessage * msg)
{
node_ref nref;
trackeddata * td;
BEntry entry;
BString path;
msg->FindInt64("node", &nref.node);
msg->FindInt32("device", &nref.device);
td = FindTrackedEntry(nref);
if (td != NULL && !IsInIgnoredList(td->path->Path()))
{
entry = BEntry(td->path->Path());
path = BString(td->path->Path());
if (entry.IsDirectory())
{
RemoveTrackedEntriesForPath(td->path->Path());
}
else
{
StopWatchingNodeRef(&td->nref);
}
ConvertFullPathToCloudRelativePath(path);
fManager->QueueDelete(path);
}
}
void LocalFilesystem::HandleChanged(BMessage * msg)
{
BPath path;
node_ref nref;
trackeddata * td;
BEntry entry;
time_t modified;
off_t size;
msg->FindInt64("node", &nref.node);
msg->FindInt32("device", &nref.device);
td = FindTrackedEntry(nref);
if (td != NULL)
{
entry.SetTo(td->path->Path());
BString dbpath = BString(td->path->Path());
ConvertFullPathToCloudRelativePath(dbpath);
entry.GetModificationTime(&modified);
entry.GetSize(&size);
if (!IsInIgnoredList(td->path->Path())) {
fManager->QueueUpload(td->path->Path(), dbpath, modified, size);
}
}
}
void LocalFilesystem::MessageReceived(BMessage *msg)
{
switch(msg->what)
{
case B_NODE_MONITOR:
{
HandleNodeEvent(msg);
break;
}
}
}
void LocalFilesystem::HandleNodeEvent(BMessage *msg)
{
int32 opcode;
if (msg->FindInt32("opcode",&opcode) == B_OK)
{
//many of these ops are triggered twice due to watching the folders
//and the files themselves
switch(opcode)
{
case B_ENTRY_CREATED:
HandleCreated(msg);
break;
case B_ENTRY_MOVED:
HandleMoved(msg);
break;
case B_ENTRY_REMOVED:
HandleRemoved(msg);
break;
case B_STAT_CHANGED:
{
int32 fields;
msg->FindInt32("fields", &fields);
if ((fields & B_STAT_MODIFICATION_TIME) != 0)
{
HandleChanged(msg);
}
break;
}
}
}
}