1.2.1
by Jonas Smedegaard
Import upstream version 0.4.6 |
1 |
// Copyright Joyent, Inc. and other Node contributors.
|
2 |
//
|
|
3 |
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
4 |
// copy of this software and associated documentation files (the
|
|
5 |
// "Software"), to deal in the Software without restriction, including
|
|
6 |
// without limitation the rights to use, copy, modify, merge, publish,
|
|
7 |
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
8 |
// persons to whom the Software is furnished to do so, subject to the
|
|
9 |
// following conditions:
|
|
10 |
//
|
|
11 |
// The above copyright notice and this permission notice shall be included
|
|
12 |
// in all copies or substantial portions of the Software.
|
|
13 |
//
|
|
14 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
15 |
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16 |
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
17 |
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
18 |
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
19 |
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
20 |
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
21 |
||
1.1.13
by Jonas Smedegaard
Import upstream version 0.10.0~dfsg1 |
22 |
#include "node_stat_watcher.h" |
1.1.1
by Jérémy Lal
Import upstream version 0.1.33 |
23 |
|
24 |
#include <assert.h> |
|
25 |
#include <string.h> |
|
26 |
#include <stdlib.h> |
|
27 |
||
28 |
namespace node { |
|
29 |
||
30 |
using namespace v8; |
|
31 |
||
32 |
Persistent<FunctionTemplate> StatWatcher::constructor_template; |
|
1.1.13
by Jonas Smedegaard
Import upstream version 0.10.0~dfsg1 |
33 |
static Persistent<String> onchange_sym; |
34 |
static Persistent<String> onstop_sym; |
|
35 |
||
1.1.1
by Jérémy Lal
Import upstream version 0.1.33 |
36 |
|
37 |
void StatWatcher::Initialize(Handle<Object> target) { |
|
38 |
HandleScope scope; |
|
39 |
||
40 |
Local<FunctionTemplate> t = FunctionTemplate::New(StatWatcher::New); |
|
41 |
constructor_template = Persistent<FunctionTemplate>::New(t); |
|
42 |
constructor_template->InstanceTemplate()->SetInternalFieldCount(1); |
|
43 |
constructor_template->SetClassName(String::NewSymbol("StatWatcher")); |
|
44 |
||
45 |
NODE_SET_PROTOTYPE_METHOD(constructor_template, "start", StatWatcher::Start); |
|
46 |
NODE_SET_PROTOTYPE_METHOD(constructor_template, "stop", StatWatcher::Stop); |
|
47 |
||
48 |
target->Set(String::NewSymbol("StatWatcher"), constructor_template->GetFunction()); |
|
49 |
}
|
|
50 |
||
51 |
||
1.1.13
by Jonas Smedegaard
Import upstream version 0.10.0~dfsg1 |
52 |
static void Delete(uv_handle_t* handle) { |
53 |
delete reinterpret_cast<uv_fs_poll_t*>(handle); |
|
54 |
}
|
|
55 |
||
56 |
||
57 |
StatWatcher::StatWatcher() |
|
58 |
: ObjectWrap() |
|
59 |
, watcher_(new uv_fs_poll_t) |
|
60 |
{
|
|
61 |
uv_fs_poll_init(uv_default_loop(), watcher_); |
|
62 |
watcher_->data = static_cast<void*>(this); |
|
63 |
}
|
|
64 |
||
65 |
||
66 |
StatWatcher::~StatWatcher() { |
|
67 |
Stop(); |
|
68 |
uv_close(reinterpret_cast<uv_handle_t*>(watcher_), Delete); |
|
69 |
}
|
|
70 |
||
71 |
||
72 |
void StatWatcher::Callback(uv_fs_poll_t* handle, |
|
73 |
int status, |
|
74 |
const uv_statbuf_t* prev, |
|
75 |
const uv_statbuf_t* curr) { |
|
76 |
StatWatcher* wrap = static_cast<StatWatcher*>(handle->data); |
|
77 |
assert(wrap->watcher_ == handle); |
|
1.1.1
by Jérémy Lal
Import upstream version 0.1.33 |
78 |
HandleScope scope; |
1.1.13
by Jonas Smedegaard
Import upstream version 0.10.0~dfsg1 |
79 |
Local<Value> argv[3]; |
80 |
argv[0] = BuildStatsObject(curr); |
|
81 |
argv[1] = BuildStatsObject(prev); |
|
82 |
argv[2] = Integer::New(status); |
|
83 |
if (status == -1) { |
|
84 |
SetErrno(uv_last_error(wrap->watcher_->loop)); |
|
85 |
}
|
|
86 |
if (onchange_sym.IsEmpty()) { |
|
87 |
onchange_sym = NODE_PSYMBOL("onchange"); |
|
88 |
}
|
|
89 |
MakeCallback(wrap->handle_, onchange_sym, ARRAY_SIZE(argv), argv); |
|
1.1.1
by Jérémy Lal
Import upstream version 0.1.33 |
90 |
}
|
91 |
||
92 |
||
93 |
Handle<Value> StatWatcher::New(const Arguments& args) { |
|
1.1.13
by Jonas Smedegaard
Import upstream version 0.10.0~dfsg1 |
94 |
assert(args.IsConstructCall()); |
1.1.1
by Jérémy Lal
Import upstream version 0.1.33 |
95 |
HandleScope scope; |
1.1.13
by Jonas Smedegaard
Import upstream version 0.10.0~dfsg1 |
96 |
StatWatcher* s = new StatWatcher(); |
1.1.1
by Jérémy Lal
Import upstream version 0.1.33 |
97 |
s->Wrap(args.Holder()); |
98 |
return args.This(); |
|
99 |
}
|
|
100 |
||
101 |
||
102 |
Handle<Value> StatWatcher::Start(const Arguments& args) { |
|
1.1.13
by Jonas Smedegaard
Import upstream version 0.10.0~dfsg1 |
103 |
assert(args.Length() == 3); |
1.1.1
by Jérémy Lal
Import upstream version 0.1.33 |
104 |
HandleScope scope; |
105 |
||
1.1.13
by Jonas Smedegaard
Import upstream version 0.10.0~dfsg1 |
106 |
StatWatcher* wrap = ObjectWrap::Unwrap<StatWatcher>(args.Holder()); |
107 |
String::Utf8Value path(args[0]); |
|
108 |
const bool persistent = args[1]->BooleanValue(); |
|
109 |
const uint32_t interval = args[2]->Uint32Value(); |
|
110 |
||
111 |
if (!persistent) uv_unref(reinterpret_cast<uv_handle_t*>(wrap->watcher_)); |
|
112 |
uv_fs_poll_start(wrap->watcher_, Callback, *path, interval); |
|
113 |
wrap->Ref(); |
|
1.1.1
by Jérémy Lal
Import upstream version 0.1.33 |
114 |
|
115 |
return Undefined(); |
|
116 |
}
|
|
117 |
||
118 |
||
119 |
Handle<Value> StatWatcher::Stop(const Arguments& args) { |
|
120 |
HandleScope scope; |
|
1.1.13
by Jonas Smedegaard
Import upstream version 0.10.0~dfsg1 |
121 |
StatWatcher* wrap = ObjectWrap::Unwrap<StatWatcher>(args.Holder()); |
122 |
if (onstop_sym.IsEmpty()) { |
|
123 |
onstop_sym = NODE_PSYMBOL("onstop"); |
|
124 |
}
|
|
125 |
MakeCallback(wrap->handle_, onstop_sym, 0, NULL); |
|
126 |
wrap->Stop(); |
|
1.1.1
by Jérémy Lal
Import upstream version 0.1.33 |
127 |
return Undefined(); |
128 |
}
|
|
129 |
||
130 |
||
131 |
void StatWatcher::Stop () { |
|
1.1.13
by Jonas Smedegaard
Import upstream version 0.10.0~dfsg1 |
132 |
if (!uv_is_active(reinterpret_cast<uv_handle_t*>(watcher_))) return; |
133 |
uv_fs_poll_stop(watcher_); |
|
134 |
Unref(); |
|
1.1.1
by Jérémy Lal
Import upstream version 0.1.33 |
135 |
}
|
136 |
||
137 |
||
138 |
} // namespace node |