This repository was archived by the owner on Jun 5, 2020. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathFs.cs
More file actions
432 lines (398 loc) · 19.2 KB
/
Copy pathFs.cs
File metadata and controls
432 lines (398 loc) · 19.2 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
Copyright 2014 Daniel Wirtz <dcode@dcode.io>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using Jurassic;
using Jurassic.Library;
using SharpJS.Encodings;
using SharpJS.Helpers;
using System;
using System.IO;
using System.Text;
using System.Threading;
namespace SharpJS.Libraries
{
public class Fs : /* Constructor-less */ Module
{
public bool AllowSync = true;
public string Chroot = null;
public Fs(Context context, bool allowSync = true, string chroot = null) : base(context) {
AllowSync = allowSync;
Chroot = chroot;
Populate();
}
public string CheckPath(string path, bool allowRoot = false) {
if (path == null)
throw new ArgumentNullException("path is null");
path = Context.Path.Resolve(path);
if (Chroot != null) {
if (path.Equals(Chroot)) {
if (!allowRoot)
throw new AccessViolationException("Unauthorized access to " + path);
} else if (!path.StartsWith(Chroot + Path.Sep, Path.IsWindows ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal))
throw new AccessViolationException("Unauthorized access to " + path + ", restricted to " + Chroot + Path.Sep + "**");
}
return path;
}
[JSFunction(Name = "rename", IsEnumerable = true)]
public void Rename(string oldPath, string newPath, [DefaultParameterValue(null)] FunctionInstance callback = null) {
ThreadPool.QueueUserWorkItem(new WaitCallback(RenameAsync), new object[] { oldPath, newPath, callback });
}
private void RenameAsync(object arguments) {
object[] args = arguments as object[];
FunctionInstance callback = args[2] as FunctionInstance;
try {
string oldPath = CheckPath(args[0] as string);
string newPath = CheckPath(args[1] as string);
File.Move(oldPath, newPath);
if (callback != null)
Context.NextTick(callback.Bind(this, null));
} catch (Exception ex) {
if (callback != null)
Context.NextTick(callback.Bind(this, Engine.Error.Construct(ex.Message)));
}
}
[JSFunction(Name = "renameSync", IsEnumerable = true)]
public void RenameSync(string oldPath, string newPath) {
if (!AllowSync)
throw new JavaScriptException(Engine, "Error", "synchronous operations are not permitted");
try {
File.Move(CheckPath(oldPath), CheckPath(newPath));
} catch (Exception ex) {
throw new JavaScriptException(Engine, "Error", ex.Message);
}
}
[JSFunction(Name = "truncate", IsEnumerable = true)]
public void Truncate(string path, [DefaultParameterValue(0)] int len = 0, [DefaultParameterValue(null)] FunctionInstance callback = null) {
ThreadPool.QueueUserWorkItem(new WaitCallback(TruncateAsync), new object[] { path, len, callback });
}
private void TruncateAsync(object arguments) {
object[] args = arguments as object[];
FunctionInstance callback = args[2] as FunctionInstance;
try {
string path = args[0] as string;
int len = (int)args[1];
FileStream fs = new FileStream(CheckPath(path), FileMode.Truncate);
try {
fs.SetLength(len);
} finally {
fs.Close();
}
if (callback != null)
Context.NextTick(callback.Bind(this, null));
} catch (Exception ex) {
if (callback != null)
Context.NextTick(callback.Bind(this, Engine.Error.Construct(ex.Message)));
}
}
[JSFunction(Name = "truncateSync", IsEnumerable = true)]
public void TruncateSync(string path, [DefaultParameterValue(0)] int len = 0) {
if (!AllowSync)
throw new JavaScriptException(Engine, "Error", "synchronous operations are not permitted");
try {
FileStream fs = new FileStream(CheckPath(path), FileMode.Truncate);
try {
fs.SetLength(len);
} finally {
fs.Close();
}
} catch (Exception ex) {
throw new JavaScriptException(Engine, "Error", ex.Message);
}
}
[JSFunction(Name = "unlink", IsEnumerable = true)]
public void Unlink(string path, [DefaultParameterValue(null)] FunctionInstance callback = null) {
ThreadPool.QueueUserWorkItem(new WaitCallback(UnlinkAsync), new object[] { path, callback });
}
private void UnlinkAsync(object arguments) {
object[] args = arguments as object[];
FunctionInstance callback = args[1] as FunctionInstance;
try {
string path = args[0] as string;
File.Delete(CheckPath(path));
if (callback != null)
Context.NextTick(callback.Bind(this, null));
} catch (Exception ex) {
if (callback != null)
Context.NextTick(callback.Bind(this, Engine.Error.Construct(ex.Message)));
}
}
[JSFunction(Name = "unlinkSync", IsEnumerable = true)]
public void UnlinkSync(string path) {
if (!AllowSync)
throw new JavaScriptException(Engine, "Error", "synchronous operations are not permitted");
try {
File.Delete(CheckPath(path));
} catch (Exception ex) {
throw new JavaScriptException(Engine, "Error", ex.Message);
}
}
[JSFunction(Name = "rmdir", IsEnumerable = true)]
public void Rmdir(string path, [DefaultParameterValue(null)] FunctionInstance callback = null) {
ThreadPool.QueueUserWorkItem(new WaitCallback(RmdirAsync), new object[] { path, callback });
}
private void RmdirAsync(object arguments) {
object[] args = arguments as object[];
FunctionInstance callback = args[1] as FunctionInstance;
try {
string path = args[0] as string;
Directory.Delete(CheckPath(path));
if (callback != null)
Context.NextTick(callback.Bind(this, null));
} catch (Exception ex) {
if (callback != null)
Context.NextTick(callback.Bind(this, Engine.Error.Construct(ex.Message)));
}
}
[JSFunction(Name = "rmdirSync", IsEnumerable = true)]
public void RmdirSync(string path) {
if (!AllowSync)
throw new JavaScriptException(Engine, "Error", "synchronous operations are not permitted");
try {
Directory.Delete(CheckPath(path));
} catch (Exception ex) {
throw new JavaScriptException(Engine, "Error", ex.Message);
}
}
[JSFunction(Name = "mkdir", IsEnumerable = true)]
public void Mkdir(string path, params object[] args) {
ThreadPool.QueueUserWorkItem(new WaitCallback(MkdirAsync), new object[] { path, args });
}
private void MkdirAsync(object arguments) {
object[] args = arguments as object[];
object[] mkdirArgs = args[1] as object[];
FunctionInstance callback = null;
try {
int mode = 0777;
if (mkdirArgs.Length >= 2) {
mode = (int)mkdirArgs[0];
callback = mkdirArgs[1] as FunctionInstance;
} else if (mkdirArgs.Length >= 1) {
if (mkdirArgs[0] is int) {
mode = (int)mkdirArgs[0];
} else if (mkdirArgs[0] is FunctionInstance) {
callback = mkdirArgs[0] as FunctionInstance;
}
}
string path = CheckPath(args[0] as string);
Directory.CreateDirectory(path);
if (callback != null)
Context.NextTick(callback.Bind(this, null));
} catch (Exception ex) {
if (callback != null)
Context.NextTick(callback.Bind(this, Engine.Error.Construct(ex.Message)));
}
}
[JSFunction(Name = "mkdirSync", IsEnumerable = true)]
public void MkdirSync(string path, [DefaultParameterValue(0777)] int mode = 0777) {
if (!AllowSync)
throw new JavaScriptException(Engine, "Error", "synchronous operations are not permitted");
try {
Directory.CreateDirectory(CheckPath(path));
} catch (Exception ex) {
throw new JavaScriptException(Engine, "Error", ex.Message);
}
}
[JSFunction(Name = "readdir", IsEnumerable = true)]
public void Readdir(string path, FunctionInstance callback) {
ThreadPool.QueueUserWorkItem(new WaitCallback(ReaddirAsync), new object[] { path, callback });
}
private void ReaddirAsync(object arguments) {
object[] args = arguments as object[];
FunctionInstance callback = args[1] as FunctionInstance;
try {
string path = args[0] as string;
string[] files = Directory.GetFileSystemEntries(CheckPath(path, true));
ArrayInstance result = Engine.Array.Construct();
foreach (string file in files) {
if (file != "." && file != "..")
result.Push(System.IO.Path.GetFileName(file));
}
if (callback != null)
Context.NextTick(callback.Bind(this, null, result));
} catch (Exception ex) {
if (callback != null)
Context.NextTick(callback.Bind(this, Engine.Error.Construct(ex.Message)));
}
}
[JSFunction(Name = "readdirSync", IsEnumerable = true)]
public ArrayInstance ReaddirSync(string path) {
if (!AllowSync)
throw new JavaScriptException(Engine, "Error", "synchronous operations are not permitted");
try {
string[] files = Directory.GetFileSystemEntries(CheckPath(path));
ArrayInstance result = Engine.Array.Construct();
foreach (string file in files) {
if (file != "." && file != "..")
result.Push(System.IO.Path.GetFileName(file));
}
return result;
} catch (Exception ex) {
throw new JavaScriptException(Engine, "Error", ex.Message);
}
}
[JSFunction(Name = "readFile", IsEnumerable = true)]
public void ReadFile(string path, params object[] args) {
ThreadPool.QueueUserWorkItem(new WaitCallback(ReadFileAsync), new object[] { path, args });
}
private void ReadFileAsync(object arguments) {
object[] args = arguments as object[];
FunctionInstance callback = null;
ObjectInstance options = null;
try {
object[] readArgs = args[1] as object[];
if (readArgs.Length >= 2) {
options = readArgs[0] as ObjectInstance;
callback = readArgs[1] as FunctionInstance;
} else if (readArgs.Length >= 1) {
if (readArgs[0] is FunctionInstance) {
callback = readArgs[0] as FunctionInstance;
}
}
string path = CheckPath(args[0] as string);
string encoding = options != null ? options.GetPropertyValue("encoding") as string : null;
byte[] b = File.ReadAllBytes(path);
object result = new BufferInstance(Context, Context.Buffer.Constructor.InstancePrototype, b, 0, b.Length);
if (encoding != null) {
result = (result as BufferInstance).toString(encoding);
}
if (callback != null)
Context.NextTick(callback.Bind(this, null, result));
} catch (Exception ex) {
if (callback != null)
Context.NextTick(callback.Bind(this, Engine.Error.Construct(ex.Message)));
}
}
[JSFunction(Name = "readFileSync", IsEnumerable = true)]
public object ReadFileSync(string path, [DefaultParameterValue(null)] ObjectInstance options = null) {
if (!AllowSync)
throw new JavaScriptException(Engine, "Error", "synchronous operations are not permitted");
try {
string encoding = options != null ? options.GetPropertyValue("encoding") as string : null;
byte[] b = File.ReadAllBytes(CheckPath(path));
object result = new BufferInstance(Context, Context.Buffer.Constructor.InstancePrototype, b, 0, b.Length);
if (encoding != null) {
result = (result as BufferInstance).toString(encoding);
}
return result;
} catch (Exception ex) {
throw new JavaScriptException(Engine, "Error", ex.Message);
}
}
[JSFunction(Name = "writeFile", IsEnumerable = true)]
public void WriteFile(string path, object data, params object[] args) {
ThreadPool.QueueUserWorkItem(new WaitCallback(WriteFileAsync), new object[] { path, data, args });
}
private void WriteFileAsync(object arguments) {
object[] args = arguments as object[];
FunctionInstance callback = null;
ObjectInstance options = null;
try {
object data = args[1];
object[] writeArgs = args[2] as object[];
if (writeArgs.Length >= 2) {
options = writeArgs[0] as ObjectInstance;
callback = writeArgs[1] as FunctionInstance;
} else if (writeArgs.Length >= 1) {
if (writeArgs[0] is FunctionInstance) {
callback = writeArgs[0] as FunctionInstance;
}
}
string path = CheckPath(args[0] as string);
if (TypeUtil.IsString(data)) {
string str = TypeUtil.ToString(data);
string encoding = options != null ? options.GetPropertyValue("encoding") as string : null;
if (encoding == null)
encoding = "utf8";
Encoding enc = Context.GetEncoding(encoding);
if (enc == null)
throw new Exception("Unknown encoding: " + encoding);
File.WriteAllBytes(path, enc.GetBytes(str));
} else if (data is BufferInstance) {
BufferInstance buf = data as BufferInstance;
if (buf.Offset == 0 && buf.Limit == buf.Buffer.Length) {
File.WriteAllBytes(path, buf.Buffer);
} else {
ArraySegment<byte> seg = new ArraySegment<byte>(buf.Buffer, buf.Offset, buf.Limit - buf.Offset);
File.WriteAllBytes(path, seg.Array);
}
} else {
throw new Exception("illegal data");
}
if (callback != null)
Context.NextTick(callback.Bind(this, null));
} catch (Exception ex) {
System.Console.WriteLine(ex.ToString()+"\n"+ex.StackTrace);
if (callback != null)
Context.NextTick(callback.Bind(this, Engine.Error.Construct(ex.Message)));
}
}
[JSFunction(Name = "writeFileSync", IsEnumerable = true)]
public void WriteFileSync(string path, object data, [DefaultParameterValue(null)] ObjectInstance options = null) {
if (!AllowSync)
throw new JavaScriptException(Engine, "Error", "synchronous operations are not permitted");
try {
path = CheckPath(path);
if (data is string) {
string str = data as string;
string encoding = options != null ? options.GetPropertyValue("encoding") as string : null;
if (encoding == null)
encoding = "utf8";
Encoding enc = Context.GetEncoding(encoding);
if (enc == null)
throw new Exception("Unknown encoding: " + encoding);
byte[] b = enc.GetBytes(str);
File.WriteAllBytes(path, b);
} else if (data is BufferInstance) {
BufferInstance buf = data as BufferInstance;
if (buf.Offset == 0 && buf.Limit == buf.Buffer.Length) {
File.WriteAllBytes(path, buf.Buffer);
} else {
ArraySegment<byte> seg = new ArraySegment<byte>(buf.Buffer, buf.Offset, buf.Limit - buf.Offset);
File.WriteAllBytes(path, seg.Array);
}
} else {
throw new Exception("illegal data");
}
} catch (Exception ex) {
throw new JavaScriptException(Engine, "Error", ex.Message);
}
}
[JSFunction(Name = "exists", IsEnumerable = true)]
public void Exists(string path, FunctionInstance callback) {
ThreadPool.QueueUserWorkItem(new WaitCallback(ExistsAsync), new object[] { path, callback });
}
private void ExistsAsync(object arguments) {
object[] args = arguments as object[];
FunctionInstance callback = args[1] as FunctionInstance;
try {
string path = CheckPath(args[0] as string);
bool result = File.Exists(path);
if (callback != null)
Context.NextTick(callback.Bind(this, null, result));
} catch (Exception ex) {
if (callback != null)
Context.NextTick(callback.Bind(this, Engine.Error.Construct(ex.Message)));
}
}
[JSFunction(Name = "existsSync", IsEnumerable = true)]
public bool ExistsSync(string path) {
if (!AllowSync)
throw new JavaScriptException(Engine, "Error", "synchronous operations are not permitted");
try {
return File.Exists(CheckPath(path));
} catch (Exception) {
return false;
}
}
// TODO: fs.WriteStream/ReadStream
}
}