2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE 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 SOPE 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 SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include "NGFileManager.h"
24 #include "NSFileManager+Extensions.h"
25 #include "EOCacheDataSource.h"
32 cwd - readonly - current working directory, string
37 Object ls([path|paths])
38 bool mkdir(path[,path..])
39 bool rmdir(path[,path..])
40 bool rm(path[,path..])
41 bool trash(path[,path..])
42 bool cp(frompath[,frompath..], topath)
43 bool mv(frompath[,frompath..], topath)
44 bool ln(frompath, topath)
46 bool exists(path[,path..])
47 bool isdir(path[,path..])
48 bool islink(path[,path..])
50 Object getDataSource([String path, [bool cache]])
53 @implementation NGFileManager(JSSupport)
55 static NSNumber *boolYes = nil;
56 static NSNumber *boolNo = nil;
58 static void _ensureBools(void) {
59 if (boolYes == nil) boolYes = [[NSNumber numberWithBool:YES] retain];
60 if (boolNo == nil) boolNo = [[NSNumber numberWithBool:NO] retain];
66 return [self currentDirectoryPath];
71 - (id)_jsfunc_cd:(NSArray *)_args {
73 return [self changeCurrentDirectoryPath:[_args objectAtIndex:0]]
78 - (id)_jsfunc_ls:(NSArray *)_args {
81 if ((count = [_args count]) == 0) {
82 return [self directoryContentsAtPath:@"."];
84 else if (count == 1) {
85 return [self directoryContentsAtPath:
86 [[_args objectAtIndex:0] stringValue]];
89 NSMutableDictionary *md;
92 md = [NSMutableDictionary dictionaryWithCapacity:count];
93 for (i = 0; i < count; i++) {
97 path = [_args objectAtIndex:i];
98 contents = [self directoryContentsAtPath:path];
101 [md setObject:contents forKey:path];
108 - (id)_jsfunc_mkdir:(NSArray *)_args {
112 if ((count = [_args count]) == 0) {
118 for (i = 0; i < count; i++) {
121 path = [_args objectAtIndex:i];
123 if (![self createDirectoryAtPath:path attributes:nil])
131 - (id)_jsfunc_rmdir:(NSArray *)_args {
135 if ((count = [_args count]) == 0) {
141 for (i = 0; i < count; i++) {
145 path = [_args objectAtIndex:i];
147 if (![self fileExistsAtPath:path isDirectory:&isDir])
151 /* not a directory ! */
154 if ([[self directoryContentsAtPath:path] count] > 0)
155 /* directory has contents */
158 if (![self removeFileAtPath:path handler:nil])
166 - (id)_jsfunc_rm:(NSArray *)_args {
170 if ((count = [_args count]) == 0) {
176 for (i = 0; i < count; i++) {
180 path = [_args objectAtIndex:i];
182 if (![self fileExistsAtPath:path isDirectory:&isDir])
186 if ([[self directoryContentsAtPath:path] count] > 0)
187 /* directory has contents */
191 if (![self removeFileAtPath:path handler:nil])
199 - (id)_jsfunc_trash:(NSArray *)_args {
203 if ((count = [_args count]) == 0) {
209 for (i = 0; i < count; i++) {
213 path = [_args objectAtIndex:i];
214 if (![self supportsTrashFolderAtPath:path])
217 if (![self fileExistsAtPath:path isDirectory:&isDir])
220 if (![self trashFileAtPath:path handler:nil])
228 - (id)_jsfunc_mv:(NSArray *)_args {
232 if ((count = [_args count]) == 0)
235 /* missing target path */
242 destpath = [_args objectAtIndex:(count - 1)];
244 if (![self fileExistsAtPath:destpath isDirectory:&isDir])
247 for (i = 0; i < (count - 1); i++) {
248 NSString *path, *dpath = nil;
250 path = [_args objectAtIndex:i];
253 ? [dpath stringByAppendingPathComponent:[path lastPathComponent]]
256 if (![self movePath:path toPath:dpath handler:nil])
265 - (id)_jsfunc_cp:(NSArray *)_args {
269 if ((count = [_args count]) == 0)
272 /* missing target path */
279 destpath = [_args objectAtIndex:(count - 1)];
281 if (![self fileExistsAtPath:destpath isDirectory:&isDir])
284 for (i = 0; i < (count - 1); i++) {
285 NSString *path, *dpath = nil;
287 path = [_args objectAtIndex:i];
290 ? [dpath stringByAppendingPathComponent:[path lastPathComponent]]
293 if (![self copyPath:path toPath:dpath handler:nil])
302 - (id)_jsfunc_ln:(NSArray *)_args {
306 if ((count = [_args count]) == 0)
309 /* missing target path */
315 srcpath = [_args objectAtIndex:0];
316 destpath = [_args objectAtIndex:1];
318 if (![self createSymbolicLinkAtPath:destpath pathContent:srcpath])
325 - (id)_jsfunc_exists:(NSArray *)_args {
329 if ((count = [_args count]) == 0)
334 for (i = 0; i < count; i++) {
337 path = [_args objectAtIndex:i];
339 if (![self fileExistsAtPath:path])
346 - (id)_jsfunc_isdir:(NSArray *)_args {
350 if ((count = [_args count]) == 0) {
356 for (i = 0; i < count; i++) {
360 path = [_args objectAtIndex:i];
363 NSLog(@"CHECK PATH: %@", path);
366 if (![self fileExistsAtPath:path isDirectory:&isDir]) {
368 NSLog(@" does not exist ..");
375 NSLog(@" not a directory ..");
382 NSLog(@"%s: returning yes, %@ are directories",
383 __PRETTY_FUNCTION__, _args);
389 - (id)_jsfunc_islink:(NSArray *)_args {
393 if ((count = [_args count]) == 0)
398 for (i = 0; i < count; i++) {
403 path = [_args objectAtIndex:i];
405 if (![self fileExistsAtPath:path isDirectory:&isDir])
411 if ((attrs = [self fileAttributesAtPath:path traverseLink:NO])==nil)
414 if (![[attrs objectForKey:NSFileType]
415 isEqualToString:NSFileTypeSymbolicLink])
424 - (id)_jsfunc_getDataSource:(NSArray *)_args {
426 NSString *path = nil;
433 if ((count = [_args count]) == 0) {
436 else if (count == 1) {
437 path = [[_args objectAtIndex:0] stringValue];
439 else if (count == 2) {
440 path = [[_args objectAtIndex:0] stringValue];
441 lcache = [[_args objectAtIndex:1] boolValue];
444 if (![self supportsFolderDataSourceAtPath:path])
447 if ((ds = [(id<NGFileManagerDataSources>)self dataSourceAtPath:path])==nil)
451 ds = [[[EOCacheDataSource alloc] initWithDataSource:ds] autorelease];
456 @end /* NGFileManager(JSSupport) */