Added/fixed mkdtemp method (issue 441)

This commit is contained in:
Alexei Kozachenko 2018-09-22 23:37:11 -04:00
parent 37da7097b0
commit b506dceda8
1 changed files with 10 additions and 33 deletions

View File

@ -1678,7 +1678,7 @@ function mkdir(fs, context, path, mode, callback) {
make_directory(context, path, callback); make_directory(context, path, callback);
} }
function mkdtemp(fs, context, prefix, callback) { function mkdtemp(fs, context, prefix, options, callback) {
callback = arguments[arguments.length - 1]; callback = arguments[arguments.length - 1];
// this function is ised to generate a random character set to append // this function is ised to generate a random character set to append
@ -1689,32 +1689,8 @@ function mkdtemp(fs, context, prefix, callback) {
return v.toString(16); return v.toString(16);
}) })
} }
if (prefix) {
var tmpDir = "/tmp"; //tmp dir in root var path = "/"+prefix+generateRandom();
var tmpDirExists = false; //flag to check if already created
if (!prefix) {
prefix = generateRandom(); //if user did not specify prefix
}
prefix = prefix + "-" + generateRandom();
var path = Path.join(tmpDir, prefix); //path to a new tmp dir
// check if root temporary directory '/tmp' already exists
fs.stat(tmpDir, function(error, stats) {
if (!error) {
if (stats.isDirectory()) {
tmpDirExists = true;
}
}
});
//try to create /tmp in root
if (!tmpDirExists) {
make_directory(context, tmpDir, function(error) {
if (error) {
callback(error, path)
} else {
//create new temp dir in /tmp
make_directory(context, path, function(error) { make_directory(context, path, function(error) {
if (error) { if (error) {
callback(error, path) callback(error, path)
@ -1722,8 +1698,8 @@ function mkdtemp(fs, context, prefix, callback) {
callback(null, path); callback(null, path);
} }
}); });
} } else {
}); callback(new Error('filename prefix is required'), path)
} }
} }
@ -2433,6 +2409,7 @@ module.exports = {
close: close, close: close,
mknod: mknod, mknod: mknod,
mkdir: mkdir, mkdir: mkdir,
mkdtemp: mkdtemp,
rmdir: rmdir, rmdir: rmdir,
unlink: unlink, unlink: unlink,
stat: stat, stat: stat,