-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tmp): safe tmp dir creation/management util (#73)
This might seem odd for a caching library, but it turns out a lot of work that will end up *in* the cache needs to be frobbed before it can be used. In these cases, users may request a unique tmp directory with safe ownership management.
- Loading branch information
Showing
4 changed files
with
119 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict' | ||
|
||
const BB = require('bluebird') | ||
|
||
const fixOwner = require('./fix-owner') | ||
const path = require('path') | ||
const rimraf = BB.promisify(require('rimraf')) | ||
const uniqueFilename = require('unique-filename') | ||
|
||
module.exports.mkdir = mktmpdir | ||
function mktmpdir (cache, opts) { | ||
opts = opts || {} | ||
const tmpTarget = uniqueFilename(path.join(cache, 'tmp'), opts.tmpPrefix) | ||
return fixOwner.mkdirfix(tmpTarget, opts.uid, opts.gid).then(() => { | ||
return tmpTarget | ||
}) | ||
} | ||
|
||
module.exports.withTmp = withTmp | ||
function withTmp (cache, opts, cb) { | ||
if (!cb) { | ||
cb = opts | ||
opts = null | ||
} | ||
opts = opts || {} | ||
return BB.using(mktmpdir(cache, opts).disposer(rimraf), cb) | ||
} | ||
|
||
module.exports.fix = fixtmpdir | ||
function fixtmpdir (cache, opts) { | ||
return fixOwner(path.join(cache, 'tmp'), opts.uid, opts.gid) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
'use strict' | ||
|
||
const BB = require('bluebird') | ||
|
||
const fs = BB.promisifyAll(require('graceful-fs')) | ||
const path = require('path') | ||
const test = require('tap').test | ||
|
||
const CACHE = require('./util/test-dir')(__filename) | ||
|
||
const tmp = require('../lib/util/tmp') | ||
|
||
test('creates a unique tmpdir inside the cache', t => { | ||
return tmp.mkdir(CACHE).then(dir => { | ||
t.match(path.relative(CACHE, dir), /^tmp[\\/].*/, 'returns a path inside tmp') | ||
return fs.statAsync(dir) | ||
}).then(stat => { | ||
t.ok(stat.isDirectory(), 'path points to an existing directory') | ||
}) | ||
}) | ||
|
||
test('provides a utility that does resource disposal on tmp', t => { | ||
return tmp.withTmp(CACHE, dir => { | ||
return fs.statAsync(dir).then(stat => { | ||
t.ok(stat.isDirectory(), 'path points to an existing directory') | ||
}).then(() => dir) | ||
}).then(dir => { | ||
return BB.join( | ||
fs.statAsync(dir).then(() => { | ||
throw new Error('expected fail') | ||
}).catch({code: 'ENOENT'}, () => {}), | ||
fs.statAsync(path.join(CACHE, 'tmp')), | ||
(nope, yes) => { | ||
t.notOk(nope, 'tmp subdir removed') | ||
t.ok(yes.isDirectory(), 'tmp parent dir left intact') | ||
} | ||
) | ||
}) | ||
}) | ||
|
||
test('makes sure ownership is correct') | ||
test('provides a function for fixing ownership in the tmp dir') |