Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 127 lines (95 sloc) 3.877 kB
55048cd @ry Update copyright headers
ry authored
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
8492c52 @ry Use uniform watcher names
ry authored
22 #include <node_stat_watcher.h>
8d2f9e8 @ry Add process.watchFile() process.unwatchFile()
ry authored
23
24 #include <assert.h>
8e5b91c @ry Revert "Check for strings.h"
ry authored
25 #include <string.h>
8d2f9e8 @ry Add process.watchFile() process.unwatchFile()
ry authored
26 #include <stdlib.h>
27
28 namespace node {
29
30 using namespace v8;
31
8492c52 @ry Use uniform watcher names
ry authored
32 Persistent<FunctionTemplate> StatWatcher::constructor_template;
8d2f9e8 @ry Add process.watchFile() process.unwatchFile()
ry authored
33
8492c52 @ry Use uniform watcher names
ry authored
34 void StatWatcher::Initialize(Handle<Object> target) {
8d2f9e8 @ry Add process.watchFile() process.unwatchFile()
ry authored
35 HandleScope scope;
36
8492c52 @ry Use uniform watcher names
ry authored
37 Local<FunctionTemplate> t = FunctionTemplate::New(StatWatcher::New);
8d2f9e8 @ry Add process.watchFile() process.unwatchFile()
ry authored
38 constructor_template = Persistent<FunctionTemplate>::New(t);
39 constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
5d5c8cf @ry Fix StatWatcher typo
ry authored
40 constructor_template->SetClassName(String::NewSymbol("StatWatcher"));
8d2f9e8 @ry Add process.watchFile() process.unwatchFile()
ry authored
41
8492c52 @ry Use uniform watcher names
ry authored
42 NODE_SET_PROTOTYPE_METHOD(constructor_template, "start", StatWatcher::Start);
43 NODE_SET_PROTOTYPE_METHOD(constructor_template, "stop", StatWatcher::Stop);
8d2f9e8 @ry Add process.watchFile() process.unwatchFile()
ry authored
44
5d5c8cf @ry Fix StatWatcher typo
ry authored
45 target->Set(String::NewSymbol("StatWatcher"), constructor_template->GetFunction());
8d2f9e8 @ry Add process.watchFile() process.unwatchFile()
ry authored
46 }
47
48
8492c52 @ry Use uniform watcher names
ry authored
49 void StatWatcher::Callback(EV_P_ ev_stat *watcher, int revents) {
8d2f9e8 @ry Add process.watchFile() process.unwatchFile()
ry authored
50 assert(revents == EV_STAT);
8492c52 @ry Use uniform watcher names
ry authored
51 StatWatcher *handler = static_cast<StatWatcher*>(watcher->data);
8d2f9e8 @ry Add process.watchFile() process.unwatchFile()
ry authored
52 assert(watcher == &handler->watcher_);
53 HandleScope scope;
44d5f21 @choonkeat Stat::Callback has 2 arguments for callback: current stat info and pr…
choonkeat authored
54 Handle<Value> argv[2];
55 argv[0] = Handle<Value>(BuildStatsObject(&watcher->attr));
56 argv[1] = Handle<Value>(BuildStatsObject(&watcher->prev));
0a3fc1d @ry Remove StatWatcher's dep on C++ EventEmitter
ry authored
57 MakeCallback(handler->handle_, "onchange", 2, argv);
8d2f9e8 @ry Add process.watchFile() process.unwatchFile()
ry authored
58 }
59
60
8492c52 @ry Use uniform watcher names
ry authored
61 Handle<Value> StatWatcher::New(const Arguments& args) {
55c65cc @bnoordhuis Safe constructor for ObjectWrapped classes
bnoordhuis authored
62 if (!args.IsConstructCall()) {
63 return FromConstructorTemplate(constructor_template, args);
64 }
65
8d2f9e8 @ry Add process.watchFile() process.unwatchFile()
ry authored
66 HandleScope scope;
8492c52 @ry Use uniform watcher names
ry authored
67 StatWatcher *s = new StatWatcher();
8d2f9e8 @ry Add process.watchFile() process.unwatchFile()
ry authored
68 s->Wrap(args.Holder());
69 return args.This();
70 }
71
72
8492c52 @ry Use uniform watcher names
ry authored
73 Handle<Value> StatWatcher::Start(const Arguments& args) {
8d2f9e8 @ry Add process.watchFile() process.unwatchFile()
ry authored
74 HandleScope scope;
75
76 if (args.Length() < 1 || !args[0]->IsString()) {
77 return ThrowException(Exception::TypeError(String::New("Bad arguments")));
78 }
79
8492c52 @ry Use uniform watcher names
ry authored
80 StatWatcher *handler = ObjectWrap::Unwrap<StatWatcher>(args.Holder());
8d2f9e8 @ry Add process.watchFile() process.unwatchFile()
ry authored
81 String::Utf8Value path(args[0]->ToString());
82
83 assert(handler->path_ == NULL);
84 handler->path_ = strdup(*path);
85
6de2173 @ry Add options to process.watchFile()
ry authored
86 ev_tstamp interval = 0.;
87 if (args[2]->IsInt32()) {
88 interval = NODE_V8_UNIXTIME(args[2]);
89 }
90
91 ev_stat_set(&handler->watcher_, handler->path_, interval);
8d2f9e8 @ry Add process.watchFile() process.unwatchFile()
ry authored
92 ev_stat_start(EV_DEFAULT_UC_ &handler->watcher_);
93
94 handler->persistent_ = args[1]->IsTrue();
95
96 if (!handler->persistent_) {
97 ev_unref(EV_DEFAULT_UC);
98 }
99
c1baa70 @ry Attach/Detach -> Ref/Unref
ry authored
100 handler->Ref();
8d2f9e8 @ry Add process.watchFile() process.unwatchFile()
ry authored
101
102 return Undefined();
103 }
104
105
8492c52 @ry Use uniform watcher names
ry authored
106 Handle<Value> StatWatcher::Stop(const Arguments& args) {
8d2f9e8 @ry Add process.watchFile() process.unwatchFile()
ry authored
107 HandleScope scope;
8492c52 @ry Use uniform watcher names
ry authored
108 StatWatcher *handler = ObjectWrap::Unwrap<StatWatcher>(args.Holder());
0a3fc1d @ry Remove StatWatcher's dep on C++ EventEmitter
ry authored
109 MakeCallback(handler->handle_, "onstop", 0, NULL);
8d2f9e8 @ry Add process.watchFile() process.unwatchFile()
ry authored
110 handler->Stop();
111 return Undefined();
112 }
113
114
8492c52 @ry Use uniform watcher names
ry authored
115 void StatWatcher::Stop () {
8d2f9e8 @ry Add process.watchFile() process.unwatchFile()
ry authored
116 if (watcher_.active) {
117 if (!persistent_) ev_ref(EV_DEFAULT_UC);
118 ev_stat_stop(EV_DEFAULT_UC_ &watcher_);
119 free(path_);
120 path_ = NULL;
c1baa70 @ry Attach/Detach -> Ref/Unref
ry authored
121 Unref();
8d2f9e8 @ry Add process.watchFile() process.unwatchFile()
ry authored
122 }
123 }
124
125
126 } // namespace node
Something went wrong with that request. Please try again.