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
20 Contributor(s): Pete Collins,
28 if (typeof(JS_LIB_LOADED
)=='boolean') {
30 /***************************
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 /***************************
56 ***************************/
58 /***************************
59 * FileSystem Object Class *
60 ***************************/
61 function FileSystem(aPath
)
63 return (aPath
?this.initPath(arguments
):void(null));
66 /***************************
67 * FileSystem Prototype *
68 ***************************/
69 FileSystem
.prototype = {
74 /***************************
76 ***************************/
77 initPath : function(args
)
79 // check if the argument is a file:// url
81 if(typeof(args
)=='object') {
82 for (var i
=0; i
<args
.length
; i
++) {
83 if(args
[i
].search(/^file:/) == 0) {
85 fileURL
= new JS_FS_URL();
87 args
[i
] = fileURL
.path
;
89 jslibError(e
, "(problem getting file instance)",
90 "NS_ERROR_UNEXPECTED", JS_FILESYSTEM_FILE
+":initPath");
96 if(args
.search(/^file:/) == 0) {
98 fileURL
= new JS_FS_URL();
102 jslibError(e
, "(problem getting file instance)",
103 "NS_ERROR_UNEXPECTED", JS_FILESYSTEM_FILE
+":initPath");
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
115 if (typeof(args
)=='object') {
116 this.mFileInst
= new JS_FS_File_Path(args
[0]?args
[0]:this.mPath
);
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;
122 this.mFileInst
= new JS_FS_File_Path(args
?args
:this.mPath
);
123 this.mFileInst
.path
?rv
=this.mPath
= this.mFileInst
.path
:rv
=null;
127 "initPath (nsILocalFile problem)",
128 "NS_ERROR_UNEXPECTED",
129 JS_FILESYSTEM_FILE
+":initPath");
135 /***************************
137 ***************************/
138 checkInst : function ()
140 if (!this.mFileInst
) {
143 "NS_ERROR_NOT_INITIALIZED",
144 JS_FILESYSTEM_FILE
+":checkInstance");
150 /***************************
152 ***************************/
155 if (!this.checkInst())
156 throw jslibRes
.NS_ERROR_NOT_INITIALIZED
;
157 return this.mFileInst
.path
;
160 /***************************
162 ***************************/
165 if (!this.checkInst())
166 throw jslibRes
.NS_ERROR_NOT_INITIALIZED
;
169 rv
= this.mFileInst
.exists();
172 "exists (nsILocalFile problem)",
173 "NS_ERROR_UNEXPECTED",
174 JS_FILESYSTEM_FILE
+":exists");
180 /***************************
182 ***************************/
185 if (!this.checkInst())
186 throw jslibRes
.NS_ERROR_NOT_INITIALIZED
;
189 rv
= this.mFileInst
.leafName
;
192 "(problem getting file instance)",
194 JS_FILESYSTEM_FILE
+":leaf");
200 /***************************
202 ***************************/
207 "(missing argument)",
208 "NS_ERROR_INVALID_ARG",
209 JS_FILESYSTEM_FILE
+":leaf");
212 if (!this.checkInst())
213 throw jslibRes
.NS_ERROR_NOT_INITIALIZED
;
216 rv
= (this.mFileInst
.leafName
=aLeaf
);
219 "(problem getting file instance)",
221 JS_FILESYSTEM_FILE
+":leaf");
227 /***************************
229 ***************************/
232 if (!this.checkInst())
233 throw jslibRes
.NS_ERROR_NOT_INITIALIZED
;
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
);
243 "(problem getting file parent)",
244 "NS_ERROR_UNEXPECTED",
245 JS_FILESYSTEM_FILE
+":parent");
251 /***************************
253 ***************************/
256 if (!this.checkInst())
257 throw jslibRes
.NS_ERROR_NOT_INITIALIZED
;
258 if (!this.exists()) {
260 "(file doesn't exist)",
262 JS_FILESYSTEM_FILE
+":permisions");
267 rv
= this.mFileInst
.permissions
.toString(8);
270 "(problem getting file instance)",
271 "NS_ERROR_UNEXPECTED",
272 JS_FILESYSTEM_FILE
+":permissions");
278 /***************************
280 ***************************/
281 set permissions(aPermission
)
283 if (!this.checkInst())
284 throw jslibRes
.NS_ERROR_NOT_INITIALIZED
;
288 "(no new permission defined)",
289 "NS_ERROR_INVALID_ARG",
290 JS_FILESYSTEM_FILE
+":permissions");
293 if (!this.exists()) {
295 "(file doesn't exist)",
297 JS_FILESYSTEM_FILE
+":permisions");
300 if (!this.validatePermissions(aPermission
)) {
302 "(invalid permission argument)",
303 "NS_ERROR_INVALID_ARG",
304 JS_FILESYSTEM_FILE
+":permissions");
309 rv
= this.mFileInst
.permissions
=aPermission
;
312 "(problem getting file instance)",
313 "NS_ERROR_UNEXPECTED",
314 JS_FILESYSTEM_FILE
+":permissions");
321 /***************************
322 * VALIDATE PERMISSIONS *
323 ***************************/
324 validatePermissions : function (aNum
)
326 if (typeof(aNum
)!='number')
328 if (parseInt(aNum
.toString(10).length
) < 3 )
333 /***************************
335 ***************************/
338 if (!this.checkInst())
339 throw jslibRes
.NS_ERROR_NOT_INITIALIZED
;
340 if (!this.exists()) {
342 "(file doesn't exist)",
344 JS_FILESYSTEM_FILE
+":dateModified");
349 rv
= (new Date(this.mFileInst
.lastModifiedTime
));
352 "(problem getting file instance)",
353 "NS_ERROR_UNEXPECTED",
354 JS_FILESYSTEM_FILE
+":dateModified");
360 /***************************
362 ***************************/
363 resetCache : function()
365 if (!this.checkInst())
366 throw jslibRes
.NS_ERROR_NOT_INITIALIZED
;
369 delete this.mFileInst
;
371 this.mFileInst
=new JS_FS_File_Path(this.mPath
);
375 "(unable to get nsILocalFile)",
376 "NS_ERROR_UNEXPECTED",
377 JS_FILESYSTEM_FILE
+":resetCache");
384 /***************************
386 ***************************/
389 if (!this.checkInst())
390 throw jslibRes
.NS_ERROR_NOT_INITIALIZED
;
393 rv
= this.mFileInst
.clone();
396 "(problem getting file instance)",
397 "NS_ERROR_UNEXPECTED",
398 JS_FILESYSTEM_FILE
+":nsIFile");
404 /***************************
405 * NOTE: after a move *
406 * successful, 'this' will *
408 * to the moved file! *
409 ***************************/
410 move : function (aDest
)
412 if (!this.checkInst())
413 throw jslibRes
.NS_ERROR_NOT_INITIALIZED
;
416 "(no destination path defined)",
417 "NS_ERROR_INVALID_ARG",
418 JS_FILESYSTEM_FILE
+":move");
424 "NS_ERROR_INVALID_ARG",
425 JS_FILESYSTEM_FILE
+":move");
431 var f
= new JS_FS_File_Path(aDest
);
432 if (f
.exists() && !f
.isDirectory()) {
434 "(destination file exists remove it)",
435 "NS_ERROR_INVALID_ARG",
436 JS_FILESYSTEM_FILE
+":move");
439 if (f
.equals(this.mFileInst
)) {
441 "(destination file is this file)",
442 "NS_ERROR_INVALID_ARG",
443 JS_FILESYSTEM_FILE
+":move");
446 if (!f
.exists() && f
.parent
.exists())
448 if (f
.equals(this.mFileInst
.parent
) && !newName
) {
450 "(destination file is this file)",
451 "NS_ERROR_INVALID_ARG",
452 JS_FILESYSTEM_FILE
+":move");
456 if (dir
.exists() && dir
.isDirectory()) {
458 this.mFileInst
.moveTo(dir
, newName
);
459 jslibDebug(JS_FILESYSTEM_FILE
+':move successful!\n');
466 "(destination "+dir
.parent
.path
+" doesn't exists)",
467 "NS_ERROR_INVALID_ARG",
468 JS_FILESYSTEM_FILE
+":move");
473 "(problem getting file instance)",
474 "NS_ERROR_UNEXPECTED",
475 JS_FILESYSTEM_FILE
+":move");
481 /***************************
483 ***************************/
484 append : function(aLeaf
)
486 if (!this.checkInst())
487 throw jslibRes
.NS_ERROR_NOT_INITIALIZED
;
490 "(no argument defined)",
491 "NS_ERROR_INVALID_ARG",
492 JS_FILESYSTEM_FILE
+":append");
498 "NS_ERROR_INVALID_ARG",
499 JS_FILESYSTEM_FILE
+":append");
504 this.mFileInst
.append(aLeaf
);
505 rv
= this.mPath
=this.path
;
508 "(unexpected error)",
509 "NS_ERROR_UNEXPECTED",
510 JS_FILESYSTEM_FILE
+":append");
516 /***************************
518 ***************************/
519 appendRelativePath : function(aRelPath
)
521 if (!this.checkInst())
522 throw jslibRes
.NS_ERROR_NOT_INITIALIZED
;
525 "(no argument defined)",
526 "NS_ERROR_INVALID_ARG",
527 JS_FILESYSTEM_FILE
+":appendRelativePath");
533 "NS_ERROR_INVALID_ARG",
534 JS_FILESYSTEM_FILE
+":appendRelativePath");
539 this.mFileInst
.appendRelativePath(aRelPath
);
540 rv
= this.mPath
=this.path
;
543 "(unexpected error)",
544 "NS_ERROR_UNEXPECTED",
545 JS_FILESYSTEM_FILE
+":appendRelativePath");
551 /***************************
553 ***************************/
556 return (this.path
?'file:///'+this.path
.replace(/\\/g, "\/").replace(/^\s
*\/?/, "").replace(/\ /g
, "%20"):'');
559 /***************************
561 ***************************/
566 rv
= this.mFileInst
.isDirectory();
567 } catch (e
) { rv
= false; }
572 /***************************
574 ***************************/
579 rv
= this.mFileInst
.isFile();
580 } catch (e
) { rv
= false; }
585 /***************************
587 ***************************/
592 rv
= this.mFileInst
.isExecutable();
593 } catch (e
) { rv
= false; }
598 /***************************
600 ***************************/
601 isSymlink : function()
605 rv
= this.mFileInst
.isSymlink();
606 } catch (e
) { rv
= false; }
611 /***************************
613 ***************************/
617 "\n\nFunction and Attribute List:\n" +
619 " initPath(aPath);\n" +
628 " append(aLeaf);\n" +
629 " appendRelativePath(aRelPath);\n" +
638 }; // END FileSystem Class
640 jslibDebug('*** load: '+JS_FILESYSTEM_FILE
+' OK');
642 } // END BLOCK JS_LIB_LOADED CHECK
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");