Skip to content
This repository has been archived by the owner on Aug 11, 2022. It is now read-only.

Commit

Permalink
Close #1509 Add 'umask' config option
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Oct 12, 2011
1 parent f85a393 commit dd7359a
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 18 deletions.
12 changes: 12 additions & 0 deletions doc/cli/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,18 @@ The location of a user-level ignore file to apply to all packages.
If not found, but there is a .gitignore file in the same directory, then
that will be used instead.

### umask

* Default: 022
* Type: Octal numeric string

The "umask" value to use when setting the file creation mode on files
and folders.

Folders and executables are given a mode which is `0777` masked against
this value. Other files are given a mode which is `0666` masked against
this value. Thus, the defaults are `0755` and `0644` respectively.

### version

* Default: false
Expand Down
12 changes: 5 additions & 7 deletions lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ var mkdir = require("./utils/mkdir-p.js")
, asyncMap = require("slide").asyncMap
, semver = require("semver")
, tar = require("./utils/tar.js")
, FMODE = tar.FMODE
, DMODE = tar.DMODE
, fileCompletion = require("./utils/completion/file-completion.js")
, url = require("url")

Expand Down Expand Up @@ -543,7 +541,7 @@ function getCacheStat (cb) {
}

function makeCacheDir (cb) {
if (!process.getuid) return mkdir(npm.cache, DMODE, cb)
if (!process.getuid) return mkdir(npm.cache, npm.modes.exec, cb)

var uid = +process.getuid()
, gid = +process.getgid()
Expand All @@ -554,15 +552,15 @@ function makeCacheDir (cb) {
}
if (uid !== 0 || !process.env.HOME) {
cacheStat = {uid: uid, gid: gid}
return mkdir(npm.cache, DMODE, uid, gid, function (er) {
return mkdir(npm.cache, npm.modes.exec, uid, gid, function (er) {
return cb(er, cacheStat)
})
}
fs.stat(process.env.HOME, function (er, st) {
if (er) return log.er(cb, "homeless?")(er)
cacheStat = st
log.silly([st.uid, st.gid], "uid, gid for cache dir")
return mkdir(npm.cache, DMODE, st.uid, st.gid, function (er) {
return mkdir(npm.cache, npm.modes.exec, st.uid, st.gid, function (er) {
return cb(er, cacheStat)
})
})
Expand Down Expand Up @@ -600,8 +598,8 @@ function addPlacedTarball_ (p, name, uid, gid, cb) {
if (shasum) data.dist.shasum = shasum
deprCheck(data)
asyncMap([p], function (f, cb) {
log.verbose(FMODE.toString(8), "chmod "+f)
fs.chmod(f, FMODE, cb)
log.verbose(npm.modes.file.toString(8), "chmod "+f)
fs.chmod(f, npm.modes.file, cb)
}, function (f, cb) {
if (typeof uid === "number" && typeof gid === "number") {
fs.chown(f, uid, gid, cb)
Expand Down
20 changes: 19 additions & 1 deletion lib/utils/config-defs.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,26 @@ var path = require("path")
, nopt = require("nopt")
, log = require("./log.js")

nopt.typeDefs.semver = { type: semver, validate: validateSemver }
function Octal () {}
function validateOctal (data, k, val) {
// must be either an integer or an octal string.
if (typeof val === "number") {
data[k] = "0" + val.toString(8)
}
if (typeof val === "string") {
if (val.charAt(0) !== "0" || isNaN(val)) return false
data[k] = "0" + parseInt(val, 8).toString(8)
}
}

function validateSemver (data, k, val) {
if (!semver.valid(val)) return false
data[k] = semver.valid(val)
}

nopt.typeDefs.semver = { type: semver, validate: validateSemver }
nopt.typeDefs.Octal = { type: Octal, validate: validateOctal }

nopt.invalidHandler = function (k, val, type, data) {
log.warn(k + "=" + JSON.stringify(val), "invalid config")

Expand All @@ -26,6 +39,9 @@ nopt.invalidHandler = function (k, val, type, data) {
}

switch (type) {
case Octal:
log.warn("Must be octal number, starting with 0", "invalid config")
break
case url:
log.warn("Must be a full url with 'http://'", "invalid config")
break
Expand Down Expand Up @@ -171,6 +187,7 @@ Object.defineProperty(exports, "defaults", {get: function () {
, username : ""
, userconfig : path.resolve(home, ".npmrc")
, userignorefile : path.resolve(home, ".npmignore")
, umask: 022
, version : false
, viewer: process.platform === "win32" ? "browser" : "man"
, yes: null
Expand Down Expand Up @@ -236,6 +253,7 @@ exports.types =
, username : String
, userconfig : path
, userignorefile : path
, umask: Octal
, version : Boolean
, viewer: String
, yes: [false, null, Boolean]
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function fetch (remote, local, headers, cb) {
}

function fetch_ (remote, local, headers, cb) {
var fstr = fs.createWriteStream(local, { mode : 0644 })
var fstr = fs.createWriteStream(local, { mode : npm.modes.file })
fstr.on("error", function (er) {
fs.close(fstr.fd, function () {})
if (fstr._ERROR) return
Expand Down
16 changes: 7 additions & 9 deletions lib/utils/tar.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// commands for packing and unpacking tarballs
// this file is used by lib/cache.js

var FMODE = exports.FMODE = 0644
, DMODE = exports.DMODE = 0755
, npm = require("../../npm.js")
var npm = require("../../npm.js")
, fs = require("graceful-fs")
, exec = require("./exec.js")
, spawn = exec.spawn
Expand Down Expand Up @@ -94,7 +92,7 @@ function packFiles (targetTarball, parent, files, pkg, cb) {
target.on("close", function (er, ok) {
if (errState) return
if (er) return cb(errState = er)
fs.chmod(targetTarball, 0644, function (er) {
fs.chmod(targetTarball, npm.modes.file, function (er) {
if (errState) return
return cb(errState = er)
})
Expand All @@ -106,8 +104,8 @@ function packFiles (targetTarball, parent, files, pkg, cb) {
function unpack (tarball, unpackTarget, dMode, fMode, uid, gid, cb) {
if (typeof cb !== "function") cb = gid, gid = null
if (typeof cb !== "function") cb = uid, uid = null
if (typeof cb !== "function") cb = fMode, fMode = FMODE
if (typeof cb !== "function") cb = dMode, dMode = DMODE
if (typeof cb !== "function") cb = fMode, fMode = npm.modes.file
if (typeof cb !== "function") cb = dMode, dMode = npm.modes.exec

uidNumber(uid, gid, function (er, uid, gid) {
if (er) return cb(er)
Expand All @@ -123,7 +121,7 @@ function unpack_ ( tarball, unpackTarget, dMode, fMode, uid, gid, cb ) {
, base = path.basename(unpackTarget)
, tmp = path.resolve(parent, "___" + base + ".npm")

mkdir(tmp, dMode || DMODE, uid, gid, function (er) {
mkdir(tmp, dMode || npm.modes.exec, uid, gid, function (er) {
log.verbose([uid, gid], "unpack_ uid, gid")
log.verbose(unpackTarget, "unpackTarget")
if (er) return log.er(cb, "Could not create "+tmp)(er)
Expand Down Expand Up @@ -157,8 +155,8 @@ function unpack_ ( tarball, unpackTarget, dMode, fMode, uid, gid, cb ) {
}

function gunzTarPerm (tarball, tmp, dMode, fMode, uid, gid, cb) {
if (!dMode) dMode = DMODE
if (!fMode) fMode = FMODE
if (!dMode) dMode = npm.modes.exec
if (!fMode) fMode = npm.modes.file
log.silly([dMode.toString(8), fMode.toString(8)], "gunzTarPerm modes")

// HACK tar on windows prefers forward slashes (or double backslashes)
Expand Down
4 changes: 4 additions & 0 deletions npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,10 @@ function load (npm, conf, cb) {
loadPrefix(npm, conf, next)
loadUid(npm, conf, next)

var umask = parseInt(conf.umask, 8)
npm.modes = { exec: 0777 ^ umask
, file: 0666 ^ umask }

function next (er) {
//console.error("next", er && er.stack)
if (errState) return
Expand Down

0 comments on commit dd7359a

Please sign in to comment.