]> err.no Git - sope/blob - sope-core/NGExtensions/NGFileManager.m
renamed packages as discussed in the developer list
[sope] / sope-core / NGExtensions / NGFileManager.m
1 /*
2   Copyright (C) 2000-2004 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
5
6   OGo is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with OGo; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21 // $Id$
22
23 #include "NGFileManager.h"
24 #include "NGFileManagerURL.h"
25 #include "NSObject+Logs.h"
26 #include "common.h"
27
28 @implementation NGFileManager
29
30 static BOOL logPathOps = NO;
31
32 + (int)version {
33   return 0;
34 }
35
36 - (id)init {
37   if ((self = [super init])) {
38     self->cwd = @"/";
39   }
40   return self;
41 }
42 - (void)dealloc {
43   [self->cwd release];
44   [super dealloc];
45 }
46
47 /* path modifications */
48
49 - (NSString *)standardizePath:(NSString *)_path {
50   NSMutableArray *rpc;
51   NSArray        *pc;
52   unsigned       i, pcn;
53   NSString       *result;
54
55   if (logPathOps) [self logWithFormat:@"standardize: %@", _path];
56   
57   pc = [_path pathComponents];
58   if ((pcn = [pc count]) == 0) return _path;
59
60   if (logPathOps) {
61     [self logWithFormat:@"  components: %@", 
62             [pc componentsJoinedByString:@" => "]];
63   }
64   
65   rpc = [NSMutableArray arrayWithCapacity:pcn];
66   
67   for (i = 0; i < pcn; i++) {
68     NSString *p;
69     
70     p = [pc objectAtIndex:i];
71     
72     if ([p isEqualToString:@"/"]) {
73       /* found root */
74       [rpc removeAllObjects];
75       [rpc addObject:@"/"];
76     }
77     else if ([p isEqualToString:@"."]) {
78       /* found current directory */
79       /* '.' can always be removed, right ??? ... */
80     }
81     else if ([p isEqualToString:@".."]) {
82       /* found parent directory */
83       if (i == 0)
84         /* relative path starting with '..' */
85         [rpc addObject:p];
86       else {
87         /* remove last path component .. */
88         unsigned count;
89         
90         if ((count = [rpc count]) > 0)
91           [rpc removeObjectAtIndex:(count - 1)];
92       }
93     }
94     else if ([p isEqualToString:@""]) {
95       /* ignore empty strings */
96     }
97     else {
98       /* usual path */
99       [rpc addObject:p];
100     }
101   }
102   
103   if (logPathOps) {
104     [self logWithFormat:@"  new components: %@", 
105             [rpc componentsJoinedByString:@" => "]];
106   }
107   result = [NSString pathWithComponents:rpc];
108   if ([result length] == 0)
109     return _path;
110   
111   if (logPathOps) [self logWithFormat:@"  standardized: %@", result];
112   return result;
113 }
114
115 - (NSString *)resolveSymlinksInPath:(NSString *)_path {
116   return _path;
117 }
118
119 - (NSString *)expandTildeInPath:(NSString *)_path {
120   return _path;
121 }
122
123 /* directory operations */
124
125 - (BOOL)changeCurrentDirectoryPath:(NSString *)_path {
126   BOOL isDir;
127
128   if (![self fileExistsAtPath:_path isDirectory:&isDir])
129     return NO;
130   if (!isDir)
131     return NO;
132   ASSIGNCOPY(self->cwd, _path);
133   return YES;
134 }
135 - (NSString *)currentDirectoryPath {
136   return self->cwd;
137 }
138
139 - (BOOL)createDirectoryAtPath:(NSString *)_path
140   attributes:(NSDictionary *)_ats
141 {
142   return NO;
143 }
144
145 /* file operations */
146
147 - (BOOL)copyPath:(NSString *)_s toPath:(NSString *)_d handler:(id)_handler {
148   return NO;
149 }
150 - (BOOL)movePath:(NSString *)_s toPath:(NSString *)_d handler:(id)_handler {
151   return NO;
152 }
153 - (BOOL)linkPath:(NSString *)_s toPath:(NSString *)_d handler:(id)_handler {
154   return NO;
155 }
156
157 - (BOOL)removeFileAtPath:(NSString *)_path handler:(id)_handler {
158   return NO;
159 }
160
161 - (BOOL)createFileAtPath:(NSString *)_path contents:(NSData *)_contents
162   attributes:(NSDictionary *)_attributes
163 {
164   return NO;
165 }
166
167 /* getting and comparing file contents */
168
169 - (NSData *)contentsAtPath:(NSString *)_path {
170   return nil;
171 }
172 - (BOOL)contentsEqualAtPath:(NSString *)_path1 andPath:(NSString *)_path2 {
173   NSData *data1, *data2;
174   
175   data1 = [self contentsAtPath:_path1];
176   data2 = [self contentsAtPath:_path2];
177   
178   if (data1 == data2) return YES;
179   if (data1 == nil || data2 == nil) return NO;
180   
181   return [data1 isEqual:data2];
182 }
183
184 /* determining access to files */
185
186 - (BOOL)fileExistsAtPath:(NSString *)_path {
187   BOOL dummy = NO;
188   return [self fileExistsAtPath:_path isDirectory:&dummy];
189 }
190 - (BOOL)fileExistsAtPath:(NSString *)_path isDirectory:(BOOL*)_isDirectory {
191   return NO;
192 }
193 - (BOOL)isReadableFileAtPath:(NSString *)_path {
194   return NO;
195 }
196 - (BOOL)isWritableFileAtPath:(NSString *)_path {
197   return NO;
198 }
199 - (BOOL)isExecutableFileAtPath:(NSString *)_path {
200   return NO;
201 }
202 - (BOOL)isDeletableFileAtPath:(NSString *)_path {
203   return NO;
204 }
205
206 /* Getting and setting attributes */
207
208 - (NSDictionary *)fileAttributesAtPath:(NSString *)_p traverseLink:(BOOL)_flag{
209   return nil;
210 }
211 - (NSDictionary *)fileSystemAttributesAtPath:(NSString *)_p {
212   return nil;
213 }
214 - (BOOL)changeFileAttributes:(NSDictionary *)_attributes atPath:(NSString *)_p{
215   return NO;
216 }
217
218 /* discovering directory contents */
219
220 - (NSArray *)directoryContentsAtPath:(NSString *)_path {
221   return nil;
222 }
223 - (NSDirectoryEnumerator *)enumeratorAtPath:(NSString *)_path {
224   return nil;
225 }
226 - (NSArray *)subpathsAtPath:(NSString *)_path {
227   return nil;
228 }
229
230 /* symbolic-link operations */
231
232 - (BOOL)createSymbolicLinkAtPath:(NSString *)_p pathContent:(NSString *)_dpath{
233   return NO;
234 }
235 - (NSString *)pathContentOfSymbolicLinkAtPath:(NSString *)_path {
236   return nil;
237 }
238
239 /* feature check */
240
241 - (BOOL)supportsVersioningAtPath:(NSString *)_path {
242   return [self supportsFeature:NGFileManagerFeature_Versioning atPath:_path];
243 }
244 - (BOOL)supportsLockingAtPath:(NSString *)_path {
245   return [self supportsFeature:NGFileManagerFeature_Locking atPath:_path];
246 }
247 - (BOOL)supportsFolderDataSourceAtPath:(NSString *)_path {
248   return [self supportsFeature:NGFileManagerFeature_DataSources atPath:_path];
249 }
250 - (BOOL)supportsFeature:(NSString *)_featureURI atPath:(NSString *)_path {
251   return NO;
252 }
253
254 /* writing */
255
256 - (BOOL)writeContents:(NSData *)_content atPath:(NSString *)_path {
257   return NO;
258 }
259
260 /* global-IDs */
261
262 - (EOGlobalID *)globalIDForPath:(NSString *)_path; {
263   return nil;
264 }
265 - (NSString *)pathForGlobalID:(EOGlobalID *)_gid {
266   return nil;
267 }
268
269 /* trash */
270
271 - (BOOL)supportsTrashFolderAtPath:(NSString *)_path {
272   return NO;
273 }
274 - (NSString *)trashFolderForPath:(NSString *)_path {
275   return nil;
276 }
277
278 - (BOOL)trashFileAtPath:(NSString *)_path handler:(id)_handler {
279   NSString *trash, *destPath;
280   BOOL     isDir;
281   unsigned i;
282   NSString *tmp;
283   
284   if (![self supportsTrashFolderAtPath:_path])
285     return NO;
286   if ([(trash = [self trashFolderForPath:_path]) length] == 0)
287     return NO;
288   
289   if ([_path hasPrefix:trash])
290     /* path already is in trash ... */
291     return YES;
292   
293   /* ensure that the trash folder is existent */
294
295   if ([self fileExistsAtPath:trash isDirectory:&isDir]) {
296     if (!isDir) {
297       NSLog(@"%s: '%@' exists, but isn't a folder !", __PRETTY_FUNCTION__,
298             trash);
299       return NO;
300     }
301   }
302   else { /* trash doesn't exist yet */
303     if (![self createDirectoryAtPath:trash attributes:nil]) {
304       NSLog(@"%s: couldn't create trash folder '%@' !", __PRETTY_FUNCTION__,
305             trash);
306       return NO;
307     }
308   }
309
310   /* construct trash path for target ... */
311
312   destPath = [trash stringByAppendingPathComponent:
313                       [_path lastPathComponent]];
314   tmp = destPath;
315   i = 0;
316   while ([self fileExistsAtPath:tmp]) {
317     i++;
318     tmp = [destPath stringByAppendingFormat:@"%d", i];
319     if (i > 40) {
320       NSLog(@"%s: too many files named similiar to '%@' in trash folder '%@'",
321             __PRETTY_FUNCTION__, destPath, trash);
322       return NO;
323     }
324   }
325   destPath = tmp;
326   
327   /* move to trash */
328   if (![self movePath:_path toPath:destPath handler:_handler])
329     return NO;
330   
331   return YES;
332 }
333
334 /* URLs */
335
336 - (NSURL *)urlForPath:(NSString *)_path {
337   return [[[NGFileManagerURL alloc]
338                              initWithPath:_path fileManager:self]
339                              autorelease];
340 }
341
342 @end /* NGFileManager */