Skip to content

Commit

Permalink
feat(util): use promises for api
Browse files Browse the repository at this point in the history
BREAKING CHANGE: fix-owner now uses Promises instead of callbacks
  • Loading branch information
zkat committed Feb 27, 2017
1 parent a004fe6 commit ae204bb
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions lib/util/fix-owner.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,44 @@
'use strict'

var chownr = require('chownr')
var dezalgo = require('dezalgo')
var inflight = require('inflight')
var mkdirp = require('mkdirp')
const Promise = require('bluebird')

const chownr = Promise.promisify(require('chownr'))
const mkdirp = Promise.promisify(require('mkdirp'))

module.exports.chownr = fixOwner
function fixOwner (filepath, uid, gid, cb) {
cb = dezalgo(cb)
function fixOwner (filepath, uid, gid) {
if (!process.getuid) {
// This platform doesn't need ownership fixing
return cb()
return Promise.resolve()
}
if (typeof uid !== 'number' && typeof gid !== 'number') {
// There's no permissions override. Nothing to do here.
return cb()
return Promise.resolve()
}
if ((typeof uid === 'number' && process.getuid() === uid) &&
(typeof gid === 'number' && process.getgid() === gid)) {
// No need to override if it's already what we used.
return cb()
}
cb = inflight('fixOwner: fixing ownership on ' + filepath, cb)
if (!cb) {
// We're inflight! whoosh!
return
return Promise.resolve()
}
// cb = inflight('fixOwner: fixing ownership on ' + filepath, cb)
// if (!cb) {
// // We're inflight! whoosh!
// return
// }

// *now* we override perms
chownr(
return chownr(
filepath,
typeof uid === 'number' ? uid : process.getuid(),
typeof gid === 'number' ? gid : process.getgid(),
cb
typeof gid === 'number' ? gid : process.getgid()
)
}

module.exports.mkdirfix = mkdirfix
function mkdirfix (p, uid, gid, cb) {
mkdirp(p, function (err, made) {
if (err || !made) { return cb(err, made) }
fixOwner(made, uid, gid, cb)
return mkdirp(p).then(made => {
if (made) {
return fixOwner(made, uid, gid).then(() => made)
}
})
}

0 comments on commit ae204bb

Please sign in to comment.