home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Minami 83
/
MINAMI83.iso
/
Extra
/
DivXInstaller.exe
/
$PLUGINSDIR
/
GoogleToolbarFirefox.msi
/
xpi
/
amulet-jslib
/
firefox
/
filesystem.js
< prev
next >
Wrap
Text File
|
2006-08-07
|
5KB
|
147 lines
var G_File = {};
G_File.getHomeFile = function(opt_file) {
return this.getSpecialFile("Home", opt_file);
}
G_File.getProfileFile = function(opt_file) {
return this.getSpecialFile("ProfD", opt_file);
}
G_File.getTempFile = function(opt_file) {
return this.getSpecialFile("TmpD", opt_file);
}
G_File.getSpecialFile = function(loc, opt_file) {
var file = Cc["@mozilla.org/file/directory_service;1"]
.getService(Ci.nsIProperties)
.get(loc, Ci.nsILocalFile);
if (opt_file) {
file.append(opt_file);
}
return file;
}
G_File.createUniqueTempFile = function(opt_baseName) {
var baseName = (opt_baseName || (new Date().getTime())) + ".tmp";
var file = this.getSpecialFile("TmpD", baseName);
file.createUnique(file.NORMAL_FILE_TYPE, 0644);
return file;
}
G_File.createUniqueTempDir = function(opt_baseName) {
var baseName = (opt_baseName || (new Date().getTime())) + ".tmp";
var dir = this.getSpecialFile("TmpD", baseName);
dir.createUnique(dir.DIRECTORY_TYPE, 0744);
return dir;
}
G_File.fromFileURI = function(uri) {
if (uri.indexOf("file://") != 0)
throw new Error("File path must be a file:// URL");
var fileHandler = Cc["@mozilla.org/network/protocol;1?name=file"]
.getService(Ci.nsIFileProtocolHandler);
return fileHandler.getFileFromURLSpec(uri);
}
G_File.PR_RDONLY = 0x01; // read-only
G_File.PR_WRONLY = 0x02; // write only
G_File.PR_RDWR = 0x04; // reading and writing
G_File.PR_CREATE_FILE = 0x08; // create if it doesn't exist
G_File.PR_APPEND = 0x10; // file pntr reset to end prior to writes
G_File.PR_TRUNCATE = 0x20; // file exists its length is set to zero
G_File.PR_SYNC = 0x40; // writes wait for data to be physically written
G_File.PR_EXCL = 0x80; // file does not exist ? created : no action
G_File.__defineGetter__("LINE_END_CHAR", function() {
var end_char;
if ("@mozilla.org/xre/app-info;1" in Cc) {
end_char = Cc["@mozilla.org/xre/app-info;1"]
.getService(Ci.nsIXULRuntime)
.OS == "WINNT" ? "\r\n" : "\n";
} else {
end_char = Cc["@mozilla.org/network/protocol;1?name=http"]
.getService(Ci.nsIHttpProtocolHandler)
.platform.toLowerCase().indexOf("win") == 0 ? "\r\n" : "\n";
}
G_File.__defineGetter__("LINE_END_CHAR", function() { return end_char; });
return end_char;
});
function G_FileReader(file) {
this.file_ = isString(file) ? G_File.fromFileURI(file) : file;
}
G_FileReader.readAll = function(file) {
var reader = new G_FileReader(file);
try {
return reader.read();
} finally {
reader.close();
}
}
G_FileReader.prototype.read = function(opt_maxBytes) {
if (!this.stream_) {
var fs = Cc["@mozilla.org/network/file-input-stream;1"]
.createInstance(Ci.nsIFileInputStream);
fs.init(this.file_, G_File.PR_RDONLY, 0444, 0);
this.stream_ = Cc["@mozilla.org/scriptableinputstream;1"]
.createInstance(Ci.nsIScriptableInputStream);
this.stream_.init(fs);
}
if (!isDef(opt_maxBytes)) {
opt_maxBytes = this.stream_.available();
}
return this.stream_.read(opt_maxBytes);
}
G_FileReader.prototype.close = function(opt_maxBytes) {
if (this.stream_) {
this.stream_.close();
this.stream_ = null;
}
}
function G_FileWriter(file, opt_append) {
this.file_ = typeof file == "string" ? G_File.fromFileURI(file) : file;
this.append_ = !!opt_append;
}
G_FileWriter.writeAll = function(file, data, opt_append) {
var writer = new G_FileWriter(file, opt_append);
try {
return writer.write(data);
} finally {
writer.close();
return 0;
}
}
G_FileWriter.prototype.write = function(data) {
if (!this.stream_) {
this.stream_ = Cc["@mozilla.org/network/file-output-stream;1"]
.createInstance(Ci.nsIFileOutputStream);
var flags = G_File.PR_WRONLY |
G_File.PR_CREATE_FILE |
(this.append_ ? G_File.PR_APPEND : G_File.PR_TRUNCATE);
this.stream_.init(this.file_,
flags,
-1 /* default perms */,
0 /* no special behavior */);
}
return this.stream_.write(data, data.length);
}
G_FileWriter.prototype.writeLine = function(data) {
this.write(data + G_File.LINE_END_CHAR);
}
G_FileWriter.prototype.close = function() {
if (this.stream_) {
this.stream_.close();
this.stream_ = null;
}
}
function TEST_G_File() {
if (G_GDEBUG) {
var z = "gfile UNITTEST";
G_debugService.enableZone(z);
G_Debug(z, "Starting");
try {
var dir = G_File.createUniqueTempDir();
} catch(e) {
G_Debug(z, e);
G_Assert(z, false, "Failed to create temp dir");
}
G_Assert(z, dir.exists(), "Temp dir doesn't exist: " + dir.path);
G_Assert(z, dir.isDirectory(), "Temp dir isn't a directory: " + dir.path);
G_Assert(z, dir.isReadable(), "Temp dir isn't readable: " + dir.path);
G_Assert(z, dir.isWritable(), "Temp dir isn't writable: " + dir.path);
dir.remove(true /* recurse */);
G_Debug(z, "PASS");
}
}