1 /*** -*- Mode: Javascript; tab-width: 2;
3 The contents of this file are subject to the Mozilla Public
4 License Version 1.1 (the "License"); you may not use this file
5 except in compliance with the License. You may obtain a copy of
6 the License at http://www.mozilla.org/MPL/
8 Software distributed under the License is distributed on an "AS
9 IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10 implied. See the License for the specific language governing
11 rights and limitations under the License.
13 The Original Code is Collabnet code.
14 The Initial Developer of the Original Code is Collabnet.
16 Portions created by Collabnet are Copyright (C) 2000 Collabnet.
19 Contributor(s): Pete Collins, Doug Turner, Brendan Eich, Warren Harris,
20 Eric Plaster, Martin Kutschker
23 JS Directory Class API
29 create(aPermissions); // permissions are optional
31 files(); // returns an array listing all files of a dirs contents
32 dirs(); // returns an array listing all dirs of a dirs contents
33 list(aDirPath); // returns an array listing of a dirs contents
36 help(); // currently dumps a list of available functions
43 if (typeof(JS_LIB_LOADED)=='boolean') {
45 /************* INCLUDE FILESYSTEM *****************/
46 if(typeof(JS_FILESYSTEM_LOADED)!='boolean')
47 include(jslib_filesystem);
48 /************* INCLUDE FILESYSTEM *****************/
51 /****************** Globals **********************/
52 const JS_DIR_FILE = "dir.js";
53 const JS_DIR_LOADED = true;
55 const JS_DIR_LOCAL_CID = "@mozilla.org/file/local;1";
56 const JS_DIR_LOCATOR_PROGID = '@mozilla.org/filelocator;1';
57 const JS_DIR_CID = "@mozilla.org/file/directory_service;1";
59 const JS_DIR_I_LOCAL_FILE = "nsILocalFile";
60 const JS_DIR_INIT_W_PATH = "initWithPath";
62 const JS_DIR_PREFS_DIR = 65539;
64 const JS_DIR_DIRECTORY = 0x01; // 1
65 const JS_DIR_OK = true;
67 const JS_DIR_DEFAULT_PERMS = 0766;
69 const JS_DIR_FilePath = new C.Constructor(JS_DIR_LOCAL_CID,
72 /****************** Globals **********************/
74 /****************** Dir Object Class *********************/
80 "Please enter a local file path to initialize",
81 "NS_ERROR_XPC_NOT_ENOUGH_ARGS", JS_DIR_FILE);
82 throw C.results.NS_ERROR_XPC_NOT_ENOUGH_ARGS;
85 return this.initPath(arguments);
88 Dir.prototype = new FileSystem;
89 Dir.prototype.fileInst = null;
91 /********************* CREATE ****************************/
92 Dir.prototype.create = function(aPermissions)
95 jslibError(null, "create (no file path defined)", "NS_ERROR_NOT_INITIALIZED");
96 return C.results.NS_ERROR_NOT_INITIALIZED;
100 jslibError(null, "(Dir already exists", "NS_ERROR_FAILURE", JS_DIR_FILE+":create");
104 if (typeof(aPermissions) == "number") {
105 var checkPerms = this.validatePermissions(aPermissions);
108 jslibError(null, "create (invalid permissions)",
109 "NS_ERROR_INVALID_ARG", JS_DIR_FILE+":create");
110 return C.results.NS_ERROR_INVALID_ARG;
113 checkPerms = this.mFileInst.parent.permissions;
119 rv=this.mFileInst.create(JS_DIR_DIRECTORY, checkPerms);
121 jslibError(e, "(unable to create)", "NS_ERROR_FAILURE", JS_DIR_FILE+":create");
128 /********************* READDIR **************************/
129 Dir.prototype.readDir = function ()
133 jslibError(null, "(Dir already exists", "NS_ERROR_FAILURE", JS_DIR_FILE+":readDir");
141 jslibError(null, "(file is not a directory)", "NS_ERROR_FAILURE", JS_DIR_FILE+":readDir");
145 var files = this.mFileInst.directoryEntries;
146 var listings = new Array();
149 if(typeof(JS_FILE_LOADED)!='boolean')
150 include(JS_LIB_PATH+'io/file.js');
152 while(files.hasMoreElements()) {
153 file = files.getNext().QueryInterface(C.interfaces.nsILocalFile);
155 listings.push(new File(file.path));
157 if(file.isDirectory())
158 listings.push(new Dir(file.path));
163 jslibError(e, "(unexpected error)", "NS_ERROR_UNEXPECTED", JS_FILE_FILE+":readDir");
170 /********************* REMOVE *******************************/
171 Dir.prototype.remove = function (aRecursive)
174 if(typeof(aRecursive)!='boolean')
177 if(!this.checkInst())
178 throw C.results.NS_ERROR_NOT_INITIALIZED;
182 jslibError(null, "remove (no path defined)",
183 "NS_ERROR_INVALID_ARG", JS_DIR_FILE+":remove");
191 jslibError(null, "(directory doesn't exist)", "NS_ERROR_FAILURE", JS_DIR_FILE+":remove");
196 jslibError(null, "(file is not a directory)", "NS_ERROR_FAILURE", JS_DIR_FILE+":remove");
200 rv=this.mFileInst.remove(aRecursive);
202 jslibError(e, "(dir not empty, use 'remove(true)' for recursion)", "NS_ERROR_UNEXPECTED",
203 JS_DIR_FILE+":remove");
210 /********************* HELP *****************************/
211 Dir.prototype.super_help = FileSystem.prototype.help;
213 Dir.prototype.__defineGetter__('help',
215 var help = this.super_help() +
217 " create(aPermissions);\n" +
218 " remove(aRecursive);\n" +
219 " readDir(aDirPath);\n";
224 jslibDebug('*** load: '+JS_DIR_FILE+' OK');
227 dump("JSLIB library not loaded:\n" +
228 " \tTo load use: chrome://jslib/content/jslib.js\n" +
229 " \tThen: include(jslib_dir);\n\n");