Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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