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 668 669 670 671 672 673 674
|
/* DDBPathsManager.m
*
* Copyright (C) 2005-2013 Free Software Foundation, Inc.
*
* Author: Enrico Sersale <enrico@imago.ro>
* Date: July 2005
*
* This file is part of the GNUstep GWorkspace application
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 31 Milk Street #960789 Boston, MA 02196 USA.
*/
#import "DBKBTreeNode.h"
#import "DBKVarLenRecordsFile.h"
#import "DDBPathsManager.h"
#import "DDBMDStorage.h"
#import "MDModulesProtocol.h"
#import "ddbd.h"
@implementation DDBPathsManager
- (void)dealloc
{
RELEASE (mdstorage);
RELEASE (vlfile);
RELEASE (tree);
RELEASE (dummyPaths[0]);
RELEASE (dummyPaths[1]);
RELEASE (dummyOffsets[0]);
RELEASE (dummyOffsets[1]);
RELEASE (mdmodules);
[super dealloc];
}
- (id)initWithBasePath:(NSString *)bpath
{
self = [super init];
if (self) {
NSEnumerator *enumerator;
NSString *path;
NSBundle *bundle;
NSString *bundlesDir;
NSArray *bnames;
NSUInteger i;
ulen = sizeof(unsigned);
llen = sizeof(unsigned long);
path = [bpath stringByAppendingPathComponent: @"paths"];
vlfile = [[DBKVarLenRecordsFile alloc] initWithPath: path cacheLength: 10];
path = [bpath stringByAppendingPathComponent: @"paths.index"];
tree = [[DBKBTree alloc] initWithPath: path order: 16 delegate: self];
path = [bpath stringByAppendingPathComponent: @"docs"];
mdstorage = [[DDBMDStorage alloc] initWithPath: path
levelCount: 100
dirsDepth: 3];
ASSIGN (dummyOffsets[0], [NSNumber numberWithUnsignedLong: 1L]);
ASSIGN (dummyOffsets[1], [NSNumber numberWithUnsignedLong: 2L]);
fm = [NSFileManager defaultManager];
mdmodules = [NSMutableDictionary new];
enumerator = [NSSearchPathForDirectoriesInDomains
(NSLibraryDirectory, NSAllDomainsMask, YES) objectEnumerator];
while ((bundlesDir = [enumerator nextObject]) != nil)
{
bundlesDir = [bundlesDir stringByAppendingPathComponent: @"Bundles"];
bnames = [fm directoryContentsAtPath: bundlesDir];
for (i = 0; i < [bnames count]; i++) {
NSString *bname = [bnames objectAtIndex: i];
if ([[bname pathExtension] isEqual: @"mdm"]) {
NSString *bpath;
bpath = [bundlesDir stringByAppendingPathComponent: bname];
bundle = [NSBundle bundleWithPath: bpath];
if (bundle) {
Class principalClass = [bundle principalClass];
if ([principalClass conformsToProtocol:
@protocol(MDModulesProtocol)]) {
CREATE_AUTORELEASE_POOL (pool);
id module = [[principalClass alloc] init];
[mdmodules setObject: module forKey: [module mdtype]];
RELEASE ((id)module);
RELEASE (pool);
}
}
}
}
}
[self addPath: pathsep()];
[self synchronize];
}
return self;
}
- (void)synchronize
{
[vlfile flush];
[tree synchronize];
}
- (DDBPath *)ddbpathForPath:(NSString *)path
{
CREATE_AUTORELEASE_POOL(arp);
DDBPath *ddbpath = nil;
DBKBTreeNode *node;
BOOL exists;
NSUInteger index;
DESTROY (dummyPaths[1]);
DESTROY (dummyPaths[0]);
dummyPaths[0] = [[DDBPath alloc] initForPath: path];
[tree begin];
node = [tree nodeOfKey: dummyOffsets[0] getIndex: &index didExist: &exists];
if (exists) {
NSNumber *offset = [node keyAtIndex: index];
NSData *data = [vlfile dataAtOffset: offset];
ddbpath = [NSUnarchiver unarchiveObjectWithData: data];
}
[tree end];
DESTROY (dummyPaths[0]);
RETAIN (ddbpath);
RELEASE (arp);
return AUTORELEASE (ddbpath);
}
- (DDBPath *)addPath:(NSString *)path
{
CREATE_AUTORELEASE_POOL(arp);
DDBPath *ddbpath = nil;
DBKBTreeNode *node;
DESTROY (dummyPaths[1]);
DESTROY (dummyPaths[0]);
dummyPaths[0] = [[DDBPath alloc] initForPath: path];
[tree begin];
node = [tree insertKey: dummyOffsets[0]];
if (node) {
NSString *mdpath = [mdstorage nextEntry];
NSTimeInterval stamp = [[NSDate date] timeIntervalSinceReferenceDate];
NSData *data;
NSNumber *offset;
[dummyPaths[0] setMDPath: mdpath];
[dummyPaths[0] setTimestamp: stamp];
data = [NSArchiver archivedDataWithRootObject: dummyPaths[0]];
offset = [vlfile writeData: data];
[node replaceKey: dummyOffsets[0] withKey: offset];
[self synchronize];
ddbpath = dummyPaths[0];
RETAIN (ddbpath);
}
[tree end];
DESTROY (dummyPaths[0]);
RELEASE (arp);
return AUTORELEASE (ddbpath);
}
- (void)removePath:(NSString *)path
{
CREATE_AUTORELEASE_POOL(arp);
DBKBTreeNode *node;
NSUInteger index;
BOOL exists;
DESTROY (dummyPaths[1]);
DESTROY (dummyPaths[0]);
dummyPaths[0] = [[DDBPath alloc] initForPath: path];
[tree begin];
node = [tree nodeOfKey: dummyOffsets[0] getIndex: &index didExist: &exists];
if (exists) {
NSNumber *offset = [node keyAtIndex: index];
NSData *data = [vlfile dataAtOffset: offset];
DDBPath *ddbpath = [NSUnarchiver unarchiveObjectWithData: data];
NSString *mdpath = [ddbpath mdpath];
RETAIN (offset);
[tree deleteKey: offset];
[vlfile deleteDataAtOffset: offset];
[mdstorage removeEntry: mdpath];
RELEASE (offset);
}
[tree end];
DESTROY (dummyPaths[0]);
RELEASE (arp);
[self synchronize];
}
- (void)setMetadata:(id)mdata
ofType:(NSString *)mdtype
forPath:(NSString *)apath
{
CREATE_AUTORELEASE_POOL(arp);
DDBPath *ddbpath = [self ddbpathForPath: apath];
NSString *path = [mdstorage basePath];
id module = [self mdmoduleForMDType: mdtype];
if (ddbpath == nil) {
ddbpath = [self addPath: apath];
}
path = [path stringByAppendingPathComponent: [ddbpath mdpath]];
[module saveData: mdata withBasePath: path];
[self metadataDidChangeForPath: ddbpath];
if ([apath isEqual: pathsep()] == NO) {
NSString *parent = [apath stringByDeletingLastPathComponent];
DDBPath *ppath = [self ddbpathForPath: parent];
if (ppath == nil) {
[self addPath: parent];
} else {
[self metadataDidChangeForPath: ppath];
}
}
[[NSDistributedNotificationCenter defaultCenter]
postNotificationName: @"GSMetadataUserAttributeModifiedNotification"
object: apath
userInfo: nil];
RELEASE (arp);
}
- (id)metadataOfType:(NSString *)mdtype
forPath:(NSString *)apath
{
DDBPath *ddbpath = [self ddbpathForPath: apath];
id mddata = nil;
if (ddbpath) {
id module = [self mdmoduleForMDType: mdtype];
NSString *path = [mdstorage basePath];
path = [path stringByAppendingPathComponent: [ddbpath mdpath]];
mddata = [module dataWithBasePath: path];
}
return mddata;
}
- (NSArray *)metadataForPath:(NSString *)apath
{
CREATE_AUTORELEASE_POOL(arp);
NSMutableArray *alldata = [NSMutableArray array];
NSArray *types = [mdmodules allKeys];
NSUInteger i;
for (i = 0; i < [types count]; i++) {
NSString *type = [types objectAtIndex: i];
id data = [self metadataOfType: type forPath: apath];
if (data) {
NSDictionary *dict;
dict = [NSDictionary dictionaryWithObjectsAndKeys: type, @"key",
data, @"attribute",
nil];
[alldata addObject: dict];
}
}
[alldata retain];
RELEASE (arp);
return [alldata autorelease];
}
- (NSTimeInterval)timestampOfPath:(NSString *)path
{
DDBPath *ddbpath = [self ddbpathForPath: path];
if (ddbpath) {
return [ddbpath timestamp];
}
return 0.0;
}
- (void)metadataDidChangeForPath:(DDBPath *)ddbpath
{
CREATE_AUTORELEASE_POOL(arp);
DBKBTreeNode *node;
NSUInteger index;
BOOL exists;
DESTROY (dummyPaths[1]);
ASSIGN (dummyPaths[0], ddbpath);
[tree begin];
node = [tree nodeOfKey: dummyOffsets[0] getIndex: &index didExist: &exists];
if (exists) {
NSNumber *offset = [node keyAtIndex: index];
NSTimeInterval stamp = [[NSDate date] timeIntervalSinceReferenceDate];
NSData *data;
[ddbpath setTimestamp: stamp];
data = [NSArchiver archivedDataWithRootObject: ddbpath];
[vlfile writeData: data atOffset: offset];
[self synchronize];
}
[tree end];
RELEASE (arp);
}
- (void)duplicateDataOfPath:(NSString *)srcpath
forPath:(NSString *)dstpath
{
NSArray *types = [mdmodules allKeys];
NSUInteger i;
for (i = 0; i < [types count]; i++) {
NSString *type = [types objectAtIndex: i];
id module = [mdmodules objectForKey: type];
if ([module duplicable]) {
id mddata = [self metadataOfType: type forPath: srcpath];
if (mddata) {
[self setMetadata: mddata ofType: type forPath: dstpath];
}
}
}
}
- (void)duplicateDataOfPaths:(NSArray *)srcpaths
forPaths:(NSArray *)dstpaths
{
NSUInteger i, j;
for (i = 0; i < [srcpaths count]; i++) {
CREATE_AUTORELEASE_POOL(arp);
NSString *srcpath = [srcpaths objectAtIndex: i];
NSString *dstpath = [dstpaths objectAtIndex: i];
NSDictionary *attrs = [fm fileAttributesAtPath: dstpath traverseLink: NO];
DDBPath *ddbpath = [self ddbpathForPath: srcpath];
if (ddbpath) {
[self duplicateDataOfPath: srcpath forPath: dstpath];
}
if ([attrs fileType] == NSFileTypeDirectory) {
NSArray *subpaths = [self subpathsFromPath: srcpath];
for (j = 0; j < [subpaths count]; j++) {
NSString *subpath = [[subpaths objectAtIndex: j] path];
NSString *newpath = removePrefix(subpath, srcpath);
newpath = [dstpath stringByAppendingPathComponent: newpath];
if ([fm fileExistsAtPath: newpath]) {
[self duplicateDataOfPath: subpath forPath: newpath];
}
}
}
RELEASE (arp);
}
}
- (NSArray *)subpathsFromPath:(NSString *)path
{
CREATE_AUTORELEASE_POOL(pool);
NSMutableArray *paths = [NSMutableArray array];
NSMutableArray *toremove = [NSMutableArray array];
NSArray *keys = nil;
NSString *dmstr[2];
NSUInteger i;
[tree begin];
if ([path isEqual: pathsep()] == NO) {
dmstr[0] = [path stringByAppendingString: pathsep()];
dmstr[1] = [path stringByAppendingString: @"0"];
} else {
dmstr[0] = path;
dmstr[1] = @"0";
}
dummyPaths[0] = [[DDBPath alloc] initForPath: dmstr[0]];
dummyPaths[1] = [[DDBPath alloc] initForPath: dmstr[1]];
keys = [tree keysGreaterThenKey: dummyOffsets[0]
andLesserThenKey: dummyOffsets[1]];
[tree end];
if (keys) {
for (i = 0; i < [keys count]; i++) {
CREATE_AUTORELEASE_POOL(arp);
NSData *data = [vlfile dataAtOffset: [keys objectAtIndex: i]];
DDBPath *ddbpath = [NSUnarchiver unarchiveObjectWithData: data];
if ([fm fileExistsAtPath: [ddbpath path]]) {
[paths addObject: ddbpath];
} else {
[toremove addObject: [ddbpath path]];
}
RELEASE(arp);
}
}
for (i = 0; i < [toremove count]; i++) {
[self removePath: [toremove objectAtIndex: i]];
}
RETAIN (paths);
RELEASE(pool);
return [paths autorelease];
}
- (id)mdmoduleForMDType:(NSString *)type
{
return [mdmodules objectForKey: type];
}
//
// DBKBTreeDelegate methods
//
- (unsigned long)nodesize
{
return 512;
}
- (NSArray *)keysFromData:(NSData *)data
withLength:(unsigned *)dlen
{
NSMutableArray *keys = [NSMutableArray array];
NSRange range;
unsigned kcount;
unsigned long key;
NSUInteger i;
range = NSMakeRange(0, sizeof(unsigned));
[data getBytes: &kcount range: range];
range.location += sizeof(unsigned);
range.length = sizeof(unsigned long);
for (i = 0; i < kcount; i++) {
[data getBytes: &key range: range];
[keys addObject: [NSNumber numberWithUnsignedLong: key]];
range.location += sizeof(unsigned long);
}
*dlen = range.location;
return keys;
}
- (NSData *)dataFromKeys:(NSArray *)keys
{
NSMutableData *data = [NSMutableData dataWithCapacity: 1];
unsigned kcount = [keys count];
NSUInteger i;
[data appendData: [NSData dataWithBytes: &kcount length: sizeof(unsigned)]];
for (i = 0; i < kcount; i++) {
unsigned long kl = [[keys objectAtIndex: i] unsignedLongValue];
[data appendData: [NSData dataWithBytes: &kl length: sizeof(unsigned long)]];
}
return data;
}
- (NSComparisonResult)compareNodeKey:(id)akey
withKey:(id)bkey
{
CREATE_AUTORELEASE_POOL(arp);
DDBPath *apath;
DDBPath *bpath;
NSComparisonResult result;
if ([akey isEqual: dummyOffsets[0]]) {
apath = RETAIN (dummyPaths[0]);
} else {
NSData *data = [vlfile dataAtOffset: (NSNumber *)akey];
apath = [NSUnarchiver unarchiveObjectWithData: data];
}
if ([bkey isEqual: dummyOffsets[0]]) {
bpath = dummyPaths[0];
} else if ([bkey isEqual: dummyOffsets[1]]) {
bpath = dummyPaths[1];
} else {
NSData *data = [vlfile dataAtOffset: (NSNumber *)bkey];
bpath = [NSUnarchiver unarchiveObjectWithData: data];
}
result = [apath compare: bpath];
RELEASE (arp);
return result;
}
@end
@implementation DDBPath
- (void)dealloc
{
RELEASE (path);
RELEASE (mdpath);
[super dealloc];
}
- (id)initForPath:(NSString *)apath
{
self = [super init];
if (self) {
ASSIGN (path, apath);
mdpath = nil;
timestamp = 0.0;
}
return self;
}
- (id)initWithCoder:(NSCoder *)decoder
{
self = [super init];
if (self) {
if ([decoder allowsKeyedCoding]) {
ASSIGN (path, [decoder decodeObjectForKey: @"path"]);
ASSIGN (mdpath, [decoder decodeObjectForKey: @"mdpath"]);
timestamp = [decoder decodeDoubleForKey: @"timestamp"];
} else {
ASSIGN (path, [decoder decodeObject]);
ASSIGN (mdpath, [decoder decodeObject]);
[decoder decodeValueOfObjCType: @encode(double) at: ×tamp];
}
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder
{
if ([encoder allowsKeyedCoding]) {
[encoder encodeObject: path forKey: @"path"];
[encoder encodeObject: mdpath forKey: @"mdpath"];
[encoder encodeDouble: timestamp forKey: @"timestamp"];
} else {
[encoder encodeObject: path];
[encoder encodeObject: mdpath];
[encoder encodeValueOfObjCType: @encode(double) at: ×tamp];
}
}
- (NSUInteger)hash
{
return [path hash];
}
- (BOOL)isEqual:(id)other
{
if (other == self) {
return YES;
}
if ([other isKindOfClass: [DDBPath class]]) {
return [path isEqual: [other path]];
}
return NO;
}
- (void)setPath:(NSString *)apath
{
ASSIGN (path, apath);
}
- (NSString *)path
{
return path;
}
- (void)setMDPath:(NSString *)apath
{
ASSIGN (mdpath, apath);
}
- (NSString *)mdpath
{
return mdpath;
}
- (void)setTimestamp:(NSTimeInterval)stamp
{
timestamp = stamp;
}
- (NSTimeInterval)timestamp
{
return timestamp;
}
- (NSComparisonResult)compare:(DDBPath *)apath
{
return [path compare: [apath path]];
}
@end
|