Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prepend opts.dir || tmpDir to template if no path is given #144

Merged
merged 1 commit into from
Nov 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,14 @@ console.log('Dir: ', tmpobj.name);

Creates a new temporary directory with mode `0700` and filename like `/tmp/tmp-nk2J1u`.

IMPORTANT NOTE: template no longer accepts a path. Use the dir option instead if you
Copy link
Owner

Choose a reason for hiding this comment

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

This does not apply because we still support full paths.

require tmp to create your temporary filesystem object in a different place than the
default `tmp.tmpdir`.

```javascript
var tmp = require('tmp');

tmp.dir({ template: '/tmp/tmp-XXXXXX' }, function _tempDirCreated(err, path) {
tmp.dir({ template: 'tmp-XXXXXX' }, function _tempDirCreated(err, path) {
if (err) throw err;

console.log('Dir: ', path);
Expand All @@ -254,7 +258,7 @@ This will behave similarly to the asynchronous version.
```javascript
var tmp = require('tmp');

var tmpobj = tmp.dirSync({ template: '/tmp/tmp-XXXXXX' });
var tmpobj = tmp.dirSync({ template: 'tmp-XXXXXX' });
console.log('Dir: ', tmpobj.name);
```

Expand Down
6 changes: 5 additions & 1 deletion lib/tmp.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ function _generateTmpName(opts) {

// mkstemps like template
if (opts.template) {
return opts.template.replace(TEMPLATE_PATTERN, _randomChars(6));
var template = opts.template;
// make sure that we prepend the tmp path if none was given
if (path.basename(template) == template)
Copy link
Owner

@raszi raszi Nov 28, 2017

Choose a reason for hiding this comment

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

This will be still a breaking change, but since this is still a beta software and most probably nobody used the template option as it is without a full path, therefore I am okay with the change.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@raszi Yes, this is somewhat breaking. And there may still exist security concerns for templates that resemble paths...

template = path.join(opts.dir || tmpDir, template);
return template.replace(TEMPLATE_PATTERN, _randomChars(6));
}

// prefix and postfix
Expand Down
2 changes: 1 addition & 1 deletion test/dir-sync-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ vows.describe('Synchronous directory creation').addBatch({

'when using template': {
topic: function () {
return tmp.dirSync({ template: path.join(tmp.tmpdir, 'clike-XXXXXX-postfix') });
return tmp.dirSync({ template: 'clike-XXXXXX-postfix' });
},

'should return with a name': Test.assertNameSync,
Expand Down
2 changes: 1 addition & 1 deletion test/dir-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ vows.describe('Directory creation').addBatch({

'when using template': {
topic: function () {
tmp.dir({ template: path.join(tmp.tmpdir, 'clike-XXXXXX-postfix') }, this.callback);
tmp.dir({ template: 'clike-XXXXXX-postfix' }, this.callback);
},

'should not return with error': assert.isNull,
Expand Down
2 changes: 1 addition & 1 deletion test/file-sync-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ vows.describe('Synchronous file creation').addBatch({

'when using template': {
topic: function () {
return tmp.fileSync({ template: path.join(tmp.tmpdir, 'clike-XXXXXX-postfix') });
return tmp.fileSync({ template: 'clike-XXXXXX-postfix' });
},

'should return with a name': Test.assertNameSync,
Expand Down
2 changes: 1 addition & 1 deletion test/file-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ vows.describe('File creation').addBatch({

'when using template': {
topic: function () {
tmp.file({ template: path.join(tmp.tmpdir, 'clike-XXXXXX-postfix') }, this.callback);
tmp.file({ template: 'clike-XXXXXX-postfix' }, this.callback);
},

'should not return with an error': assert.isNull,
Expand Down
2 changes: 1 addition & 1 deletion test/name-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ vows.describe('Name creation').addBatch({

'when using template': {
topic: function () {
tmp.tmpName({ template: path.join(tmp.tmpdir, 'clike-XXXXXX-postfix') }, this.callback);
tmp.tmpName({ template: 'clike-XXXXXX-postfix' }, this.callback);
},

'should not return with error': assert.isNull,
Expand Down