Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/utils/platforms/rhino/lib/file-platform.js
blob: 99819b18067e1e3f7313cfaba91cba86931d14fa (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224

// use the "file" module as the exports object.
var exports = require('./file');

// File: Rhino

var IO = require("./io").IO;
var os = require('./os');

var javaRuntime = function () {
    return Packages.java.lang.Runtime.getRuntime();
};

var javaPopen = function (command) {
    return javaRuntime().exec(command);
};

/* streams */

exports.FileIO = function (path, mode, permissions) {
    path = JavaPath(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) {
        return new IO(null, new Packages.java.io.FileOutputStream(path, append));
    } else if (read) {
        return new IO(new Packages.java.io.FileInputStream(path), null);
    } else {
        throw new Error("Files must be opened either for read, write, or update mode.");
    }
};

/* paths */

exports.cwd = function () {
    return String(Packages.java.lang.System.getProperty("user.dir"));
};

var JavaPath = function (path) {
    return new java.io.File(String(path));
};

exports.canonical = function (path) {
    return String(JavaPath(path).getCanonicalPath());
};

exports.mtime = function (path) {
    path = JavaPath(path);
    var lastModified = path.lastModified();
    if (lastModified === 0) return undefined;
    else return new Date(lastModified);
};

exports.size = function (path) {
    path = JavaPath(path);
    return path.length();
};

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

exports.exists = function (path) {
    return JavaPath(path).exists();
};

exports.linkExists = function (path) {
    return exports.isLink(path) || exports.exists(path);
};

exports.isDirectory = function (path) {
    try { return JavaPath(path).isDirectory(); } catch (e) {}
    return false;
};

exports.isFile = function (path) {
    try { return JavaPath(path).isFile(); } catch (e) {}
    return false;
};

// XXX not standard
exports.isAbsolute = function (path) {
    return new java.io.File(path).isAbsolute();
};

/* java doesn't provide isLink, but File.getCanonical leaks
   information about whether a file is a link, so we use the canonical
   file name of a path and the canonical file name of the
   containing directory to infer whether the file is a link.
*/
exports.isLink = function (path) {
    path = exports.path(path);
    var canonical = path.canonical().toString();
    var container = path.resolve('.').canonical();
    if (path.isDirectory()) {
        return container.toString() != canonical;
    } else {
        return container.join('').resolve(path.basename()).toString() != canonical;
    }
};

exports.isReadable = function (path) {
    return JavaPath(path).canRead();
};

exports.isWritable = function (path) {
    return JavaPath(path).canWrite();
};

exports.chmod = function (path, mode) {
    os.command(['chmod', mode.toString(8), path]);
};

exports.chown = function (path, owner, group) {

    if (!owner)
        owner = "";
    else
        owner = String(owner);

    if (group)
        group = String(group);

    if (/:/.test(owner))
        throw new Error("Invalid owner name");
    if (/:/.test(group))
        throw new Error("Invalid group name");

    if (group)
        owner = owner + ":" + String(group);

    os.command(['chown', owner, path]);
};

exports.link = function (source, target) {
    os.command(['ln', source, target]);
};

exports.symlink = function (source, target) {
    // XXX this behavior of resolving the source
    // path from the target path when the source 
    // path is relative ought to be discussed
    // on ServerJS
    if (exports.isRelative(source))
        source = exports.relative(target, source);
    os.command(['ln', '-s', source, target]);
};

exports.rename = function (source, target) {
    source = exports.path(source);
    target = source.resolve(target);
    source = JavaPath(source);
    target = JavaPath(target);
    if (!source.renameTo(target))
        throw new Error("failed to rename " + source + " to " + target);
};

exports.move = function (source, target) {
    source = exports.path(source);
    target = exports.path(target);
    source = JavaPath(source);
    target = JavaPath(target);
    if (!source.renameTo(target))
        throw new Error("failed to rename " + source + " to " + target);
};

exports.remove = function (path) {
    if (!JavaPath(path)['delete']())
        throw new Error("failed to delete " + path);
};

exports.mkdir = function (path) {
    if (!JavaPath(path).mkdir())
        throw new Error("failed to make directory " + path);
};

exports.mkdirs = function(path) {
    JavaPath(path).mkdirs();
    if (!exports.isDirectory(path))
        throw new Error("failed to make directories leading to " + path);
};

exports.rmdir = function(path) {
    if (!JavaPath(String(path))['delete']())
        throw new Error("failed to remove the directory " + path);
};

exports.list = function (path) {
    path = JavaPath(String(path));
    var listing = path.list();

    if (!(listing instanceof Array)) {
        throw new Error("no such directory: " + path);
    }

    var paths = [];
    for (var i = 0; i < listing.length; i++) {
        paths[i] = String(listing[i]);
    }

    return paths;
};

exports.touch = function (path, mtime) {
    if (mtime === undefined || mtime === null)
        mtime = new Date();
    path = JavaPath(path);
    path.createNewFile();
    if (!path.setLastModified(mtime.getTime()))
        throw new Error("unable to set mtime of " + path + " to " + mtime);
};