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,
28 JS FileUtils IO API (The purpose of this file is to make it a little easier to do file IO from js)
34 chromeToPath(aPath) // Converts a chrome://bob/content uri to a path.
35 // NOTE: although this gives you the
36 // path to a file in the chrome directory, you will
37 // most likely not have permisions
38 // to create or write to files there.
39 chromeToURL(aPath) // Converts a chrome://bob/content file:// uri.
40 urlToPath(aPath) // Converts a file:// url to a path
41 exists(aPath); // check to see if a file exists
42 append(aDirPath, aFileName); // append is for abstracting platform specific file paths
43 remove(aPath); // remove a file
44 copy(aSource, aDest); // copy a file from source to destination
45 leaf(aPath); // leaf is the endmost file string
46 // eg: foo.html in /myDir/foo.html
47 permissions(aPath); // returns the files permissions
48 dateModified(aPath); // returns the last modified date in locale string
49 size(aPath); // returns the file size
50 ext(aPath); // returns a file extension if there is one
51 parent(aPath) // returns the dir part of a path
52 dirPath(aPath) // *Depriciated* use parent
53 spawn(aPath, aArgs) // spawns another program
54 nsIFile(aPath) // returns an nsIFile obj
55 help; // currently returns a list of available functions
59 chrome_to_path(aPath); // synonym for chromeToPath
60 URL_to_path(aPath) // synonym for use urlToPath
61 rm(aPath); // synonym for remove
62 extension(aPath); // synonym for ext
66 First include this js file
68 var file = new FileUtils();
72 var path='/usr/X11R6/bin/Eterm';
73 file.spawn(path, ['-e/usr/bin/vi']);
74 *note* all args passed to spawn must be in the form of an array
79 Warning: these API's are not for religious types
81 *******************************************/
83 // Make sure jslib is loaded
84 if (typeof(JS_LIB_LOADED
)=='boolean')
87 /****************** Globals **********************/
89 const JS_FILEUTILS_FILE
= "fileUtils.js";
90 const JS_FILEUTILS_LOADED
= true;
92 const JS_FILEUTILS_LOCAL_CID
= "@mozilla.org/file/local;1";
93 const JS_FILEUTILS_FILESPEC_PROGID
= '@mozilla.org/filespec;1';
94 const JS_FILEUTILS_NETWORK_STD_CID
= '@mozilla.org/network/standard-url;1';
95 const JS_FILEUTILS_SIMPLEURI_PROGID
= "@mozilla.org/network/simple-uri;1";
96 const JS_FILEUTILS_CHROME_REG_PROGID
= '@mozilla.org/chrome/chrome-registry;1';
97 const JS_FILEUTILS_DR_PROGID
= "@mozilla.org/file/directory_service;1";
98 const JS_FILEUTILS_PROCESS_CID
= "@mozilla.org/process/util;1";
100 const JS_FILEUTILS_I_LOCAL_FILE
= "nsILocalFile";
101 const JS_FILEUTILS_INIT_W_PATH
= "initWithPath";
102 const JS_FILEUTILS_I_PROPS
= "nsIProperties";
104 const JS_FILEUTILS_CHROME_DIR
= "AChrom";
106 const JS_FILEUTILS_OK
= true;
107 const JS_FILEUTILS_FilePath
= new C
.Constructor(JS_FILEUTILS_LOCAL_CID
,
108 JS_FILEUTILS_I_LOCAL_FILE
,
109 JS_FILEUTILS_INIT_W_PATH
);
112 /****************** FileUtils Object Class *********************/
113 function FileUtils ()
115 include (jslib_dirutils
);
116 this.mDirUtils
= new DirUtils();
119 FileUtils
.prototype =
124 /********************* CHROME_TO_PATH ***************************/
125 // this is here for backward compatability but is deprecated --pete
126 chrome_to_path : function (aPath
) { return this.chromeToPath(aPath
); },
128 chromeToPath : function (aPath
)
131 return jslibErrorMsg("NS_ERROR_INVALID_ARG");
133 return this.urlToPath(this.chromeToURL(aPath
));
136 chromeToURL : function (aPath
)
139 return jslibErrorMsg("NS_ERROR_INVALID_ARG");
141 var uri
= jslibCreateInstance(JS_FILEUTILS_SIMPLEURI_PROGID
, "nsIURI");
144 if (/^chrome:/.test(aPath
)) {
146 var cr
= jslibGetService(JS_FILEUTILS_CHROME_REG_PROGID
);
148 cr
= jslibQI(cr
, "nsIChromeRegistry");
150 uri
.spec
= cr
.convertChromeURL(uri
);
153 // deal w/ jar resource files
154 if (/.jar!/.test(rv
)) {
155 rv
= rv
.replace(/resource:/, "");
156 rv
= "file://"+this.mDirUtils
.getCurProcDir()+rv
;
161 if (/^\/|\\|:chrome/.test(rv
)) {
163 // prepend the system path to this process dir
164 rv
= "file:///"+(''+this.mDirUtils
.getCurProcDir()+rv
)
165 .replace(/\\/g, "\/").replace(/^\s
*\/?/, "").replace(/\ /g
, "%20");
170 } else if (/^file:/.test(aPath
)) {
171 rv
= this.urlToPath(aPath
);
177 /********************* URL_TO_PATH ***************************/
178 URL_to_path : function (aPath
) { return this.urlToPath(aPath
); },
180 urlToPath : function (aPath
)
183 return jslibErrorMsg("NS_ERROR_INVALID_ARG");
185 // xpcshell doesn't have unescape func
186 const hasUnescape
= (typeof(unescape
)=="function");
190 if (/^file:/.test(path
)) {
192 var uri
= jslibCreateInstance(JS_FILEUTILS_NETWORK_STD_CID
, "nsIURI");
196 var file
= jslibCreateInstance(JS_FILEUTILS_LOCAL_CID
, "nsILocalFile");
200 file
.initWithPath(rv
);
201 rv
= hasUnescape
? unescape(file
.path
) : file
.path
;
207 file
.initWithPath(rv
.replace(/^\//,"").replace(/\//g,"\\"));
208 rv
= hasUnescape
? unescape(file
.path
) : file
.path
;
212 // FIXME: add checking for Mac
222 /********************* EXISTS ***************************/
223 exists : function (aPath
)
226 return jslibErrorMsg("NS_ERROR_INVALID_ARG");
230 var file
= new JS_FILEUTILS_FilePath(aPath
);
239 /********************* RM *******************************/
240 rm : function (aPath
) { return this.remove(aPath
); },
242 remove : function (aPath
)
245 return jslibErrorMsg("NS_ERROR_INVALID_ARG");
247 if (!this.exists(aPath
))
248 return jslibErrorMsg("NS_ERROR_FILE_TARGET_DOES_NOT_EXIST");
253 var fileInst
= new JS_FILEUTILS_FilePath(aPath
);
254 if (fileInst
.isDirectory())
255 return jslibErrorMsg("NS_ERROR_INVALID_ARG");
257 fileInst
.remove(false);
266 /********************* COPY *****************************/
267 copy : function (aSource
, aDest
)
269 if (!aSource
|| !aDest
)
270 return jslibErrorMsg("NS_ERROR_INVALID_ARG");
272 if (!this.exists(aSource
))
273 return jslibErrorMsg("NS_ERROR_UNEXPECTED");
278 var fileInst
= new JS_FILEUTILS_FilePath(aSource
);
279 var dir
= new JS_FILEUTILS_FilePath(aDest
);
280 var copyName
= fileInst
.leafName
;
282 if (fileInst
.isDirectory())
283 return jslibErrorMsg("NS_ERROR_FILE_COPY_OR_MOVE_FAILED");
285 if (!this.exists(aDest
) || !dir
.isDirectory()) {
286 copyName
= dir
.leafName
;
287 dir
= new JS_FILEUTILS_FilePath(dir
.path
.replace(copyName
,''));
289 if (!this.exists(dir
.path
))
290 return jslibErrorMsg("NS_ERROR_FILE_ALREADY_EXISTS");
292 if (!dir
.isDirectory())
293 return jslibErrorMsg("NS_ERROR_FILE_INVALID_PATH");
296 if (this.exists(this.append(dir
.path
, copyName
)))
297 return jslibError("NS_ERROR_FILE_ALREADY_EXISTS");
299 rv
= fileInst
.copyTo(dir
, copyName
);
302 return jslibError(e
);
308 /********************* LEAF *****************************/
309 leaf : function (aPath
)
312 return jslibErrorMsg("NS_ERROR_INVALID_ARG");
314 if (!this.exists(aPath
))
315 jslibErrorWarn("NS_ERROR_FILE_NOT_FOUND ["+aPath
+"]")
320 var fileInst
= new JS_FILEUTILS_FilePath(aPath
);
321 rv
=fileInst
.leafName
;
325 return jslibError(e
);
331 /********************* APPEND ***************************/
332 append : function (aDirPath
, aFileName
)
334 if (!aDirPath
|| !aFileName
)
335 jslibErrorMsg("NS_ERROR_INVALID_ARG");
337 if (!this.exists(aDirPath
))
338 return jslibErrorMsg("NS_ERROR_FILE_NOT_FOUND");
343 var fileInst
= new JS_FILEUTILS_FilePath(aDirPath
);
344 if (fileInst
.exists() && !fileInst
.isDirectory())
345 return jslibErrorMsg("NS_ERROR_INVALID_ARG");
347 fileInst
.append(aFileName
);
351 return jslibError(e
);
357 /********************* VALIDATE PERMISSIONS *************/
358 validatePermissions : function(aNum
)
360 if ( parseInt(aNum
.toString(10).length
) < 3 )
363 return JS_FILEUTILS_OK
;
366 /********************* PERMISSIONS **********************/
367 permissions : function (aPath
)
370 return jslibErrorMsg("NS_ERROR_INVALID_ARG");
372 if (!this.exists(aPath
))
373 return jslibErrorMsg("NS_ERROR_FILE_NOT_FOUND");
378 rv
=(new JS_FILEUTILS_FilePath(aPath
)).permissions
.toString(8);
386 /********************* MODIFIED *************************/
387 dateModified : function (aPath
)
390 return jslibErrorMsg("NS_ERROR_INVALID_ARG");
392 if (!this.exists(aPath
))
393 jslibErrorMsg("NS_ERROR_FILE_NOT_FOUND");
398 var date
= new Date((new JS_FILEUTILS_FilePath(aPath
)).lastModifiedTime
).toLocaleString();
407 /********************* SIZE *****************************/
408 size : function (aPath
)
411 return jslibErrorMsg("NS_ERROR_INVALID_ARG");
413 if (!this.exists(aPath
))
414 return jslibErrorMsg("NS_ERROR_FILE_NOT_FOUND");
419 rv
= (new JS_FILEUTILS_FilePath(aPath
)).fileSize
;
428 /********************* EXTENSION ************************/
429 extension : function (aPath
) { return this.ext(aPath
); },
431 ext : function (aPath
)
434 return jslibErrorMsg("NS_ERROR_INVALID_ARG");
436 if (!this.exists(aPath
))
437 return jslibErrorMsg("NS_ERROR_FILE_NOT_FOUND");
442 var leafName
= (new JS_FILEUTILS_FilePath(aPath
)).leafName
;
443 var dotIndex
= leafName
.lastIndexOf('.');
444 rv
=(dotIndex
>= 0) ? leafName
.substring(dotIndex
+1) : "";
446 return jslibError(e
);
452 /********************* DIRPATH **************************/
453 dirPath : function (aPath
) { return this.parent(aPath
); },
455 parent : function (aPath
)
458 return jslibErrorMsg("NS_ERROR_INVALID_ARG");
463 var fileInst
= new JS_FILEUTILS_FilePath(aPath
);
465 if (!fileInst
.exists())
466 return jslibErrorMsg("NS_ERROR_FILE_NOT_FOUND");
468 if (fileInst
.isFile())
469 rv
= fileInst
.parent
.path
;
471 else if (fileInst
.isDirectory())
479 return jslibError(e
);
485 /********************* SPAWN ****************************/
486 spawn : function (aPath
, aArgs
) { this.run(aPath
, aArgs
); },
487 run : function (aPath
, aArgs
)
489 * Trys to execute the requested file as a separate *non-blocking* process.
491 * Passes the supplied *array* of arguments on the command line if
492 * the OS supports it.
497 return jslibErrorMsg("NS_ERROR_INVALID_ARG");
499 if (!this.exists(aPath
))
500 return jslibErrorMsg("NS_ERROR_FILE_NOT_FOUND");
510 var fileInst
= new JS_FILEUTILS_FilePath(aPath
);
512 if (!fileInst
.isExecutable())
513 return jslibErrorMsg("NS_ERROR_INVALID_ARG");
515 if (fileInst
.isDirectory())
516 return jslibErrorMsg("NS_ERROR_FILE_IS_DIRECTORY");
517 // Create and execute the process...
519 * NOTE: The first argument of the process instance's 'run' method
520 * below specifies the blocking state (false = non-blocking).
521 * The last argument, in theory, contains the process ID (PID)
522 * on return if a variable is supplied--not sure how to implement
523 * this with JavaScript though.
526 var theProcess
= jslibCreateInstance(JS_FILEUTILS_PROCESS_CID
, "nsIProcess");
528 theProcess
.init(fileInst
);
530 rv
= theProcess
.run(false, aArgs
, len
);
531 jslib_debug("rv="+rv
);
542 /********************* nsIFILE **************************/
543 nsIFile : function (aPath
)
546 return jslibErrorMsg("NS_ERROR_INVALID_ARG");
550 rv
= new JS_FILEUTILS_FilePath(aPath
);
558 /********************* HELP *****************************/
563 "\n\nFunction List:\n" +
565 " exists(aPath);\n" +
566 " chromeToPath(aPath);\n" +
567 " chromeToURL(aPath);\n" +
568 " urlToPath(aPath);\n" +
569 " append(aDirPath, aFileName);\n" +
570 " remove(aPath);\n" +
571 " copy(aSource, aDest);\n" +
573 " permissions(aPath);\n" +
574 " dateModified(aPath);\n" +
577 " parent(aPath);\n" +
578 " run(aPath, aArgs);\n" +
579 " nsIFile(aPath);\n" +
587 jslibDebug('*** load: '+JS_FILEUTILS_FILE
+' OK');
589 } // END BLOCK JS_LIB_LOADED CHECK
591 // If jslib base library is not loaded, dump this error.
594 dump("JS_FILE library not loaded:\n" +
595 " \tTo load use: chrome://jslib/content/jslib.js\n" +
596 " \tThen: include(jslib_fileutils);\n\n");