22#include " v8.h"
33#include " env.h"
44#include " env-inl.h"
5+ #include " string_bytes.h"
56
67#include < errno.h>
78#include < string.h>
@@ -32,6 +33,7 @@ using v8::Context;
3233using v8::FunctionCallbackInfo;
3334using v8::Integer;
3435using v8::Local;
36+ using v8::Null;
3537using v8::Number;
3638using v8::Object;
3739using v8::String;
@@ -290,6 +292,72 @@ static void GetHomeDirectory(const FunctionCallbackInfo<Value>& args) {
290292}
291293
292294
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+
293361void Initialize (Local<Object> target,
294362 Local<Value> unused,
295363 Local<Context> context) {
@@ -304,6 +372,7 @@ void Initialize(Local<Object> target,
304372 env->SetMethod (target, " getOSRelease" , GetOSRelease);
305373 env->SetMethod (target, " getInterfaceAddresses" , GetInterfaceAddresses);
306374 env->SetMethod (target, " getHomeDirectory" , GetHomeDirectory);
375+ env->SetMethod (target, " getUserInfo" , GetUserInfo);
307376 target->Set (FIXED_ONE_BYTE_STRING (env->isolate (), " isBigEndian" ),
308377 Boolean::New (env->isolate (), IsBigEndian ()));
309378}
0 commit comments