Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Fix #3407 os.tmpDir()
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Jun 13, 2012
1 parent 0dba4ad commit a11bf99
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions doc/api/os.markdown
Expand Up @@ -6,6 +6,10 @@ Provides a few basic operating-system related utility functions.

Use `require('os')` to access this module.

## os.tmpDir()

Returns the operating system's default directory for temp files.

## os.hostname()

Returns the hostname of the operating system.
Expand Down
9 changes: 9 additions & 0 deletions lib/os.js
Expand Up @@ -30,13 +30,22 @@ exports.cpus = binding.getCPUs;
exports.type = binding.getOSType;
exports.release = binding.getOSRelease;
exports.networkInterfaces = binding.getInterfaceAddresses;

exports.arch = function() {
return process.arch;
};

exports.platform = function() {
return process.platform;
};

exports.tmpDir = function() {
return process.env.TMPDIR ||
process.env.TMP ||
process.env.TEMP ||
(process.platform === 'win32' ? 'c:\\windows\\temp' : '/tmp');

This comment has been minimized.

Copy link
@AndreasMadsen

AndreasMadsen Jun 13, 2012

Member

Should this not use some windows env there tell where the windows directory exist and then add the \\temp postfix?

I think it is process.env.WINDIR + "\\temp".

This comment has been minimized.

Copy link
@fb55

fb55 Jun 13, 2012

+1. Not every windows-installation is on the C: drive, it's even possible to have no C: drive (at least as far as I remember). This code is (probably) buggy.

This comment has been minimized.

Copy link
@bnoordhuis

bnoordhuis Jun 13, 2012

Member

I think it is process.env.WINDIR + "\temp"

What if WINDIR is not set?

This comment has been minimized.

Copy link
@Mithgol

Mithgol Mar 16, 2013

Putting the temp files in process.env.WINDIR + "\\temp" is sometimes going against the admin's intentions if process.env.TEMP and process.env.TMP are different. The latter should be used.

};

exports.getNetworkInterfaces = function() {
return exports.networkInterfaces();
};
Expand Down
12 changes: 12 additions & 0 deletions test/simple/test-os.js
Expand Up @@ -27,6 +27,18 @@ var assert = require('assert');
var os = require('os');


process.env.TMPDIR = '/tmpdir';
process.env.TMP = '/tmp';
process.env.TEMP = '/temp';
var t = ( process.platform === 'win32' ? 'c:\\windows\\temp' : '/tmp' );

This comment has been minimized.

Copy link
@eungjun-yi

eungjun-yi Mar 16, 2013

What occurs if windows is installed in D drive? Is it okay?

assert.equal(os.tmpDir(), '/tmpdir');
process.env.TMPDIR = '';
assert.equal(os.tmpDir(), '/tmp');
process.env.TMP = '';
assert.equal(os.tmpDir(), '/temp');
process.env.TEMP = '';
assert.equal(os.tmpDir(), t);

var hostname = os.hostname();
console.log('hostname = %s', hostname);
assert.ok(hostname.length > 0);
Expand Down

1 comment on commit a11bf99

@jbergstroem
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference, here's pythons current logic: http://docs.python.org/2/library/tempfile.html#tempfile.tempdir

Please sign in to comment.