Revision 0224a49f pithos-macos/PithosUtilities.m

b/pithos-macos/PithosUtilities.m
56 56
        fileName = [fileName stringByAppendingString:@"/"];    
57 57
    fileName = [fileName stringByReplacingOccurrencesOfString:@"/" withString:@":"];
58 58
    
59
    NSFileManager *defaultManager = [NSFileManager defaultManager];
59
    NSFileManager *fileManager = [NSFileManager defaultManager];
60 60
    
61 61
    NSString *destinationPath = [directoryPath stringByAppendingPathComponent:fileName];
62
    if (ifExists && [defaultManager fileExistsAtPath:destinationPath]) {
62
    if (ifExists && [fileManager fileExistsAtPath:destinationPath]) {
63 63
        NSAlert *alert = [[[NSAlert alloc] init] autorelease];
64 64
        [alert setMessageText:@"File Exists"];
65 65
        [alert setInformativeText:[NSString stringWithFormat:@"A file or directory named '%@' already exists, do you want to replace it?", fileName]];
......
71 71
    }
72 72
    
73 73
    BOOL directoryIsDirectory;
74
    BOOL directoryExists = [defaultManager fileExistsAtPath:directoryPath isDirectory:&directoryIsDirectory];
74
    BOOL directoryExists = [fileManager fileExistsAtPath:directoryPath isDirectory:&directoryIsDirectory];
75 75
    NSError *error = nil;
76 76
    if (!directoryExists) {
77
        [defaultManager createDirectoryAtPath:directoryPath withIntermediateDirectories:YES attributes:nil error:&error];
77
        [fileManager createDirectoryAtPath:directoryPath withIntermediateDirectories:YES attributes:nil error:&error];
78 78
    } else if (!directoryIsDirectory) {
79
        [defaultManager removeItemAtPath:directoryPath error:&error];
79
        [fileManager removeItemAtPath:directoryPath error:&error];
80 80
    }
81 81
    if (error) {
82 82
        NSLog(@"Cannot remove existing file '%@': %@", fileName, error);
......
124 124
    if (objects == nil)
125 125
        return nil;
126 126
    
127
    NSFileManager *defaultManager = [NSFileManager defaultManager];
127
    NSFileManager *fileManager = [NSFileManager defaultManager];
128 128
    NSMutableArray *objectRequests = [NSMutableArray arrayWithCapacity:[objects count]];
129 129
    NSUInteger subdirPrefixLength = [objectName length];
130 130

  
131 131
    NSError *error = nil;
132
    [defaultManager createDirectoryAtPath:[directoryPath stringByAppendingPathComponent:subdirName] withIntermediateDirectories:YES attributes:nil error:&error];
132
    [fileManager createDirectoryAtPath:[directoryPath stringByAppendingPathComponent:subdirName] withIntermediateDirectories:YES attributes:nil error:&error];
133 133
    if (error) {
134 134
        NSLog(@"Cannot create directory at '%@': %@", directoryPath, error);
135 135
        NSAlert *alert = [[[NSAlert alloc] init] autorelease];
......
146 146
            subdirDirectoryPath = [subdirDirectoryPath stringByAppendingPathComponent:[object.name substringFromIndex:subdirPrefixLength]];
147 147
            
148 148
            BOOL directoryIsDirectory;
149
            BOOL directoryExists = [defaultManager fileExistsAtPath:subdirDirectoryPath isDirectory:&directoryIsDirectory];
149
            BOOL directoryExists = [fileManager fileExistsAtPath:subdirDirectoryPath isDirectory:&directoryIsDirectory];
150 150
            NSError *error = nil;
151 151
            if (!directoryExists) {
152
                [defaultManager createDirectoryAtPath:subdirDirectoryPath withIntermediateDirectories:YES attributes:nil error:&error];
152
                [fileManager createDirectoryAtPath:subdirDirectoryPath withIntermediateDirectories:YES attributes:nil error:&error];
153 153
                if (error) {
154 154
                    NSLog(@"Cannot create directory at '%@': %@", subdirDirectoryPath, error);
155 155
                    NSAlert *alert = [[[NSAlert alloc] init] autorelease];
......
160 160
                    [alert runModal];
161 161
                }
162 162
            } else if (!directoryIsDirectory) {
163
                [defaultManager removeItemAtPath:subdirDirectoryPath error:&error];
163
                [fileManager removeItemAtPath:subdirDirectoryPath error:&error];
164 164
                if (error) {
165 165
                    NSLog(@"Cannot remove existing file at '%@': %@", subdirDirectoryPath, error);
166 166
                    NSAlert *alert = [[[NSAlert alloc] init] autorelease];
......
193 193
}
194 194

  
195 195
#pragma mark -
196
#pragma mark Download Block
197

  
198
+ (ASIPithosObjectRequest *)objectBlockDataRequestWithContainerName:(NSString *)containerName 
199
                                                             object:(ASIPithosObject *)object 
200
                                                         blockIndex:(NSUInteger)blockIndex 
201
                                                          blockSize:(NSUInteger)blockSize {
202
    NSUInteger rangeStart = blockIndex * blockSize;
203
    NSUInteger rangeEnd = (rangeStart + blockSize <= object.bytes) ? (rangeStart + blockSize - 1) : (object.bytes - 1);
204
    ASIPithosObjectRequest *objectRequest = [ASIPithosObjectRequest objectDataRequestWithContainerName:containerName
205
                                                                                            objectName:object.name
206
                                                                                               version:nil
207
                                                                                                 range:[NSString stringWithFormat:@"bytes=%lu-%lu", rangeStart, rangeEnd]
208
                                                                                               ifMatch:object.hash];
209
    return objectRequest;
210
}
211

  
212
+ (NSIndexSet *)missingBlocksForFile:(NSString *)filePath
213
                           blockSize:(NSUInteger)blockSize 
214
                           blockHash:(NSString *)blockHash 
215
                          withHashes:(NSArray *)hashes {
216
    NSArray *fileHashes = [HashMapHash objectHashMapStrings:filePath withBlockHash:blockHash andBlockSize:blockSize];
217
    return [hashes indexesOfObjectsPassingTest:^(id obj, NSUInteger idx, BOOL *stop) {
218
        if ((idx >= [fileHashes count]) || ![(NSString *)obj isEqualToString:[fileHashes objectAtIndex:idx]])
219
            return YES;
220
        return NO;
221
    }];
222
}
223

  
224
#pragma mark -
196 225
#pragma mark Upload
197 226

  
198 227
+ (ASIPithosObjectRequest *)writeObjectDataRequestWithContainerName:(NSString *)containerName
......
259 288
                                                            sharingAccount:(NSString *)sharingAccount {
260 289
    NSIndexSet *missingBlocks = [self missingBlocksForHashes:hashes withMissingHashesResponse:missingHashesResponse];
261 290
    
262
    NSFileManager *defaultManager = [NSFileManager defaultManager];
291
    NSFileManager *fileManager = [NSFileManager defaultManager];
263 292
    NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:filePath];
264 293
    
265 294
    // http://cocoawithlove.com/2009/07/temporary-files-and-folders-in-cocoa.html
......
271 300
    char *tempFileNameCString = (char *)malloc(strlen(tempFileTemplateCString) + 1);
272 301
    strcpy(tempFileNameCString, tempFileTemplateCString);
273 302
    int fileDescriptor = mkstemp(tempFileNameCString);
274
    NSString *tempFilePath = [defaultManager stringWithFileSystemRepresentation:tempFileNameCString length:strlen(tempFileNameCString)];
303
    NSString *tempFilePath = [fileManager stringWithFileSystemRepresentation:tempFileNameCString length:strlen(tempFileNameCString)];
275 304
    if (fileDescriptor == -1) {
276 305
        NSAlert *alert = [[[NSAlert alloc] init] autorelease];
277 306
        [alert setMessageText:@"Create Temporary File Error"];
......
334 363
    if (ifExists && ![self proceedIfObjectExistsAtContainerName:containerName objectName:objectName sharingAccount:sharingAccount])
335 364
        return nil;
336 365

  
337
    NSFileManager *defaultManager = [NSFileManager defaultManager];
366
    NSFileManager *fileManager = [NSFileManager defaultManager];
338 367
    NSError *error = nil;
339
    NSArray *subPaths = [defaultManager subpathsOfDirectoryAtPath:directoryPath error:&error];
368
    NSArray *subPaths = [fileManager subpathsOfDirectoryAtPath:directoryPath error:&error];
340 369
    if (error) {
341 370
        NSAlert *alert = [[[NSAlert alloc] init] autorelease];
342 371
        [alert setMessageText:@"Directory Read Error"];
......
380 409
    NSString *fileName;
381 410
    for (NSString *objectNameSuffix in subPaths) {
382 411
        filePath = [directoryPath stringByAppendingPathComponent:objectNameSuffix];
383
        if ([defaultManager fileExistsAtPath:filePath isDirectory:&isDirectory]) {
412
        if ([fileManager fileExistsAtPath:filePath isDirectory:&isDirectory]) {
384 413
            if (!isDirectory) {
385 414
                hashes = [HashMapHash objectHashMapStrings:filePath withBlockHash:blockHash andBlockSize:blockSize];
386 415
                if (hashes) {
......
795 824

  
796 825
// Size of the file in bytes
797 826
+ (NSUInteger)bytesOfFile:(NSString *)filePath {
798
    NSFileManager *defaultManager = [NSFileManager defaultManager];
799
    NSDictionary *attributes = [defaultManager attributesOfItemAtPath:filePath error:nil];
827
    NSFileManager *fileManager = [NSFileManager defaultManager];
828
    NSDictionary *attributes = [fileManager attributesOfItemAtPath:filePath error:nil];
800 829
    return [[attributes objectForKey:NSFileSize] intValue];
801 830
}
802 831

  
......
1057 1086
    return [alert runModal];
1058 1087
}
1059 1088

  
1089
+ (NSInteger)fileActionFailedAlertWithTitle:(NSString *)title message:(NSString *)message error:(NSError *)error {
1090
    NSAlert *alert = [[[NSAlert alloc] init] autorelease];
1091
    [alert setMessageText:title];
1092
    if (error)
1093
        [alert setInformativeText:[NSString stringWithFormat:@"%@: %@", message, error]];
1094
    else
1095
        [alert setInformativeText:message];
1096
    [alert addButtonWithTitle:@"OK"];
1097
    return [alert runModal];
1098
}
1099

  
1060 1100
#pragma mark -
1061 1101
#pragma mark Request Helper Methods
1062 1102

  

Also available in: Unified diff