Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 156 lines (117 sloc) 4.767 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
8fe5712 fs watcher binding
Igor Zinkovsky authored
22 #include <node.h>
23 #include <handle_wrap.h>
24
25 #include <stdlib.h>
26
27 using namespace v8;
28
29 namespace node {
30
31 #define UNWRAP \
32 assert(!args.Holder().IsEmpty()); \
33 assert(args.Holder()->InternalFieldCount() > 0); \
34 FSEventWrap* wrap = \
35 static_cast<FSEventWrap*>(args.Holder()->GetPointerFromInternalField(0)); \
36 if (!wrap) { \
6cc4292 @ry Display sys_errno when UV_UNKNOWN is returned
ry authored
37 uv_err_t err; \
38 err.code = UV_EBADF; \
39 SetErrno(err); \
8fe5712 fs watcher binding
Igor Zinkovsky authored
40 return scope.Close(Integer::New(-1)); \
41 }
42
43 class FSEventWrap: public HandleWrap {
44 public:
45 static void Initialize(Handle<Object> target);
46 static Handle<Value> New(const Arguments& args);
47 static Handle<Value> Start(const Arguments& args);
48
49 private:
50 FSEventWrap(Handle<Object> object);
51 virtual ~FSEventWrap();
52
53 static void OnEvent(uv_fs_event_t* handle, const char* filename, int events,
54 int status);
55
56 uv_fs_event_t handle_;
57 };
58
59
60 FSEventWrap::FSEventWrap(Handle<Object> object): HandleWrap(object,
61 (uv_handle_t*)&handle_) {
62 handle_.data = reinterpret_cast<void*>(this);
63 }
64
65
66 FSEventWrap::~FSEventWrap() {
67 }
68
69
70 void FSEventWrap::Initialize(Handle<Object> target) {
71 HandleWrap::Initialize(target);
72
73 HandleScope scope;
74
75 Local<FunctionTemplate> t = FunctionTemplate::New(New);
76 t->InstanceTemplate()->SetInternalFieldCount(1);
77 t->SetClassName(String::NewSymbol("FSEvent"));
78
79 NODE_SET_PROTOTYPE_METHOD(t, "start", Start);
80 NODE_SET_PROTOTYPE_METHOD(t, "close", Close);
81
82 target->Set(String::NewSymbol("FSEvent"),
83 Persistent<FunctionTemplate>::New(t)->GetFunction());
84 }
85
86
87 Handle<Value> FSEventWrap::New(const Arguments& args) {
88 HandleScope scope;
89
90 assert(args.IsConstructCall());
91 new FSEventWrap(args.This());
92
93 return scope.Close(args.This());
94 }
95
96
97 Handle<Value> FSEventWrap::Start(const Arguments& args) {
98 HandleScope scope;
99
100 UNWRAP
101
102 if (args.Length() < 1 || !args[0]->IsString()) {
103 return ThrowException(Exception::TypeError(String::New("Bad arguments")));
104 }
105
106 String::Utf8Value path(args[0]->ToString());
107
108 int r = uv_fs_event_init(uv_default_loop(), &wrap->handle_, *path, OnEvent);
109 if (r == 0) {
110 // Check for persistent argument
111 if (!args[1]->IsTrue()) {
112 uv_unref(uv_default_loop());
113 }
189dd8f @piscisaureus Fix line endings and trailing whitespace
piscisaureus authored
114 } else {
6cc4292 @ry Display sys_errno when UV_UNKNOWN is returned
ry authored
115 SetErrno(uv_last_error(uv_default_loop()));
8fe5712 fs watcher binding
Igor Zinkovsky authored
116 }
117
118 return scope.Close(Integer::New(r));
119 }
120
121
122 void FSEventWrap::OnEvent(uv_fs_event_t* handle, const char* filename,
123 int events, int status) {
124 HandleScope scope;
125 Local<String> eventStr;
126
127 FSEventWrap* wrap = reinterpret_cast<FSEventWrap*>(handle->data);
128
129 assert(wrap->object_.IsEmpty() == false);
130
131 if (status) {
6cc4292 @ry Display sys_errno when UV_UNKNOWN is returned
ry authored
132 SetErrno(uv_last_error(uv_default_loop()));
8fe5712 fs watcher binding
Igor Zinkovsky authored
133 eventStr = String::Empty();
134 } else {
135 switch (events) {
136 case UV_RENAME:
137 eventStr = String::New("rename");
138 break;
139 case UV_CHANGE:
140 eventStr = String::New("change");
141 break;
142 }
143 }
144
145 Local<Value> argv[3] = {
146 Integer::New(status),
147 eventStr,
148 filename ? (Local<Value>)String::New(filename) : Local<Value>::New(v8::Null())
149 };
150
151 MakeCallback(wrap->object_, "onchange", 3, argv);
152 }
153 } // namespace node
154
155 NODE_MODULE(node_fs_event_wrap, node::FSEventWrap::Initialize);
Something went wrong with that request. Please try again.