2018-01-19 03:59:58 +08:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, you can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
|
|
var EXPORTED_SYMBOLS = ['listDirectory', 'getFileForPath', 'abspath', 'getPlatform'];
|
|
|
|
|
|
2018-10-06 06:57:51 +02:00
|
|
|
const Cc = Components.classes;
|
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
|
const Cu = Components.utils;
|
2018-01-19 03:59:58 +08:00
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
|
|
|
|
|
|
function listDirectory(file) {
|
|
|
|
|
// file is the given directory (nsIFile)
|
|
|
|
|
var entries = file.directoryEntries;
|
|
|
|
|
var array = [];
|
|
|
|
|
|
|
|
|
|
while (entries.hasMoreElements()) {
|
|
|
|
|
var entry = entries.getNext();
|
|
|
|
|
entry.QueryInterface(Ci.nsIFile);
|
|
|
|
|
array.push(entry);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return array;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getFileForPath(path) {
|
|
|
|
|
var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
|
|
|
|
|
file.initWithPath(path);
|
|
|
|
|
return file;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function abspath(rel, file) {
|
|
|
|
|
var relSplit = rel.split('/');
|
|
|
|
|
|
|
|
|
|
if (relSplit[0] == '..' && !file.isDirectory()) {
|
|
|
|
|
file = file.parent;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-06 06:57:51 +02:00
|
|
|
for each(var p in relSplit) {
|
2018-01-19 03:59:58 +08:00
|
|
|
if (p == '..') {
|
|
|
|
|
file = file.parent;
|
|
|
|
|
} else if (p == '.') {
|
|
|
|
|
if (!file.isDirectory()) {
|
|
|
|
|
file = file.parent;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
file.append(p);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return file.path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getPlatform() {
|
|
|
|
|
return Services.appinfo.OS.toLowerCase();
|
|
|
|
|
}
|