2
2
#include " v8.h"
3
3
#include " env.h"
4
4
#include " env-inl.h"
5
+ #include " string_bytes.h"
5
6
6
7
#include < errno.h>
7
8
#include < string.h>
@@ -32,6 +33,7 @@ using v8::Context;
32
33
using v8::FunctionCallbackInfo;
33
34
using v8::Integer;
34
35
using v8::Local;
36
+ using v8::Null;
35
37
using v8::Number;
36
38
using v8::Object;
37
39
using v8::String;
@@ -290,6 +292,72 @@ static void GetHomeDirectory(const FunctionCallbackInfo<Value>& args) {
290
292
}
291
293
292
294
295
+ static void GetUserInfo (const FunctionCallbackInfo<Value>& args) {
296
+ Environment* env = Environment::GetCurrent (args);
297
+ uv_passwd_t pwd;
298
+ enum encoding encoding;
299
+
300
+ if (args[0 ]->IsObject ()) {
301
+ Local<Object> options = args[0 ].As <Object>();
302
+ Local<Value> encoding_opt = options->Get (env->encoding_string ());
303
+ encoding = ParseEncoding (env->isolate (), encoding_opt, UTF8);
304
+ } else {
305
+ encoding = UTF8;
306
+ }
307
+
308
+ const int err = uv_os_get_passwd (&pwd);
309
+
310
+ if (err) {
311
+ return env->ThrowUVException (err, " uv_os_get_passwd" );
312
+ }
313
+
314
+ Local<Value> uid = Number::New (env->isolate (), pwd.uid );
315
+ Local<Value> gid = Number::New (env->isolate (), pwd.gid );
316
+ Local<Value> username = StringBytes::Encode (env->isolate (),
317
+ pwd.username ,
318
+ encoding);
319
+ Local<Value> homedir = StringBytes::Encode (env->isolate (),
320
+ pwd.homedir ,
321
+ encoding);
322
+ Local<Value> shell;
323
+
324
+ if (pwd.shell == NULL )
325
+ shell = Null (env->isolate ());
326
+ else
327
+ shell = StringBytes::Encode (env->isolate (), pwd.shell , encoding);
328
+
329
+ uv_os_free_passwd (&pwd);
330
+
331
+ if (username.IsEmpty ()) {
332
+ return env->ThrowUVException (UV_EINVAL,
333
+ " uv_os_get_passwd" ,
334
+ " Invalid character encoding for username" );
335
+ }
336
+
337
+ if (homedir.IsEmpty ()) {
338
+ return env->ThrowUVException (UV_EINVAL,
339
+ " uv_os_get_passwd" ,
340
+ " Invalid character encoding for homedir" );
341
+ }
342
+
343
+ if (shell.IsEmpty ()) {
344
+ return env->ThrowUVException (UV_EINVAL,
345
+ " uv_os_get_passwd" ,
346
+ " Invalid character encoding for shell" );
347
+ }
348
+
349
+ Local<Object> entry = Object::New (env->isolate ());
350
+
351
+ entry->Set (env->uid_string (), uid);
352
+ entry->Set (env->gid_string (), gid);
353
+ entry->Set (env->username_string (), username);
354
+ entry->Set (env->homedir_string (), homedir);
355
+ entry->Set (env->shell_string (), shell);
356
+
357
+ args.GetReturnValue ().Set (entry);
358
+ }
359
+
360
+
293
361
void Initialize (Local<Object> target,
294
362
Local<Value> unused,
295
363
Local<Context> context) {
@@ -304,6 +372,7 @@ void Initialize(Local<Object> target,
304
372
env->SetMethod (target, " getOSRelease" , GetOSRelease);
305
373
env->SetMethod (target, " getInterfaceAddresses" , GetInterfaceAddresses);
306
374
env->SetMethod (target, " getHomeDirectory" , GetHomeDirectory);
375
+ env->SetMethod (target, " getUserInfo" , GetUserInfo);
307
376
target->Set (FIXED_ONE_BYTE_STRING (env->isolate (), " isBigEndian" ),
308
377
Boolean::New (env->isolate (), IsBigEndian ()));
309
378
}
0 commit comments