Skip to content

Commit 6e78e5f

Browse files
committed
os: add homedir()
os.homedir() calls libuv's uv_os_homedir() to retrieve the current user's home directory. PR-URL: #1791 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Rod Vagg <rod@vagg.org>
1 parent 02c3450 commit 6e78e5f

File tree

6 files changed

+79
-1
lines changed

6 files changed

+79
-1
lines changed

doc/api/os.markdown

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ Use `require('os')` to access this module.
1010

1111
Returns the operating system's default directory for temporary files.
1212

13+
## os.homedir()
14+
15+
Returns the home directory of the current user.
16+
1317
## os.endianness()
1418

1519
Returns the endianness of the CPU. Possible values are `'BE'` for big endian

lib/os.js

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ exports.cpus = binding.getCPUs;
1313
exports.type = binding.getOSType;
1414
exports.release = binding.getOSRelease;
1515
exports.networkInterfaces = binding.getInterfaceAddresses;
16+
exports.homedir = binding.getHomeDirectory;
17+
1618

1719
exports.arch = function() {
1820
return process.arch;

src/node_os.cc

+21
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#endif // __MINGW32__
1212

1313
#ifdef __POSIX__
14+
# include <limits.h> // PATH_MAX on Solaris.
1415
# include <netdb.h> // MAXHOSTNAMELEN on Solaris.
1516
# include <unistd.h> // gethostname, sysconf
1617
# include <sys/param.h> // MAXHOSTNAMELEN on Linux and the BSDs.
@@ -271,6 +272,25 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo<Value>& args) {
271272
}
272273

273274

275+
static void GetHomeDirectory(const FunctionCallbackInfo<Value>& args) {
276+
Environment* env = Environment::GetCurrent(args);
277+
char buf[PATH_MAX];
278+
279+
size_t len = sizeof(buf);
280+
const int err = uv_os_homedir(buf, &len);
281+
282+
if (err) {
283+
return env->ThrowUVException(err, "uv_os_homedir");
284+
}
285+
286+
Local<String> home = String::NewFromUtf8(env->isolate(),
287+
buf,
288+
String::kNormalString,
289+
len);
290+
args.GetReturnValue().Set(home);
291+
}
292+
293+
274294
void Initialize(Handle<Object> target,
275295
Handle<Value> unused,
276296
Handle<Context> context) {
@@ -284,6 +304,7 @@ void Initialize(Handle<Object> target,
284304
env->SetMethod(target, "getOSType", GetOSType);
285305
env->SetMethod(target, "getOSRelease", GetOSRelease);
286306
env->SetMethod(target, "getInterfaceAddresses", GetInterfaceAddresses);
307+
env->SetMethod(target, "getHomeDirectory", GetHomeDirectory);
287308
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "isBigEndian"),
288309
Boolean::New(env->isolate(), IsBigEndian()));
289310
}

test/common.js

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ exports.fixturesDir = path.join(exports.testDir, 'fixtures');
1010
exports.libDir = path.join(exports.testDir, '../lib');
1111
exports.tmpDirName = 'tmp';
1212
exports.PORT = +process.env.NODE_COMMON_PORT || 12346;
13+
exports.isWindows = process.platform === 'win32';
1314

1415
if (process.env.TEST_THREAD_ID) {
1516
// Distribute ports in parallel tests
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
var common = require('../common');
3+
var assert = require('assert');
4+
var cp = require('child_process');
5+
var os = require('os');
6+
var path = require('path');
7+
8+
9+
if (process.argv[2] === 'child') {
10+
if (common.isWindows)
11+
assert.equal(process.env.USERPROFILE, undefined);
12+
else
13+
assert.equal(process.env.HOME, undefined);
14+
15+
var home = os.homedir();
16+
17+
assert.ok(typeof home === 'string');
18+
assert.ok(home.indexOf(path.sep) !== -1);
19+
} else {
20+
if (common.isWindows)
21+
delete process.env.USERPROFILE;
22+
else
23+
delete process.env.HOME;
24+
25+
var child = cp.spawnSync(process.execPath, [__filename, 'child'], {
26+
env: process.env
27+
});
28+
29+
assert.equal(child.status, 0);
30+
}

test/parallel/test-os.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
var common = require('../common');
33
var assert = require('assert');
44
var os = require('os');
5+
var path = require('path');
56

67

78
process.env.TMPDIR = '/tmpdir';
89
process.env.TMP = '/tmp';
910
process.env.TEMP = '/temp';
10-
if (process.platform === 'win32') {
11+
if (common.isWindows) {
1112
assert.equal(os.tmpdir(), '/temp');
1213
process.env.TEMP = '';
1314
assert.equal(os.tmpdir(), '/tmp');
@@ -101,3 +102,22 @@ switch (platform) {
101102

102103
var EOL = os.EOL;
103104
assert.ok(EOL.length > 0);
105+
106+
107+
var home = os.homedir();
108+
109+
console.log('homedir = ' + home);
110+
assert.ok(typeof home === 'string');
111+
assert.ok(home.indexOf(path.sep) !== -1);
112+
113+
if (common.isWindows && process.env.USERPROFILE) {
114+
assert.equal(home, process.env.USERPROFILE);
115+
delete process.env.USERPROFILE;
116+
assert.ok(os.homedir().indexOf(path.sep) !== -1);
117+
process.env.USERPROFILE = home;
118+
} else if (!common.isWindows && process.env.HOME) {
119+
assert.equal(home, process.env.HOME);
120+
delete process.env.HOME;
121+
assert.ok(os.homedir().indexOf(path.sep) !== -1);
122+
process.env.HOME = home;
123+
}

0 commit comments

Comments
 (0)