2 // PithosLocalObjectState.m
5 // Copyright 2011 GRNET S.A. All rights reserved.
7 // Redistribution and use in source and binary forms, with or
8 // without modification, are permitted provided that the following
11 // 1. Redistributions of source code must retain the above
12 // copyright notice, this list of conditions and the following
15 // 2. Redistributions in binary form must reproduce the above
16 // copyright notice, this list of conditions and the following
17 // disclaimer in the documentation and/or other materials
18 // provided with the distribution.
20 // THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
21 // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
24 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
27 // USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 // AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 // POSSIBILITY OF SUCH DAMAGE.
33 // The views and conclusions contained in the software and
34 // documentation are those of the authors and should not be
35 // interpreted as representing official policies, either expressed
36 // or implied, of GRNET S.A.
38 #import "PithosLocalObjectState.h"
39 #import "PithosUtilities.h"
40 #import "FileMD5Hash.h"
41 #import "HashMapHash.h"
43 @implementation PithosLocalObjectState
44 @synthesize md5, hashMapHash, tmpDownloadFile, isDirectory, exists, hash;
47 #pragma mark Object Lifecycle
49 - (id)initWithFile:(NSString *)filePath blockHash:(NSString *)blockHash blockSize:(NSUInteger)blockSize {
50 if ((self = [self init])) {
51 if ([[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory] && !isDirectory) {
52 self.md5 = (NSString *)FileMD5HashCreateWithPath((CFStringRef)filePath,
53 FileHashDefaultChunkSizeForReadingData);
54 self.hashMapHash = [HashMapHash calculateHashMapHash:[HashMapHash calculateObjectHashMap:filePath
55 withBlockHash:blockHash
56 andBlockSize:blockSize]];
62 - (id)initWithHash:(NSString *)aHash directory:(BOOL)anIsDirectory {
63 if ((self = [self init])) {
66 else if ([aHash length] == 32)
68 else if ([aHash length] == 64)
69 self.hashMapHash = aHash;
74 + (id)localObjectState {
75 return [[[self alloc] init] autorelease];
78 + (id)localObjectStateWithFile:(NSString *)filePath blockHash:(NSString *)blockHash blockSize:(NSUInteger)blockSize {
79 return [[[self alloc] initWithFile:filePath blockHash:blockHash blockSize:blockSize] autorelease];
82 + (id)localObjectStateWithHash:(NSString *)aHash directory:(BOOL)anIsDirectory {
83 return [[[self alloc] initWithHash:aHash directory:anIsDirectory] autorelease];
87 self.tmpDownloadFile = nil;
88 [hashMapHash release];
93 - (NSString *)description {
94 return [NSString stringWithFormat:@"md5: %@, hashMapHash: %@, tmpDownloadFile: %@, isDirectory: %d",
95 md5, hashMapHash, tmpDownloadFile, isDirectory];
98 - (BOOL)isEqualToState:(PithosLocalObjectState *)aState {
100 // Object is a directory, check the other
101 return aState.isDirectory;
102 else if (aState.isDirectory)
103 // Object is not a directory, while the other is
105 else if (!self.exists)
106 // Object doesn't exist, check the other
107 return (!aState.exists);
108 else if (!aState.exists)
109 // Object exists, while the other doesn't
112 // Both objects exist, check that they have at least one hash in common
113 return ([self.md5 isEqualToString:aState.md5] || [self.hashMapHash isEqualToString:aState.hashMapHash]);
117 #pragma mark Properties
119 - (void)setIsDirectory:(BOOL)anIsDirectory {
120 isDirectory = anIsDirectory;
123 self.hashMapHash = nil;
124 self.tmpDownloadFile = nil;
128 - (void)setTmpDownloadFile:(NSString *)aTmpDownloadFile {
129 if (![tmpDownloadFile isEqualToString:aTmpDownloadFile]) {
130 if (!aTmpDownloadFile) {
131 NSFileManager *fileManager = [NSFileManager defaultManager];
132 if ([fileManager fileExistsAtPath:tmpDownloadFile]) {
133 NSError *error = nil;
134 if (![fileManager removeItemAtPath:tmpDownloadFile error:&error] || error)
135 [PithosUtilities fileActionFailedAlertWithTitle:@"Remove File Error"
136 message:[NSString stringWithFormat:@"Cannot remove file at '%@'", tmpDownloadFile]
140 [tmpDownloadFile release];
141 tmpDownloadFile = [aTmpDownloadFile retain];
146 return (isDirectory || md5 || hashMapHash);
156 - (void)setHash:(NSString *)aHash {
158 self.hashMapHash = nil;
159 if ([aHash length] == 32) {
161 } else if ([aHash length] == 64) {
162 self.hashMapHash = aHash;
167 #pragma mark NSCoding
169 - (id)initWithCoder:(NSCoder *)decoder {
170 if ((self = [super init])) {
171 self.md5 = [decoder decodeObjectForKey:@"md5"];
172 self.hashMapHash = [decoder decodeObjectForKey:@"hashMapHash"];
173 self.tmpDownloadFile = [decoder decodeObjectForKey:@"tmpDownloadFile"];
174 self.isDirectory = [decoder decodeBoolForKey:@"isDirectory"];
179 - (void)encodeWithCoder:(NSCoder *)encoder {
180 [encoder encodeObject:md5 forKey:@"md5"];
181 [encoder encodeObject:hashMapHash forKey:@"hashMapHash"];
182 [encoder encodeObject:tmpDownloadFile forKey:@"tmpDownloadFile"];
183 [encoder encodeBool:isDirectory forKey:@"isDirectory"];