root / pithos-macos / PithosLocalObjectState.m @ cb6abe72
History | View | Annotate | Download (6.8 kB)
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 "HashMapHash.h" |
41 |
|
42 |
@implementation PithosLocalObjectState |
43 |
@synthesize filePath, isDirectory, hash, tmpFilePath, fileModificationDate, fileSize; |
44 |
|
45 |
#pragma mark - |
46 |
#pragma mark Object Lifecycle |
47 |
|
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]]; |
55 |
} |
56 |
} |
57 |
return self; |
58 |
} |
59 |
|
60 |
- (id)initWithHash:(NSString *)aHash directory:(BOOL)anIsDirectory { |
61 |
if ((self = [self init])) { |
62 |
if (anIsDirectory) |
63 |
isDirectory = YES; |
64 |
else if ([aHash length] == 64) |
65 |
self.hash = aHash; |
66 |
} |
67 |
return self; |
68 |
} |
69 |
|
70 |
+ (id)localObjectState { |
71 |
return [[self alloc] init]; |
72 |
} |
73 |
|
74 |
+ (id)localObjectStateWithFile:(NSString *)aFilePath blockHash:(NSString *)blockHash blockSize:(NSUInteger)blockSize { |
75 |
return [[self alloc] initWithFile:aFilePath blockHash:blockHash blockSize:blockSize]; |
76 |
} |
77 |
|
78 |
+ (id)localObjectStateWithHash:(NSString *)aHash directory:(BOOL)anIsDirectory { |
79 |
return [[self alloc] initWithHash:aHash directory:anIsDirectory]; |
80 |
} |
81 |
|
82 |
- (void)dealloc { |
83 |
self.tmpFilePath = nil; |
84 |
} |
85 |
|
86 |
- (NSString *)description { |
87 |
return [NSString stringWithFormat:@"hash: %@, filePath: %@, isDirectory: %d, fileModificationDate: %@, fileSize: %@", |
88 |
hash, filePath, isDirectory, fileModificationDate, fileSize]; |
89 |
} |
90 |
|
91 |
- (BOOL)isEqualToState:(PithosLocalObjectState *)aState { |
92 |
if (self.isDirectory) |
93 |
// Object is a directory, check the other |
94 |
return aState.isDirectory; |
95 |
else if (aState.isDirectory) |
96 |
// Object is not a directory, while the other is |
97 |
return NO; |
98 |
else if (![self exists]) |
99 |
// Object doesn't exist, check the other |
100 |
return (![aState exists]); |
101 |
else if (![aState exists]) |
102 |
// Object exists, while the other doesn't |
103 |
return NO; |
104 |
else |
105 |
// Both objects exist, check that they have the same hash |
106 |
return [self.hash isEqualToString:aState.hash]; |
107 |
} |
108 |
|
109 |
#pragma mark - |
110 |
#pragma mark Properties |
111 |
|
112 |
- (void)setIsDirectory:(BOOL)anIsDirectory { |
113 |
isDirectory = anIsDirectory; |
114 |
if (isDirectory) { |
115 |
self.hash = nil; |
116 |
self.tmpFilePath = nil; |
117 |
} |
118 |
} |
119 |
|
120 |
- (void)setTmpFilePath:(NSString *)aTmpFilePath { |
121 |
if (![tmpFilePath isEqualToString:aTmpFilePath]) { |
122 |
if (!aTmpFilePath) { |
123 |
NSFileManager *fileManager = [NSFileManager defaultManager]; |
124 |
if ([fileManager fileExistsAtPath:tmpFilePath]) { |
125 |
NSError *error = nil; |
126 |
if (![fileManager removeItemAtPath:tmpFilePath error:&error] || error) |
127 |
[PithosUtilities fileActionFailedAlertWithTitle:@"Remove File Error" |
128 |
message:[NSString stringWithFormat:@"Cannot remove file at '%@'", tmpFilePath] |
129 |
error:error]; |
130 |
} |
131 |
} |
132 |
tmpFilePath = aTmpFilePath; |
133 |
} |
134 |
} |
135 |
|
136 |
- (void)setHash:(NSString *)aHash { |
137 |
if ([aHash length] == 64) |
138 |
hash = aHash; |
139 |
else |
140 |
hash = nil; |
141 |
if (filePath) { |
142 |
NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil]; |
143 |
self.fileModificationDate = [attributes objectForKey:NSFileModificationDate]; |
144 |
self.fileSize = [attributes objectForKey:NSFileSize]; |
145 |
} else { |
146 |
self.fileModificationDate = nil; |
147 |
self.fileSize = nil; |
148 |
} |
149 |
} |
150 |
|
151 |
#pragma mark - |
152 |
#pragma mark Methods |
153 |
|
154 |
- (BOOL)exists { |
155 |
return (isDirectory || hash); |
156 |
} |
157 |
|
158 |
- (BOOL)isModified { |
159 |
NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil]; |
160 |
return (![fileSize isEqualToNumber:[attributes objectForKey:NSFileSize]] || |
161 |
![fileModificationDate isEqualToDate:[attributes objectForKey:NSFileModificationDate]]); |
162 |
} |
163 |
|
164 |
#pragma mark - |
165 |
#pragma mark NSCoding |
166 |
|
167 |
- (id)initWithCoder:(NSCoder *)decoder { |
168 |
if ((self = [super init])) { |
169 |
self.hash = [decoder decodeObjectForKey:@"hash"]; |
170 |
self.filePath = [decoder decodeObjectForKey:@"filePath"]; |
171 |
self.isDirectory = [decoder decodeBoolForKey:@"isDirectory"]; |
172 |
self.tmpFilePath = [decoder decodeObjectForKey:@"tmpFilePath"]; |
173 |
self.fileModificationDate = [decoder decodeObjectForKey:@"fileModificationDate"]; |
174 |
self.fileSize = [decoder decodeObjectForKey:@"fileSize"]; |
175 |
} |
176 |
return self; |
177 |
} |
178 |
|
179 |
- (void)encodeWithCoder:(NSCoder *)encoder { |
180 |
[encoder encodeObject:hash forKey:@"hash"]; |
181 |
[encoder encodeObject:filePath forKey:@"filePath"]; |
182 |
[encoder encodeBool:isDirectory forKey:@"isDirectory"]; |
183 |
[encoder encodeObject:tmpFilePath forKey:@"tmpFilePath"]; |
184 |
[encoder encodeObject:fileModificationDate forKey:@"fileModificationDate"]; |
185 |
[encoder encodeObject:fileSize forKey:@"fileSize"]; |
186 |
} |
187 |
|
188 |
@end |