The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / src / file.js @ 90fc8d36

History | View | Annotate | Download (3.48 KB)

1
File.rename = function (file1, file2, callback) {
2
  this._addAction("rename", [file1, file2], callback);
3
};
4

    
5
File.stat = function (path, callback) {
6
  this._addAction("stat", [path], callback);
7
};
8

    
9
File.exists = function (path, callback) {
10
  this._addAction("stat", [path], function (status) {
11
      callback(status == 0);
12
  });
13
}
14

    
15
File.cat = function (path, callback) {
16
  var content = "";
17
  var file = new File;
18
  var pos = 0;
19
  var chunkSize = 10*1024;
20

    
21
  function readChunk () {
22
    file.read(chunkSize, pos, function (status, chunk) {
23
      if (chunk) {
24
        content += chunk.encodeUtf8();
25
        pos += chunk.length;
26
        readChunk();
27
      } else {
28
        callback(0, content);
29
        file.close();
30
      }
31
    });
32
  }
33

    
34
  file.open(path, "r", function (status) {
35
    if (status == 0) 
36
      readChunk();
37
    else
38
      callback (status);
39
  });
40
}
41

    
42
File.prototype.puts = function (data, callback) {
43
  this.write(data + "\n", -1, callback);
44
};
45

    
46
File.prototype.print = function (data, callback) {
47
  this.write(data, -1, callback);
48
};
49

    
50
File.prototype.open = function (path, mode, callback) {
51
  this._addAction("open", [path, mode], callback);
52
};
53

    
54
File.prototype.close = function (callback) {
55
  this._addAction("close", [], callback);
56
};
57

    
58
File.prototype.write = function (buf, pos, callback) {
59
  this._addAction("write", [buf, pos], callback);
60
};
61

    
62
File.prototype.read = function (length, pos, callback) {
63
  this._addAction("read", [length, pos], callback);
64
};
65

    
66
// Some explanation of the File binding.
67
//
68
// All file operations are blocking. To get around this they are executed
69
// in a thread pool in C++ (libeio). 
70
//
71
// The ordering of method calls to a file should be preserved, so they are
72
// only executed one at a time. A queue, called _actionQueue is employed. 
73
//
74
// The constructor File() is implemented in C++. It initlizes 
75
// the member _actionQueue = []
76
//
77
// Any of the methods called on a file are put into this queue. When they
78
// reach the head of the queue they will be executed. C++ calles the
79
// method _pollActions each time it becomes idle. If there is no action
80
// currently being executed then _pollActions will not be called. Thus when
81
// actions are added to an empty _actionQueue, they should be immediately
82
// executed.
83
//
84
// When an action has completed, the C++ side is going to look at the first
85
// element of _actionQueue in order to get a handle on the callback
86
// function. Only after that completion callback has been made can the
87
// action be shifted out of the queue.
88
// 
89
// See File::CallTopCallback() in file.cc to see the other side of the
90
// binding.
91
File._addAction = File.prototype._addAction = function (method, args, callback) {
92
  this._actionQueue.push({ method: method 
93
                         , callback: callback
94
                         , args: args
95
                         });
96
  if (this._actionQueue.length == 1) this._act();
97
}
98

    
99
File._act = File.prototype._act = function () {
100
  var action = this._actionQueue[0];
101
  if (action)
102
    // TODO FIXME what if the action throws an error?
103
    this["_ffi_" + action.method].apply(this, action.args);
104
};
105

    
106
// called from C++ after each action finishes
107
// (i.e. when it returns from the thread pool)
108
File._pollActions = File.prototype._pollActions = function () {
109
  this._actionQueue.shift();
110
  this._act();
111
};
112

    
113
var stdout = new File();
114
stdout.fd = File.STDOUT_FILENO;
115

    
116
var stderr = new File();
117
stderr.fd = File.STDERR_FILENO;
118

    
119
var stdin = new File();
120
stdin.fd = File.STDIN_FILENO;
121

    
122
this.puts = function (data, callback) {
123
  stdout.puts(data, callback);
124
}