Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/utils/platforms/xulrunner/lib/file-platform.js
blob: 2e554f3cf773a6fbe680b4dcee4e2bd024949372 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
var exports = require('./file');
var IO = require("./io").IO;

const Cc = Components.classes;
const Ci = Components.interfaces;
const DirService = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties)

function MozFile(path) {
    var file = Cc['@mozilla.org/file/local;1'].createInstance(Ci.nsILocalFile);
    try {
        file.initWithPath(path);
    } catch(e) {
        file.initWithPath(exports.cwd());
        path.split(exports.SEPARATOR).filter(function(part) part != '').
            forEach(function(part) file.append(part));
    }
    return file;
}

exports.cwd = function () DirService.get("CurWorkD", Ci.nsIFile).path;

exports.SEPARATOR = (function() (exports.cwd().search(/\\/) != -1) ? "\\" : "/")();
exports.ROOT = '/';


exports.list = function (path) {
    var entries = MozFile(path).directoryEntries;
    var entryNames = [];
    while(entries.hasMoreElements()) {
        var entry = entries.getNext();
        entry.QueryInterface(Ci.nsIFile);
        entryNames.push(entry.leafName);
    }
    return entryNames;
};

exports.canonical = function (path) {
    var file = MozFile(path);
    try {
        file.normalize();
    } catch(e) {}
    return file.path;
}

exports.exists = function (path) MozFile(path).exists();

exports.mtime = function (path) new Date(MozFile(path).lastModifiedTime);

exports.size = function (path) MozFile(path).fileSize;

exports.stat = function (path) {
    return {
        mtime: exports.mtime(path),
        size: exports.size(path)
    }
};

exports.isDirectory = function (path) {
    var file = MozFile(path);
    return file.exists() && file.isDirectory();
}

exports.isFile = function (path) {
    var file = MozFile(path);
    return file.exists() && file.isFile();
}

exports.isLink = function (path) MozFile(path).isSymlink();

exports.isReadable = function (path) MozFile(path).isReadable();

exports.isWritable = function (path) MozFile(path).isWritable();

exports.rename = function (source, target) {
    source = file.path(source);
    target = source.resolve(target);
    source = MozFile(source);
    target = MozFile(target);
    try {
        source.moveTo(target.parent, target.leafName);
    } catch(e) {
        throw new Error("failed to rename " + source.path + " to " + target.path);
    }
};

exports.move = function (source, target) {
    source = file.path(source);
    target = source.resolve(target);
    source = MozFile(source);
    target = MozFile(target);
    try {
        source.moveTo(target.parent, target.leafName);
    } catch(e) {
        throw new Error("failed to move " + source.path + " to " + target.path);
    }
};

exports.remove = function (path) {
    try {
        MozFile(path).remove(false)
    } catch(e) {
        throw new Error("failed to delete " + path);
    }
};

exports.mkdir = function (path) MozFile(path).create(Ci.nsIFile.NORMAL_FILE_TYPE, 0777);

exports.mkdirs = function (path) MozFile(path).create(Ci.nsIFile.NORMAL_FILE_TYPE, 0777);

exports.rmdir = function(path) {
    try {
        MozFile(path).remove(false)
    } catch(e) {
        throw new Error("failed to delete " + path);
    }
};

exports.rmtree = function(path) {
    try {
        MozFile(path).remove(true)
    } catch(e) {
        throw new Error("failed to delete " + path);
    }
};

exports.touch = function (path, mtime) {
    var file = MozFile(path);
    if (!file.exists()) file.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
    else file.lastModifiedTime = new Date().getTime().toString();
};

exports.symlink = function (path) {
    throw "NYI";
};

exports.FileIO = function (path, mode, permissions) {
    file = MozFile(path);

    var {
        read: read,
        write: write,
        append: append,
        update: update
    } = exports.mode(mode);

    if (update) {
        throw new Error("Updating IO not yet implemented.");
    } else if (write || append) {
        var stream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(Ci.nsIFileOutputStream);
        stream.init(file, -1, -1, 0);
        return new IO(stream, null);
    } else if (read) {
        var stream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(Ci.nsIFileInputStream);
        stream.init(file, -1, 0, 0);
        return new IO(stream, null);
    } else {
        throw new Error("Files must be opened either for read, write, or update mode.");
    }
};