forked from scrod/nv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotationDirectoryManager.m
669 lines (507 loc) · 25.5 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
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
//
// NotationDirectoryManager.m
// Notation
//
// Created by Zachary Schneirov on 12/10/09.
/*Copyright (c) 2010, Zachary Schneirov. All rights reserved.
This file is part of Notational Velocity.
Notational Velocity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Notational Velocity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Notational Velocity. If not, see <http://www.gnu.org/licenses/>. */
#import "NotationDirectoryManager.h"
#import "NSFileManager_NV.h"
#import "NotationPrefs.h"
#import "BufferUtils.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;
}
//used to find notes corresponding to a group of existing files in the notes dir, with the understanding
//that the files' contents are up-to-date and the filename property of the note objs is also up-to-date
//e.g. caller should know that if notes are stored as a single DB, then the file could still be out-of-date
- (NSSet*)notesWithFilenames:(NSArray*)filenames unknownFiles:(NSArray**)unknownFiles {
//intersects a list of filenames with the current set of available notes
NSUInteger i = 0;
NSMutableDictionary *lcNamesDict = [NSMutableDictionary dictionaryWithCapacity:[filenames count]];
for (i=0; i<[filenames count]; i++) {
NSString *path = [filenames objectAtIndex:i];
//assume that paths are of NSFileManager origin, not Carbon File Manager
//(note filenames are derived with the expectation of matching against Carbon File Manager)
[lcNamesDict setObject:path forKey:[[[[path lastPathComponent] precomposedStringWithCanonicalMapping]
lowercaseString] stringByReplacingOccurrencesOfString:@":" withString:@"/"]];
}
NSMutableSet *foundNotes = [NSMutableSet setWithCapacity:[filenames count]];
for (i=0; i<[allNotes count]; i++) {
NoteObject *aNote = [allNotes objectAtIndex:i];
NSString *existingRequestedFilename = [filenameOfNote(aNote) lowercaseString];
if (existingRequestedFilename && [lcNamesDict objectForKey:existingRequestedFilename]) {
[foundNotes addObject:aNote];
//remove paths from the dict as they are matched to existing notes; those left over will be new ("unknown") files
[lcNamesDict removeObjectForKey:existingRequestedFilename];
}
}
if (unknownFiles) *unknownFiles = [lcNamesDict allValues];
return foundNotes;
}
void FSEventsCallback(ConstFSEventStreamRef stream, void* info, size_t num_events, void* event_paths,
const FSEventStreamEventFlags flags[],
const FSEventStreamEventId event_ids[]) {
NotationController* self = (NotationController*)info;
BOOL rootChanged = NO;
size_t i = 0;
for (i = 0; i < num_events; i++) {
//on 10.5, could also check whether all the events are bookended by eventIDs that were contemporaneous with a change by NotationFileManager
//as it lacks kFSEventStreamCreateFlagIgnoreSelf
if ((flags[i] & kFSEventStreamEventFlagRootChanged) && !event_ids[i]) {
rootChanged = YES;
break;
}
}
//the directory was moved; re-initialize the event stream for the new path
//but do so after this callback ends to avoid confusing FSEvents
if (rootChanged) {
NSLog(@"FSEventsCallback detected directory dislocation; reconfiguring stream");
[self performSelector:@selector(_configureDirEventStream) withObject:nil afterDelay:0];
}
//NSLog(@"FSEventsCallback got a path change");
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(synchronizeNotesFromDirectory) object:nil];
[self performSelector:@selector(synchronizeNotesFromDirectory) withObject:nil afterDelay:0.0];
}
- (void)_configureDirEventStream {
//"updates" the event stream to point to the current notation directory path
//or if the stream doesn't exist, creates it
if (!eventStreamStarted) return;
if (noteDirEventStreamRef) {
//remove the event stream if it already exists, so that a new one can be created
[self _destroyDirEventStream];
}
NSString *path = [[NSFileManager defaultManager] pathWithFSRef:¬eDirectoryRef];
FSEventStreamContext context = { 0, self, CFRetain, CFRelease, CFCopyDescription };
noteDirEventStreamRef = FSEventStreamCreate(NULL, &FSEventsCallback, &context, (CFArrayRef)[NSArray arrayWithObject:path], kFSEventStreamEventIdSinceNow,
1.0, kFSEventStreamCreateFlagWatchRoot | 0x00000008 /*kFSEventStreamCreateFlagIgnoreSelf*/);
FSEventStreamScheduleWithRunLoop(noteDirEventStreamRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
if (!FSEventStreamStart(noteDirEventStreamRef)) {
NSLog(@"could not start the FSEvents stream!");
}
}
- (void)_destroyDirEventStream {
if (eventStreamStarted) {
NSAssert(noteDirEventStreamRef != NULL, @"can't destroy a NULL event stream");
FSEventStreamStop(noteDirEventStreamRef);
FSEventStreamInvalidate(noteDirEventStreamRef);
FSEventStreamRelease(noteDirEventStreamRef);
noteDirEventStreamRef = NULL;
}
}
- (void)startFileNotifications {
eventStreamStarted = YES;
#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
if (IsZeros(¬eDirSubscription, sizeof(FNSubscriptionRef))) {
OSStatus err = FNSubscribe(¬eDirectoryRef, subscriptionCallback, self, kFNNoImplicitAllSubscription | kFNNotifyInBackground, ¬eDirSubscription);
if (err != noErr) {
NSLog(@"Could not subscribe to changes in notes directory!");
//just check modification time of directory?
}
}
#endif
if (IsLeopardOrLater) {
[self _configureDirEventStream];
}
}
- (void)stopFileNotifications {
if (!eventStreamStarted) return;
#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
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];
}
#endif
if (IsLeopardOrLater) {
[self _destroyDirEventStream];
}
eventStreamStarted = NO;
}
#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
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?");
}
}
#endif
- (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
[deletionManager addDeletedNotes:allNotes];
}
}
if (directoryChangesFound) {
[self resortAllNotes];
[self refilterNotes];
[self updateTitlePrefixConnections];
}
//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 |
kFSCatInfoAttrMod | 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;
entry->lastAttrModified = fsCatInfoArray[i].attributeModDate;
ResizeArray(&(entry->filenameChars), filename->length, &(entry->filenameCharCount));
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);
// mipe: Normalize the filename to make sure that it will be found regardless of international characters
CFStringNormalize(entry->filename, kCFStringNormalizationFormC);
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 *lastAttrModDate = attrsModifiedDateOfNote(aNoteObject);
//should we always update the note's stored inode here regardless?
// NSLog(@"content mod: %d,%d,%d, attr mod: %d,%d,%d", catEntry->lastModified.highSeconds,catEntry->lastModified.lowSeconds,catEntry->lastModified.fraction,
// catEntry->lastAttrModified.highSeconds,catEntry->lastAttrModified.lowSeconds,catEntry->lastAttrModified.fraction);
updateForVerifiedExistingNote(deletionManager, aNoteObject);
if (fileSizeOfNote(aNoteObject) != catEntry->logicalSize ||
*(int64_t*)&lastReadDate != *(int64_t*)&(catEntry->lastModified) ||
*(int64_t*)lastAttrModDate != *(int64_t*)&(catEntry->lastAttrModified)) {
//assume the file on disk was modified by someone other than us
//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;
ResizeArray(&allNotesBuffer, aSize, &allNotesBufferSize);
NSAssert(allNotesBuffer != NULL, @"sorting buffer not initialized");
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 addDeletedNotes:removedEntries];
}
}
}
//find renamed notes through unique file IDs
- (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];
break;
} else if (order == 0) { //if (A[i] == B[j])
lastInserted = j + 1;
exitedEarly = YES;
//note was renamed!
NSLog(@"File %@ renamed as per CNID 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);
[hfsAddedEntries addObject:[NSValue valueWithPointer: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];
}
}
}
for (j=lastInserted; j<bSize; j++) {
NoteCatalogEntry *appendedCatEntry = (NoteCatalogEntry *)[[addedEntries objectAtIndex:j] pointerValue];
NSLog(@"File added as per CNID: %@", appendedCatEntry->filename);
[hfsAddedEntries addObject:[NSValue valueWithPointer:appendedCatEntry]];
}
if ([hfsAddedEntries count] && [hfsRemovedEntries count]) {
[self processNotesAddedByContent:hfsAddedEntries removed:hfsRemovedEntries];
} else {
//NSLog(@"hfsAddedEntries: %@, hfsRemovedEntries: %@", hfsAddedEntries, hfsRemovedEntries);
if (![hfsRemovedEntries count]) {
for (i=0; i<[hfsAddedEntries count]; i++) {
NSLog(@"File _actually_ added: %@ (%s)", ((NoteCatalogEntry*)[[hfsAddedEntries objectAtIndex:i] pointerValue])->filename, _cmd);
[self addNoteFromCatalogEntry:(NoteCatalogEntry*)[[hfsAddedEntries objectAtIndex:i] pointerValue]];
}
}
if (![hfsAddedEntries count]) {
[deletionManager addDeletedNotes:hfsRemovedEntries];
}
}
}
//reconcile the "actually" added/deleted files into renames for files with identical content, looking at logical size first
- (void)processNotesAddedByContent:(NSMutableArray*)addedEntries removed:(NSMutableArray*)removedEntries {
//more than 1 entry in the same list could have the same file size, so sort-algo assumptions above don't apply here
//instead of sorting, build a dict keyed by file size, with duplicate sizes (on the same side) chained into arrays
//make temporary notes out of the new NoteCatalogEntries to allow their contents to be compared directly where sizes match
NSUInteger i;
NSMutableDictionary *addedDict = [NSMutableDictionary dictionaryWithCapacity:[addedEntries count]];
for (i=0; i<[addedEntries count]; i++) {
NSNumber *sizeKey = [NSNumber numberWithUnsignedInt:((NoteCatalogEntry*)[[addedEntries objectAtIndex:i] pointerValue])->logicalSize];
id sameSizeObj = [addedDict objectForKey:sizeKey];
if ([sameSizeObj isKindOfClass:[NSArray class]]) {
//just insert it directly; an array already exists
NSAssert([sameSizeObj isKindOfClass:[NSMutableArray class]], @"who's inserting immutable collections into my dictionary?");
[sameSizeObj addObject:[addedEntries objectAtIndex:i]];
} else if (sameSizeObj) {
//two objects need to be inserted into the new array
[addedDict setObject:[NSMutableArray arrayWithObjects:sameSizeObj, [addedEntries objectAtIndex:i], nil] forKey:sizeKey];
} else {
//nothing with this key, just insert it directly
[addedDict setObject:[addedEntries objectAtIndex:i] forKey:sizeKey];
}
}
// NSLog(@"removedEntries: %@", removedEntries);
// NSLog(@"addedDict: %@", addedDict);
for (i=0; i<[removedEntries count]; i++) {
NoteObject *removedObj = [removedEntries objectAtIndex:i];
NSNumber *sizeKey = [NSNumber numberWithUnsignedInt:fileSizeOfNote(removedObj)];
BOOL foundMatchingContent = NO;
//does any added item have the same size as removedObj?
//if sizes match, see if that added item's actual content fully matches removedObj's
//if content matches, then both items cancel each other out, with a rename operation resulting on the item in the removedEntries list
//if content doesn't match, then check the next item in the array (if there is more than one matching size), and so on
//any item in removedEntries that has no match in the addedEntries list is marked deleted
//everything left over in the addedEntries list is marked as new
id sameSizeObj = [addedDict objectForKey:sizeKey];
NSUInteger addedObjCount = [sameSizeObj isKindOfClass:[NSArray class]] ? [sameSizeObj count]: 1;
while (sameSizeObj && !foundMatchingContent && addedObjCount-- > 0) {
NSValue *val = [sameSizeObj isKindOfClass:[NSArray class]] ? [sameSizeObj objectAtIndex:addedObjCount] : sameSizeObj;
NoteObject *addedObjToCompare = [[NoteObject alloc] initWithCatalogEntry:[val pointerValue] delegate:self];
if ([[[addedObjToCompare contentString] string] isEqualToString:[[removedObj contentString] string]]) {
//process this pair as a modification
NSLog(@"File %@ renamed as per content to %@", filenameOfNote(removedObj), filenameOfNote(addedObjToCompare));
if (![self modifyNoteIfNecessary:removedObj usingCatalogEntry:[val pointerValue]]) {
//at least update the file name, because we _know_ that changed
directoryChangesFound = YES;
notesChanged = YES;
[removedObj setFilename:filenameOfNote(addedObjToCompare) withExternalTrigger:YES];
}
if ([sameSizeObj isKindOfClass:[NSArray class]]) {
[sameSizeObj removeObjectIdenticalTo:val];
} else {
[addedDict removeObjectForKey:sizeKey];
}
//also remove it from original array, which is easier to process for the leftovers that will actually be added
[addedEntries removeObjectIdenticalTo:val];
foundMatchingContent = YES;
}
[addedObjToCompare release];
}
if (!foundMatchingContent) {
NSLog(@"File %@ _actually_ removed (size: %u)", filenameOfNote(removedObj), fileSizeOfNote(removedObj));
[deletionManager addDeletedNote:removedObj];
}
}
for (i=0; i<[addedEntries count]; i++) {
NoteCatalogEntry *appendedCatEntry = (NoteCatalogEntry *)[[addedEntries objectAtIndex:i] pointerValue];
NSLog(@"File _actually_ added: %@ (%s)", appendedCatEntry->filename, _cmd);
[self addNoteFromCatalogEntry:appendedCatEntry];
}
}
@end