Skip to content

Commit

Permalink
Fix race condition with file.mkdir and make it operate more similaril…
Browse files Browse the repository at this point in the history
…y to mkdir -p (#1627) r=@vladikoff
  • Loading branch information
shama authored and vladikoff committed Mar 15, 2018
1 parent 303d445 commit 0105524
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 15 deletions.
20 changes: 5 additions & 15 deletions lib/grunt/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var YAML = require('js-yaml');
var rimraf = require('rimraf');
var iconv = require('iconv-lite');
var pathIsAbsolute = require('path-is-absolute');
var mkdirp = require('mkdirp').sync;

// Windows?
var win32 = process.platform === 'win32';
Expand Down Expand Up @@ -180,22 +181,11 @@ file.expandMapping = function(patterns, destBase, options) {
// Like mkdir -p. Create a directory and any intermediary directories.
file.mkdir = function(dirpath, mode) {
if (grunt.option('no-write')) { return; }
// Set directory mode in a strict-mode-friendly way.
if (mode == null) {
mode = parseInt('0777', 8) & (~process.umask());
try {
mkdirp(dirpath, { mode: mode });
} catch (e) {
throw grunt.util.error('Unable to create directory "' + dirpath + '" (Error code: ' + e.code + ').', e);
}
dirpath.split(pathSeparatorRe).reduce(function(parts, part) {
parts += part + '/';
var subpath = path.resolve(parts);
if (!file.exists(subpath)) {
try {
fs.mkdirSync(subpath, mode);
} catch (e) {
throw grunt.util.error('Unable to create directory "' + subpath + '" (Error code: ' + e.code + ').', e);
}
}
return parts;
}, '');
};

// Recurse into a directory, executing callback for each file.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"iconv-lite": "~0.4.13",
"js-yaml": "~3.5.2",
"minimatch": "~3.0.2",
"mkdirp": "~0.5.1",
"nopt": "~3.0.6",
"path-is-absolute": "~1.0.0",
"rimraf": "~2.2.8"
Expand Down

0 comments on commit 0105524

Please sign in to comment.