2010-10-01 22:35:29 +02:00
|
|
|
/* MAPIStoreMapping.m - this file is part of SOGo
|
2010-10-01 20:54:30 +02:00
|
|
|
*
|
2012-08-17 21:04:57 +02:00
|
|
|
* Copyright (C) 2010-2012 Inverse inc.
|
2010-10-01 20:54:30 +02:00
|
|
|
*
|
|
|
|
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
|
|
|
|
*
|
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2010-10-18 14:57:31 +02:00
|
|
|
* the Free Software Foundation; either version 3, or (at your option)
|
2010-10-01 20:54:30 +02:00
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This file 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; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2012-10-10 17:55:06 +02:00
|
|
|
#include <inttypes.h>
|
|
|
|
|
2010-12-13 17:54:32 +01:00
|
|
|
#import <Foundation/NSArray.h>
|
2010-10-01 20:54:30 +02:00
|
|
|
#import <Foundation/NSDictionary.h>
|
2010-10-13 17:30:01 +02:00
|
|
|
#import <Foundation/NSException.h>
|
2010-10-01 20:54:30 +02:00
|
|
|
#import <Foundation/NSString.h>
|
|
|
|
#import <Foundation/NSValue.h>
|
|
|
|
|
|
|
|
#import <NGExtensions/NSObject+Logs.h>
|
2012-10-09 20:14:58 +02:00
|
|
|
#import <NGExtensions/NSObject+Values.h>
|
2010-10-01 20:54:30 +02:00
|
|
|
|
2012-08-13 19:48:57 +02:00
|
|
|
#import <SOGo/NSString+Utilities.h>
|
|
|
|
|
2013-11-09 18:00:44 +01:00
|
|
|
#undef DEBUG
|
|
|
|
#include <mapistore/mapistore.h>
|
|
|
|
#include <mapistore/mapistore_errors.h>
|
|
|
|
|
2011-06-04 01:53:30 +02:00
|
|
|
#import "MAPIStoreTypes.h"
|
|
|
|
|
2010-10-01 20:54:30 +02:00
|
|
|
#import "MAPIStoreMapping.h"
|
|
|
|
|
2010-12-13 17:54:32 +01:00
|
|
|
#include <talloc.h>
|
|
|
|
|
2011-08-10 20:32:53 +02:00
|
|
|
static NSMutableDictionary *mappingRegistry = nil;
|
|
|
|
|
2011-06-04 01:53:30 +02:00
|
|
|
@implementation MAPIStoreMapping
|
2010-12-13 17:54:32 +01:00
|
|
|
|
2011-08-10 20:32:53 +02:00
|
|
|
+ (void) initialize
|
|
|
|
{
|
|
|
|
mappingRegistry = [NSMutableDictionary new];
|
|
|
|
}
|
|
|
|
|
2012-10-03 17:25:47 +02:00
|
|
|
static inline id
|
|
|
|
MAPIStoreMappingKeyFromId (uint64_t idNbr)
|
|
|
|
{
|
2012-10-09 20:14:58 +02:00
|
|
|
return [NSString stringWithUnsignedLongLong: idNbr];
|
2012-10-03 17:25:47 +02:00
|
|
|
}
|
|
|
|
|
2010-12-13 17:54:32 +01:00
|
|
|
|
2011-08-10 20:32:53 +02:00
|
|
|
+ (id) mappingForUsername: (NSString *) username
|
2013-11-08 17:02:50 +01:00
|
|
|
withIndexing: (struct indexing_context *) indexing
|
2010-12-13 17:54:32 +01:00
|
|
|
{
|
2011-08-10 20:32:53 +02:00
|
|
|
id mapping;
|
2010-12-13 17:54:32 +01:00
|
|
|
|
2011-08-10 20:32:53 +02:00
|
|
|
mapping = [mappingRegistry objectForKey: username];
|
|
|
|
if (!mapping)
|
|
|
|
{
|
|
|
|
mapping = [[self alloc] initForUsername: username
|
|
|
|
withIndexing: indexing];
|
|
|
|
[mapping autorelease];
|
|
|
|
}
|
2010-10-01 20:54:30 +02:00
|
|
|
|
2011-08-10 20:32:53 +02:00
|
|
|
return mapping;
|
2011-06-04 01:53:30 +02:00
|
|
|
}
|
2010-10-01 20:54:30 +02:00
|
|
|
|
2011-06-04 01:53:30 +02:00
|
|
|
- (id) init
|
2010-10-15 19:18:34 +02:00
|
|
|
{
|
2011-06-04 01:53:30 +02:00
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
2011-10-17 17:29:14 +02:00
|
|
|
memCtx = talloc_zero (NULL, TALLOC_CTX);
|
2011-06-04 01:53:30 +02:00
|
|
|
indexing = NULL;
|
2011-08-10 20:32:53 +02:00
|
|
|
useCount = 0;
|
2011-06-04 01:53:30 +02:00
|
|
|
}
|
2010-10-15 19:18:34 +02:00
|
|
|
|
2011-06-04 01:53:30 +02:00
|
|
|
return self;
|
2010-10-15 19:18:34 +02:00
|
|
|
}
|
|
|
|
|
2011-08-10 20:32:53 +02:00
|
|
|
- (void) increaseUseCount
|
|
|
|
{
|
|
|
|
if (useCount == 0)
|
|
|
|
{
|
|
|
|
[mappingRegistry setObject: self forKey: username];
|
|
|
|
[self logWithFormat: @"mapping registered (%@)", username];
|
|
|
|
}
|
|
|
|
useCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) decreaseUseCount
|
|
|
|
{
|
|
|
|
useCount--;
|
|
|
|
if (useCount == 0)
|
|
|
|
{
|
|
|
|
[mappingRegistry removeObjectForKey: username];
|
|
|
|
[self logWithFormat: @"mapping deregistered (%@)", username];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initForUsername: (NSString *) newUsername
|
2013-11-08 17:02:50 +01:00
|
|
|
withIndexing: (struct indexing_context *) newIndexing
|
2010-10-01 20:54:30 +02:00
|
|
|
{
|
2011-06-04 01:53:30 +02:00
|
|
|
if ((self = [self init]))
|
2010-10-01 22:35:29 +02:00
|
|
|
{
|
2011-08-10 20:32:53 +02:00
|
|
|
ASSIGN (username, newUsername);
|
2011-06-04 01:53:30 +02:00
|
|
|
indexing = newIndexing;
|
2014-08-28 04:19:28 +02:00
|
|
|
/* Workaround so all indexing context are valid and won't be freed. */
|
|
|
|
// TODO refactor indexing interface
|
|
|
|
talloc_reference(memCtx, indexing);
|
2010-10-01 22:35:29 +02:00
|
|
|
}
|
2010-10-13 23:40:50 +02:00
|
|
|
|
2010-10-01 22:35:29 +02:00
|
|
|
return self;
|
2010-10-01 20:54:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
2011-08-10 20:32:53 +02:00
|
|
|
[username release];
|
2011-10-17 17:29:14 +02:00
|
|
|
talloc_free (memCtx);
|
2010-10-01 22:35:29 +02:00
|
|
|
[super dealloc];
|
2010-10-01 20:54:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) urlFromID: (uint64_t) idNbr
|
|
|
|
{
|
2013-11-11 03:17:53 +01:00
|
|
|
char* url = NULL;
|
2013-12-24 11:03:03 +01:00
|
|
|
enum mapistore_error ret;
|
|
|
|
bool soft_delete = false;
|
2013-11-09 18:00:44 +01:00
|
|
|
|
2013-12-24 11:03:03 +01:00
|
|
|
ret = indexing->get_uri(indexing, [username UTF8String],
|
|
|
|
memCtx, idNbr, &url, &soft_delete);
|
|
|
|
if (ret != MAPISTORE_SUCCESS)
|
|
|
|
return NULL;
|
2013-11-09 18:00:44 +01:00
|
|
|
NSString *res = [[[NSString alloc] initWithUTF8String:url] autorelease];
|
|
|
|
talloc_free(url);
|
|
|
|
|
|
|
|
return res;
|
2010-10-01 20:54:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (uint64_t) idFromURL: (NSString *) url
|
|
|
|
{
|
2013-11-09 18:00:44 +01:00
|
|
|
enum mapistore_error ret;
|
2010-10-01 22:35:29 +02:00
|
|
|
uint64_t idNbr;
|
2013-11-09 18:00:44 +01:00
|
|
|
bool softDeleted;
|
2011-06-04 01:53:30 +02:00
|
|
|
|
2013-11-09 18:00:44 +01:00
|
|
|
ret = indexing->get_fmid(indexing, [username UTF8String], [url UTF8String],
|
|
|
|
false, &idNbr, &softDeleted);
|
2010-10-13 17:30:01 +02:00
|
|
|
|
2013-11-09 18:00:44 +01:00
|
|
|
if (ret == MAPISTORE_SUCCESS && !softDeleted)
|
|
|
|
return idNbr;
|
|
|
|
else
|
|
|
|
return NSNotFound;
|
2010-10-01 20:54:30 +02:00
|
|
|
}
|
|
|
|
|
2014-10-15 00:35:17 +02:00
|
|
|
- (uint64_t) idFromURL: (NSString *) url
|
|
|
|
isSoftDeleted: (bool *) softDeleted
|
|
|
|
{
|
|
|
|
enum mapistore_error ret;
|
|
|
|
uint64_t idNbr;
|
|
|
|
|
|
|
|
ret = indexing->get_fmid(indexing, [username UTF8String], [url UTF8String],
|
|
|
|
false, &idNbr, softDeleted);
|
|
|
|
|
|
|
|
if (ret != MAPISTORE_SUCCESS)
|
|
|
|
return NSNotFound;
|
|
|
|
|
|
|
|
return idNbr;
|
|
|
|
}
|
|
|
|
|
2012-08-13 05:55:48 +02:00
|
|
|
- (void) _updateFolderWithURL: (NSString *) oldURL
|
|
|
|
withURL: (NSString *) urlString
|
|
|
|
{
|
2013-11-11 03:17:53 +01:00
|
|
|
const char *searchURL;
|
|
|
|
uint64_t idNbr;
|
|
|
|
bool softDeleted;
|
|
|
|
NSString *current;
|
|
|
|
NSString *newURL;
|
2012-08-13 05:55:48 +02:00
|
|
|
|
2014-02-10 12:38:42 +01:00
|
|
|
if ([oldURL isEqualToString: urlString]) return;
|
|
|
|
|
2013-11-11 03:17:53 +01:00
|
|
|
searchURL = [[oldURL stringByAppendingString:@"*"] UTF8String];
|
2012-08-16 18:30:58 +02:00
|
|
|
|
2013-11-11 03:17:53 +01:00
|
|
|
while (indexing->get_fmid(indexing, [username UTF8String],
|
|
|
|
searchURL,true, &idNbr, &softDeleted) == MAPISTORE_SUCCESS)
|
|
|
|
{
|
|
|
|
// Ignore deleted
|
|
|
|
if (softDeleted) continue;
|
2012-08-16 18:30:58 +02:00
|
|
|
|
2013-11-11 03:17:53 +01:00
|
|
|
current = [self urlFromID:idNbr];
|
|
|
|
newURL = [current stringByReplacingPrefix: oldURL withPrefix: urlString];
|
|
|
|
indexing->update_fmid(indexing, [username UTF8String], idNbr, [newURL UTF8String]);
|
|
|
|
}
|
2012-08-13 05:55:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) updateID: (uint64_t) idNbr
|
|
|
|
withURL: (NSString *) urlString
|
|
|
|
{
|
|
|
|
NSString *oldURL;
|
|
|
|
|
2013-11-11 03:17:53 +01:00
|
|
|
oldURL = [self urlFromID: idNbr];
|
2012-08-13 05:55:48 +02:00
|
|
|
if (oldURL)
|
|
|
|
{
|
|
|
|
if ([oldURL hasSuffix: @"/"]) /* is container ? */
|
|
|
|
{
|
|
|
|
if (![urlString hasSuffix: @"/"])
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"a container url must have an ending '/'"];
|
|
|
|
[self _updateFolderWithURL: oldURL withURL: urlString];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ([urlString hasSuffix: @"/"])
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"a leaf url must not have an ending '/'"];
|
2013-11-11 03:17:53 +01:00
|
|
|
|
|
|
|
indexing->update_fmid(indexing, [username UTF8String],
|
|
|
|
idNbr, [urlString UTF8String]);
|
2012-08-13 05:55:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-13 11:48:42 +02:00
|
|
|
- (BOOL) updateURL: (NSString *) urlString
|
|
|
|
withID: (uint64_t) idNbr
|
|
|
|
{
|
|
|
|
BOOL rc = NO;
|
|
|
|
uint64_t oldIDNbr;
|
|
|
|
|
|
|
|
oldIDNbr = [self idFromURL: urlString];
|
|
|
|
if (oldIDNbr)
|
|
|
|
{
|
|
|
|
[self logWithFormat: @"Updating URL: %@ with id %.16"PRIx64" from old id %.16"PRIx64,
|
|
|
|
urlString, idNbr, oldIDNbr];
|
|
|
|
[self unregisterURLWithID: oldIDNbr];
|
|
|
|
[self registerURL: urlString
|
|
|
|
withID: idNbr];
|
|
|
|
rc = YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2010-10-01 20:54:30 +02:00
|
|
|
- (BOOL) registerURL: (NSString *) urlString
|
|
|
|
withID: (uint64_t) idNbr
|
|
|
|
{
|
2013-11-11 03:17:53 +01:00
|
|
|
NSString *oldURL;
|
|
|
|
uint64_t oldIdNbr;
|
2015-04-13 11:48:42 +02:00
|
|
|
bool softDeleted;
|
2010-10-01 22:35:29 +02:00
|
|
|
|
2013-11-11 03:17:53 +01:00
|
|
|
oldURL = [self urlFromID: idNbr];
|
2014-06-13 14:20:00 +02:00
|
|
|
if (oldURL != NULL)
|
|
|
|
{
|
|
|
|
[self errorWithFormat:
|
|
|
|
@"url with idNbr already registered: (oldUrl='%@', newUrl='%@', id=x%.16"PRIx64")",
|
|
|
|
oldURL, urlString, idNbr];
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2014-10-15 00:35:17 +02:00
|
|
|
oldIdNbr = [self idFromURL: urlString isSoftDeleted: &softDeleted];
|
2014-06-13 14:20:00 +02:00
|
|
|
if (oldIdNbr != NSNotFound)
|
2010-10-01 22:35:29 +02:00
|
|
|
{
|
2014-10-15 00:35:17 +02:00
|
|
|
if (softDeleted)
|
|
|
|
[self logWithFormat: @"Attempt to register a soft-deleted %@", urlString];
|
|
|
|
else
|
|
|
|
[self errorWithFormat:
|
|
|
|
@"attempt to double register an entry with idNbr ('%@', %lld,"
|
|
|
|
@" 0x%.16"PRIx64", oldid=0x%.16"PRIx64")",
|
|
|
|
urlString, idNbr, idNbr, oldIdNbr];
|
2014-06-13 14:20:00 +02:00
|
|
|
return NO;
|
2010-10-01 22:35:29 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-10-04 00:21:36 +02:00
|
|
|
// [self logWithFormat: @"registered url '%@' with id %lld (0x%.16"PRIx64")",
|
|
|
|
// urlString, idNbr, idNbr];
|
2011-06-04 01:53:30 +02:00
|
|
|
|
|
|
|
/* Add the record given its fid and mapistore_uri */
|
2013-11-11 03:17:53 +01:00
|
|
|
indexing->add_fmid(indexing, [username UTF8String],
|
|
|
|
idNbr, [urlString UTF8String]);
|
2010-10-01 22:35:29 +02:00
|
|
|
}
|
|
|
|
|
2015-04-13 11:48:42 +02:00
|
|
|
return YES;
|
2010-10-01 20:54:30 +02:00
|
|
|
}
|
|
|
|
|
2012-10-12 23:44:26 +02:00
|
|
|
- (void) registerURLs: (NSArray *) urlStrings
|
2012-10-13 05:27:05 +02:00
|
|
|
withIDs: (NSArray *) idNbrs
|
2012-10-12 23:44:26 +02:00
|
|
|
{
|
2012-10-13 05:27:05 +02:00
|
|
|
uint64_t count, max, newID;
|
2012-10-12 23:44:26 +02:00
|
|
|
|
|
|
|
max = [urlStrings count];
|
2012-10-13 05:27:05 +02:00
|
|
|
if (max == [idNbrs count])
|
2012-10-12 23:44:26 +02:00
|
|
|
{
|
|
|
|
for (count = 0; count < max; count++)
|
2012-10-13 05:27:05 +02:00
|
|
|
{
|
|
|
|
newID = [[idNbrs objectAtIndex: count]
|
|
|
|
unsignedLongLongValue];
|
|
|
|
[self registerURL: [urlStrings objectAtIndex: count]
|
|
|
|
withID: newID];
|
|
|
|
}
|
2012-10-12 23:44:26 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"number of urls and ids do not match"];
|
|
|
|
}
|
|
|
|
|
2011-02-03 23:09:41 +01:00
|
|
|
- (void) unregisterURLWithID: (uint64_t) idNbr
|
|
|
|
{
|
2013-11-11 03:17:53 +01:00
|
|
|
indexing->del_fmid(indexing, [username UTF8String],
|
|
|
|
idNbr, MAPISTORE_PERMANENT_DELETE);
|
2011-02-03 23:09:41 +01:00
|
|
|
}
|
|
|
|
|
2014-10-14 23:48:36 +02:00
|
|
|
- (void) unregisterURLWithID: (uint64_t) idNbr
|
|
|
|
andFlags: (uint8_t) flags
|
|
|
|
{
|
|
|
|
indexing->del_fmid(indexing, [username UTF8String],
|
|
|
|
idNbr,
|
|
|
|
(flags == MAPISTORE_SOFT_DELETE) ? MAPISTORE_SOFT_DELETE : MAPISTORE_PERMANENT_DELETE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-01 20:54:30 +02:00
|
|
|
@end
|