Skip to content

Commit d7b214f

Browse files
committed
More like lib/libc/stdio/mktemp.c
1 parent 7d4579f commit d7b214f

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

lib/binding.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -711,13 +711,13 @@ Binding.prototype.rmdir = function(pathname, callback) {
711711
};
712712

713713

714-
var letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
715-
// see https://github.com/joyent/libuv/pull/1368/files#diff-aa0127b468d5b4716e070fce75ecdc54R725
714+
var PATH_CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
716715

717-
var attempts = 62 * 62 * 62;
716+
var MAX_ATTEMPTS = 62 * 62 * 62;
718717

719718
/**
720-
* Create a directory.
719+
* Create a directory based on a template.
720+
* See http://web.mit.edu/freebsd/head/lib/libc/stdio/mktemp.c
721721
* @param {string} template Path template (trailing Xs will be replaced).
722722
* @param {string} encoding The encoding ('utf-8' or 'buffer').
723723
* @param {function(Error, string)} callback Optional callback.
@@ -742,11 +742,11 @@ Binding.prototype.mkdtemp = function(prefix, encoding, callback) {
742742
var unique = false;
743743
var count = 0;
744744
var name;
745-
while (!unique && count < attempts) {
745+
while (!unique && count < MAX_ATTEMPTS) {
746746
var position = template.length - 1;
747747
var replacement = '';
748748
while (template.charAt(position) === 'X') {
749-
replacement += letters.charAt(Math.floor(letters.length * Math.random()));
749+
replacement += PATH_CHARS.charAt(Math.floor(PATH_CHARS.length * Math.random()));
750750
position -= 1;
751751
}
752752
var candidate = template.slice(0, position + 1) + replacement;
@@ -757,7 +757,7 @@ Binding.prototype.mkdtemp = function(prefix, encoding, callback) {
757757
count += 1;
758758
}
759759
if (!name) {
760-
throw new Error('Failed to find a unique name in ' + attempts + ' attempts');
760+
throw new FSError('EEXIST', prefix);
761761
}
762762
var dir = new Directory();
763763
parent.addItem(name, dir);

0 commit comments

Comments
 (0)