2 Copyright (C) 2000-2003 SKYRIX Software AG
4 This file is part of OGo
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
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.
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
24 #include "NGFileManager.h"
25 #include "NSFileManager+Extensions.h"
26 #include "EOCacheDataSource.h"
33 cwd - readonly - current working directory, string
38 Object ls([path|paths])
39 bool mkdir(path[,path..])
40 bool rmdir(path[,path..])
41 bool rm(path[,path..])
42 bool trash(path[,path..])
43 bool cp(frompath[,frompath..], topath)
44 bool mv(frompath[,frompath..], topath)
45 bool ln(frompath, topath)
47 bool exists(path[,path..])
48 bool isdir(path[,path..])
49 bool islink(path[,path..])
51 Object getDataSource([String path, [bool cache]])
54 @implementation NGFileManager(JSSupport)
56 static NSNumber *boolYes = nil;
57 static NSNumber *boolNo = nil;
59 static void _ensureBools(void) {
60 if (boolYes == nil) boolYes = [[NSNumber numberWithBool:YES] retain];
61 if (boolNo == nil) boolNo = [[NSNumber numberWithBool:NO] retain];
67 return [self currentDirectoryPath];
72 - (id)_jsfunc_cd:(NSArray *)_args {
74 return [self changeCurrentDirectoryPath:[_args objectAtIndex:0]]
79 - (id)_jsfunc_ls:(NSArray *)_args {
82 if ((count = [_args count]) == 0) {
83 return [self directoryContentsAtPath:@"."];
85 else if (count == 1) {
86 return [self directoryContentsAtPath:
87 [[_args objectAtIndex:0] stringValue]];
90 NSMutableDictionary *md;
93 md = [NSMutableDictionary dictionaryWithCapacity:count];
94 for (i = 0; i < count; i++) {
98 path = [_args objectAtIndex:i];
99 contents = [self directoryContentsAtPath:path];
102 [md setObject:contents forKey:path];
109 - (id)_jsfunc_mkdir:(NSArray *)_args {
113 if ((count = [_args count]) == 0) {
119 for (i = 0; i < count; i++) {
122 path = [_args objectAtIndex:i];
124 if (![self createDirectoryAtPath:path attributes:nil])
132 - (id)_jsfunc_rmdir:(NSArray *)_args {
136 if ((count = [_args count]) == 0) {
142 for (i = 0; i < count; i++) {
146 path = [_args objectAtIndex:i];
148 if (![self fileExistsAtPath:path isDirectory:&isDir])
152 /* not a directory ! */
155 if ([[self directoryContentsAtPath:path] count] > 0)
156 /* directory has contents */
159 if (![self removeFileAtPath:path handler:nil])
167 - (id)_jsfunc_rm:(NSArray *)_args {
171 if ((count = [_args count]) == 0) {
177 for (i = 0; i < count; i++) {
181 path = [_args objectAtIndex:i];
183 if (![self fileExistsAtPath:path isDirectory:&isDir])
187 if ([[self directoryContentsAtPath:path] count] > 0)
188 /* directory has contents */
192 if (![self removeFileAtPath:path handler:nil])
200 - (id)_jsfunc_trash:(NSArray *)_args {
204 if ((count = [_args count]) == 0) {
210 for (i = 0; i < count; i++) {
214 path = [_args objectAtIndex:i];
215 if (![self supportsTrashFolderAtPath:path])
218 if (![self fileExistsAtPath:path isDirectory:&isDir])
221 if (![self trashFileAtPath:path handler:nil])
229 - (id)_jsfunc_mv:(NSArray *)_args {
233 if ((count = [_args count]) == 0)
236 /* missing target path */
243 destpath = [_args objectAtIndex:(count - 1)];
245 if (![self fileExistsAtPath:destpath isDirectory:&isDir])
248 for (i = 0; i < (count - 1); i++) {
249 NSString *path, *dpath = nil;
251 path = [_args objectAtIndex:i];
254 ? [dpath stringByAppendingPathComponent:[path lastPathComponent]]
257 if (![self movePath:path toPath:dpath handler:nil])
266 - (id)_jsfunc_cp:(NSArray *)_args {
270 if ((count = [_args count]) == 0)
273 /* missing target path */
280 destpath = [_args objectAtIndex:(count - 1)];
282 if (![self fileExistsAtPath:destpath isDirectory:&isDir])
285 for (i = 0; i < (count - 1); i++) {
286 NSString *path, *dpath = nil;
288 path = [_args objectAtIndex:i];
291 ? [dpath stringByAppendingPathComponent:[path lastPathComponent]]
294 if (![self copyPath:path toPath:dpath handler:nil])
303 - (id)_jsfunc_ln:(NSArray *)_args {
307 if ((count = [_args count]) == 0)
310 /* missing target path */
316 srcpath = [_args objectAtIndex:0];
317 destpath = [_args objectAtIndex:1];
319 if (![self createSymbolicLinkAtPath:destpath pathContent:srcpath])
326 - (id)_jsfunc_exists:(NSArray *)_args {
330 if ((count = [_args count]) == 0)
335 for (i = 0; i < count; i++) {
338 path = [_args objectAtIndex:i];
340 if (![self fileExistsAtPath:path])
347 - (id)_jsfunc_isdir:(NSArray *)_args {
351 if ((count = [_args count]) == 0) {
357 for (i = 0; i < count; i++) {
361 path = [_args objectAtIndex:i];
364 NSLog(@"CHECK PATH: %@", path);
367 if (![self fileExistsAtPath:path isDirectory:&isDir]) {
369 NSLog(@" does not exist ..");
376 NSLog(@" not a directory ..");
383 NSLog(@"%s: returning yes, %@ are directories",
384 __PRETTY_FUNCTION__, _args);
390 - (id)_jsfunc_islink:(NSArray *)_args {
394 if ((count = [_args count]) == 0)
399 for (i = 0; i < count; i++) {
404 path = [_args objectAtIndex:i];
406 if (![self fileExistsAtPath:path isDirectory:&isDir])
412 if ((attrs = [self fileAttributesAtPath:path traverseLink:NO])==nil)
415 if (![[attrs objectForKey:NSFileType]
416 isEqualToString:NSFileTypeSymbolicLink])
425 - (id)_jsfunc_getDataSource:(NSArray *)_args {
427 NSString *path = nil;
434 if ((count = [_args count]) == 0) {
437 else if (count == 1) {
438 path = [[_args objectAtIndex:0] stringValue];
440 else if (count == 2) {
441 path = [[_args objectAtIndex:0] stringValue];
442 lcache = [[_args objectAtIndex:1] boolValue];
445 if (![self supportsFolderDataSourceAtPath:path])
448 if ((ds = [(id<NGFileManagerDataSources>)self dataSourceAtPath:path])==nil)
452 ds = [[[EOCacheDataSource alloc] initWithDataSource:ds] autorelease];
457 @end /* NGFileManager(JSSupport) */