forked from mmalecki/node-mount
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmount.cc
232 lines (177 loc) · 6.19 KB
/
mount.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include <sys/mount.h>
#include <v8.h>
#include <node.h>
#include <errno.h>
#include <string.h>
#include <iostream>
//Uses an example of kkaefer's node addon tutorials for an async worker
//
//Link for the used template:
//https://github.com/kkaefer/node-cpp-modules/blob/master/05_threadpool/modulename.cpp#L88
using namespace v8;
Handle<Value> Mount(const Arguments &args);
void AsyncMount(uv_work_t* req);
void AsyncUmount(uv_work_t *req);
void AsyncAfter(uv_work_t* req);
struct Mounty {
Persistent<Function> callback;
//All values excpect target are only used by mount
std::string devFile;
std::string fsType;
std::string target; //used by umount
std::string data;
int flags;
int error;
};
// 0 1 2 3 4
//mount(devFile, target, fsType, options, data)
Handle<Value> Mount(const Arguments &args) {
HandleScope scope;
int argsLength = args.Length();
if (argsLength <= 2) {
return ThrowException(Exception::Error(String::New("mount needs at least 3 parameters")));
}
Local<Function> cb;
Handle<Array> options = Array::New(0);
Local<Value> data = String::New("");
if(!args[0]->IsString() || !args[1]->IsString() || !args[2]->IsString()){
return ThrowException(Exception::Error(String::New("Invalid arguments")));
}
//Check and set options array (ignores all non-array values)
if(argsLength >= 3) {
if (args[3]->IsArray()) {
options = Handle<Array>::Cast(args[3]);
}
}
if(argsLength == 6) {
if (args[4]->IsString()) {
data = args[4];
}
}
//Set callback, if provided as last argument
if(args[argsLength - 1]->IsFunction()){
cb = Local<Function>::Cast(args[argsLength - 1]);
}
else{
cb = FunctionTemplate::New()->GetFunction();
}
int flags = 0;
for (unsigned int i = 0; i < options->Length(); i++) {
String::Utf8Value str(options->Get(i)->ToString());
if(!strcmp(*str, "bind")) {
flags |= MS_BIND;
} else if(!strcmp(*str, "readonly")) {
flags |= MS_RDONLY;
} else if(!strcmp(*str, "remount")) {
flags |= MS_REMOUNT;
} else if(!strcmp(*str, "noexec")){
flags |= MS_NOEXEC;
} else{
//return ThrowException(Exception::Error(String::New(strcat("Invalid option: ", *str))));
return ThrowException(Exception::Error(String::New("Invalid option")));
}
}
String::Utf8Value devFile(args[0]->ToString());
String::Utf8Value target(args[1]->ToString());
String::Utf8Value fsType(args[2]->ToString());
String::Utf8Value dataStr(data->ToString());
//Prepare data for the async work
Mounty* mounty = new Mounty();
mounty->callback = Persistent<Function>::New(cb);
mounty->devFile = std::string(*devFile);
mounty->target = std::string(*target);
mounty->fsType = std::string(*fsType);
mounty->data = std::string(*dataStr);
mounty->flags = flags;
//Create the Async work and set the prepared data
uv_work_t *req = new uv_work_t();
req->data = mounty;
int status = uv_queue_work(uv_default_loop(), req, AsyncMount, (uv_after_work_cb)AsyncAfter);
assert(status == 0);
return scope.Close(Undefined());
}
void AsyncMount(uv_work_t* req){
Mounty* mounty = static_cast<Mounty*>(req->data);
int ret = mount(mounty->devFile.c_str(),
mounty->target.c_str(),
mounty->fsType.c_str(),
mounty->flags,
mounty->data.c_str());
//Save error-code
if(ret == -1){
mounty->error = errno;
}
}
//Used for both, mount and umount since they have the same callback interface
void AsyncAfter(uv_work_t* req){
HandleScope scope;
Mounty* mounty = static_cast<Mounty*>(req->data);
const unsigned argc = 1;
Local<Value> argv[argc];
//Call error-callback, if error... otherwise send result
if(mounty->error > 0){
Local<String> s = Integer::New((int32_t)mounty->error)->ToString();
Local<Value> err = Exception::Error(s);
//const unsigned argc = 1;
argv[0] = err;
//TryCatch tc;
//mounty->callback->Call(Context::GetCurrent()->Global(), argc, argv);
//if(tc.HasCaught()){
//node::FatalException(tc);
//}
}
else{
//const unsigned argc = 1;
argv[0] = Local<Value>::New(Null());
//TryCatch tc;
//mounty->callback->Call(Context::GetCurrent()->Global(), argc, argv);
//if(tc.HasCaught()){
//node::FatalException(tc);
//}
}
TryCatch tc;
mounty->callback->Call(Context::GetCurrent()->Global(), argc, argv);
if(tc.HasCaught()){
node::FatalException(tc);
}
mounty->callback.Dispose();
delete mounty;
delete req;
}
Handle<Value> Umount(const Arguments &args) {
HandleScope scope;
if (args.Length() < 1) {
return ThrowException(String::New("Missing argument 'target'"));
}
Local<Function> cb;
if(args.Length() == 2 && args[1]->IsFunction()) {
cb = Local<Function>::Cast(args[1]);
}
else{
cb = FunctionTemplate::New()->GetFunction();
}
String::Utf8Value target(args[0]->ToString());
//Prepare data for the async work
Mounty* mounty = new Mounty();
mounty->callback = Persistent<Function>::New(cb);
mounty->target = std::string(*target);
//Create the Async work and set the prepared data
uv_work_t *req = new uv_work_t();
req->data = mounty;
int status = uv_queue_work(uv_default_loop(), req, AsyncUmount, (uv_after_work_cb)AsyncAfter);
assert(status == 0);
return scope.Close(Undefined());
}
void AsyncUmount(uv_work_t *req){
Mounty* mounty = static_cast<Mounty*>(req->data);
int ret = umount(mounty->target.c_str());
//Save error-code
if(ret == -1){
mounty->error = errno;
}
}
void init (Handle<Object> exports, Handle<Object> module) {
exports->Set(String::NewSymbol("mount"), FunctionTemplate::New(Mount)->GetFunction());
exports->Set(String::NewSymbol("umount"), FunctionTemplate::New(Umount)->GetFunction());
}
NODE_MODULE(mount, init)