* Filters/FilterTiff.cs: Compilation fix for 1.1.14
[beagle.git] / mozilla-extension / content / jslib / io / fileUtils.js
blob9283ff402080886a1f55dfba8a5d5426614438d2
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.
17 All Rights Reserved.
19 Contributor(s): Pete Collins,
20 Doug Turner,
21 Brendan Eich,
22 Warren Harris,
23 Eric Plaster,
24 Martin Kutschker
25 Philip Lindsay
28 JS FileUtils IO API (The purpose of this file is to make it a little easier to do file IO from js)
30 fileUtils.js
32 Function List
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
57 Deprecated
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
64 Instructions:
66 First include this js file
68 var file = new FileUtils();
70 Examples:
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
76 // to list help
77 dump(file.help);
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();
117 } // constructor
119 FileUtils.prototype =
121 mFileInst : null,
122 mDirUtils : null,
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)
130 if (!aPath)
131 return jslibErrorMsg("NS_ERROR_INVALID_ARG");
133 return this.urlToPath(this.chromeToURL(aPath));
136 chromeToURL : function (aPath)
138 if (!aPath)
139 return jslibErrorMsg("NS_ERROR_INVALID_ARG");
141 var uri = jslibCreateInstance(JS_FILEUTILS_SIMPLEURI_PROGID, "nsIURI");
143 var rv = null;
144 if (/^chrome:/.test(aPath)) {
145 try {
146 var cr = jslibGetService(JS_FILEUTILS_CHROME_REG_PROGID);
147 if (cr) {
148 cr = jslibQI(cr, "nsIChromeRegistry");
149 uri.spec = aPath;
150 uri.spec = cr.convertChromeURL(uri);
151 rv = uri.path;
153 // deal w/ jar resource files
154 if (/.jar!/.test(rv)) {
155 rv = rv.replace(/resource:/, "");
156 rv = "file://"+this.mDirUtils.getCurProcDir()+rv;
159 } catch (e) {}
161 if (/^\/|\\|:chrome/.test(rv)) {
162 try {
163 // prepend the system path to this process dir
164 rv = "file:///"+(''+this.mDirUtils.getCurProcDir()+rv)
165 .replace(/\\/g, "\/").replace(/^\s*\/?/, "").replace(/\ /g, "%20");
166 } catch (e) {
167 rv = jslibError(e);
170 } else if (/^file:/.test(aPath)) {
171 rv = this.urlToPath(aPath);
174 return rv;
177 /********************* URL_TO_PATH ***************************/
178 URL_to_path : function (aPath) { return this.urlToPath(aPath); },
180 urlToPath : function (aPath)
182 if (!aPath)
183 return jslibErrorMsg("NS_ERROR_INVALID_ARG");
185 // xpcshell doesn't have unescape func
186 const hasUnescape = (typeof(unescape)=="function");
187 var path = aPath;
188 var rv;
190 if (/^file:/.test(path)) {
191 try {
192 var uri = jslibCreateInstance(JS_FILEUTILS_NETWORK_STD_CID, "nsIURI");
193 uri.spec = path;
194 rv = uri.path;
196 var file = jslibCreateInstance(JS_FILEUTILS_LOCAL_CID, "nsILocalFile");
198 // unix and friends
199 try {
200 file.initWithPath(rv);
201 rv = hasUnescape ? unescape(file.path) : file.path;
202 return rv;
203 } catch (e) {}
205 // windows
206 try {
207 file.initWithPath(rv.replace(/^\//,"").replace(/\//g,"\\"));
208 rv = hasUnescape ? unescape(file.path) : file.path;
209 return rv;
210 } catch (e) {}
212 // FIXME: add checking for Mac
214 } catch (e) {
215 rv = jslibError(e);
219 return rv;
222 /********************* EXISTS ***************************/
223 exists : function (aPath)
225 if (!aPath)
226 return jslibErrorMsg("NS_ERROR_INVALID_ARG");
228 var rv;
229 try {
230 var file = new JS_FILEUTILS_FilePath(aPath);
231 rv = file.exists();
232 } catch (e) {
233 rv = jslibError(e);
236 return rv;
239 /********************* RM *******************************/
240 rm : function (aPath) { return this.remove(aPath); },
242 remove : function (aPath)
244 if (!aPath)
245 return jslibErrorMsg("NS_ERROR_INVALID_ARG");
247 if (!this.exists(aPath))
248 return jslibErrorMsg("NS_ERROR_FILE_TARGET_DOES_NOT_EXIST");
250 var rv;
252 try {
253 var fileInst = new JS_FILEUTILS_FilePath(aPath);
254 if (fileInst.isDirectory())
255 return jslibErrorMsg("NS_ERROR_INVALID_ARG");
257 fileInst.remove(false);
258 rv = jslibRes.NS_OK;
259 } catch (e) {
260 rv = jslibError(e);
263 return rv;
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");
275 var rv;
277 try {
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);
300 rv = jslibRes.NS_OK;
301 } catch (e) {
302 return jslibError(e);
305 return rv;
308 /********************* LEAF *****************************/
309 leaf : function (aPath)
311 if (!aPath)
312 return jslibErrorMsg("NS_ERROR_INVALID_ARG");
314 if (!this.exists(aPath))
315 jslibErrorWarn("NS_ERROR_FILE_NOT_FOUND ["+aPath+"]")
317 var rv;
319 try {
320 var fileInst = new JS_FILEUTILS_FilePath(aPath);
321 rv=fileInst.leafName;
324 catch (e) {
325 return jslibError(e);
328 return rv;
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");
340 var rv;
342 try {
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);
348 rv=fileInst.path;
349 delete fileInst;
350 } catch (e) {
351 return jslibError(e);
354 return rv;
357 /********************* VALIDATE PERMISSIONS *************/
358 validatePermissions : function(aNum)
360 if ( parseInt(aNum.toString(10).length) < 3 )
361 return false;
363 return JS_FILEUTILS_OK;
366 /********************* PERMISSIONS **********************/
367 permissions : function (aPath)
369 if (!aPath)
370 return jslibErrorMsg("NS_ERROR_INVALID_ARG");
372 if (!this.exists(aPath))
373 return jslibErrorMsg("NS_ERROR_FILE_NOT_FOUND");
375 var rv;
377 try {
378 rv=(new JS_FILEUTILS_FilePath(aPath)).permissions.toString(8);
379 } catch (e) {
380 rv = jslibError(e);
383 return rv;
386 /********************* MODIFIED *************************/
387 dateModified : function (aPath)
389 if (!aPath)
390 return jslibErrorMsg("NS_ERROR_INVALID_ARG");
392 if (!this.exists(aPath))
393 jslibErrorMsg("NS_ERROR_FILE_NOT_FOUND");
395 var rv;
397 try {
398 var date = new Date((new JS_FILEUTILS_FilePath(aPath)).lastModifiedTime).toLocaleString();
399 rv=date;
400 } catch (e) {
401 rv = jslibError(e);
404 return rv;
407 /********************* SIZE *****************************/
408 size : function (aPath)
410 if (!aPath)
411 return jslibErrorMsg("NS_ERROR_INVALID_ARG");
413 if (!this.exists(aPath))
414 return jslibErrorMsg("NS_ERROR_FILE_NOT_FOUND");
416 var rv;
418 try {
419 rv = (new JS_FILEUTILS_FilePath(aPath)).fileSize;
420 } catch (e) {
421 jslibError(e);
422 rv=0;
425 return rv;
428 /********************* EXTENSION ************************/
429 extension : function (aPath) { return this.ext(aPath); },
431 ext : function (aPath)
433 if (!aPath)
434 return jslibErrorMsg("NS_ERROR_INVALID_ARG");
436 if (!this.exists(aPath))
437 return jslibErrorMsg("NS_ERROR_FILE_NOT_FOUND");
439 var rv;
441 try {
442 var leafName = (new JS_FILEUTILS_FilePath(aPath)).leafName;
443 var dotIndex = leafName.lastIndexOf('.');
444 rv=(dotIndex >= 0) ? leafName.substring(dotIndex+1) : "";
445 } catch (e) {
446 return jslibError(e);
449 return rv;
452 /********************* DIRPATH **************************/
453 dirPath : function (aPath) { return this.parent(aPath); },
455 parent : function (aPath)
457 if (!aPath)
458 return jslibErrorMsg("NS_ERROR_INVALID_ARG");
460 var rv;
462 try {
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())
472 rv = fileInst.path;
474 else
475 rv = null;
478 catch (e) {
479 return jslibError(e);
482 return rv;
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.
496 if (!aPath)
497 return jslibErrorMsg("NS_ERROR_INVALID_ARG");
499 if (!this.exists(aPath))
500 return jslibErrorMsg("NS_ERROR_FILE_NOT_FOUND");
502 var len = 0;
503 if (aArgs)
504 len = aArgs.length;
505 else
506 aArgs = null;
508 var rv;
509 try {
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.
525 try {
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);
532 } catch (e) {
533 rv = jslibError(e);
535 } catch (e) {
536 rv = jslibError(e);
539 return rv;
542 /********************* nsIFILE **************************/
543 nsIFile : function (aPath)
545 if (!aPath)
546 return jslibErrorMsg("NS_ERROR_INVALID_ARG");
548 var rv;
549 try {
550 rv = new JS_FILEUTILS_FilePath(aPath);
551 } catch (e) {
552 rv = jslibError(e);
555 return rv;
558 /********************* HELP *****************************/
559 get help()
561 var help =
563 "\n\nFunction List:\n" +
564 "\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" +
572 " leaf(aPath);\n" +
573 " permissions(aPath);\n" +
574 " dateModified(aPath);\n" +
575 " size(aPath);\n" +
576 " ext(aPath);\n" +
577 " parent(aPath);\n" +
578 " run(aPath, aArgs);\n" +
579 " nsIFile(aPath);\n" +
580 " help;\n";
582 return help;
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.
592 else
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");