2010-10-01 22:35:29 +02:00
|
|
|
/* MAPIStoreMapping.m - this file is part of SOGo
|
2010-10-01 20:54:30 +02:00
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Inverse inc.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
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>
|
|
|
|
|
|
|
|
#import "MAPIStoreMapping.h"
|
|
|
|
|
2010-12-13 17:54:32 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <tdb.h>
|
|
|
|
#include <talloc.h>
|
|
|
|
|
|
|
|
struct tdb_wrap {
|
|
|
|
struct tdb_context *tdb;
|
|
|
|
|
|
|
|
const char *name;
|
|
|
|
struct tdb_wrap *next, *prev;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern struct tdb_wrap *tdb_wrap_open(TALLOC_CTX *mem_ctx,
|
|
|
|
const char *name, int hash_size, int tdb_flags,
|
|
|
|
int open_flags, mode_t mode);
|
|
|
|
|
|
|
|
static int
|
|
|
|
MAPIStoreMappingTDBTraverse (TDB_CONTEXT *ctx, TDB_DATA data1, TDB_DATA data2,
|
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
NSMutableDictionary *mapping;
|
|
|
|
NSNumber *idNbr;
|
|
|
|
NSString *uri;
|
|
|
|
char *idStr, *uriStr;
|
|
|
|
uint64_t idVal;
|
|
|
|
|
|
|
|
idStr = (char *) data1.dptr;
|
|
|
|
idVal = strtoll (idStr, NULL, 16);
|
|
|
|
idNbr = [NSNumber numberWithUnsignedLongLong: idVal];
|
|
|
|
|
|
|
|
uriStr = strdup ((const char *) data2.dptr);
|
|
|
|
*(uriStr+(data2.dsize)) = 0;
|
|
|
|
uri = [NSString stringWithUTF8String: uriStr];
|
|
|
|
free (uriStr);
|
|
|
|
|
|
|
|
mapping = data;
|
|
|
|
[mapping setObject: uri forKey: idNbr];
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
MAPIStoreMappingInitDictionary (NSMutableDictionary *mapping)
|
|
|
|
{
|
|
|
|
struct tdb_wrap *wrap;
|
|
|
|
TDB_CONTEXT *context;
|
|
|
|
char *tdb_path;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
tdb_path = "/usr/local/samba/private/mapistore/openchange/indexing.tdb";
|
|
|
|
wrap = tdb_wrap_open(NULL, tdb_path, 0, TDB_NOLOCK, O_RDONLY, 0600);
|
|
|
|
|
|
|
|
context = wrap->tdb;
|
|
|
|
rc = tdb_traverse_read(wrap->tdb, MAPIStoreMappingTDBTraverse, mapping);
|
|
|
|
|
|
|
|
}
|
2010-10-01 20:54:30 +02:00
|
|
|
|
|
|
|
@implementation MAPIStoreMapping
|
|
|
|
|
2010-10-15 19:18:34 +02:00
|
|
|
+ (id) sharedMapping
|
|
|
|
{
|
|
|
|
static id sharedMapping = nil;
|
|
|
|
|
|
|
|
if (!sharedMapping)
|
|
|
|
sharedMapping = [self new];
|
|
|
|
|
|
|
|
return sharedMapping;
|
|
|
|
}
|
|
|
|
|
2010-10-01 20:54:30 +02:00
|
|
|
- (id) init
|
|
|
|
{
|
2010-12-13 17:54:32 +01:00
|
|
|
NSNumber *idNbr;
|
|
|
|
NSString *uri;
|
|
|
|
NSArray *keys;
|
|
|
|
NSUInteger count, max;
|
|
|
|
|
2010-10-01 22:35:29 +02:00
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
|
|
|
mapping = [NSMutableDictionary new];
|
2010-12-13 17:54:32 +01:00
|
|
|
MAPIStoreMappingInitDictionary (mapping);
|
2010-10-01 22:35:29 +02:00
|
|
|
reverseMapping = [NSMutableDictionary new];
|
2010-12-13 17:54:32 +01:00
|
|
|
|
|
|
|
keys = [mapping allKeys];
|
|
|
|
max = [keys count];
|
|
|
|
for (count = 0; count < max; count++)
|
|
|
|
{
|
|
|
|
idNbr = [keys objectAtIndex: count];
|
|
|
|
uri = [mapping objectForKey: idNbr];
|
|
|
|
[reverseMapping setObject: idNbr forKey: uri];
|
|
|
|
}
|
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
|
|
|
|
{
|
2010-10-01 22:35:29 +02:00
|
|
|
[mapping release];
|
|
|
|
[reverseMapping release];
|
|
|
|
[super dealloc];
|
2010-10-01 20:54:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *) urlFromID: (uint64_t) idNbr
|
|
|
|
{
|
2010-10-01 22:35:29 +02:00
|
|
|
NSNumber *key;
|
|
|
|
|
|
|
|
key = [NSNumber numberWithUnsignedLongLong: idNbr];
|
|
|
|
|
|
|
|
return [mapping objectForKey: key];
|
2010-10-01 20:54:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (uint64_t) idFromURL: (NSString *) url
|
|
|
|
{
|
2010-10-01 22:35:29 +02:00
|
|
|
NSNumber *idKey;
|
|
|
|
uint64_t idNbr;
|
|
|
|
|
|
|
|
idKey = [reverseMapping objectForKey: url];
|
|
|
|
if (idKey)
|
|
|
|
idNbr = [idKey unsignedLongLongValue];
|
|
|
|
else
|
|
|
|
idNbr = NSNotFound;
|
2010-10-13 17:30:01 +02:00
|
|
|
|
2010-10-01 22:35:29 +02:00
|
|
|
return idNbr;
|
2010-10-01 20:54:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) registerURL: (NSString *) urlString
|
|
|
|
withID: (uint64_t) idNbr
|
|
|
|
{
|
2010-10-01 22:35:29 +02:00
|
|
|
NSNumber *idKey;
|
|
|
|
BOOL rc;
|
|
|
|
|
|
|
|
idKey = [NSNumber numberWithUnsignedLongLong: idNbr];
|
|
|
|
if ([mapping objectForKey: idKey]
|
|
|
|
|| [reverseMapping objectForKey: urlString])
|
|
|
|
{
|
|
|
|
[self errorWithFormat: @"attempt to double register an entry ('%@', %lld)",
|
|
|
|
urlString, idNbr];
|
|
|
|
rc = NO;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[mapping setObject: urlString forKey: idKey];
|
|
|
|
[reverseMapping setObject: idKey forKey: urlString];
|
|
|
|
rc = YES;
|
|
|
|
[self logWithFormat: @"registered url '%@' with id %lld (0x%.8x)",
|
|
|
|
urlString, idNbr, (uint32_t) idNbr];
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
2010-10-01 20:54:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|