summaryrefslogtreecommitdiff
path: root/lib/file.js
blob: 9844f97b53c835a434b6aae100f38c1cca357fa4 (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
exports.debugLevel = 0; // Increase to get more verbose debug output

function debugMessage (msg) {
  if (exports.debugLevel > 0) {
    node.error(__filename + ": " + msg.toString());
  }
}

function debugObject (obj) {
  if (exports.debugLevel > 0) {
    node.error(__filename + ": " + JSON.stringify(obj));
  }
}

exports.read = node.fs.cat;

exports.write = function (filename, data, encoding) {
  var promise = new node.Promise();

  encoding = encoding || "utf8"; // default to utf8

  node.fs.open(filename, node.O_WRONLY | node.O_TRUNC | node.O_CREAT, 0666)
    .addCallback(function (fd) {
      function doWrite (_data) {
        node.fs.write(fd, _data, 0, encoding)
          .addErrback(function () {
            node.fs.close(fd);
          })
          .addCallback(function (written) {
            if (written == _data.length) {
              node.fs.close(fd);
            } else {
              doWrite(_data.slice(written));
            }
          });
      }
      doWrite(data);
    })
    .addErrback(function () {
      promise.emitError()
    });

  return promise;
};

exports.File = function (filename, mode, options) {
  var self = this;

  options = options || {};
  self.encoding = options.encoding || "utf8";

  self.filename = filename;

  self.actionQueue = [];
  self.currentAction = null;

  switch (mode) {
    case "r":
      self.flags = node.O_RDONLY;
      break;

    case "r+":
      self.flags = node.O_RDWR;
      break;

    case "w":
      self.flags = node.O_CREAT | node.O_TRUNC | node.O_WRONLY;
      break;

    case "w+":
      self.flags = node.O_CREAT | node.O_TRUNC | node.O_RDWR;
      break;

    case "a":
      self.flags = node.O_APPEND | node.O_CREAT | node.O_WRONLY; 
      break;

    case "a+":
      self.flags = node.O_APPEND | node.O_CREAT | node.O_RDWR; 
      break;

    default:
      throw new Error("Unknown mode");
  }

  self.open(self.filename, self.flags, 0666).addCallback(function (fd) {
    debugMessage(self.filename + " opened. fd = " + fd);
    self.fd = fd;
  }).addErrback(function () {
    self.emit("error", ["open"]);
  });
};

var proto = exports.File.prototype;

proto._maybeDispatch = function () {
  var self = this;

  if (self.currentAction) return;
  self.currentAction = self.actionQueue.shift();
  if (!self.currentAction) return;

  debugObject(self.currentAction);

  var args = self.currentAction.args || [];

  if (self.currentAction.method != "open") {
    args.unshift(self.fd);
  }

  var method = self.currentAction.method;

  if (!args[3] && (method == "read" || method == "write")) {
    args[3] = self.encoding;
  }

  var promise = node.fs[method].apply(self, args);

  var userPromise = self.currentAction.promise;

  promise.addCallback(function () {
    node.assert(self.currentAction.promise == userPromise);
    userPromise.emitSuccess.apply(userPromise, arguments);
    self.currentAction = null;
    self._maybeDispatch();
  }).addErrback(function () {
    debugMessage("Error in method " + method);
    node.assert(self.currentAction.promise == userPromise);
    userPromise.emitError.apply(userPromise, arguments);
    self.currentAction = null;
    self._maybeDispatch();
  });
};

proto._queueAction = function (method, args) {
  var userPromise = new node.Promise;
  this.actionQueue.push({ method: method, args: args, promise: userPromise });
  this._maybeDispatch();
  return userPromise;
};

// FIXME the following can probably be DRY'd up with some fancy getter
// stuff.

proto.open = function (filename, flags, mode) {
  return this._queueAction("open", [filename, flags, mode]);
};

proto.write = function (data, pos, encoding) {
  return this._queueAction("write", [data, pos, encoding]);
};

proto.read = function (length, pos, encoding) {
  return this._queueAction("read", [length, pos, encoding]);
};

proto.close = function () {
  return this._queueAction("close");
};