Skip to content
This repository
Newer
Older
100644 186 lines (140 sloc) 5.611 kb
60818b95 » ry
2011-11-02 Add missing copyright headers
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
ff4a9d38 » bnoordhuis
2012-03-10 core: use proper #include directives
22 #include "node.h"
23 #include "handle_wrap.h"
8fe57124 » Igor Zinkovsky
2011-09-22 fs watcher binding
24
25 #include <stdlib.h>
26
27 using namespace v8;
28
29 namespace node {
30
a26bee8f » isaacs
2012-04-12 MakeCallback: Consistent symbol usage
31 static Persistent<String> onchange_sym;
32
8fe57124 » Igor Zinkovsky
2011-09-22 fs watcher binding
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);
8dd4fcbe » bnoordhuis
2011-11-10 fs: don't close uninitialized fs.watch handle
38 static Handle<Value> Close(const Arguments& args);
8fe57124 » Igor Zinkovsky
2011-09-22 fs watcher binding
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_;
8dd4fcbe » bnoordhuis
2011-11-10 fs: don't close uninitialized fs.watch handle
48 bool initialized_;
8fe57124 » Igor Zinkovsky
2011-09-22 fs watcher binding
49 };
50
51
52 FSEventWrap::FSEventWrap(Handle<Object> object): HandleWrap(object,
53 (uv_handle_t*)&handle_) {
54 handle_.data = reinterpret_cast<void*>(this);
8dd4fcbe » bnoordhuis
2011-11-10 fs: don't close uninitialized fs.watch handle
55 initialized_ = false;
8fe57124 » Igor Zinkovsky
2011-09-22 fs watcher binding
56 }
57
58
59 FSEventWrap::~FSEventWrap() {
8dd4fcbe » bnoordhuis
2011-11-10 fs: don't close uninitialized fs.watch handle
60 assert(initialized_ == false);
8fe57124 » Igor Zinkovsky
2011-09-22 fs watcher binding
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
45de259b » Sannis
2012-05-21 Make UNWRAP macro generic.
94 UNWRAP(FSEventWrap)
8fe57124 » Igor Zinkovsky
2011-09-22 fs watcher binding
95
96 if (args.Length() < 1 || !args[0]->IsString()) {
97 return ThrowException(Exception::TypeError(String::New("Bad arguments")));
98 }
99
249c3c16 » ssuda
2012-03-21 Avoiding unnecessary ToString() calls
100 String::Utf8Value path(args[0]);
8fe57124 » Igor Zinkovsky
2011-09-22 fs watcher binding
101
74a8215a » bnoordhuis
2012-02-03 Revert support for isolates.
102 int r = uv_fs_event_init(uv_default_loop(), &wrap->handle_, *path, OnEvent, 0);
8fe57124 » Igor Zinkovsky
2011-09-22 fs watcher binding
103 if (r == 0) {
104 // Check for persistent argument
105 if (!args[1]->IsTrue()) {
039fac63 » bnoordhuis
2012-05-17 deps: upgrade libuv to a478847
106 uv_unref(reinterpret_cast<uv_handle_t*>(&wrap->handle_));
8fe57124 » Igor Zinkovsky
2011-09-22 fs watcher binding
107 }
8dd4fcbe » bnoordhuis
2011-11-10 fs: don't close uninitialized fs.watch handle
108 wrap->initialized_ = true;
189dd8f8 » piscisaureus
2011-11-04 Fix line endings and trailing whitespace
109 } else {
74a8215a » bnoordhuis
2012-02-03 Revert support for isolates.
110 SetErrno(uv_last_error(uv_default_loop()));
8fe57124 » Igor Zinkovsky
2011-09-22 fs watcher binding
111 }
112
113 return scope.Close(Integer::New(r));
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
122 FSEventWrap* wrap = reinterpret_cast<FSEventWrap*>(handle->data);
123
124 assert(wrap->object_.IsEmpty() == false);
125
22c2c349 » bnoordhuis
2011-12-07 fs: fix fs.watch() segmentation fault
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.
8fe57124 » Igor Zinkovsky
2011-09-22 fs watcher binding
137 if (status) {
74a8215a » bnoordhuis
2012-02-03 Revert support for isolates.
138 SetErrno(uv_last_error(uv_default_loop()));
8fe57124 » Igor Zinkovsky
2011-09-22 fs watcher binding
139 eventStr = String::Empty();
22c2c349 » bnoordhuis
2011-12-07 fs: fix fs.watch() segmentation fault
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();
8fe57124 » Igor Zinkovsky
2011-09-22 fs watcher binding
150 }
151
152 Local<Value> argv[3] = {
153 Integer::New(status),
154 eventStr,
155 filename ? (Local<Value>)String::New(filename) : Local<Value>::New(v8::Null())
156 };
157
a26bee8f » isaacs
2012-04-12 MakeCallback: Consistent symbol usage
158 if (onchange_sym.IsEmpty()) {
159 onchange_sym = NODE_PSYMBOL("onchange");
160 }
161
162 MakeCallback(wrap->object_, onchange_sym, ARRAY_SIZE(argv), argv);
8fe57124 » Igor Zinkovsky
2011-09-22 fs watcher binding
163 }
8dd4fcbe » bnoordhuis
2011-11-10 fs: don't close uninitialized fs.watch handle
164
165
166 Handle<Value> FSEventWrap::Close(const Arguments& args) {
167 HandleScope scope;
168
db5c26e3 » bnoordhuis
2012-09-14 fs: fix assert in fs.watch()
169 // Unwrap manually here. The UNWRAP() macro asserts that wrap != NULL.
170 // That usually indicates an error but not here: double closes are possible
171 // and legal, HandleWrap::Close() deals with them the same way.
172 assert(!args.Holder().IsEmpty());
173 assert(args.Holder()->InternalFieldCount() > 0);
174 void* ptr = args.Holder()->GetPointerFromInternalField(0);
175 FSEventWrap* wrap = static_cast<FSEventWrap*>(ptr);
176
177 if (wrap == NULL || wrap->initialized_ == false) return Undefined();
8dd4fcbe » bnoordhuis
2011-11-10 fs: don't close uninitialized fs.watch handle
178 wrap->initialized_ = false;
db5c26e3 » bnoordhuis
2012-09-14 fs: fix assert in fs.watch()
179
8dd4fcbe » bnoordhuis
2011-11-10 fs: don't close uninitialized fs.watch handle
180 return HandleWrap::Close(args);
181 }
182
183
8fe57124 » Igor Zinkovsky
2011-09-22 fs watcher binding
184 } // namespace node
185
cdcb1118 » bnoordhuis
2011-11-09 Remove stray NODE_MODULE() semi-colons.
186 NODE_MODULE(node_fs_event_wrap, node::FSEventWrap::Initialize)
Something went wrong with that request. Please try again.