Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 187 lines (140 sloc) 5.611 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 Ben Noordhuis 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 Ben Noordhuis 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 Ben Noordhuis 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_) {
54 handle_.data = reinterpret_cast<void*>(this);
8dd4fcb Ben Noordhuis 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 Ben Noordhuis 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 Oleg Efimov 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 Ben Noordhuis 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 Ben Noordhuis 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 Ben Noordhuis fs: don't close uninitialized fs.watch handle
bnoordhuis authored
108 wrap->initialized_ = true;
189dd8f Bert Belder Fix line endings and trailing whitespace
piscisaureus authored
109 } else {
74a8215 Ben Noordhuis Revert support for isolates.
bnoordhuis authored
110 SetErrno(uv_last_error(uv_default_loop()));
8fe5712 fs watcher binding
Igor Zinkovsky authored
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
22c2c34 Ben Noordhuis 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 Ben Noordhuis Revert support for isolates.
bnoordhuis authored
138 SetErrno(uv_last_error(uv_default_loop()));
8fe5712 fs watcher binding
Igor Zinkovsky authored
139 eventStr = String::Empty();
22c2c34 Ben Noordhuis 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] = {
153 Integer::New(status),
154 eventStr,
155 filename ? (Local<Value>)String::New(filename) : Local<Value>::New(v8::Null())
156 };
157
a26bee8 isaacs MakeCallback: Consistent symbol usage
isaacs authored
158 if (onchange_sym.IsEmpty()) {
159 onchange_sym = NODE_PSYMBOL("onchange");
160 }
161
162 MakeCallback(wrap->object_, onchange_sym, ARRAY_SIZE(argv), argv);
8fe5712 fs watcher binding
Igor Zinkovsky authored
163 }
8dd4fcb Ben Noordhuis fs: don't close uninitialized fs.watch handle
bnoordhuis authored
164
165
166 Handle<Value> FSEventWrap::Close(const Arguments& args) {
167 HandleScope scope;
168
db5c26e Ben Noordhuis fs: fix assert in fs.watch()
bnoordhuis authored
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();
8dd4fcb Ben Noordhuis fs: don't close uninitialized fs.watch handle
bnoordhuis authored
178 wrap->initialized_ = false;
db5c26e Ben Noordhuis fs: fix assert in fs.watch()
bnoordhuis authored
179
8dd4fcb Ben Noordhuis fs: don't close uninitialized fs.watch handle
bnoordhuis authored
180 return HandleWrap::Close(args);
181 }
182
183
8fe5712 fs watcher binding
Igor Zinkovsky authored
184 } // namespace node
185
cdcb111 Ben Noordhuis Remove stray NODE_MODULE() semi-colons.
bnoordhuis authored
186 NODE_MODULE(node_fs_event_wrap, node::FSEventWrap::Initialize)
Something went wrong with that request. Please try again.