Browse Source

os: refine tmpdir() trailing slash stripping

os.tmpdir() began stripping trailing slashes in
b57cc51d8d. This causes problems if
the temp directory is simply '/'. It also stripped trailing
slashes without first determining which slash type is used by
the current operating system. This commit only strips trailing
slashes if another character precedes the slash. On Windows, it
checks for ':', as not to strip slashes from something like 'C:\'.
It also only strips slashes that are appropriate for the user's
operating system.

Fixes: https://github.com/iojs/io.js/issues/1669
PR-URL: https://github.com/iojs/io.js/pull/1673
Reviewed-By: Rod Vagg <rod@vagg.org>
Reviewed-By: Christian Tellnes <christian@tellnes.no>
v2.0.2
cjihrig 10 years ago
parent
commit
76937051f8
  1. 5
      lib/os.js
  2. 10
      test/parallel/test-os.js

5
lib/os.js

@ -22,6 +22,9 @@ exports.platform = function() {
return process.platform;
};
const trailingSlashRe = isWindows ? /[^:]\\$/
: /.\/$/;
exports.tmpdir = function() {
var path;
if (isWindows) {
@ -34,7 +37,7 @@ exports.tmpdir = function() {
process.env.TEMP ||
'/tmp';
}
if (/[\\\/]$/.test(path))
if (trailingSlashRe.test(path))
path = path.slice(0, -1);
return path;
};

10
test/parallel/test-os.js

@ -15,6 +15,12 @@ if (process.platform === 'win32') {
assert.equal(os.tmpdir(), expected);
process.env.TEMP = '\\temp\\';
assert.equal(os.tmpdir(), '\\temp');
process.env.TEMP = '\\tmpdir/';
assert.equal(os.tmpdir(), '\\tmpdir/');
process.env.TEMP = '\\';
assert.equal(os.tmpdir(), '\\');
process.env.TEMP = 'C:\\';
assert.equal(os.tmpdir(), 'C:\\');
} else {
assert.equal(os.tmpdir(), '/tmpdir');
process.env.TMPDIR = '';
@ -25,6 +31,10 @@ if (process.platform === 'win32') {
assert.equal(os.tmpdir(), '/tmp');
process.env.TMPDIR = '/tmpdir/';
assert.equal(os.tmpdir(), '/tmpdir');
process.env.TMPDIR = '/tmpdir\\';
assert.equal(os.tmpdir(), '/tmpdir\\');
process.env.TMPDIR = '/';
assert.equal(os.tmpdir(), '/');
}
var endianness = os.endianness();

Loading…
Cancel
Save