os: add homedir()
os.homedir() calls libuv's uv_os_homedir() to retrieve the current user's home directory. PR-URL: https://github.com/nodejs/io.js/pull/1791 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Rod Vagg <rod@vagg.org>
This commit is contained in:
parent
02c345020a
commit
6e78e5feaa
@ -10,6 +10,10 @@ Use `require('os')` to access this module.
|
|||||||
|
|
||||||
Returns the operating system's default directory for temporary files.
|
Returns the operating system's default directory for temporary files.
|
||||||
|
|
||||||
|
## os.homedir()
|
||||||
|
|
||||||
|
Returns the home directory of the current user.
|
||||||
|
|
||||||
## os.endianness()
|
## os.endianness()
|
||||||
|
|
||||||
Returns the endianness of the CPU. Possible values are `'BE'` for big endian
|
Returns the endianness of the CPU. Possible values are `'BE'` for big endian
|
||||||
|
@ -13,6 +13,8 @@ exports.cpus = binding.getCPUs;
|
|||||||
exports.type = binding.getOSType;
|
exports.type = binding.getOSType;
|
||||||
exports.release = binding.getOSRelease;
|
exports.release = binding.getOSRelease;
|
||||||
exports.networkInterfaces = binding.getInterfaceAddresses;
|
exports.networkInterfaces = binding.getInterfaceAddresses;
|
||||||
|
exports.homedir = binding.getHomeDirectory;
|
||||||
|
|
||||||
|
|
||||||
exports.arch = function() {
|
exports.arch = function() {
|
||||||
return process.arch;
|
return process.arch;
|
||||||
|
@ -11,6 +11,7 @@
|
|||||||
#endif // __MINGW32__
|
#endif // __MINGW32__
|
||||||
|
|
||||||
#ifdef __POSIX__
|
#ifdef __POSIX__
|
||||||
|
# include <limits.h> // PATH_MAX on Solaris.
|
||||||
# include <netdb.h> // MAXHOSTNAMELEN on Solaris.
|
# include <netdb.h> // MAXHOSTNAMELEN on Solaris.
|
||||||
# include <unistd.h> // gethostname, sysconf
|
# include <unistd.h> // gethostname, sysconf
|
||||||
# include <sys/param.h> // MAXHOSTNAMELEN on Linux and the BSDs.
|
# include <sys/param.h> // MAXHOSTNAMELEN on Linux and the BSDs.
|
||||||
@ -271,6 +272,25 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo<Value>& args) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void GetHomeDirectory(const FunctionCallbackInfo<Value>& args) {
|
||||||
|
Environment* env = Environment::GetCurrent(args);
|
||||||
|
char buf[PATH_MAX];
|
||||||
|
|
||||||
|
size_t len = sizeof(buf);
|
||||||
|
const int err = uv_os_homedir(buf, &len);
|
||||||
|
|
||||||
|
if (err) {
|
||||||
|
return env->ThrowUVException(err, "uv_os_homedir");
|
||||||
|
}
|
||||||
|
|
||||||
|
Local<String> home = String::NewFromUtf8(env->isolate(),
|
||||||
|
buf,
|
||||||
|
String::kNormalString,
|
||||||
|
len);
|
||||||
|
args.GetReturnValue().Set(home);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void Initialize(Handle<Object> target,
|
void Initialize(Handle<Object> target,
|
||||||
Handle<Value> unused,
|
Handle<Value> unused,
|
||||||
Handle<Context> context) {
|
Handle<Context> context) {
|
||||||
@ -284,6 +304,7 @@ void Initialize(Handle<Object> target,
|
|||||||
env->SetMethod(target, "getOSType", GetOSType);
|
env->SetMethod(target, "getOSType", GetOSType);
|
||||||
env->SetMethod(target, "getOSRelease", GetOSRelease);
|
env->SetMethod(target, "getOSRelease", GetOSRelease);
|
||||||
env->SetMethod(target, "getInterfaceAddresses", GetInterfaceAddresses);
|
env->SetMethod(target, "getInterfaceAddresses", GetInterfaceAddresses);
|
||||||
|
env->SetMethod(target, "getHomeDirectory", GetHomeDirectory);
|
||||||
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "isBigEndian"),
|
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "isBigEndian"),
|
||||||
Boolean::New(env->isolate(), IsBigEndian()));
|
Boolean::New(env->isolate(), IsBigEndian()));
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,7 @@ exports.fixturesDir = path.join(exports.testDir, 'fixtures');
|
|||||||
exports.libDir = path.join(exports.testDir, '../lib');
|
exports.libDir = path.join(exports.testDir, '../lib');
|
||||||
exports.tmpDirName = 'tmp';
|
exports.tmpDirName = 'tmp';
|
||||||
exports.PORT = +process.env.NODE_COMMON_PORT || 12346;
|
exports.PORT = +process.env.NODE_COMMON_PORT || 12346;
|
||||||
|
exports.isWindows = process.platform === 'win32';
|
||||||
|
|
||||||
if (process.env.TEST_THREAD_ID) {
|
if (process.env.TEST_THREAD_ID) {
|
||||||
// Distribute ports in parallel tests
|
// Distribute ports in parallel tests
|
||||||
|
30
test/parallel/test-os-homedir-no-envvar.js
Normal file
30
test/parallel/test-os-homedir-no-envvar.js
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
'use strict';
|
||||||
|
var common = require('../common');
|
||||||
|
var assert = require('assert');
|
||||||
|
var cp = require('child_process');
|
||||||
|
var os = require('os');
|
||||||
|
var path = require('path');
|
||||||
|
|
||||||
|
|
||||||
|
if (process.argv[2] === 'child') {
|
||||||
|
if (common.isWindows)
|
||||||
|
assert.equal(process.env.USERPROFILE, undefined);
|
||||||
|
else
|
||||||
|
assert.equal(process.env.HOME, undefined);
|
||||||
|
|
||||||
|
var home = os.homedir();
|
||||||
|
|
||||||
|
assert.ok(typeof home === 'string');
|
||||||
|
assert.ok(home.indexOf(path.sep) !== -1);
|
||||||
|
} else {
|
||||||
|
if (common.isWindows)
|
||||||
|
delete process.env.USERPROFILE;
|
||||||
|
else
|
||||||
|
delete process.env.HOME;
|
||||||
|
|
||||||
|
var child = cp.spawnSync(process.execPath, [__filename, 'child'], {
|
||||||
|
env: process.env
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.equal(child.status, 0);
|
||||||
|
}
|
@ -2,12 +2,13 @@
|
|||||||
var common = require('../common');
|
var common = require('../common');
|
||||||
var assert = require('assert');
|
var assert = require('assert');
|
||||||
var os = require('os');
|
var os = require('os');
|
||||||
|
var path = require('path');
|
||||||
|
|
||||||
|
|
||||||
process.env.TMPDIR = '/tmpdir';
|
process.env.TMPDIR = '/tmpdir';
|
||||||
process.env.TMP = '/tmp';
|
process.env.TMP = '/tmp';
|
||||||
process.env.TEMP = '/temp';
|
process.env.TEMP = '/temp';
|
||||||
if (process.platform === 'win32') {
|
if (common.isWindows) {
|
||||||
assert.equal(os.tmpdir(), '/temp');
|
assert.equal(os.tmpdir(), '/temp');
|
||||||
process.env.TEMP = '';
|
process.env.TEMP = '';
|
||||||
assert.equal(os.tmpdir(), '/tmp');
|
assert.equal(os.tmpdir(), '/tmp');
|
||||||
@ -101,3 +102,22 @@ switch (platform) {
|
|||||||
|
|
||||||
var EOL = os.EOL;
|
var EOL = os.EOL;
|
||||||
assert.ok(EOL.length > 0);
|
assert.ok(EOL.length > 0);
|
||||||
|
|
||||||
|
|
||||||
|
var home = os.homedir();
|
||||||
|
|
||||||
|
console.log('homedir = ' + home);
|
||||||
|
assert.ok(typeof home === 'string');
|
||||||
|
assert.ok(home.indexOf(path.sep) !== -1);
|
||||||
|
|
||||||
|
if (common.isWindows && process.env.USERPROFILE) {
|
||||||
|
assert.equal(home, process.env.USERPROFILE);
|
||||||
|
delete process.env.USERPROFILE;
|
||||||
|
assert.ok(os.homedir().indexOf(path.sep) !== -1);
|
||||||
|
process.env.USERPROFILE = home;
|
||||||
|
} else if (!common.isWindows && process.env.HOME) {
|
||||||
|
assert.equal(home, process.env.HOME);
|
||||||
|
delete process.env.HOME;
|
||||||
|
assert.ok(os.homedir().indexOf(path.sep) !== -1);
|
||||||
|
process.env.HOME = home;
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user