Revision cb6abe72 pithos-macos/PithosSyncDaemon.m

b/pithos-macos/PithosSyncDaemon.m
96 96
            resetLocalState:(BOOL)resetLocalState {
97 97
    if ((self = [super init])) {
98 98
        directoryPath = [aDirectoryPath copy];
99
        pithosAccount = [aPithosAccount retain];
99
        pithosAccount = aPithosAccount;
100 100
        self.accountsDictionary = anAccountsDictionary;
101 101
        skipHidden = aSkipHidden;
102 102
        self.pithos = pithosAccount.pithos;        
......
249 249
- (void)dealloc {
250 250
    [[NSNotificationCenter defaultCenter] removeObserver:self];
251 251
    [self resetDaemon];
252
    [callbackQueue release];
253
    [networkQueue release];
254
    [tempTrashDirPath release];
255
    [tempDownloadsDirPath release];
256
    [pithosStateFilePath release];
257
    [currentLocalObjectStates release];
258
    [storedLocalObjectStates release];
259
    [previousRemoteObjects release];
260
    [remoteObjects release];
261
    [objects release];
262
    [lastCompletedSync release];
263
    [accountsPithosContainers release];
264
    [accountsNames release];
265
    [pithos release];
266
    [accountsDictionary release];
267
    [pithosAccount release];
268
    [directoryPath release];
269
    [super dealloc];
270 252
}
271 253

  
272 254
#pragma mark -
......
281 263

  
282 264
- (NSString *)pithosStateFilePath {
283 265
    if (!pithosStateFilePath) {
284
        pithosStateFilePath = [[[[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] 
266
        pithosStateFilePath = [[[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] 
285 267
                                 stringByAppendingPathComponent:[[NSBundle mainBundle] bundleIdentifier]] 
286 268
                                stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-PithosLocalObjectStates.archive", 
287
                                                                pithosAccount.uniqueName]] retain];
269
                                                                pithosAccount.uniqueName]];
288 270
        NSString *pithosStateFileDirPath = [pithosStateFilePath stringByDeletingLastPathComponent];
289 271
        NSFileManager *fileManager = [NSFileManager defaultManager];
290 272
        BOOL isDirectory;
......
298 280
        //  pithosStateFilePath = nil;
299 281
        // XXX create a dir using mktmps?
300 282
    }
301
    return [[pithosStateFilePath copy] autorelease];
283
    return [pithosStateFilePath copy];
302 284
}
303 285

  
304 286
- (NSString *)tempDownloadsDirPath {
305 287
    if (!tempDownloadsDirPath) {
306
        tempDownloadsDirPath = [[[[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] 
288
        tempDownloadsDirPath = [[[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] 
307 289
                                  stringByAppendingPathComponent:[[NSBundle mainBundle] bundleIdentifier]] 
308 290
                                 stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-TempDownloads", 
309
                                                                 pithosAccount.uniqueName]] retain];
291
                                                                 pithosAccount.uniqueName]];
310 292
        NSFileManager *fileManager = [NSFileManager defaultManager];
311 293
        BOOL isDirectory;
312 294
        BOOL fileExists = [fileManager fileExistsAtPath:tempDownloadsDirPath isDirectory:&isDirectory];
......
324 306

  
325 307
- (NSString *)tempTrashDirPath {
326 308
    if (!tempTrashDirPath) {
327
        tempTrashDirPath = [[[[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] 
309
        tempTrashDirPath = [[[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] 
328 310
                                  stringByAppendingPathComponent:[[NSBundle mainBundle] bundleIdentifier]] 
329 311
                                 stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-TempTrash", 
330
                                                                 pithosAccount.uniqueName]] retain];
312
                                                                 pithosAccount.uniqueName]];
331 313
        NSFileManager *fileManager = [NSFileManager defaultManager];
332 314
        BOOL isDirectory;
333 315
        BOOL fileExists = [fileManager fileExistsAtPath:tempTrashDirPath isDirectory:&isDirectory];
......
347 329
    if (aDirectoryPath && ![aDirectoryPath isEqualToString:directoryPath]) {
348 330
        [self resetDaemon];
349 331
        [self resetLocalStateWithAll:YES];
350
        [directoryPath release];
351 332
        directoryPath = [aDirectoryPath copy];
352 333
    }
353 334
}
......
358 339
        if (reset)
359 340
            [self resetDaemon];
360 341
        
361
        [accountsDictionary release];
362 342
        accountsDictionary = [anAccountsDictionary copy];
363 343
        
364 344
        accountsCount = [accountsDictionary count];
......
396 376

  
397 377
- (void)setPithos:(ASIPithos *)aPithos {
398 378
    if (!pithos) {
399
        pithos = [[ASIPithos pithos] retain];
400
        pithos.authUser = [[aPithos.authUser copy] autorelease];
401
        pithos.authToken = [[aPithos.authToken copy] autorelease];
402
        pithos.storageURLPrefix = [[aPithos.storageURLPrefix copy] autorelease];
403
        pithos.authURL = [[aPithos.authURL copy] autorelease];
404
        pithos.publicURLPrefix = [[aPithos.publicURLPrefix copy] autorelease];
379
        pithos = [ASIPithos pithos];
380
        pithos.authUser = [aPithos.authUser copy];
381
        pithos.authToken = [aPithos.authToken copy];
382
        pithos.storageURLPrefix = [aPithos.storageURLPrefix copy];
383
        pithos.authURL = [aPithos.authURL copy];
384
        pithos.publicURLPrefix = [aPithos.publicURLPrefix copy];
405 385
    }
406 386
    if (aPithos && 
407 387
        (![aPithos.authUser isEqualToString:pithos.authUser] || 
......
411 391
        if (![aPithos.authUser isEqualToString:pithos.authUser] || 
412 392
            ![aPithos.storageURLPrefix isEqualToString:pithos.storageURLPrefix])
413 393
            [self resetLocalStateWithAll:YES];
414
        pithos.authUser = [[aPithos.authUser copy] autorelease];
415
        pithos.authToken = [[aPithos.authToken copy] autorelease];
416
        pithos.storageURLPrefix = [[aPithos.storageURLPrefix copy] autorelease];
417
        pithos.authURL = [[aPithos.authURL copy] autorelease];
418
        pithos.publicURLPrefix = [[aPithos.publicURLPrefix copy] autorelease];
394
        pithos.authUser = [aPithos.authUser copy];
395
        pithos.authToken = [aPithos.authToken copy];
396
        pithos.storageURLPrefix = [aPithos.storageURLPrefix copy];
397
        pithos.authURL = [aPithos.authURL copy];
398
        pithos.publicURLPrefix = [aPithos.publicURLPrefix copy];
419 399
    }        
420 400
}
421 401

  
......
1264 1244

  
1265 1245
- (void)performRequestFinishedDelegateInBackground:(ASIPithosRequest *)request {
1266 1246
    // Add an operation to the callbackQueue with a completionBlock for the case of cancellation
1267
    NSInvocationOperation *operation = [[[NSInvocationOperation alloc] initWithTarget:self 
1247
    NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self 
1268 1248
                                                                             selector:NSSelectorFromString([request.userInfo objectForKey:@"didFinishSelector"]) 
1269
                                                                               object:request] autorelease];
1249
                                                                               object:request];
1270 1250
    operation.completionBlock = ^{
1271 1251
        @autoreleasepool {
1272 1252
            if ([[request.userInfo objectForKey:@"operation"] isCancelled]) {
......
1290 1270
                   withObject:request];
1291 1271
    } else {
1292 1272
        // Add an operation to the callbackQueue with a completionBlock for the case of cancellation
1293
        NSInvocationOperation *operation = [[[NSInvocationOperation alloc] initWithTarget:self 
1273
        NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self 
1294 1274
                                                                                 selector:NSSelectorFromString([request.userInfo objectForKey:@"didFailSelector"]) 
1295
                                                                                   object:request] autorelease];
1275
                                                                                   object:request];
1296 1276
        operation.completionBlock = ^{
1297 1277
            @autoreleasepool {
1298 1278
                if ([[request.userInfo objectForKey:@"operation"] isCancelled]) {
......
1336 1316
                        [containerRemoteObjects setObject:object forKey:object.name];
1337 1317
                    }
1338 1318
                    [[remoteObjects objectForKey:accountName] setObject:containerRemoteObjects forKey:pithosContainer.name];
1339
                    [objects release];
1340 1319
                    objects = nil;
1341 1320
                } else {
1342 1321
                    // Do an additional request to fetch more objects
......
1425 1404
                    NSMutableDictionary *containerStoredLocalObjectStates = [[storedLocalObjectStates objectForKey:accountName] 
1426 1405
                                                                             objectForKey:pithosContainer.name];
1427 1406
                    NSDirectoryEnumerator *dirEnumerator = [fileManager enumeratorAtPath:containerDirectoryPath];
1428
                    for (NSString *objectName in dirEnumerator) {
1407
                    for (__strong NSString *objectName in dirEnumerator) {
1429 1408
                        objectName = [objectName precomposedStringWithCanonicalMapping];
1430 1409
                        if (operation.isCancelled) {
1431 1410
                            operation.completionBlock = nil;
......
1715 1694
                                }
1716 1695
                                __block NSInteger choice;
1717 1696
                                dispatch_sync(dispatch_get_main_queue(), ^{
1718
                                    NSAlert *alert = [[[NSAlert alloc] init] autorelease];
1697
                                    NSAlert *alert = [[NSAlert alloc] init];
1719 1698
                                    [alert setMessageText:@"Conflict"];
1720 1699
                                    [alert setInformativeText:informativeText];
1721 1700
                                    [alert addButtonWithTitle:firstButtonText];
......
1755 1734
    @autoreleasepool {
1756 1735
        NSOperation *operation = [containerRequest.userInfo objectForKey:@"operation"];
1757 1736
        if (operation.isCancelled) {
1758
            [objects release];
1759 1737
            objects = nil;
1760 1738
            return;        
1761 1739
        }
......
1764 1742
                [activityFacility endActivity:[containerRequest.userInfo objectForKey:@"activity"] 
1765 1743
                                  withMessage:[containerRequest.userInfo objectForKey:@"stoppedActivityMessage"]];
1766 1744
            });
1767
            [objects release];
1768 1745
            objects = nil;
1769 1746
            [self syncOperationFinishedWithSuccess:NO];
1770 1747
            return;
......
1772 1749
        // If the server listing fails, the sync should start over, so just retrying is enough
1773 1750
        NSUInteger retries = [[containerRequest.userInfo objectForKey:@"retries"] unsignedIntegerValue];
1774 1751
        if (retries > 0) {
1775
            ASIPithosContainerRequest *newContainerRequest = (ASIPithosContainerRequest *)[[PithosUtilities copyRequest:containerRequest] autorelease];
1752
            ASIPithosContainerRequest *newContainerRequest = (ASIPithosContainerRequest *)[PithosUtilities copyRequest:containerRequest];
1776 1753
            [(NSMutableDictionary *)(newContainerRequest.userInfo)setObject:[NSNumber numberWithUnsignedInteger:(--retries)] forKey:@"retries"];
1777 1754
            [networkQueue addOperation:[PithosUtilities prepareRequest:newContainerRequest priority:[[newContainerRequest.userInfo objectForKey:@"priority"] integerValue]]];
1778 1755
        } else {
......
1780 1757
                [activityFacility endActivity:[containerRequest.userInfo objectForKey:@"activity"] 
1781 1758
                                  withMessage:[containerRequest.userInfo objectForKey:@"failedActivityMessage"]];
1782 1759
            });
1783
            [objects release];
1784 1760
            objects = nil;
1785 1761
            // Since the server listing failed in all retries, the operation finished and the sync cycle is completeted unsuccesfully
1786 1762
            [self syncOperationFinishedWithSuccess:NO];
......
1908 1884
                if (newSyncRequested || syncLate || operation.isCancelled) {
1909 1885
                    [self requestFailed:objectRequest];
1910 1886
                } else {
1911
                    __block ASIPithosObjectRequest *newObjectRequest = [PithosUtilities objectBlockDataRequestWithPithos:pithos 
1887
                    __unsafe_unretained ASIPithosObjectRequest *newObjectRequest = [PithosUtilities objectBlockDataRequestWithPithos:pithos 
1912 1888
                                                                                                           containerName:pithosContainer.name 
1913 1889
                                                                                                                  object:object 
1914 1890
                                                                                                              blockIndex:missingBlockIndex 
......
1978 1954
                                        currentBytes:(activity.totalBytes - [missingBlocks count]*pithosContainer.blockSize)];
1979 1955
                });
1980 1956

  
1981
                __block ASIPithosObjectRequest *newObjectRequest = [PithosUtilities objectBlockDataRequestWithPithos:pithos 
1957
                __unsafe_unretained ASIPithosObjectRequest *newObjectRequest = [PithosUtilities objectBlockDataRequestWithPithos:pithos 
1982 1958
                                                                                                       containerName:pithosContainer.name 
1983 1959
                                                                                                              object:object 
1984 1960
                                                                                                          blockIndex:missingBlockIndex 
......
2130 2106
                                        currentBytes:currentBytes];
2131 2107
                });
2132 2108
                NSUInteger missingBlockIndex = [missingBlocks firstIndex];
2133
                __block ASIPithosContainerRequest *newContainerRequest = [PithosUtilities updateContainerDataRequestWithPithos:pithos 
2109
                __unsafe_unretained ASIPithosContainerRequest *newContainerRequest = [PithosUtilities updateContainerDataRequestWithPithos:pithos 
2134 2110
                                                                                                                 containerName:pithosContainer.name 
2135 2111
                                                                                                                     blockSize:pithosContainer.blockSize 
2136 2112
                                                                                                                       forFile:[objectRequest.userInfo objectForKey:@"filePath"] 
......
2202 2178
                if (newSyncRequested || syncLate || operation.isCancelled) {
2203 2179
                    [self requestFailed:containerRequest];
2204 2180
                } else {
2205
                    __block ASIPithosContainerRequest *newContainerRequest = [PithosUtilities updateContainerDataRequestWithPithos:pithos 
2181
                    __unsafe_unretained ASIPithosContainerRequest *newContainerRequest = [PithosUtilities updateContainerDataRequestWithPithos:pithos 
2206 2182
                                                                                                                     containerName:pithosContainer.name
2207 2183
                                                                                                                         blockSize:pithosContainer.blockSize
2208 2184
                                                                                                                           forFile:[containerRequest.userInfo objectForKey:@"filePath"] 
......
2247 2223
        }
2248 2224
        NSUInteger retries = [[request.userInfo objectForKey:@"retries"] unsignedIntegerValue];
2249 2225
        if (retries > 0) {
2250
            ASIPithosRequest *newRequest = (ASIPithosRequest *)[[PithosUtilities copyRequest:request] autorelease];
2226
            ASIPithosRequest *newRequest = (ASIPithosRequest *)[PithosUtilities copyRequest:request];
2251 2227
            [(NSMutableDictionary *)(newRequest.userInfo)setObject:[NSNumber numberWithUnsignedInteger:(--retries)] forKey:@"retries"];
2252 2228
            [networkQueue addOperation:[PithosUtilities prepareRequest:newRequest priority:[[newRequest.userInfo objectForKey:@"priority"] integerValue]]];
2253 2229
        } else {

Also available in: Unified diff