Skip to content
This repository
Newer
Older
100644 190 lines (143 sloc) 5.723 kb
60818b95 »
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 »
2012-03-10 core: use proper #include directives
22 #include "node.h"
23 #include "handle_wrap.h"
8fe57124 »
2011-09-22 fs watcher binding
24
25 #include <stdlib.h>
26
27 using namespace v8;
28
29 namespace node {
30
a26bee8f »
2012-04-12 MakeCallback: Consistent symbol usage
31 static Persistent<String> onchange_sym;
32
8fe57124 »
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 »
2011-11-10 fs: don't close uninitialized fs.watch handle
38 static Handle<Value> Close(const Arguments& args);
8fe57124 »
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 »
2011-11-10 fs: don't close uninitialized fs.watch handle
48 bool initialized_;
8fe57124 »
2011-09-22 fs watcher binding
49 };
50
51
52 FSEventWrap::FSEventWrap(Handle<Object> object): HandleWrap(object,
53 (uv_handle_t*)&handle_) {
5664dd2f »
2013-01-05 src: use static_cast where appropriate
54 handle_.data = static_cast<void*>(this);
8dd4fcbe »
2011-11-10 fs: don't close uninitialized fs.watch handle
55 initialized_ = false;
8fe57124 »
2011-09-22 fs watcher binding
56 }
57
58
59 FSEventWrap::~FSEventWrap() {
8dd4fcbe »
2011-11-10 fs: don't close uninitialized fs.watch handle
60 assert(initialized_ == false);
8fe57124 »
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 »
2012-05-21 Make UNWRAP macro generic.
94 UNWRAP(FSEventWrap)
8fe57124 »
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 »
2012-03-21 Avoiding unnecessary ToString() calls
100 String::Utf8Value path(args[0]);
8fe57124 »
2011-09-22 fs watcher binding
101
74a8215a »
2012-02-03 Revert support for isolates.
102 int r = uv_fs_event_init(uv_default_loop(), &wrap->handle_, *path, OnEvent, 0);
8fe57124 »
2011-09-22 fs watcher binding
103 if (r == 0) {
104 // Check for persistent argument
105 if (!args[1]->IsTrue()) {
039fac63 »
2012-05-17 deps: upgrade libuv to a478847
106 uv_unref(reinterpret_cast<uv_handle_t*>(&wrap->handle_));
8fe57124 »
2011-09-22 fs watcher binding
107 }
8dd4fcbe »
2011-11-10 fs: don't close uninitialized fs.watch handle
108 wrap->initialized_ = true;
189dd8f8 »
2011-11-04 Fix line endings and trailing whitespace
109 } else {
74a8215a »
2012-02-03 Revert support for isolates.
110 SetErrno(uv_last_error(uv_default_loop()));
8fe57124 »
2011-09-22 fs watcher binding
111 }
112
6573fc35 »
2013-01-06 src: pass node_isolate to Integer::New
113 return scope.Close(Integer::New(r, node_isolate));
8fe57124 »
2011-09-22 fs watcher binding
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
5664dd2f »
2013-01-05 src: use static_cast where appropriate
122 FSEventWrap* wrap = static_cast<FSEventWrap*>(handle->data);
8fe57124 »
2011-09-22 fs watcher binding
123
124 assert(wrap->object_.IsEmpty() == false);
125
22c2c349 »
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 »
2011-09-22 fs watcher binding
137 if (status) {
74a8215a »
2012-02-03 Revert support for isolates.
138 SetErrno(uv_last_error(uv_default_loop()));
412b3cee »
2013-01-06 src: pass node_isolate to String::Empty
139 eventStr = String::Empty(node_isolate);
22c2c349 »
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 »
2011-09-22 fs watcher binding
150 }
151
152 Local<Value> argv[3] = {
6573fc35 »
2013-01-06 src: pass node_isolate to Integer::New
153 Integer::New(status, node_isolate),
8fe57124 »
2011-09-22 fs watcher binding
154 eventStr,
01c3d0aa »
2013-01-06 src: pass node_isolate to Local<>::New
155 filename ? static_cast<Local<Value> >(String::New(filename))
c7d7ae1f »
2013-01-06 src: pass node_isolate to Null()
156 : Local<Value>::New(node_isolate, v8::Null(node_isolate))
8fe57124 »
2011-09-22 fs watcher binding
157 };
158
a26bee8f »
2012-04-12 MakeCallback: Consistent symbol usage
159 if (onchange_sym.IsEmpty()) {
160 onchange_sym = NODE_PSYMBOL("onchange");
161 }
162
163 MakeCallback(wrap->object_, onchange_sym, ARRAY_SIZE(argv), argv);
8fe57124 »
2011-09-22 fs watcher binding
164 }
8dd4fcbe »
2011-11-10 fs: don't close uninitialized fs.watch handle
165
166
167 Handle<Value> FSEventWrap::Close(const Arguments& args) {
168 HandleScope scope;
169
db5c26e3 »
2012-09-14 fs: fix assert in fs.watch()
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);
5e57bcc3 »
2013-01-01 bindings: update to new v8 apis
175 void* ptr = args.Holder()->GetAlignedPointerFromInternalField(0);
db5c26e3 »
2012-09-14 fs: fix assert in fs.watch()
176 FSEventWrap* wrap = static_cast<FSEventWrap*>(ptr);
177
7788a6bf »
2013-01-07 src: pass node_isolate to Undefined()
178 if (wrap == NULL || wrap->initialized_ == false) {
179 return Undefined(node_isolate);
180 }
8dd4fcbe »
2011-11-10 fs: don't close uninitialized fs.watch handle
181 wrap->initialized_ = false;
db5c26e3 »
2012-09-14 fs: fix assert in fs.watch()
182
8dd4fcbe »
2011-11-10 fs: don't close uninitialized fs.watch handle
183 return HandleWrap::Close(args);
184 }
185
186
8fe57124 »
2011-09-22 fs watcher binding
187 } // namespace node
188
cdcb1118 »
2011-11-09 Remove stray NODE_MODULE() semi-colons.
189 NODE_MODULE(node_fs_event_wrap, node::FSEventWrap::Initialize)
Something went wrong with that request. Please try again.