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 "HashMapHash.h"
42 @implementation PithosLocalObjectState
43 @synthesize filePath, isDirectory, hash, tmpFilePath, fileModificationDate, fileSize;
46 #pragma mark Object Lifecycle
48 - (id)initWithFile:(NSString *)aFilePath blockHash:(NSString *)blockHash blockSize:(NSUInteger)blockSize {
49 if ((self = [self init])) {
50 self.filePath = aFilePath;
51 if ([[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory] && !isDirectory) {
52 self.hash = [HashMapHash calculateHashMapHash:[HashMapHash calculateObjectHashMap:filePath
53 withBlockHash:blockHash
54 andBlockSize:blockSize]];
60 - (id)initWithHash:(NSString *)aHash directory:(BOOL)anIsDirectory {
61 if ((self = [self init])) {
64 else if ([aHash length] == 64)
70 + (id)localObjectState {
71 return [[[self alloc] init] autorelease];
74 + (id)localObjectStateWithFile:(NSString *)aFilePath blockHash:(NSString *)blockHash blockSize:(NSUInteger)blockSize {
75 return [[[self alloc] initWithFile:aFilePath blockHash:blockHash blockSize:blockSize] autorelease];
78 + (id)localObjectStateWithHash:(NSString *)aHash directory:(BOOL)anIsDirectory {
79 return [[[self alloc] initWithHash:aHash directory:anIsDirectory] autorelease];
84 [fileModificationDate release];
85 self.tmpFilePath = nil;
91 - (NSString *)description {
92 return [NSString stringWithFormat:@"hash: %@, filePath: %@, isDirectory: %d, fileModificationDate: %@, fileSize: %@",
93 hash, filePath, isDirectory, fileModificationDate, fileSize];
96 - (BOOL)isEqualToState:(PithosLocalObjectState *)aState {
98 // Object is a directory, check the other
99 return aState.isDirectory;
100 else if (aState.isDirectory)
101 // Object is not a directory, while the other is
103 else if (![self exists])
104 // Object doesn't exist, check the other
105 return (![aState exists]);
106 else if (![aState exists])
107 // Object exists, while the other doesn't
110 // Both objects exist, check that they have the same hash
111 return [self.hash isEqualToString:aState.hash];
115 #pragma mark Properties
117 - (void)setIsDirectory:(BOOL)anIsDirectory {
118 isDirectory = anIsDirectory;
121 self.tmpFilePath = nil;
125 - (void)setTmpFilePath:(NSString *)aTmpFilePath {
126 if (![tmpFilePath isEqualToString:aTmpFilePath]) {
128 NSFileManager *fileManager = [NSFileManager defaultManager];
129 if ([fileManager fileExistsAtPath:tmpFilePath]) {
130 NSError *error = nil;
131 if (![fileManager removeItemAtPath:tmpFilePath error:&error] || error)
132 [PithosUtilities fileActionFailedAlertWithTitle:@"Remove File Error"
133 message:[NSString stringWithFormat:@"Cannot remove file at '%@'", tmpFilePath]
137 [tmpFilePath release];
138 tmpFilePath = [aTmpFilePath retain];
142 - (void)setHash:(NSString *)aHash {
144 if ([aHash length] == 64)
145 hash = [aHash retain];
149 NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
150 self.fileModificationDate = [attributes objectForKey:NSFileModificationDate];
151 self.fileSize = [attributes objectForKey:NSFileSize];
153 self.fileModificationDate = nil;
162 return (isDirectory || hash);
166 NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
167 return (![fileSize isEqualToNumber:[attributes objectForKey:NSFileSize]] ||
168 ![fileModificationDate isEqualToDate:[attributes objectForKey:NSFileModificationDate]]);
172 #pragma mark NSCoding
174 - (id)initWithCoder:(NSCoder *)decoder {
175 if ((self = [super init])) {
176 self.hash = [decoder decodeObjectForKey:@"hash"];
177 self.filePath = [decoder decodeObjectForKey:@"filePath"];
178 self.isDirectory = [decoder decodeBoolForKey:@"isDirectory"];
179 self.tmpFilePath = [decoder decodeObjectForKey:@"tmpFilePath"];
180 self.fileModificationDate = [decoder decodeObjectForKey:@"fileModificationDate"];
181 self.fileSize = [decoder decodeObjectForKey:@"fileSize"];
186 - (void)encodeWithCoder:(NSCoder *)encoder {
187 [encoder encodeObject:hash forKey:@"hash"];
188 [encoder encodeObject:filePath forKey:@"filePath"];
189 [encoder encodeBool:isDirectory forKey:@"isDirectory"];
190 [encoder encodeObject:tmpFilePath forKey:@"tmpFilePath"];
191 [encoder encodeObject:fileModificationDate forKey:@"fileModificationDate"];
192 [encoder encodeObject:fileSize forKey:@"fileSize"];