Skip to content

Commit ace2be8

Browse files
pquernary
authored andcommitted
Move the Stat structure functions to node_file.cc
from node.cc, so we can convert fs to a module.
1 parent 82daa46 commit ace2be8

4 files changed

Lines changed: 98 additions & 92 deletions

File tree

src/node.cc

Lines changed: 0 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,6 @@ static Persistent<String> errno_symbol;
5959
static Persistent<String> syscall_symbol;
6060
static Persistent<String> errpath_symbol;
6161

62-
static Persistent<String> dev_symbol;
63-
static Persistent<String> ino_symbol;
64-
static Persistent<String> mode_symbol;
65-
static Persistent<String> nlink_symbol;
66-
static Persistent<String> uid_symbol;
67-
static Persistent<String> gid_symbol;
68-
static Persistent<String> rdev_symbol;
69-
static Persistent<String> size_symbol;
70-
static Persistent<String> blksize_symbol;
71-
static Persistent<String> blocks_symbol;
72-
static Persistent<String> atime_symbol;
73-
static Persistent<String> mtime_symbol;
74-
static Persistent<String> ctime_symbol;
75-
7662
static Persistent<String> rss_symbol;
7763
static Persistent<String> vsize_symbol;
7864
static Persistent<String> heap_total_symbol;
@@ -907,73 +893,6 @@ ssize_t DecodeWrite(char *buf,
907893
return buflen;
908894
}
909895

910-
static Persistent<FunctionTemplate> stats_constructor_template;
911-
912-
Local<Object> BuildStatsObject(struct stat * s) {
913-
HandleScope scope;
914-
915-
if (dev_symbol.IsEmpty()) {
916-
dev_symbol = NODE_PSYMBOL("dev");
917-
ino_symbol = NODE_PSYMBOL("ino");
918-
mode_symbol = NODE_PSYMBOL("mode");
919-
nlink_symbol = NODE_PSYMBOL("nlink");
920-
uid_symbol = NODE_PSYMBOL("uid");
921-
gid_symbol = NODE_PSYMBOL("gid");
922-
rdev_symbol = NODE_PSYMBOL("rdev");
923-
size_symbol = NODE_PSYMBOL("size");
924-
blksize_symbol = NODE_PSYMBOL("blksize");
925-
blocks_symbol = NODE_PSYMBOL("blocks");
926-
atime_symbol = NODE_PSYMBOL("atime");
927-
mtime_symbol = NODE_PSYMBOL("mtime");
928-
ctime_symbol = NODE_PSYMBOL("ctime");
929-
}
930-
931-
Local<Object> stats =
932-
stats_constructor_template->GetFunction()->NewInstance();
933-
934-
/* ID of device containing file */
935-
stats->Set(dev_symbol, Integer::New(s->st_dev));
936-
937-
/* inode number */
938-
stats->Set(ino_symbol, Integer::New(s->st_ino));
939-
940-
/* protection */
941-
stats->Set(mode_symbol, Integer::New(s->st_mode));
942-
943-
/* number of hard links */
944-
stats->Set(nlink_symbol, Integer::New(s->st_nlink));
945-
946-
/* user ID of owner */
947-
stats->Set(uid_symbol, Integer::New(s->st_uid));
948-
949-
/* group ID of owner */
950-
stats->Set(gid_symbol, Integer::New(s->st_gid));
951-
952-
/* device ID (if special file) */
953-
stats->Set(rdev_symbol, Integer::New(s->st_rdev));
954-
955-
/* total size, in bytes */
956-
stats->Set(size_symbol, Number::New(s->st_size));
957-
958-
/* blocksize for filesystem I/O */
959-
stats->Set(blksize_symbol, Integer::New(s->st_blksize));
960-
961-
/* number of blocks allocated */
962-
stats->Set(blocks_symbol, Integer::New(s->st_blocks));
963-
964-
/* time of last access */
965-
stats->Set(atime_symbol, NODE_UNIXTIME_V8(s->st_atime));
966-
967-
/* time of last modification */
968-
stats->Set(mtime_symbol, NODE_UNIXTIME_V8(s->st_mtime));
969-
970-
/* time of last status change */
971-
stats->Set(ctime_symbol, NODE_UNIXTIME_V8(s->st_ctime));
972-
973-
return scope.Close(stats);
974-
}
975-
976-
977896
// Extracts a C str from a V8 Utf8Value.
978897
const char* ToCString(const v8::String::Utf8Value& value) {
979898
return *value ? *value : "<str conversion failed>";
@@ -1586,17 +1505,6 @@ static Handle<Value> Binding(const Arguments& args) {
15861505
exports = Object::New();
15871506
modp->register_func(exports);
15881507
binding_cache->Set(module, exports);
1589-
} else if (!strcmp(*module_v, "fs")) {
1590-
exports = Object::New();
1591-
1592-
// Initialize the stats object
1593-
Local<FunctionTemplate> stat_templ = FunctionTemplate::New();
1594-
stats_constructor_template = Persistent<FunctionTemplate>::New(stat_templ);
1595-
exports->Set(String::NewSymbol("Stats"),
1596-
stats_constructor_template->GetFunction());
1597-
StatWatcher::Initialize(exports);
1598-
File::Initialize(exports);
1599-
binding_cache->Set(module, exports);
16001508
} else if (!strcmp(*module_v, "evals")) {
16011509
exports = Object::New();
16021510
node::Context::Initialize(exports);

src/node_extensions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ NODE_EXT_LIST_ITEM(node_child_process)
66
#ifdef HAVE_OPENSSL
77
NODE_EXT_LIST_ITEM(node_crypto)
88
#endif
9+
NODE_EXT_LIST_ITEM(node_fs)
910
NODE_EXT_LIST_ITEM(node_net)
1011
NODE_EXT_LIST_ITEM(node_http_parser)
1112
NODE_EXT_LIST_ITEM(node_signal_watcher)

src/node_file.cc

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <node.h>
33
#include <node_file.h>
44
#include <node_buffer.h>
5+
#include <node_stat_watcher.h>
56

67
#include <sys/types.h>
78
#include <sys/stat.h>
@@ -188,6 +189,87 @@ static Handle<Value> Close(const Arguments& args) {
188189
}
189190
}
190191

192+
193+
static Persistent<FunctionTemplate> stats_constructor_template;
194+
195+
static Persistent<String> dev_symbol;
196+
static Persistent<String> ino_symbol;
197+
static Persistent<String> mode_symbol;
198+
static Persistent<String> nlink_symbol;
199+
static Persistent<String> uid_symbol;
200+
static Persistent<String> gid_symbol;
201+
static Persistent<String> rdev_symbol;
202+
static Persistent<String> size_symbol;
203+
static Persistent<String> blksize_symbol;
204+
static Persistent<String> blocks_symbol;
205+
static Persistent<String> atime_symbol;
206+
static Persistent<String> mtime_symbol;
207+
static Persistent<String> ctime_symbol;
208+
209+
Local<Object> BuildStatsObject(struct stat * s) {
210+
HandleScope scope;
211+
212+
if (dev_symbol.IsEmpty()) {
213+
dev_symbol = NODE_PSYMBOL("dev");
214+
ino_symbol = NODE_PSYMBOL("ino");
215+
mode_symbol = NODE_PSYMBOL("mode");
216+
nlink_symbol = NODE_PSYMBOL("nlink");
217+
uid_symbol = NODE_PSYMBOL("uid");
218+
gid_symbol = NODE_PSYMBOL("gid");
219+
rdev_symbol = NODE_PSYMBOL("rdev");
220+
size_symbol = NODE_PSYMBOL("size");
221+
blksize_symbol = NODE_PSYMBOL("blksize");
222+
blocks_symbol = NODE_PSYMBOL("blocks");
223+
atime_symbol = NODE_PSYMBOL("atime");
224+
mtime_symbol = NODE_PSYMBOL("mtime");
225+
ctime_symbol = NODE_PSYMBOL("ctime");
226+
}
227+
228+
Local<Object> stats =
229+
stats_constructor_template->GetFunction()->NewInstance();
230+
231+
/* ID of device containing file */
232+
stats->Set(dev_symbol, Integer::New(s->st_dev));
233+
234+
/* inode number */
235+
stats->Set(ino_symbol, Integer::New(s->st_ino));
236+
237+
/* protection */
238+
stats->Set(mode_symbol, Integer::New(s->st_mode));
239+
240+
/* number of hard links */
241+
stats->Set(nlink_symbol, Integer::New(s->st_nlink));
242+
243+
/* user ID of owner */
244+
stats->Set(uid_symbol, Integer::New(s->st_uid));
245+
246+
/* group ID of owner */
247+
stats->Set(gid_symbol, Integer::New(s->st_gid));
248+
249+
/* device ID (if special file) */
250+
stats->Set(rdev_symbol, Integer::New(s->st_rdev));
251+
252+
/* total size, in bytes */
253+
stats->Set(size_symbol, Number::New(s->st_size));
254+
255+
/* blocksize for filesystem I/O */
256+
stats->Set(blksize_symbol, Integer::New(s->st_blksize));
257+
258+
/* number of blocks allocated */
259+
stats->Set(blocks_symbol, Integer::New(s->st_blocks));
260+
261+
/* time of last access */
262+
stats->Set(atime_symbol, NODE_UNIXTIME_V8(s->st_atime));
263+
264+
/* time of last modification */
265+
stats->Set(mtime_symbol, NODE_UNIXTIME_V8(s->st_mtime));
266+
267+
/* time of last status change */
268+
stats->Set(ctime_symbol, NODE_UNIXTIME_V8(s->st_ctime));
269+
270+
return scope.Close(stats);
271+
}
272+
191273
static Handle<Value> Stat(const Arguments& args) {
192274
HandleScope scope;
193275

@@ -719,4 +801,17 @@ void File::Initialize(Handle<Object> target) {
719801
encoding_symbol = NODE_PSYMBOL("node:encoding");
720802
}
721803

804+
void InitFs(Handle<Object> target) {
805+
HandleScope scope;
806+
// Initialize the stats object
807+
Local<FunctionTemplate> stat_templ = FunctionTemplate::New();
808+
stats_constructor_template = Persistent<FunctionTemplate>::New(stat_templ);
809+
target->Set(String::NewSymbol("Stats"),
810+
stats_constructor_template->GetFunction());
811+
StatWatcher::Initialize(target);
812+
File::Initialize(target);
813+
}
814+
722815
} // end namespace node
816+
817+
NODE_MODULE(node_fs, node::InitFs);

src/node_file.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ class File {
1313
static void Initialize(v8::Handle<v8::Object> target);
1414
};
1515

16+
void InitFs(v8::Handle<v8::Object> target);
17+
1618
} // namespace node
1719
#endif // SRC_FILE_H_

0 commit comments

Comments
 (0)