forked from scrod/nv
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNotationDirectoryManager.m
510 lines (372 loc) · 17.9 KB
/
NotationDirectoryManager.m
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
//
// NotationDirectoryManager.m
// Notation
//
// Created by Zachary Schneirov on 12/10/09.
/*Copyright (c) 2010, Zachary Schneirov. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this list of conditions
and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of
conditions and the following disclaimer in the documentation and/or other materials provided with
the distribution.
- Neither the name of Notational Velocity nor the names of its contributors may be used to endorse
or promote products derived from this software without specific prior written permission. */
#import "NotationDirectoryManager.h"
#import "NotationPrefs.h"
#import "GlobalPrefs.h"
#import "NotationSyncServiceManager.h"
#import "NoteObject.h"
#import "DeletionManager.h"
#import "NSCollection_utils.h"
#define kMaxFileIteratorCount 100
@implementation NotationController (NotationDirectoryManager)
NSInteger compareCatalogEntryName(const void *one, const void *two) {
return (int)CFStringCompare((CFStringRef)((*(NoteCatalogEntry **)one)->filename),
(CFStringRef)((*(NoteCatalogEntry **)two)->filename), kCFCompareCaseInsensitive);
}
NSInteger compareCatalogValueNodeID(id *a, id *b) {
NoteCatalogEntry* aEntry = (NoteCatalogEntry*)[*(id*)a pointerValue];
NoteCatalogEntry* bEntry = (NoteCatalogEntry*)[*(id*)b pointerValue];
return aEntry->nodeID - bEntry->nodeID;
}
NSInteger compareCatalogValueFileSize(id *a, id *b) {
NoteCatalogEntry* aEntry = (NoteCatalogEntry*)[*(id*)a pointerValue];
NoteCatalogEntry* bEntry = (NoteCatalogEntry*)[*(id*)b pointerValue];
return aEntry->logicalSize - bEntry->logicalSize;
}
- (void)stopFileNotifications {
OSStatus err = noErr;
if (!IsZeros(¬eDirSubscription, sizeof(FNSubscriptionRef))) {
if ((err = FNUnsubscribe(noteDirSubscription)) != noErr) {
NSLog(@"Could not unsubscribe from note changes callback: %d", err);
} else {
bzero(¬eDirSubscription, sizeof(FNSubscriptionRef));
}
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(synchronizeNotesFromDirectory) object:nil];
}
}
void NotesDirFNSubscriptionProc(FNMessage message, OptionBits flags, void * refcon, FNSubscriptionRef subscription) {
//this only works for the Finder and perhaps the navigation manager right now
if (kFNDirectoryModifiedMessage == message) {
//NSLog(@"note directory changed");
if (refcon) {
[NSObject cancelPreviousPerformRequestsWithTarget:(id)refcon selector:@selector(synchronizeNotesFromDirectory) object:nil];
[(id)refcon performSelector:@selector(synchronizeNotesFromDirectory) withObject:nil afterDelay:0.0];
}
} else {
NSLog(@"we received an FNSubscr. callback and the directory didn't actually change?");
}
}
- (BOOL)synchronizeNotesFromDirectory {
if ([self currentNoteStorageFormat] == SingleDatabaseFormat) {
//NSLog(@"%s: called when storage format is singledatabase", _cmd);
return NO;
}
//NSDate *date = [NSDate date];
if ([self _readFilesInDirectory]) {
//NSLog(@"read files in directory");
directoryChangesFound = NO;
if (catEntriesCount && [allNotes count]) {
[self makeNotesMatchCatalogEntries:sortedCatalogEntries ofSize:catEntriesCount];
} else {
unsigned int i;
if (![allNotes count]) {
//no notes exist, so every file must be new
for (i=0; i<catEntriesCount; i++) {
if ([notationPrefs catalogEntryAllowed:sortedCatalogEntries[i]])
[self addNoteFromCatalogEntry:sortedCatalogEntries[i]];
}
}
if (!catEntriesCount) {
//there is nothing at all in the directory, so remove all the notes
//we probably shouldn't get here; there should be at least a database file and random .DS_Store-like crap
[[DeletionManager sharedManager] addDeletedNotes:allNotes];
}
}
if (directoryChangesFound) {
[self resortAllNotes];
[self refilterNotes];
}
//NSLog(@"file sync time: %g, ",[[NSDate date] timeIntervalSinceDate:date]);
return YES;
}
return NO;
}
//scour the notes directory for fresh meat
- (BOOL)_readFilesInDirectory {
OSStatus status = noErr;
FSIterator dirIterator;
ItemCount totalObjects = 0, dirObjectCount = 0;
unsigned int i = 0, catIndex = 0;
//something like 16 VM pages used here?
if (!fsCatInfoArray) fsCatInfoArray = (FSCatalogInfo *)calloc(kMaxFileIteratorCount, sizeof(FSCatalogInfo));
if (!HFSUniNameArray) HFSUniNameArray = (HFSUniStr255 *)calloc(kMaxFileIteratorCount, sizeof(HFSUniStr255));
if ((status = FSOpenIterator(¬eDirectoryRef, kFSIterateFlat, &dirIterator)) == noErr) {
//catEntriesCount = 0;
do {
// Grab a batch of source files to process from the source directory
status = FSGetCatalogInfoBulk(dirIterator, kMaxFileIteratorCount, &dirObjectCount, NULL,
kFSCatInfoNodeFlags | kFSCatInfoFinderInfo | kFSCatInfoContentMod | kFSCatInfoDataSizes | kFSCatInfoNodeID,
fsCatInfoArray, NULL, NULL, HFSUniNameArray);
if ((status == errFSNoMoreItems || status == noErr) && dirObjectCount) {
status = noErr;
totalObjects += dirObjectCount;
if (totalObjects > totalCatEntriesCount) {
unsigned int oldCatEntriesCount = totalCatEntriesCount;
totalCatEntriesCount = totalObjects;
catalogEntries = (NoteCatalogEntry *)realloc(catalogEntries, totalObjects * sizeof(NoteCatalogEntry));
sortedCatalogEntries = (NoteCatalogEntry **)realloc(sortedCatalogEntries, totalObjects * sizeof(NoteCatalogEntry*));
//clear unused memory to make filename and filenameChars null
size_t newSpace = (totalCatEntriesCount - oldCatEntriesCount) * sizeof(NoteCatalogEntry);
bzero(catalogEntries + oldCatEntriesCount, newSpace);
}
for (i = 0; i < dirObjectCount; i++) {
// Only read files, not directories
if (!(fsCatInfoArray[i].nodeFlags & kFSNodeIsDirectoryMask)) {
//filter these only for files that will be added
//that way we can catch changes in files whose format is still being lazily updated
NoteCatalogEntry *entry = &catalogEntries[catIndex];
HFSUniStr255 *filename = &HFSUniNameArray[i];
entry->fileType = ((FileInfo *)fsCatInfoArray[i].finderInfo)->fileType;
entry->logicalSize = (UInt32)(fsCatInfoArray[i].dataLogicalSize & 0xFFFFFFFF);
entry->nodeID = (UInt32)fsCatInfoArray[i].nodeID;
entry->lastModified = fsCatInfoArray[i].contentModDate;
if (filename->length > entry->filenameCharCount) {
entry->filenameCharCount = filename->length;
entry->filenameChars = (UniChar*)realloc(entry->filenameChars, entry->filenameCharCount * sizeof(UniChar));
}
memcpy(entry->filenameChars, filename->unicode, filename->length * sizeof(UniChar));
if (!entry->filename)
entry->filename = CFStringCreateMutableWithExternalCharactersNoCopy(NULL, entry->filenameChars, filename->length, entry->filenameCharCount, kCFAllocatorNull);
else
CFStringSetExternalCharactersNoCopy(entry->filename, entry->filenameChars, filename->length, entry->filenameCharCount);
catIndex++;
}
}
catEntriesCount = catIndex;
}
} while (status == noErr);
FSCloseIterator(dirIterator);
for (i=0; i<catEntriesCount; i++) {
sortedCatalogEntries[i] = &catalogEntries[i];
}
return YES;
}
NSLog(@"Error opening FSIterator: %d", status);
return NO;
}
- (BOOL)modifyNoteIfNecessary:(NoteObject*)aNoteObject usingCatalogEntry:(NoteCatalogEntry*)catEntry {
//check dates
UTCDateTime lastReadDate = fileModifiedDateOfNote(aNoteObject);
UTCDateTime fileModDate = catEntry->lastModified;
//should we always update the note's stored inode here regardless?
if (lastReadDate.lowSeconds != fileModDate.lowSeconds ||
lastReadDate.highSeconds != fileModDate.highSeconds ||
lastReadDate.fraction != fileModDate.fraction) {
//assume the file on disk was modified by someone other than us
//figure out whether there is a conflict; is this file on disk older than the one that we have in memory? do we merge?
//if ((UInt64*)&fileModDate > (UInt64*)&lastReadDate)
//check if this note has changes in memory that still need to be committed -- that we _know_ the other writer never had a chance to see
if (![unwrittenNotes containsObject:aNoteObject]) {
if (![aNoteObject updateFromCatalogEntry:catEntry]) {
NSLog(@"file %@ was modified but could not be updated", catEntry->filename);
//return NO;
}
//do not call makeNoteDirty because use of the WAL in this instance would cause redundant disk activity
//in the event of a crash this change could still be recovered;
[aNoteObject registerModificationWithOwnedServices];
[self schedulePushToAllSyncServicesForNote:aNoteObject];
[self note:aNoteObject attributeChanged:NotePreviewString]; //reverse delegate?
[delegate contentsUpdatedForNote:aNoteObject];
[self performSelector:@selector(scheduleUpdateListForAttribute:) withObject:NoteDateModifiedColumnString afterDelay:0.0];
notesChanged = YES;
NSLog(@"FILE WAS MODIFIED: %@", catEntry->filename);
return YES;
} else {
//it's a conflict! we win.
NSLog(@"%@ was modified with unsaved changes in NV! Deciding the conflict in favor of NV.", catEntry->filename);
}
}
return NO;
}
- (void)makeNotesMatchCatalogEntries:(NoteCatalogEntry**)catEntriesPtrs ofSize:(size_t)catCount {
unsigned int aSize = [allNotes count];
unsigned int bSize = catCount;
ResizeBuffer((void***)&allNotesBuffer, aSize, &allNotesBufferSize);
assert(allNotesBuffer != NULL);
NoteObject **currentNotes = allNotesBuffer;
[allNotes getObjects:(id*)currentNotes];
mergesort((void *)allNotesBuffer, (size_t)aSize, sizeof(id), (int (*)(const void *, const void *))compareFilename);
mergesort((void *)catEntriesPtrs, (size_t)bSize, sizeof(NoteCatalogEntry*), (int (*)(const void *, const void *))compareCatalogEntryName);
NSMutableArray *addedEntries = [NSMutableArray array];
NSMutableArray *removedEntries = [NSMutableArray array];
//oldItems(a,i) = currentNotes
//newItems(b,j) = catEntries;
unsigned int i, j, lastInserted = 0;
for (i=0; i<aSize; i++) {
BOOL exitedEarly = NO;
for (j=lastInserted; j<bSize; j++) {
CFComparisonResult order = CFStringCompare((CFStringRef)(catEntriesPtrs[j]->filename),
(CFStringRef)filenameOfNote(currentNotes[i]),
kCFCompareCaseInsensitive);
if (order == kCFCompareGreaterThan) { //if (A[i] < B[j])
lastInserted = j;
exitedEarly = YES;
//NSLog(@"FILE DELETED (during): %@", filenameOfNote(currentNotes[i]));
[removedEntries addObject:currentNotes[i]];
break;
} else if (order == kCFCompareEqualTo) { //if (A[i] == B[j])
//the name matches, so add this to changed iff its contents also changed
lastInserted = j + 1;
exitedEarly = YES;
[self modifyNoteIfNecessary:currentNotes[i] usingCatalogEntry:catEntriesPtrs[j]];
break;
}
//NSLog(@"FILE ADDED (during): %@", catEntriesPtrs[j]->filename);
if ([notationPrefs catalogEntryAllowed:catEntriesPtrs[j]])
[addedEntries addObject:[NSValue valueWithPointer:catEntriesPtrs[j]]];
}
if (!exitedEarly) {
//element A[i] "appended" to the end of list B
if (CFStringCompare((CFStringRef)filenameOfNote(currentNotes[i]),
(CFStringRef)(catEntriesPtrs[MIN(lastInserted, bSize-1)]->filename),
kCFCompareCaseInsensitive) == kCFCompareGreaterThan) {
lastInserted = bSize;
//NSLog(@"FILE DELETED (after): %@", filenameOfNote(currentNotes[i]));
[removedEntries addObject:currentNotes[i]];
}
}
}
for (j=lastInserted; j<bSize; j++) {
//NSLog(@"FILE ADDED (after): %@", catEntriesPtrs[j]->filename);
if ([notationPrefs catalogEntryAllowed:catEntriesPtrs[j]])
[addedEntries addObject:[NSValue valueWithPointer:catEntriesPtrs[j]]];
}
if ([addedEntries count] && [removedEntries count]) {
[self processNotesAddedByCNID:addedEntries removed:removedEntries];
} else {
if (![removedEntries count]) {
for (i=0; i<[addedEntries count]; i++) {
[self addNoteFromCatalogEntry:(NoteCatalogEntry*)[[addedEntries objectAtIndex:i] pointerValue]];
}
}
if (![addedEntries count]) {
[[DeletionManager sharedManager] addDeletedNotes:removedEntries];
}
}
}
//find renamed notes through unique file IDs
//TODO: reconcile the "actually" added/deleted files into renames for files with identical content (sort by size)
- (void)processNotesAddedByCNID:(NSMutableArray*)addedEntries removed:(NSMutableArray*)removedEntries {
unsigned int aSize = [removedEntries count], bSize = [addedEntries count];
//sort on nodeID here
[addedEntries sortUnstableUsingFunction:compareCatalogValueNodeID];
[removedEntries sortUnstableUsingFunction:compareNodeID];
// NSMutableArray *hfsAddedEntries = [NSMutableArray array];
// NSMutableArray *hfsRemovedEntries = [NSMutableArray array];
//oldItems(a,i) = currentNotes
//newItems(b,j) = catEntries;
unsigned int i, j, lastInserted = 0;
for (i=0; i<aSize; i++) {
NoteObject *currentNote = [removedEntries objectAtIndex:i];
BOOL exitedEarly = NO;
for (j=lastInserted; j<bSize; j++) {
NoteCatalogEntry *catEntry = (NoteCatalogEntry *)[[addedEntries objectAtIndex:j] pointerValue];
int order = catEntry->nodeID - fileNodeIDOfNote(currentNote);
if (order > 0) { //if (A[i] < B[j])
lastInserted = j;
exitedEarly = YES;
NSLog(@"File deleted as per CNID: %@", filenameOfNote(currentNote));
//[hfsRemovedEntries addObject:currentNote];
[[DeletionManager sharedManager] addDeletedNote:currentNote];
break;
} else if (order == 0) { //if (A[i] == B[j])
lastInserted = j + 1;
exitedEarly = YES;
//note was renamed!
NSLog(@"File %@ was renamed to %@", filenameOfNote(currentNote), catEntry->filename);
if (![self modifyNoteIfNecessary:currentNote usingCatalogEntry:catEntry]) {
//at least update the file name, because we _know_ that changed
directoryChangesFound = YES;
[currentNote setFilename:(NSString*)catEntry->filename withExternalTrigger:YES];
}
notesChanged = YES;
break;
}
//a new file was found on the disk! read it into memory!
NSLog(@"File added as per CNID: %@", catEntry->filename);
[self addNoteFromCatalogEntry:catEntry];
}
if (!exitedEarly) {
NoteCatalogEntry *appendedCatEntry = (NoteCatalogEntry *)[[addedEntries objectAtIndex:MIN(lastInserted, bSize-1)] pointerValue];
if (fileNodeIDOfNote(currentNote) - appendedCatEntry->nodeID > 0) {
lastInserted = bSize;
//file deleted from disk;
NSLog(@"File deleted as per CNID: %@", filenameOfNote(currentNote));
//[hfsRemovedEntries addObject:currentNote];
[[DeletionManager sharedManager] addDeletedNote:currentNote];
}
}
}
for (j=lastInserted; j<bSize; j++) {
NoteCatalogEntry *appendedCatEntry = (NoteCatalogEntry *)[[addedEntries objectAtIndex:j] pointerValue];
NSLog(@"File _actually_ added: %@", appendedCatEntry->filename);
[self addNoteFromCatalogEntry:appendedCatEntry];
}
}
#if 0
- (void)processNotesAddedByContent:(NSMutableArray*)addedEntries removed:(NSMutableArray*)removedEntries {
unsigned int aSize = [removedEntries count], bSize = [addedEntries count];
//sort on file size here
[addedEntries sortUnstableUsingFunction:compareCatalogValueFileSize];
[removedEntries sortUnstableUsingFunction:compareFileSize];
unsigned int i, j, lastInserted = 0;
for (i=0; i<aSize; i++) {
NoteObject *currentNote = [removedEntries objectAtIndex:i];
BOOL exitedEarly = NO;
for (j=lastInserted; j<bSize; j++) {
NoteCatalogEntry *catEntry = (NoteCatalogEntry *)[[addedEntries objectAtIndex:j] pointerValue];
int order = catEntry->logicalSize - fileSizeOfNote(currentNote);
if (order > 0) { //if (A[i] < B[j])
lastInserted = j;
exitedEarly = YES;
NSLog(@"File _actually_ deleted: %@", filenameOfNote(currentNote));
[[DeletionManager sharedManager] addDeletedNote:currentNote];
break;
} else if (order == 0) { //if (A[i] == B[j])
lastInserted = j + 1;
exitedEarly = YES;
//note was renamed!
NSLog(@"File %@ was renamed to %@", filenameOfNote(currentNote), catEntry->filename);
if (![self modifyNoteIfNecessary:currentNote usingCatalogEntry:catEntry]) {
//at least update the file name, because we _know_ that changed
directoryChangesFound = YES;
[currentNote setFilename:(NSString*)catEntry->filename withExternalTrigger:YES];
}
notesChanged = YES;
break;
}
//a new file was found on the disk! read it into memory!
NSLog(@"File _actually_ added: %@", catEntry->filename);
[self addNoteFromCatalogEntry:catEntry];
}
if (!exitedEarly) {
NoteCatalogEntry *appendedCatEntry = (NoteCatalogEntry *)[[addedEntries objectAtIndex:MIN(lastInserted, bSize-1)] pointerValue];
if (fileSizeOfNote(currentNote) - appendedCatEntry->logicalSize > 0) {
lastInserted = bSize;
//file deleted from disk;
NSLog(@"File _actually_ deleted: %@", filenameOfNote(currentNote));
[[DeletionManager sharedManager] addDeletedNote:currentNote];
}
}
}
for (j=lastInserted; j<bSize; j++) {
NoteCatalogEntry *appendedCatEntry = (NoteCatalogEntry *)[[addedEntries objectAtIndex:j] pointerValue];
NSLog(@"File _actually_ added: %@", appendedCatEntry->filename);
[self addNoteFromCatalogEntry:appendedCatEntry];
}
}
#endif
@end