Some code refactoring in the sync daemon.
[pithos-macos] / pithos-macos / PithosLocalObjectState.m
1 //
2 //  PithosLocalObjectState.m
3 //  pithos-macos
4 //
5 // Copyright 2011 GRNET S.A. All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or
8 // without modification, are permitted provided that the following
9 // conditions are met:
10 // 
11 //   1. Redistributions of source code must retain the above
12 //      copyright notice, this list of conditions and the following
13 //      disclaimer.
14 // 
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.
19 // 
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.
32 // 
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.
37
38 #import "PithosLocalObjectState.h"
39 #import "PithosUtilities.h"
40 #import "FileMD5Hash.h"
41 #import "HashMapHash.h"
42
43 @implementation PithosLocalObjectState
44 @synthesize md5, hashMapHash, tmpDownloadFile, isDirectory, exists, hash;
45
46 #pragma mark -
47 #pragma mark Object Lifecycle
48
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]];
57         }
58     }
59     return self;
60 }
61
62 - (id)initWithHash:(NSString *)aHash directory:(BOOL)anIsDirectory {
63     if ((self = [self init])) {
64         if (anIsDirectory)
65             isDirectory = YES;
66         else if ([aHash length] == 32)
67             self.md5 = aHash;
68         else if ([aHash length] == 64)
69             self.hashMapHash = aHash;
70     }
71     return self;
72 }
73
74 + (id)localObjectState {
75     return [[[self alloc] init] autorelease];
76 }
77
78 + (id)localObjectStateWithFile:(NSString *)filePath blockHash:(NSString *)blockHash blockSize:(NSUInteger)blockSize {
79     return [[[self alloc] initWithFile:filePath blockHash:blockHash blockSize:blockSize] autorelease];
80 }
81
82 + (id)localObjectStateWithHash:(NSString *)aHash directory:(BOOL)anIsDirectory {
83     return [[[self alloc] initWithHash:aHash directory:anIsDirectory] autorelease];
84 }
85
86 - (void)dealloc {
87     self.tmpDownloadFile = nil;
88     [hashMapHash release];
89     [md5 release];
90     [super dealloc];    
91 }
92
93 - (NSString *)description {
94     return [NSString stringWithFormat:@"md5: %@, hashMapHash: %@, tmpDownloadFile: %@, isDirectory: %d", 
95             md5, hashMapHash, tmpDownloadFile, isDirectory];
96 }
97
98 - (BOOL)isEqualToState:(PithosLocalObjectState *)aState {
99     if (self.isDirectory)
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
104         return NO;
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
110         return NO;
111     else
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]);
114 }
115
116 #pragma mark -
117 #pragma mark Properties
118
119 - (void)setIsDirectory:(BOOL)anIsDirectory {
120     isDirectory = anIsDirectory;
121     if (isDirectory) {
122         self.md5 = nil;
123         self.hashMapHash = nil;
124         self.tmpDownloadFile = nil;
125     }
126 }
127
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] 
137                                                               error:error];
138             }
139         }
140         [tmpDownloadFile release];
141         tmpDownloadFile = [aTmpDownloadFile retain];
142     }
143 }
144
145 - (BOOL)exists {
146     return (isDirectory || md5 || hashMapHash);
147 }
148
149 - (NSString *)hash {
150     if (hashMapHash)
151         return hashMapHash;
152     else
153         return md5;
154 }
155
156 - (void)setHash:(NSString *)aHash {
157     self.md5 = nil;
158     self.hashMapHash = nil;
159     if ([aHash length] == 32) {
160         self.md5 = aHash;
161     } else if ([aHash length] == 64) {
162         self.hashMapHash = aHash;
163     }
164 }
165
166 #pragma mark -
167 #pragma mark NSCoding
168
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"];
175     }
176     return self;
177 }
178
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"];
184 }
185
186 @end