* Filters/FilterTiff.cs: Compilation fix for 1.1.14
[beagle.git] / mozilla-extension / content / jslib / io / filesystem.js
blob8e8db4b4fd587780734ec0433de99e5ea20f9dba
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
17 Copyright (C) 2000 Collabnet. All
18 Rights Reserved.
20 Contributor(s): Pete Collins,
21 Doug Turner,
22 Brendan Eich,
23 Warren Harris,
24 Eric Plaster,
25 Martin Kutschker
26 ***/
28 if (typeof(JS_LIB_LOADED)=='boolean') {
30 /***************************
31 * Globals *
32 ***************************/
34 const JS_FILESYSTEM_LOADED = true;
35 const JS_FILESYSTEM_FILE = "filesystem.js";
36 const JS_FS_LOCAL_CID = "@mozilla.org/file/local;1";
37 const JS_FS_DIR_CID = "@mozilla.org/file/directory_service;1";
38 const JS_FS_NETWORK_CID = '@mozilla.org/network/standard-url;1';
39 const JS_FS_URL_COMP = "nsIURL";
40 const JS_FS_I_LOCAL_FILE = "nsILocalFile";
41 const JS_FS_DIR_I_PROPS = "nsIProperties";
42 const JS_FS_INIT_W_PATH = "initWithPath";
43 const JS_FS_CHROME_DIR = "AChrom";
44 const JS_FS_USR_DEFAULT = "DefProfRt";
45 const JS_FS_PREF_DIR = "PrefD";
46 const JS_FS_OK = true;
47 const JS_FS_File_Path = new Components.Constructor
48 (JS_FS_LOCAL_CID, JS_FS_I_LOCAL_FILE, JS_FS_INIT_W_PATH);
49 const JS_FS_Dir = new Components.Constructor
50 (JS_FS_DIR_CID, JS_FS_DIR_I_PROPS);
51 const JS_FS_URL = new Components.Constructor
52 (JS_FS_NETWORK_CID, JS_FS_URL_COMP);
54 /***************************
55 * Globals *
56 ***************************/
58 /***************************
59 * FileSystem Object Class *
60 ***************************/
61 function FileSystem(aPath)
63 return (aPath?this.initPath(arguments):void(null));
64 } // constructor
66 /***************************
67 * FileSystem Prototype *
68 ***************************/
69 FileSystem.prototype = {
71 mPath : null,
72 mFileInst : null,
74 /***************************
75 * INIT PATH *
76 ***************************/
77 initPath : function(args)
79 // check if the argument is a file:// url
80 var fileURL;
81 if(typeof(args)=='object') {
82 for (var i=0; i<args.length; i++) {
83 if(args[i].search(/^file:/) == 0) {
84 try {
85 fileURL= new JS_FS_URL();
86 fileURL.spec=args[i];
87 args[i] = fileURL.path;
88 } catch (e) {
89 jslibError(e, "(problem getting file instance)",
90 "NS_ERROR_UNEXPECTED", JS_FILESYSTEM_FILE+":initPath");
91 rv=null;
95 } else {
96 if(args.search(/^file:/) == 0) {
97 try {
98 fileURL= new JS_FS_URL();
99 fileURL.spec=args;
100 args = fileURL.path;
101 } catch (e) {
102 jslibError(e, "(problem getting file instance)",
103 "NS_ERROR_UNEXPECTED", JS_FILESYSTEM_FILE+":initPath");
104 rv=null;
109 /**
110 * If you are wondering what all this extra cruft is, well
111 * this is here so you can reinitialize 'this' with a new path
113 var rv = null;
114 try {
115 if (typeof(args)=='object') {
116 this.mFileInst = new JS_FS_File_Path(args[0]?args[0]:this.mPath);
117 if (args.length>1)
118 for (i=1; i<args.length; i++)
119 this.mFileInst.append(args[i]);
120 (args[0] || this.mPath)?rv=this.mPath = this.mFileInst.path:rv=null;
121 } else {
122 this.mFileInst = new JS_FS_File_Path(args?args:this.mPath);
123 this.mFileInst.path?rv=this.mPath = this.mFileInst.path:rv=null;
125 } catch(e) {
126 jslibError(e?e:null,
127 "initPath (nsILocalFile problem)",
128 "NS_ERROR_UNEXPECTED",
129 JS_FILESYSTEM_FILE+":initPath");
130 rv = null;
132 return rv;
135 /***************************
136 * CHECK INST *
137 ***************************/
138 checkInst : function ()
140 if (!this.mFileInst) {
141 jslibError(null,
142 "(no path defined)",
143 "NS_ERROR_NOT_INITIALIZED",
144 JS_FILESYSTEM_FILE+":checkInstance");
145 return false;
147 return true;
150 /***************************
151 * PATH *
152 ***************************/
153 get path()
155 if (!this.checkInst())
156 throw jslibRes.NS_ERROR_NOT_INITIALIZED;
157 return this.mFileInst.path;
160 /***************************
161 * EXISTS *
162 ***************************/
163 exists : function ()
165 if (!this.checkInst())
166 throw jslibRes.NS_ERROR_NOT_INITIALIZED;
167 var rv = false;
168 try {
169 rv = this.mFileInst.exists();
170 } catch(e) {
171 jslibError(e,
172 "exists (nsILocalFile problem)",
173 "NS_ERROR_UNEXPECTED",
174 JS_FILESYSTEM_FILE+":exists");
175 rv = false;
177 return rv;
180 /***************************
181 * GET LEAF *
182 ***************************/
183 get leaf()
185 if (!this.checkInst())
186 throw jslibRes.NS_ERROR_NOT_INITIALIZED;
187 var rv = null;
188 try {
189 rv = this.mFileInst.leafName;
190 } catch(e) {
191 jslibError(e,
192 "(problem getting file instance)",
193 "NS_ERROR_FAILURE",
194 JS_FILESYSTEM_FILE+":leaf");
195 rv = null;
197 return rv;
200 /***************************
201 * SET LEAF *
202 ***************************/
203 set leaf(aLeaf)
205 if (!aLeaf) {
206 jslibError(null,
207 "(missing argument)",
208 "NS_ERROR_INVALID_ARG",
209 JS_FILESYSTEM_FILE+":leaf");
210 return null;
212 if (!this.checkInst())
213 throw jslibRes.NS_ERROR_NOT_INITIALIZED;
214 var rv = null;
215 try {
216 rv = (this.mFileInst.leafName=aLeaf);
217 } catch(e) {
218 jslibError(e,
219 "(problem getting file instance)",
220 "NS_ERROR_FAILURE",
221 JS_FILESYSTEM_FILE+":leaf");
222 rv = null;
224 return rv;
227 /***************************
228 * PARENT *
229 ***************************/
230 get parent()
232 if (!this.checkInst())
233 throw jslibRes.NS_ERROR_NOT_INITIALIZED;
234 var rv = null;
235 try {
236 if (this.mFileInst.parent.isDirectory()) {
237 if (typeof(JS_DIR_LOADED)!='boolean')
238 include(JS_LIB_PATH+'io/dir.js');
239 rv = new Dir(this.mFileInst.parent.path);
241 } catch (e) {
242 jslibError(e,
243 "(problem getting file parent)",
244 "NS_ERROR_UNEXPECTED",
245 JS_FILESYSTEM_FILE+":parent");
246 rv = null;
248 return rv;
251 /***************************
252 * GET PERMISSIONS *
253 ***************************/
254 get permissions()
256 if (!this.checkInst())
257 throw jslibRes.NS_ERROR_NOT_INITIALIZED;
258 if (!this.exists()) {
259 jslibError(null,
260 "(file doesn't exist)",
261 "NS_ERROR_FAILURE",
262 JS_FILESYSTEM_FILE+":permisions");
263 return null;
265 var rv = null;
266 try {
267 rv = this.mFileInst.permissions.toString(8);
268 } catch(e) {
269 jslibError(e,
270 "(problem getting file instance)",
271 "NS_ERROR_UNEXPECTED",
272 JS_FILESYSTEM_FILE+":permissions");
273 rv = null;
275 return rv;
278 /***************************
279 * SET PERMISSIONS *
280 ***************************/
281 set permissions(aPermission)
283 if (!this.checkInst())
284 throw jslibRes.NS_ERROR_NOT_INITIALIZED;
286 if (!aPermission) {
287 jslibError(null,
288 "(no new permission defined)",
289 "NS_ERROR_INVALID_ARG",
290 JS_FILESYSTEM_FILE+":permissions");
291 return null;
293 if (!this.exists()) {
294 jslibError(null,
295 "(file doesn't exist)",
296 "NS_ERROR_FAILURE",
297 JS_FILESYSTEM_FILE+":permisions");
298 return null;
300 if (!this.validatePermissions(aPermission)) {
301 jslibError(null,
302 "(invalid permission argument)",
303 "NS_ERROR_INVALID_ARG",
304 JS_FILESYSTEM_FILE+":permissions");
305 return null;
307 var rv = null;
308 try {
309 rv = this.mFileInst.permissions=aPermission;
310 } catch(e) {
311 jslibError(e,
312 "(problem getting file instance)",
313 "NS_ERROR_UNEXPECTED",
314 JS_FILESYSTEM_FILE+":permissions");
315 rv = null;
317 return rv;
321 /***************************
322 * VALIDATE PERMISSIONS *
323 ***************************/
324 validatePermissions : function (aNum)
326 if (typeof(aNum)!='number')
327 return false;
328 if (parseInt(aNum.toString(10).length) < 3 )
329 return false;
330 return true;
333 /***************************
334 * MODIFIED *
335 ***************************/
336 get dateModified()
338 if (!this.checkInst())
339 throw jslibRes.NS_ERROR_NOT_INITIALIZED;
340 if (!this.exists()) {
341 jslibError(null,
342 "(file doesn't exist)",
343 "NS_ERROR_FAILURE",
344 JS_FILESYSTEM_FILE+":dateModified");
345 return null;
347 var rv = null;
348 try {
349 rv = (new Date(this.mFileInst.lastModifiedTime));
350 } catch(e) {
351 jslibError(e,
352 "(problem getting file instance)",
353 "NS_ERROR_UNEXPECTED",
354 JS_FILESYSTEM_FILE+":dateModified");
355 rv = null;
357 return rv;
360 /***************************
361 * RESET CACHE *
362 ***************************/
363 resetCache : function()
365 if (!this.checkInst())
366 throw jslibRes.NS_ERROR_NOT_INITIALIZED;
367 var rv = false;
368 if (this.mPath) {
369 delete this.mFileInst;
370 try {
371 this.mFileInst=new JS_FS_File_Path(this.mPath);
372 rv = true;
373 } catch(e) {
374 jslibError(e,
375 "(unable to get nsILocalFile)",
376 "NS_ERROR_UNEXPECTED",
377 JS_FILESYSTEM_FILE+":resetCache");
378 rv = false;
381 return rv;
384 /***************************
385 * nsIFILE *
386 ***************************/
387 get nsIFile()
389 if (!this.checkInst())
390 throw jslibRes.NS_ERROR_NOT_INITIALIZED;
391 var rv = null;
392 try {
393 rv = this.mFileInst.clone();
394 } catch (e) {
395 jslibError(e,
396 "(problem getting file instance)",
397 "NS_ERROR_UNEXPECTED",
398 JS_FILESYSTEM_FILE+":nsIFile");
399 rv = null;
401 return rv;
404 /***************************
405 * NOTE: after a move *
406 * successful, 'this' will *
407 * be reinitialized *
408 * to the moved file! *
409 ***************************/
410 move : function (aDest)
412 if (!this.checkInst())
413 throw jslibRes.NS_ERROR_NOT_INITIALIZED;
414 if (!aDest) {
415 jslibError(null,
416 "(no destination path defined)",
417 "NS_ERROR_INVALID_ARG",
418 JS_FILESYSTEM_FILE+":move");
419 return false;
421 if (!this.mPath) {
422 jslibError(null,
423 "(no path defined)",
424 "NS_ERROR_INVALID_ARG",
425 JS_FILESYSTEM_FILE+":move");
426 return false;
428 var rv = null;
429 var newName=null;
430 try {
431 var f = new JS_FS_File_Path(aDest);
432 if (f.exists() && !f.isDirectory()) {
433 jslibError(null,
434 "(destination file exists remove it)",
435 "NS_ERROR_INVALID_ARG",
436 JS_FILESYSTEM_FILE+":move");
437 return false;
439 if (f.equals(this.mFileInst)) {
440 jslibError(null,
441 "(destination file is this file)",
442 "NS_ERROR_INVALID_ARG",
443 JS_FILESYSTEM_FILE+":move");
444 return false;
446 if (!f.exists() && f.parent.exists())
447 newName=f.leafName;
448 if (f.equals(this.mFileInst.parent) && !newName) {
449 jslibError(null,
450 "(destination file is this file)",
451 "NS_ERROR_INVALID_ARG",
452 JS_FILESYSTEM_FILE+":move");
453 return false;
455 var dir=f.parent;
456 if (dir.exists() && dir.isDirectory()) {
457 jslibDebug(newName);
458 this.mFileInst.moveTo(dir, newName);
459 jslibDebug(JS_FILESYSTEM_FILE+':move successful!\n');
460 this.mPath=f.path;
461 this.resetCache();
462 delete dir;
463 rv = true;
464 } else {
465 jslibError(null,
466 "(destination "+dir.parent.path+" doesn't exists)",
467 "NS_ERROR_INVALID_ARG",
468 JS_FILESYSTEM_FILE+":move");
469 return false;
471 } catch (e) {
472 jslibError(e,
473 "(problem getting file instance)",
474 "NS_ERROR_UNEXPECTED",
475 JS_FILESYSTEM_FILE+":move");
476 rv = false;
478 return rv;
481 /***************************
482 * APPEND *
483 ***************************/
484 append : function(aLeaf)
486 if (!this.checkInst())
487 throw jslibRes.NS_ERROR_NOT_INITIALIZED;
488 if (!aLeaf) {
489 jslibError(null,
490 "(no argument defined)",
491 "NS_ERROR_INVALID_ARG",
492 JS_FILESYSTEM_FILE+":append");
493 return null;
495 if (!this.mPath) {
496 jslibError(null,
497 "(no path defined)",
498 "NS_ERROR_INVALID_ARG",
499 JS_FILESYSTEM_FILE+":append");
500 return null;
502 var rv = null;
503 try {
504 this.mFileInst.append(aLeaf);
505 rv = this.mPath=this.path;
506 } catch(e) {
507 jslibError(null,
508 "(unexpected error)",
509 "NS_ERROR_UNEXPECTED",
510 JS_FILESYSTEM_FILE+":append");
511 rv = null;
513 return rv;
516 /***************************
517 * APPEND RELATIVE *
518 ***************************/
519 appendRelativePath : function(aRelPath)
521 if (!this.checkInst())
522 throw jslibRes.NS_ERROR_NOT_INITIALIZED;
523 if (!aRelPath) {
524 jslibError(null,
525 "(no argument defined)",
526 "NS_ERROR_INVALID_ARG",
527 JS_FILESYSTEM_FILE+":appendRelativePath");
528 return null;
530 if (!this.mPath) {
531 jslibError(null,
532 "(no path defined)",
533 "NS_ERROR_INVALID_ARG",
534 JS_FILESYSTEM_FILE+":appendRelativePath");
535 return null;
537 var rv = null;
538 try {
539 this.mFileInst.appendRelativePath(aRelPath);
540 rv = this.mPath=this.path;
541 } catch(e) {
542 jslibError(null,
543 "(unexpected error)",
544 "NS_ERROR_UNEXPECTED",
545 JS_FILESYSTEM_FILE+":appendRelativePath");
546 rv = null;
548 return rv;
551 /***************************
552 * GET URL *
553 ***************************/
554 get URL()
556 return (this.path?'file:///'+this.path.replace(/\\/g, "\/").replace(/^\s*\/?/, "").replace(/\ /g, "%20"):'');
559 /***************************
560 * ISDIR *
561 ***************************/
562 isDir : function()
564 var rv = false;
565 try {
566 rv = this.mFileInst.isDirectory();
567 } catch (e) { rv = false; }
569 return rv;
572 /***************************
573 * ISFILE *
574 ***************************/
575 isFile : function()
577 var rv = false;
578 try {
579 rv = this.mFileInst.isFile();
580 } catch (e) { rv = false; }
582 return rv;
585 /***************************
586 * ISEXEC *
587 ***************************/
588 isExec : function()
590 var rv = false;
591 try {
592 rv = this.mFileInst.isExecutable();
593 } catch (e) { rv = false; }
595 return rv;
598 /***************************
599 * ISSYMLINK *
600 ***************************/
601 isSymlink : function()
603 var rv = false;
604 try {
605 rv = this.mFileInst.isSymlink();
606 } catch (e) { rv = false; }
608 return rv;
611 /***************************
612 * HELP *
613 ***************************/
614 help : function()
616 const help =
617 "\n\nFunction and Attribute List:\n" +
618 "\n" +
619 " initPath(aPath);\n" +
620 " path;\n" +
621 " exists();\n" +
622 " leaf;\n" +
623 " parent;\n" +
624 " permissions;\n" +
625 " dateModified;\n" +
626 " nsIFile;\n" +
627 " move(aDest);\n" +
628 " append(aLeaf);\n" +
629 " appendRelativePath(aRelPath);\n" +
630 " URL;\n" +
631 " isDir();\n" +
632 " isFile();\n" +
633 " isExec();\n" +
634 " isSymlink();\n";
635 return help;
638 }; // END FileSystem Class
640 jslibDebug('*** load: '+JS_FILESYSTEM_FILE+' OK');
642 } // END BLOCK JS_LIB_LOADED CHECK
644 else {
645 dump("JS_FILE library not loaded:\n" +
646 " \tTo load use: chrome://jslib/content/jslib.js\n" +
647 " \tThen: include(jslib_filesystem);\n\n");