forked from nodejs/node-v0.x-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_stat_watcher.cc
More file actions
86 lines (60 loc) · 1.96 KB
/
node_stat_watcher.cc
File metadata and controls
86 lines (60 loc) · 1.96 KB
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
// Copyright 2009 Ryan Dahl <ry@tinyclouds.org>
#include <node_stat_watcher.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
namespace node {
using namespace v8;
void StatWatcher::Initialize(Handle<Object> target) {
HandleScope scope;
Local<FunctionTemplate> t = BuildTemplate<StatWatcher>("StatWatcher");
NODE_SET_PROTOTYPE_METHOD(t, "start", Start);
NODE_SET_PROTOTYPE_METHOD(t, "stop", Stop);
target->Set(String::NewSymbol("StatWatcher"), t->GetFunction());
}
void StatWatcher::Callback(EV_P_ ev_stat *watcher, int revents) {
assert(revents == EV_STAT);
StatWatcher *w = static_cast<StatWatcher*>(watcher->data);
assert(watcher == &w->watcher_);
HandleScope scope;
Handle<Value> argv[2];
argv[0] = Handle<Value>(BuildStatsObject(&watcher->attr));
argv[1] = Handle<Value>(BuildStatsObject(&watcher->prev));
w->MakeCallback(2, argv);
}
Handle<Value> StatWatcher::Start(const Arguments& args) {
HandleScope scope;
if (args.Length() < 1 || !args[0]->IsString()) {
return ThrowException(Exception::TypeError(String::New("Bad arguments")));
}
StatWatcher *w = ObjectWrap::Unwrap<StatWatcher>(args.Holder());
String::Utf8Value path(args[0]->ToString());
assert(w->path_ == NULL);
w->path_ = strdup(*path);
w->persistent_ = args[1]->IsTrue();
ev_tstamp interval = 0.;
if (args[2]->IsInt32()) {
interval = NODE_V8_UNIXTIME(args[2]);
}
ev_stat_set(&w->watcher_, w->path_, interval);
ev_stat_start(EV_DEFAULT_UC_ &w->watcher_);
w->Active();
if (!w->persistent_) ev_unref(EV_DEFAULT_UC);
return Undefined();
}
Handle<Value> StatWatcher::Stop(const Arguments& args) {
HandleScope scope;
StatWatcher *w = ObjectWrap::Unwrap<StatWatcher>(args.Holder());
w->Stop();
return Undefined();
}
void StatWatcher::Stop () {
if (watcher_.active) {
if (!persistent_) ev_ref(EV_DEFAULT_UC);
ev_stat_stop(EV_DEFAULT_UC_ &watcher_);
Inactive();
free(path_);
path_ = NULL;
}
}
} // namespace node