|
| 1 | +exports.debugLevel = 0; // Increase to get more verbose debug output |
| 2 | + |
| 3 | +function debugMessage (msg) { |
| 4 | + if (exports.debugLevel > 0) { |
| 5 | + node.error(__filename + ": " + msg.toString()); |
| 6 | + } |
| 7 | +} |
| 8 | + |
| 9 | +function debugObject (obj) { |
| 10 | + if (exports.debugLevel > 0) { |
| 11 | + node.error(__filename + ": " + JSON.stringify(obj)); |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +exports.File = function (filename, mode, options) { |
| 16 | + var self = this; |
| 17 | + |
| 18 | + options = options || {}; |
| 19 | + self.encoding = options.encoding || "utf8"; |
| 20 | + |
| 21 | + self.filename = filename; |
| 22 | + |
| 23 | + self.actionQueue = []; |
| 24 | + self.currentAction = null; |
| 25 | + |
| 26 | + switch (mode) { |
| 27 | + case "r": |
| 28 | + self.flags = node.O_RDONLY; |
| 29 | + break; |
| 30 | + |
| 31 | + case "r+": |
| 32 | + self.flags = node.O_RDWR; |
| 33 | + break; |
| 34 | + |
| 35 | + case "w": |
| 36 | + self.flags = node.O_CREAT | node.O_TRUNC | node.O_WRONLY; |
| 37 | + break; |
| 38 | + |
| 39 | + case "w+": |
| 40 | + self.flags = node.O_CREAT | node.O_TRUNC | node.O_RDWR; |
| 41 | + break; |
| 42 | + |
| 43 | + case "a": |
| 44 | + self.flags = node.O_APPEND | node.O_CREAT | node.O_WRONLY; |
| 45 | + break; |
| 46 | + |
| 47 | + case "a+": |
| 48 | + self.flags = node.O_APPEND | node.O_CREAT | node.O_RDWR; |
| 49 | + break; |
| 50 | + |
| 51 | + default: |
| 52 | + throw new Error("Unknown mode"); |
| 53 | + } |
| 54 | + |
| 55 | + self.open(self.filename, self.flags, 0666).addCallback(function (fd) { |
| 56 | + debugMessage(self.filename + " opened. fd = " + fd); |
| 57 | + self.fd = fd; |
| 58 | + }).addErrback(function () { |
| 59 | + self.emit("error", ["open"]); |
| 60 | + }); |
| 61 | +}; |
| 62 | + |
| 63 | +var proto = exports.File.prototype; |
| 64 | + |
| 65 | +proto._maybeDispatch = function () { |
| 66 | + var self = this; |
| 67 | + |
| 68 | + if (self.currentAction) return; |
| 69 | + self.currentAction = self.actionQueue.shift(); |
| 70 | + if (!self.currentAction) return; |
| 71 | + |
| 72 | + debugObject(self.currentAction); |
| 73 | + |
| 74 | + var args = self.currentAction.args || []; |
| 75 | + |
| 76 | + if (self.currentAction.method != "open") { |
| 77 | + args.unshift(self.fd); |
| 78 | + } |
| 79 | + |
| 80 | + var method = self.currentAction.method; |
| 81 | + |
| 82 | + if (!args[3] && (method == "read" || method == "write")) { |
| 83 | + args[3] = self.encoding; |
| 84 | + } |
| 85 | + |
| 86 | + var promise = node.fs[method].apply(self, args); |
| 87 | + |
| 88 | + var userPromise = self.currentAction.promise; |
| 89 | + |
| 90 | + promise.addCallback(function () { |
| 91 | + node.assert(self.currentAction.promise == userPromise); |
| 92 | + userPromise.emitSuccess.apply(userPromise, arguments); |
| 93 | + self.currentAction = null; |
| 94 | + self._maybeDispatch(); |
| 95 | + }).addErrback(function () { |
| 96 | + debugMessage("Error in method " + method); |
| 97 | + node.assert(self.currentAction.promise == userPromise); |
| 98 | + userPromise.emitError.apply(userPromise, arguments); |
| 99 | + self.currentAction = null; |
| 100 | + self._maybeDispatch(); |
| 101 | + }); |
| 102 | +}; |
| 103 | + |
| 104 | +proto._queueAction = function (method, args) { |
| 105 | + var userPromise = new node.Promise; |
| 106 | + this.actionQueue.push({ method: method, args: args, promise: userPromise }); |
| 107 | + this._maybeDispatch(); |
| 108 | + return userPromise; |
| 109 | +}; |
| 110 | + |
| 111 | +// FIXME the following can probably be DRY'd up with some fancy getter |
| 112 | +// stuff. |
| 113 | + |
| 114 | +proto.open = function (filename, flags, mode) { |
| 115 | + return this._queueAction("open", [filename, flags, mode]); |
| 116 | +}; |
| 117 | + |
| 118 | +proto.write = function (data, pos, encoding) { |
| 119 | + return this._queueAction("write", [data, pos, encoding]); |
| 120 | +}; |
| 121 | + |
| 122 | +proto.read = function (length, pos, encoding) { |
| 123 | + return this._queueAction("read", [length, pos, encoding]); |
| 124 | +}; |
| 125 | + |
| 126 | +proto.close = function () { |
| 127 | + return this._queueAction("close"); |
| 128 | +}; |
0 commit comments