4
//cache the file separator to prevent multiple lookups
5
Lazarus.file.FILE_SEPARATOR = null;
8
* delete a file or folder
10
Lazarus.file.kill = function(path){
11
var file = Lazarus.file.getFile(path);
18
* return TRUE if file exists and is a file or a directory
20
Lazarus.file.exists = function(path){
21
return Lazarus.file.getFile(path).exists();
25
* return an nsIFile object for the given path
27
Lazarus.file.getFile = function(path){
28
path = Lazarus.file.fixPath(path);
29
var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
30
file.initWithPath(path);
35
* fix slashes in a path to match the OS
37
Lazarus.file.fixPath = function(path){
39
//allow the path to start with special paths
40
var m = path.match(/^%(\w+)%/);
42
path = path.replace(m[0], Lazarus.file.getSpecialDir(m[1]));
45
if (!Lazarus.file.FILE_SEPARATOR){
46
var profilePath = Lazarus.file.getSpecialDir("profile");
47
Lazarus.file.FILE_SEPARATOR = (profilePath.indexOf("/") > -1) ? "/" : "\\";
49
return path.replace(/[\/\\]/g, Lazarus.file.FILE_SEPARATOR);
53
* return the path to a special directory
54
* directory names can be given in mozilla speak (eg "ProfD") or english ("profile")
56
Lazarus.file.getSpecialDir = function(dirName){
58
switch (dirName.toLowerCase()){
65
//assume programmer knows what he's doing and is asking for a valid directory.
68
return Components.classes["@mozilla.org/file/directory_service;1"]
69
.getService(Components.interfaces.nsIProperties)
70
.get(dirName, Components.interfaces.nsIFile).path;
74
* copy a file from src to dest
76
Lazarus.file.copy = function(src, dest, overwrite){
77
var srcFile = Lazarus.file.getFile(src);
78
dest = Lazarus.file.fixPath(dest);
80
var m = dest.match(/(.+)[\\\/]([^\\\/]+$)/);
81
var destFilename = m ? m[2] : dest;
82
var destPath = m ? m[1] : srcFile.parent.path;
84
var destDir = Lazarus.file.getFile(destPath);
87
Lazarus.file.kill(dest);
89
srcFile.copyTo(destDir, destFilename);
93
* move a file from src to dest
95
Lazarus.file.move = function(src, dest, overwrite){
96
var srcFile = Lazarus.file.getFile(src);
97
dest = Lazarus.file.fixPath(dest);
99
var m = dest.match(/(.+)[\\\/]([^\\\/]+$)/);
100
var destFilename = m ? m[2] : dest;
101
var destPath = m ? m[1] : srcFile.parent.path;
103
var destDir = Lazarus.file.getFile(destPath);
105
Lazarus.file.kill(dest);
107
srcFile.moveTo(destDir, destFilename);