summaryrefslogtreecommitdiffstats
path: root/toolkit/jetpack/sdk/io/fs.js
blob: 860a884a5c390febbacb513f00768c9e9a7d17b0 (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
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
/* 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/. */
"use strict";

module.metadata = {
  "stability": "experimental"
};

const { Cc, Ci, CC } = require("chrome");

const { setTimeout } = require("../timers");
const { Stream, InputStream, OutputStream } = require("./stream");
const { emit, on } = require("../event/core");
const { Buffer } = require("./buffer");
const { ns } = require("../core/namespace");
const { Class } = require("../core/heritage");


const nsILocalFile = CC("@mozilla.org/file/local;1", "nsILocalFile",
                        "initWithPath");
const FileOutputStream = CC("@mozilla.org/network/file-output-stream;1",
                            "nsIFileOutputStream", "init");
const FileInputStream = CC("@mozilla.org/network/file-input-stream;1",
                           "nsIFileInputStream", "init");
const BinaryInputStream = CC("@mozilla.org/binaryinputstream;1",
                             "nsIBinaryInputStream", "setInputStream");
const BinaryOutputStream = CC("@mozilla.org/binaryoutputstream;1",
                              "nsIBinaryOutputStream", "setOutputStream");
const StreamPump = CC("@mozilla.org/network/input-stream-pump;1",
                      "nsIInputStreamPump", "init");

const { createOutputTransport, createInputTransport } =
  Cc["@mozilla.org/network/stream-transport-service;1"].
  getService(Ci.nsIStreamTransportService);

const { OPEN_UNBUFFERED } = Ci.nsITransport;


const { REOPEN_ON_REWIND, DEFER_OPEN } = Ci.nsIFileInputStream;
const { DIRECTORY_TYPE, NORMAL_FILE_TYPE } = Ci.nsIFile;
const { NS_SEEK_SET, NS_SEEK_CUR, NS_SEEK_END } = Ci.nsISeekableStream;

const FILE_PERMISSION = 0o666;
const PR_UINT32_MAX = 0xfffffff;
// Values taken from:
// http://mxr.mozilla.org/mozilla-central/source/nsprpub/pr/include/prio.h#615
const PR_RDONLY =       0x01;
const PR_WRONLY =       0x02;
const PR_RDWR =         0x04;
const PR_CREATE_FILE =  0x08;
const PR_APPEND =       0x10;
const PR_TRUNCATE =     0x20;
const PR_SYNC =         0x40;
const PR_EXCL =         0x80;

const FLAGS = {
  "r":                  PR_RDONLY,
  "r+":                 PR_RDWR,
  "w":                  PR_CREATE_FILE | PR_TRUNCATE | PR_WRONLY,
  "w+":                 PR_CREATE_FILE | PR_TRUNCATE | PR_RDWR,
  "a":                  PR_APPEND | PR_CREATE_FILE | PR_WRONLY,
  "a+":                 PR_APPEND | PR_CREATE_FILE | PR_RDWR
};

function accessor() {
  let map = new WeakMap();
  return function(fd, value) {
    if (value === null) map.delete(fd);
    if (value !== undefined) map.set(fd, value);
    return map.get(fd);
  }
}

var nsIFile = accessor();
var nsIFileInputStream = accessor();
var nsIFileOutputStream = accessor();
var nsIBinaryInputStream = accessor();
var nsIBinaryOutputStream = accessor();

// Just a contstant object used to signal that all of the file
// needs to be read.
const ALL = new String("Read all of the file");

function isWritable(mode) {
  return !!(mode & PR_WRONLY || mode & PR_RDWR);
}
function isReadable(mode) {
  return !!(mode & PR_RDONLY || mode & PR_RDWR);
}

function isString(value) {
  return typeof(value) === "string";
}
function isFunction(value) {
  return typeof(value) === "function";
}

function toArray(enumerator) {
  let value = [];
  while(enumerator.hasMoreElements())
    value.push(enumerator.getNext())
  return value
}

function getFileName(file) {
  return file.QueryInterface(Ci.nsIFile).leafName;
}


function remove(path, recursive) {
  let fd = new nsILocalFile(path)
  if (fd.exists()) {
    fd.remove(recursive || false);
  }
  else {
    throw FSError("remove", "ENOENT", 34, path);
  }
}

/**
 * Utility function to convert either an octal number or string
 * into an octal number
 * 0777 => 0o777
 * "0644" => 0o644
 */
function Mode(mode, fallback) {
  return isString(mode) ? parseInt(mode, 8) : mode || fallback;
}
function Flags(flag) {
  return !isString(flag) ? flag :
         FLAGS[flag] || Error("Unknown file open flag: " + flag);
}


function FSError(op, code, errno, path, file, line) {
  let error = Error(code + ", " + op + " " + path, file, line);
  error.code = code;
  error.path = path;
  error.errno = errno;
  return error;
}

const ReadStream = Class({
  extends: InputStream,
  initialize: function initialize(path, options) {
    this.position = -1;
    this.length = -1;
    this.flags = "r";
    this.mode = FILE_PERMISSION;
    this.bufferSize = 64 * 1024;

    options = options || {};

    if ("flags" in options && options.flags)
      this.flags = options.flags;
    if ("bufferSize" in options && options.bufferSize)
      this.bufferSize = options.bufferSize;
    if ("length" in options && options.length)
      this.length = options.length;
    if ("position" in options && options.position !== undefined)
      this.position = options.position;

    let { flags, mode, position, length } = this;
    let fd = isString(path) ? openSync(path, flags, mode) : path;
    this.fd = fd;

    let input = nsIFileInputStream(fd);
    // Setting a stream position, unless it"s `-1` which means current position.
    if (position >= 0)
      input.QueryInterface(Ci.nsISeekableStream).seek(NS_SEEK_SET, position);
    // We use `nsIStreamTransportService` service to transform blocking
    // file input stream into a fully asynchronous stream that can be written
    // without blocking the main thread.
    let transport = createInputTransport(input, position, length, false);
    // Open an input stream on a transport. We don"t pass flags to guarantee
    // non-blocking stream semantics. Also we use defaults for segment size &
    // count.
    InputStream.prototype.initialize.call(this, {
      asyncInputStream: transport.openInputStream(null, 0, 0)
    });

    // Close file descriptor on end and destroy the stream.
    on(this, "end", _ => {
      this.destroy();
      emit(this, "close");
    });

    this.read();
  },
  destroy: function() {
    closeSync(this.fd);
    InputStream.prototype.destroy.call(this);
  }
});
exports.ReadStream = ReadStream;
exports.createReadStream = function createReadStream(path, options) {
  return new ReadStream(path, options);
};

const WriteStream = Class({
  extends: OutputStream,
  initialize: function initialize(path, options) {
    this.drainable = true;
    this.flags = "w";
    this.position = -1;
    this.mode = FILE_PERMISSION;

    options = options || {};

    if ("flags" in options && options.flags)
      this.flags = options.flags;
    if ("mode" in options && options.mode)
      this.mode = options.mode;
    if ("position" in options && options.position !== undefined)
      this.position = options.position;

    let { position, flags, mode } = this;
    // If pass was passed we create a file descriptor out of it. Otherwise
    // we just use given file descriptor.
    let fd = isString(path) ? openSync(path, flags, mode) : path;
    this.fd = fd;

    let output = nsIFileOutputStream(fd);
    // Setting a stream position, unless it"s `-1` which means current position.
    if (position >= 0)
      output.QueryInterface(Ci.nsISeekableStream).seek(NS_SEEK_SET, position);
    // We use `nsIStreamTransportService` service to transform blocking
    // file output stream into a fully asynchronous stream that can be written
    // without blocking the main thread.
    let transport = createOutputTransport(output, position, -1, false);
    // Open an output stream on a transport. We don"t pass flags to guarantee
    // non-blocking stream semantics. Also we use defaults for segment size &
    // count.
    OutputStream.prototype.initialize.call(this, {
      asyncOutputStream: transport.openOutputStream(OPEN_UNBUFFERED, 0, 0),
      output: output
    });

    // For write streams "finish" basically means close.
    on(this, "finish", _ => {
       this.destroy();
       emit(this, "close");
    });
  },
  destroy: function() {
    OutputStream.prototype.destroy.call(this);
    closeSync(this.fd);
  }
});
exports.WriteStream = WriteStream;
exports.createWriteStream = function createWriteStream(path, options) {
  return new WriteStream(path, options);
};

const Stats = Class({
  initialize: function initialize(path) {
    let file = new nsILocalFile(path);
    if (!file.exists()) throw FSError("stat", "ENOENT", 34, path);
    nsIFile(this, file);
  },
  isDirectory: function() {
    return nsIFile(this).isDirectory();
  },
  isFile: function() {
    return nsIFile(this).isFile();
  },
  isSymbolicLink: function() {
    return nsIFile(this).isSymlink();
  },
  get mode() {
    return nsIFile(this).permissions;
  },
  get size() {
    return nsIFile(this).fileSize;
  },
  get mtime() {
    return nsIFile(this).lastModifiedTime;
  },
  isBlockDevice: function() {
    return nsIFile(this).isSpecial();
  },
  isCharacterDevice: function() {
    return nsIFile(this).isSpecial();
  },
  isFIFO: function() {
    return nsIFile(this).isSpecial();
  },
  isSocket: function() {
    return nsIFile(this).isSpecial();
  },
  // non standard
  get exists() {
    return nsIFile(this).exists();
  },
  get hidden() {
    return nsIFile(this).isHidden();
  },
  get writable() {
    return nsIFile(this).isWritable();
  },
  get readable() {
    return nsIFile(this).isReadable();
  }
});
exports.Stats = Stats;

const LStats = Class({
  extends: Stats,
  get size() {
    return this.isSymbolicLink() ? nsIFile(this).fileSizeOfLink :
                                   nsIFile(this).fileSize;
  },
  get mtime() {
    return this.isSymbolicLink() ? nsIFile(this).lastModifiedTimeOfLink :
                                   nsIFile(this).lastModifiedTime;
  },
  // non standard
  get permissions() {
    return this.isSymbolicLink() ? nsIFile(this).permissionsOfLink :
                                   nsIFile(this).permissions;
  }
});

const FStat = Class({
  extends: Stats,
  initialize: function initialize(fd) {
    nsIFile(this, nsIFile(fd));
  }
});

function noop() {}
function Async(wrapped) {
  return function (path, callback) {
    let args = Array.slice(arguments);
    callback = args.pop();
    // If node is not given a callback argument
    // it just does not calls it.
    if (typeof(callback) !== "function") {
      args.push(callback);
      callback = noop;
    }
    setTimeout(function() {
      try {
        var result = wrapped.apply(this, args);
        if (result === undefined) callback(null);
        else callback(null, result);
      } catch (error) {
        callback(error);
      }
    }, 0);
  }
}


/**
 * Synchronous rename(2)
 */
function renameSync(oldPath, newPath) {
  let source = new nsILocalFile(oldPath);
  let target = new nsILocalFile(newPath);
  if (!source.exists()) throw FSError("rename", "ENOENT", 34, oldPath);
  return source.moveTo(target.parent, target.leafName);
};
exports.renameSync = renameSync;

/**
 * Asynchronous rename(2). No arguments other than a possible exception are
 * given to the completion callback.
 */
var rename = Async(renameSync);
exports.rename = rename;

/**
 * Test whether or not the given path exists by checking with the file system.
 */
function existsSync(path) {
  return new nsILocalFile(path).exists();
}
exports.existsSync = existsSync;

var exists = Async(existsSync);
exports.exists = exists;

/**
 * Synchronous ftruncate(2).
 */
function truncateSync(path, length) {
  let fd = openSync(path, "w");
  ftruncateSync(fd, length);
  closeSync(fd);
}
exports.truncateSync = truncateSync;

/**
 * Asynchronous ftruncate(2). No arguments other than a possible exception are
 * given to the completion callback.
 */
function truncate(path, length, callback) {
  open(path, "w", function(error, fd) {
    if (error) return callback(error);
    ftruncate(fd, length, function(error) {
      if (error) {
        closeSync(fd);
        callback(error);
      }
      else {
        close(fd, callback);
      }
    });
  });
}
exports.truncate = truncate;

function ftruncate(fd, length, callback) {
  write(fd, new Buffer(length), 0, length, 0, function(error) {
    callback(error);
  });
}
exports.ftruncate = ftruncate;

function ftruncateSync(fd, length = 0) {
  writeSync(fd, new Buffer(length), 0, length, 0);
}
exports.ftruncateSync = ftruncateSync;

function chownSync(path, uid, gid) {
  throw Error("Not implemented yet!!");
}
exports.chownSync = chownSync;

var chown = Async(chownSync);
exports.chown = chown;

function lchownSync(path, uid, gid) {
  throw Error("Not implemented yet!!");
}
exports.lchownSync = chownSync;

var lchown = Async(lchown);
exports.lchown = lchown;

/**
 * Synchronous chmod(2).
 */
function chmodSync (path, mode) {
  let file;
  try {
    file = new nsILocalFile(path);
  } catch(e) {
    throw FSError("chmod", "ENOENT", 34, path);
  }

  file.permissions = Mode(mode);
}
exports.chmodSync = chmodSync;
/**
 * Asynchronous chmod(2). No arguments other than a possible exception are
 * given to the completion callback.
 */
var chmod = Async(chmodSync);
exports.chmod = chmod;

/**
 * Synchronous chmod(2).
 */
function fchmodSync(fd, mode) {
  throw Error("Not implemented yet!!");
};
exports.fchmodSync = fchmodSync;
/**
 * Asynchronous chmod(2). No arguments other than a possible exception are
 * given to the completion callback.
 */
var fchmod = Async(fchmodSync);
exports.fchmod = fchmod;


/**
 * Synchronous stat(2). Returns an instance of `fs.Stats`
 */
function statSync(path) {
  return new Stats(path);
};
exports.statSync = statSync;

/**
 * Asynchronous stat(2). The callback gets two arguments (err, stats) where
 * stats is a `fs.Stats` object. It looks like this:
 */
var stat = Async(statSync);
exports.stat = stat;

/**
 * Synchronous lstat(2). Returns an instance of `fs.Stats`.
 */
function lstatSync(path) {
  return new LStats(path);
};
exports.lstatSync = lstatSync;

/**
 * Asynchronous lstat(2). The callback gets two arguments (err, stats) where
 * stats is a fs.Stats object. lstat() is identical to stat(), except that if
 * path is a symbolic link, then the link itself is stat-ed, not the file that
 * it refers to.
 */
var lstat = Async(lstatSync);
exports.lstat = lstat;

/**
 * Synchronous fstat(2). Returns an instance of `fs.Stats`.
 */
function fstatSync(fd) {
  return new FStat(fd);
};
exports.fstatSync = fstatSync;

/**
 * Asynchronous fstat(2). The callback gets two arguments (err, stats) where
 * stats is a fs.Stats object.
 */
var fstat = Async(fstatSync);
exports.fstat = fstat;

/**
 * Synchronous link(2).
 */
function linkSync(source, target) {
  throw Error("Not implemented yet!!");
};
exports.linkSync = linkSync;

/**
 * Asynchronous link(2). No arguments other than a possible exception are given
 * to the completion callback.
 */
var link = Async(linkSync);
exports.link = link;

/**
 * Synchronous symlink(2).
 */
function symlinkSync(source, target) {
  throw Error("Not implemented yet!!");
};
exports.symlinkSync = symlinkSync;

/**
 * Asynchronous symlink(2). No arguments other than a possible exception are
 * given to the completion callback.
 */
var symlink = Async(symlinkSync);
exports.symlink = symlink;

/**
 * Synchronous readlink(2). Returns the resolved path.
 */
function readlinkSync(path) {
  return new nsILocalFile(path).target;
};
exports.readlinkSync = readlinkSync;

/**
 * Asynchronous readlink(2). The callback gets two arguments
 * `(error, resolvedPath)`.
 */
var readlink = Async(readlinkSync);
exports.readlink = readlink;

/**
 * Synchronous realpath(2). Returns the resolved path.
 */
function realpathSync(path) {
  return new nsILocalFile(path).path;
};
exports.realpathSync = realpathSync;

/**
 * Asynchronous realpath(2). The callback gets two arguments
 * `(err, resolvedPath)`.
 */
var realpath = Async(realpathSync);
exports.realpath = realpath;

/**
 * Synchronous unlink(2).
 */
var unlinkSync = remove;
exports.unlinkSync = unlinkSync;

/**
 * Asynchronous unlink(2). No arguments other than a possible exception are
 * given to the completion callback.
 */
var unlink = Async(remove);
exports.unlink = unlink;

/**
 * Synchronous rmdir(2).
 */
var rmdirSync = remove;
exports.rmdirSync = rmdirSync;

/**
 * Asynchronous rmdir(2). No arguments other than a possible exception are
 * given to the completion callback.
 */
var rmdir = Async(rmdirSync);
exports.rmdir = rmdir;

/**
 * Synchronous mkdir(2).
 */
function mkdirSync(path, mode) {
  try {
    return nsILocalFile(path).create(DIRECTORY_TYPE, Mode(mode));
  } catch (error) {
    // Adjust exception thorw to match ones thrown by node.
    if (error.name === "NS_ERROR_FILE_ALREADY_EXISTS") {
      let { fileName, lineNumber } = error;
      error = FSError("mkdir", "EEXIST", 47, path, fileName, lineNumber);
    }
    throw error;
  }
};
exports.mkdirSync = mkdirSync;

/**
 * Asynchronous mkdir(2). No arguments other than a possible exception are
 * given to the completion callback.
 */
var mkdir = Async(mkdirSync);
exports.mkdir = mkdir;

/**
 * Synchronous readdir(3). Returns an array of filenames excluding `"."` and
 * `".."`.
 */
function readdirSync(path) {
  try {
    return toArray(new nsILocalFile(path).directoryEntries).map(getFileName);
  }
  catch (error) {
    // Adjust exception thorw to match ones thrown by node.
    if (error.name === "NS_ERROR_FILE_TARGET_DOES_NOT_EXIST" ||
        error.name === "NS_ERROR_FILE_NOT_FOUND")
    {
      let { fileName, lineNumber } = error;
      error = FSError("readdir", "ENOENT", 34, path, fileName, lineNumber);
    }
    throw error;
  }
};
exports.readdirSync = readdirSync;

/**
 * Asynchronous readdir(3). Reads the contents of a directory. The callback
 * gets two arguments `(error, files)` where `files` is an array of the names
 * of the files in the directory excluding `"."` and `".."`.
 */
var readdir = Async(readdirSync);
exports.readdir = readdir;

/**
 * Synchronous close(2).
 */
 function closeSync(fd) {
   let input = nsIFileInputStream(fd);
   let output = nsIFileOutputStream(fd);

   // Closing input stream and removing reference.
   if (input) input.close();
   // Closing output stream and removing reference.
   if (output) output.close();

   nsIFile(fd, null);
   nsIFileInputStream(fd, null);
   nsIFileOutputStream(fd, null);
   nsIBinaryInputStream(fd, null);
   nsIBinaryOutputStream(fd, null);
};
exports.closeSync = closeSync;
/**
 * Asynchronous close(2). No arguments other than a possible exception are
 * given to the completion callback.
 */
var close = Async(closeSync);
exports.close = close;

/**
 * Synchronous open(2).
 */
function openSync(aPath, aFlag, aMode) {
  let [ fd, flags, mode, file ] =
      [ { path: aPath }, Flags(aFlag), Mode(aMode), nsILocalFile(aPath) ];

  nsIFile(fd, file);

  // If trying to open file for just read that does not exists
  // need to throw exception as node does.
  if (!file.exists() && !isWritable(flags))
    throw FSError("open", "ENOENT", 34, aPath);

  // If we want to open file in read mode we initialize input stream.
  if (isReadable(flags)) {
    let input = FileInputStream(file, flags, mode, DEFER_OPEN);
    nsIFileInputStream(fd, input);
  }

  // If we want to open file in write mode we initialize output stream for it.
  if (isWritable(flags)) {
    let output = FileOutputStream(file, flags, mode, DEFER_OPEN);
    nsIFileOutputStream(fd, output);
  }

  return fd;
}
exports.openSync = openSync;
/**
 * Asynchronous file open. See open(2). Flags can be
 * `"r", "r+", "w", "w+", "a"`, or `"a+"`. mode defaults to `0666`.
 * The callback gets two arguments `(error, fd).
 */
var open = Async(openSync);
exports.open = open;

/**
 * Synchronous version of buffer-based fs.write(). Returns the number of bytes
 * written.
 */
function writeSync(fd, buffer, offset, length, position) {
  if (length + offset > buffer.length) {
    throw Error("Length is extends beyond buffer");
  }
  else if (length + offset !== buffer.length) {
    buffer = buffer.slice(offset, offset + length);
  }

  let output = BinaryOutputStream(nsIFileOutputStream(fd));
  nsIBinaryOutputStream(fd, output);
  // We write content as a byte array as this will avoid any transcoding
  // if content was a buffer.
  output.writeByteArray(buffer.valueOf(), buffer.length);
  output.flush();
};
exports.writeSync = writeSync;

/**
 * Write buffer to the file specified by fd.
 *
 * `offset` and `length` determine the part of the buffer to be written.
 *
 * `position` refers to the offset from the beginning of the file where this
 * data should be written. If `position` is `null`, the data will be written
 * at the current position. See pwrite(2).
 *
 * The callback will be given three arguments `(error, written, buffer)` where
 * written specifies how many bytes were written into buffer.
 *
 * Note that it is unsafe to use `fs.write` multiple times on the same file
 * without waiting for the callback.
 */
function write(fd, buffer, offset, length, position, callback) {
  if (!Buffer.isBuffer(buffer)) {
    // (fd, data, position, encoding, callback)
    let encoding = null;
    [ position, encoding, callback ] = Array.slice(arguments, 1);
    buffer = new Buffer(String(buffer), encoding);
    offset = 0;
  } else if (length + offset > buffer.length) {
    throw Error("Length is extends beyond buffer");
  } else if (length + offset !== buffer.length) {
    buffer = buffer.slice(offset, offset + length);
  }

  let writeStream = new WriteStream(fd, { position: position,
                                          length: length });
  writeStream.on("error", callback);
  writeStream.write(buffer, function onEnd() {
    writeStream.destroy();
    if (callback)
      callback(null, buffer.length, buffer);
  });
};
exports.write = write;

/**
 * Synchronous version of string-based fs.read. Returns the number of
 * bytes read.
 */
function readSync(fd, buffer, offset, length, position) {
  let input = nsIFileInputStream(fd);
  // Setting a stream position, unless it"s `-1` which means current position.
  if (position >= 0)
    input.QueryInterface(Ci.nsISeekableStream).seek(NS_SEEK_SET, position);
  // We use `nsIStreamTransportService` service to transform blocking
  // file input stream into a fully asynchronous stream that can be written
  // without blocking the main thread.
  let binaryInputStream = BinaryInputStream(input);
  let count = length === ALL ? binaryInputStream.available() : length;
  if (offset === 0) binaryInputStream.readArrayBuffer(count, buffer.buffer);
  else {
    let chunk = new Buffer(count);
    binaryInputStream.readArrayBuffer(count, chunk.buffer);
    chunk.copy(buffer, offset);
  }

  return buffer.slice(offset, offset + count);
};
exports.readSync = readSync;

/**
 * Read data from the file specified by `fd`.
 *
 * `buffer` is the buffer that the data will be written to.
 * `offset` is offset within the buffer where writing will start.
 *
 * `length` is an integer specifying the number of bytes to read.
 *
 * `position` is an integer specifying where to begin reading from in the file.
 * If `position` is `null`, data will be read from the current file position.
 *
 * The callback is given the three arguments, `(error, bytesRead, buffer)`.
 */
function read(fd, buffer, offset, length, position, callback) {
  let bytesRead = 0;
  let readStream = new ReadStream(fd, { position: position, length: length });
  readStream.on("data", function onData(data) {
      data.copy(buffer, offset + bytesRead);
      bytesRead += data.length;
  });
  readStream.on("end", function onEnd() {
    callback(null, bytesRead, buffer);
    readStream.destroy();
  });
};
exports.read = read;

/**
 * Asynchronously reads the entire contents of a file.
 * The callback is passed two arguments `(error, data)`, where data is the
 * contents of the file.
 */
function readFile(path, encoding, callback) {
  if (isFunction(encoding)) {
    callback = encoding
    encoding = null
  }

  let buffer = null;
  try {
    let readStream = new ReadStream(path);
    readStream.on("data", function(data) {
      if (!buffer) buffer = data;
      else buffer = Buffer.concat([buffer, data], 2);
    });
    readStream.on("error", function onError(error) {
      callback(error);
    });
    readStream.on("end", function onEnd() {
      // Note: Need to destroy before invoking a callback
      // so that file descriptor is released.
      readStream.destroy();
      callback(null, buffer);
    });
  }
  catch (error) {
    setTimeout(callback, 0, error);
  }
};
exports.readFile = readFile;

/**
 * Synchronous version of `fs.readFile`. Returns the contents of the path.
 * If encoding is specified then this function returns a string.
 * Otherwise it returns a buffer.
 */
function readFileSync(path, encoding) {
  let fd = openSync(path, "r");
  let size = fstatSync(fd).size;
  let buffer = new Buffer(size);
  try {
    readSync(fd, buffer, 0, ALL, 0);
  }
  finally {
    closeSync(fd);
  }
  return buffer;
};
exports.readFileSync = readFileSync;

/**
 * Asynchronously writes data to a file, replacing the file if it already
 * exists. data can be a string or a buffer.
 */
function writeFile(path, content, encoding, callback) {
  if (!isString(path))
    throw new TypeError('path must be a string');

  try {
    if (isFunction(encoding)) {
      callback = encoding
      encoding = null
    }
    if (isString(content))
      content = new Buffer(content, encoding);

    let writeStream = new WriteStream(path);
    let error = null;

    writeStream.end(content, function() {
      writeStream.destroy();
      callback(error);
    });

    writeStream.on("error", function onError(reason) {
      error = reason;
      writeStream.destroy();
    });
  } catch (error) {
    callback(error);
  }
};
exports.writeFile = writeFile;

/**
 * The synchronous version of `fs.writeFile`.
 */
function writeFileSync(filename, data, encoding) {
  // TODO: Implement this in bug 1148209 https://bugzilla.mozilla.org/show_bug.cgi?id=1148209
  throw Error("Not implemented");
};
exports.writeFileSync = writeFileSync;


function utimesSync(path, atime, mtime) {
  throw Error("Not implemented");
}
exports.utimesSync = utimesSync;

var utimes = Async(utimesSync);
exports.utimes = utimes;

function futimesSync(fd, atime, mtime, callback) {
  throw Error("Not implemented");
}
exports.futimesSync = futimesSync;

var futimes = Async(futimesSync);
exports.futimes = futimes;

function fsyncSync(fd, atime, mtime, callback) {
  throw Error("Not implemented");
}
exports.fsyncSync = fsyncSync;

var fsync = Async(fsyncSync);
exports.fsync = fsync;


/**
 * Watch for changes on filename. The callback listener will be called each
 * time the file is accessed.
 *
 * The second argument is optional. The options if provided should be an object
 * containing two members a boolean, persistent, and interval, a polling value
 * in milliseconds. The default is { persistent: true, interval: 0 }.
 */
function watchFile(path, options, listener) {
  throw Error("Not implemented");
};
exports.watchFile = watchFile;


function unwatchFile(path, listener) {
  throw Error("Not implemented");
}
exports.unwatchFile = unwatchFile;

function watch(path, options, listener) {
  throw Error("Not implemented");
}
exports.watch = watch;