|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, you can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 var EXPORTED_SYMBOLS = ['listDirectory', 'getFileForPath', 'abspath', 'getPlatform']; |
|
6 |
|
7 const Cc = Components.classes; |
|
8 const Ci = Components.interfaces; |
|
9 const Cu = Components.utils; |
|
10 |
|
11 Cu.import("resource://gre/modules/Services.jsm"); |
|
12 |
|
13 function listDirectory(file) { |
|
14 // file is the given directory (nsIFile) |
|
15 var entries = file.directoryEntries; |
|
16 var array = []; |
|
17 |
|
18 while (entries.hasMoreElements()) { |
|
19 var entry = entries.getNext(); |
|
20 entry.QueryInterface(Ci.nsIFile); |
|
21 array.push(entry); |
|
22 } |
|
23 |
|
24 return array; |
|
25 } |
|
26 |
|
27 function getFileForPath(path) { |
|
28 var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile); |
|
29 file.initWithPath(path); |
|
30 return file; |
|
31 } |
|
32 |
|
33 function abspath(rel, file) { |
|
34 var relSplit = rel.split('/'); |
|
35 |
|
36 if (relSplit[0] == '..' && !file.isDirectory()) { |
|
37 file = file.parent; |
|
38 } |
|
39 |
|
40 for each(var p in relSplit) { |
|
41 if (p == '..') { |
|
42 file = file.parent; |
|
43 } else if (p == '.') { |
|
44 if (!file.isDirectory()) { |
|
45 file = file.parent; |
|
46 } |
|
47 } else { |
|
48 file.append(p); |
|
49 } |
|
50 } |
|
51 |
|
52 return file.path; |
|
53 } |
|
54 |
|
55 function getPlatform() { |
|
56 return Services.appinfo.OS.toLowerCase(); |
|
57 } |