From e146ba68e64dbe259b063e7babadecbf3b8e99ba Mon Sep 17 00:00:00 2001 From: Wolfgang Sourdeau Date: Mon, 13 Aug 2012 03:55:48 +0000 Subject: [PATCH] Monotone-Parent: 8d4ca03b16c54609dce23f43fc358a43d7e96d99 Monotone-Revision: 047b040e318a98223980da2af8241b7eb6d75341 Monotone-Author: wsourdeau@inverse.ca Monotone-Date: 2012-08-13T03:55:48 Monotone-Branch: ca.inverse.sogo --- ChangeLog | 5 +++ OpenChange/MAPIStoreMapping.h | 2 + OpenChange/MAPIStoreMapping.m | 84 +++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+) diff --git a/ChangeLog b/ChangeLog index 52a6b1e59..f949ad0f0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2012-08-12 Wolfgang Sourdeau + + * OpenChange/MAPIStoreMapping.m (-updateID:withURL:): new method + that perform a change of url on container and leaf entries. + 2012-08-10 Wolfgang Sourdeau * OpenChange/MAPIStoreSOGo.m (sogo_properties_get_uri): new diff --git a/OpenChange/MAPIStoreMapping.h b/OpenChange/MAPIStoreMapping.h index 484c74da3..74b320aa0 100644 --- a/OpenChange/MAPIStoreMapping.h +++ b/OpenChange/MAPIStoreMapping.h @@ -54,6 +54,8 @@ - (BOOL) registerURL: (NSString *) urlString withID: (uint64_t) idNbr; - (void) unregisterURLWithID: (uint64_t) idNbr; +- (void) updateID: (uint64_t) idNbr + withURL: (NSString *) urlString; @end diff --git a/OpenChange/MAPIStoreMapping.m b/OpenChange/MAPIStoreMapping.m index c1a4b1f6b..8c7e29090 100644 --- a/OpenChange/MAPIStoreMapping.m +++ b/OpenChange/MAPIStoreMapping.m @@ -204,6 +204,90 @@ MAPIStoreMappingTDBTraverse (TDB_CONTEXT *ctx, TDB_DATA data1, TDB_DATA data2, return idNbr; } +- (void) _updateFolderWithURL: (NSString *) oldURL + withURL: (NSString *) urlString +{ + NSArray *allKeys; + NSUInteger count, max; + NSString *currentKey, *keyEnd, *newKey; + NSUInteger oldURLLength; + NSNumber *idKey; + TDB_DATA key, dbuf; + + oldURLLength = [oldURL length]; + + allKeys = [reverseMapping allKeys]; + max = [allKeys count]; + for (count = 0; count < max; count++) + { + currentKey = [allKeys objectAtIndex: count]; + if ([currentKey hasPrefix: oldURL]) + { + keyEnd = [currentKey substringFromIndex: oldURLLength]; + newKey = [NSString stringWithFormat: @"%@%@", urlString, keyEnd]; + + idKey = [reverseMapping objectForKey: currentKey]; + [mapping setObject: newKey forKey: idKey]; + [reverseMapping setObject: idKey forKey: newKey]; + [reverseMapping removeObjectForKey: currentKey]; + + /* update the record in the indexing database */ + key.dptr = (unsigned char *) talloc_asprintf (NULL, "0x%.16"PRIx64, + (uint64_t) [idKey unsignedLongLongValue]); + key.dsize = strlen ((const char *) key.dptr); + + dbuf.dptr = (unsigned char *) talloc_strdup (NULL, + [newKey UTF8String]); + dbuf.dsize = strlen ((const char *) dbuf.dptr); + tdb_store (indexing->tdb, key, dbuf, TDB_MODIFY); + talloc_free (key.dptr); + talloc_free (dbuf.dptr); + } + } +} + +- (void) updateID: (uint64_t) idNbr + withURL: (NSString *) urlString +{ + NSString *oldURL; + NSNumber *idKey; + TDB_DATA key, dbuf; + + idKey = [NSNumber numberWithUnsignedLongLong: idNbr]; + oldURL = [mapping objectForKey: idKey]; + if (oldURL) + { + if ([oldURL hasSuffix: @"/"]) /* is container ? */ + { + if (![urlString hasSuffix: @"/"]) + [NSException raise: NSInvalidArgumentException + format: @"a container url must have an ending '/'"]; + tdb_transaction_start (indexing->tdb); + [self _updateFolderWithURL: oldURL withURL: urlString]; + tdb_transaction_commit (indexing->tdb); + } + else + { + if ([urlString hasSuffix: @"/"]) + [NSException raise: NSInvalidArgumentException + format: @"a leaf url must not have an ending '/'"]; + [mapping setObject: urlString forKey: idKey]; + [reverseMapping setObject: idKey forKey: urlString]; + [reverseMapping removeObjectForKey: oldURL]; + + /* update the record in the indexing database */ + key.dptr = (unsigned char *) talloc_asprintf(NULL, "0x%.16"PRIx64, idNbr); + key.dsize = strlen((const char *) key.dptr); + + dbuf.dptr = (unsigned char *) talloc_strdup (NULL, [urlString UTF8String]); + dbuf.dsize = strlen((const char *) dbuf.dptr); + tdb_store (indexing->tdb, key, dbuf, TDB_MODIFY); + talloc_free (key.dptr); + talloc_free (dbuf.dptr); + } + } +} + - (BOOL) registerURL: (NSString *) urlString withID: (uint64_t) idNbr {