Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 194 lines (146 sloc) 5.928 kb
60818b9 @ry Add missing 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
ff4a9d3 @bnoordhuis core: use proper #include directives
bnoordhuis authored
22 #include "node.h"
23 #include "handle_wrap.h"
8fe5712 fs watcher binding
Igor Zinkovsky authored
24
25 #include <stdlib.h>
26
27 using namespace v8;
28
29 namespace node {
30
a26bee8 @isaacs MakeCallback: Consistent symbol usage
isaacs authored
31 static Persistent<String> onchange_sym;
32
8fe5712 fs watcher binding
Igor Zinkovsky authored
33 #define UNWRAP \
34 assert(!args.Holder().IsEmpty()); \
35 assert(args.Holder()->InternalFieldCount() > 0); \
36 FSEventWrap* wrap = \
37 static_cast<FSEventWrap*>(args.Holder()->GetPointerFromInternalField(0)); \
38 if (!wrap) { \
6cc4292 @ry Display sys_errno when UV_UNKNOWN is returned
ry authored
39 uv_err_t err; \
40 err.code = UV_EBADF; \
41 SetErrno(err); \
8fe5712 fs watcher binding
Igor Zinkovsky authored
42 return scope.Close(Integer::New(-1)); \
43 }
44
45 class FSEventWrap: public HandleWrap {
46 public:
47 static void Initialize(Handle<Object> target);
48 static Handle<Value> New(const Arguments& args);
49 static Handle<Value> Start(const Arguments& args);
8dd4fcb @bnoordhuis fs: don't close uninitialized fs.watch handle
bnoordhuis authored
50 static Handle<Value> Close(const Arguments& args);
8fe5712 fs watcher binding
Igor Zinkovsky authored
51
52 private:
53 FSEventWrap(Handle<Object> object);
54 virtual ~FSEventWrap();
55
56 static void OnEvent(uv_fs_event_t* handle, const char* filename, int events,
57 int status);
58
59 uv_fs_event_t handle_;
8dd4fcb @bnoordhuis fs: don't close uninitialized fs.watch handle
bnoordhuis authored
60 bool initialized_;
8fe5712 fs watcher binding
Igor Zinkovsky authored
61 };
62
63
64 FSEventWrap::FSEventWrap(Handle<Object> object): HandleWrap(object,
65 (uv_handle_t*)&handle_) {
66 handle_.data = reinterpret_cast<void*>(this);
8dd4fcb @bnoordhuis fs: don't close uninitialized fs.watch handle
bnoordhuis authored
67 initialized_ = false;
8fe5712 fs watcher binding
Igor Zinkovsky authored
68 }
69
70
71 FSEventWrap::~FSEventWrap() {
8dd4fcb @bnoordhuis fs: don't close uninitialized fs.watch handle
bnoordhuis authored
72 assert(initialized_ == false);
8fe5712 fs watcher binding
Igor Zinkovsky authored
73 }
74
75
76 void FSEventWrap::Initialize(Handle<Object> target) {
77 HandleWrap::Initialize(target);
78
79 HandleScope scope;
80
81 Local<FunctionTemplate> t = FunctionTemplate::New(New);
82 t->InstanceTemplate()->SetInternalFieldCount(1);
83 t->SetClassName(String::NewSymbol("FSEvent"));
84
85 NODE_SET_PROTOTYPE_METHOD(t, "start", Start);
86 NODE_SET_PROTOTYPE_METHOD(t, "close", Close);
87
88 target->Set(String::NewSymbol("FSEvent"),
89 Persistent<FunctionTemplate>::New(t)->GetFunction());
90 }
91
92
93 Handle<Value> FSEventWrap::New(const Arguments& args) {
94 HandleScope scope;
95
96 assert(args.IsConstructCall());
97 new FSEventWrap(args.This());
98
99 return scope.Close(args.This());
100 }
101
102
103 Handle<Value> FSEventWrap::Start(const Arguments& args) {
104 HandleScope scope;
105
106 UNWRAP
107
108 if (args.Length() < 1 || !args[0]->IsString()) {
109 return ThrowException(Exception::TypeError(String::New("Bad arguments")));
110 }
111
249c3c1 Avoiding unnecessary ToString() calls
ssuda authored
112 String::Utf8Value path(args[0]);
8fe5712 fs watcher binding
Igor Zinkovsky authored
113
74a8215 @bnoordhuis Revert support for isolates.
bnoordhuis authored
114 int r = uv_fs_event_init(uv_default_loop(), &wrap->handle_, *path, OnEvent, 0);
8fe5712 fs watcher binding
Igor Zinkovsky authored
115 if (r == 0) {
116 // Check for persistent argument
117 if (!args[1]->IsTrue()) {
74a8215 @bnoordhuis Revert support for isolates.
bnoordhuis authored
118 uv_unref(uv_default_loop());
8fe5712 fs watcher binding
Igor Zinkovsky authored
119 }
8dd4fcb @bnoordhuis fs: don't close uninitialized fs.watch handle
bnoordhuis authored
120 wrap->initialized_ = true;
189dd8f @piscisaureus Fix line endings and trailing whitespace
piscisaureus authored
121 } else {
74a8215 @bnoordhuis Revert support for isolates.
bnoordhuis authored
122 SetErrno(uv_last_error(uv_default_loop()));
8fe5712 fs watcher binding
Igor Zinkovsky authored
123 }
124
125 return scope.Close(Integer::New(r));
126 }
127
128
129 void FSEventWrap::OnEvent(uv_fs_event_t* handle, const char* filename,
130 int events, int status) {
131 HandleScope scope;
132 Local<String> eventStr;
133
134 FSEventWrap* wrap = reinterpret_cast<FSEventWrap*>(handle->data);
135
136 assert(wrap->object_.IsEmpty() == false);
137
22c2c34 @bnoordhuis fs: fix fs.watch() segmentation fault
bnoordhuis authored
138 // We're in a bind here. libuv can set both UV_RENAME and UV_CHANGE but
139 // the Node API only lets us pass a single event to JS land.
140 //
141 // The obvious solution is to run the callback twice, once for each event.
142 // However, since the second event is not allowed to fire if the handle is
143 // closed after the first event, and since there is no good way to detect
144 // closed handles, that option is out.
145 //
146 // For now, ignore the UV_CHANGE event if UV_RENAME is also set. Make the
147 // assumption that a rename implicitly means an attribute change. Not too
148 // unreasonable, right? Still, we should revisit this before v1.0.
8fe5712 fs watcher binding
Igor Zinkovsky authored
149 if (status) {
74a8215 @bnoordhuis Revert support for isolates.
bnoordhuis authored
150 SetErrno(uv_last_error(uv_default_loop()));
8fe5712 fs watcher binding
Igor Zinkovsky authored
151 eventStr = String::Empty();
22c2c34 @bnoordhuis fs: fix fs.watch() segmentation fault
bnoordhuis authored
152 }
153 else if (events & UV_RENAME) {
154 eventStr = String::New("rename");
155 }
156 else if (events & UV_CHANGE) {
157 eventStr = String::New("change");
158 }
159 else {
160 assert(0 && "bad fs events flag");
161 abort();
8fe5712 fs watcher binding
Igor Zinkovsky authored
162 }
163
164 Local<Value> argv[3] = {
165 Integer::New(status),
166 eventStr,
167 filename ? (Local<Value>)String::New(filename) : Local<Value>::New(v8::Null())
168 };
169
a26bee8 @isaacs MakeCallback: Consistent symbol usage
isaacs authored
170 if (onchange_sym.IsEmpty()) {
171 onchange_sym = NODE_PSYMBOL("onchange");
172 }
173
174 MakeCallback(wrap->object_, onchange_sym, ARRAY_SIZE(argv), argv);
8fe5712 fs watcher binding
Igor Zinkovsky authored
175 }
8dd4fcb @bnoordhuis fs: don't close uninitialized fs.watch handle
bnoordhuis authored
176
177
178 Handle<Value> FSEventWrap::Close(const Arguments& args) {
179 HandleScope scope;
180
181 UNWRAP
182
183 if (!wrap->initialized_)
184 return Undefined();
185
186 wrap->initialized_ = false;
187 return HandleWrap::Close(args);
188 }
189
190
8fe5712 fs watcher binding
Igor Zinkovsky authored
191 } // namespace node
192
cdcb111 @bnoordhuis Remove stray NODE_MODULE() semi-colons.
bnoordhuis authored
193 NODE_MODULE(node_fs_event_wrap, node::FSEventWrap::Initialize)
Something went wrong with that request. Please try again.