fix: mkdir accepts encoding options

This commit is contained in:
bcheidemann 2021-05-23 13:20:05 +01:00
parent 2e381d282c
commit 1c74d46208
1 changed files with 44 additions and 6 deletions

View File

@ -1120,26 +1120,53 @@ function unlink_node(context, path, callback) {
find_node(context, parentPath, read_directory_data); find_node(context, parentPath, read_directory_data);
} }
function read_directory(context, path, callback) { function read_directory(context, path, options, callback) {
path = normalize(path); path = normalize(path);
if (typeof options === 'function') {
callback = options;
options = {};
}
options = validate_directory_options(options);
var directoryNode; var directoryNode;
var directoryData; var directoryData;
function handle_directory_data(error, result) { function handle_directory_data(error, result) {
if(error) { if (error) {
callback(error); callback(error);
} else { } else {
directoryData = result; directoryData = result;
var files = Object.keys(directoryData); var files = Object.keys(directoryData);
if (options.encoding) {
var fileBuffers = files.map(function (file) {
return Buffer.from(file);
});
if (options.encoding === 'buffer') {
files = fileBuffers;
}
else {
files = fileBuffers.map(function (fileBuffer) {
return fileBuffer.toString(options.encoding);
});
}
}
if (options.withFileTypes) {
// TODO: map files to fs.Dirent
return callback(new Error('readdir does not support option withFileTypes yet'));
}
callback(null, files); callback(null, files);
} }
} }
function read_directory_data(error, result) { function read_directory_data(error, result) {
if(error) { if (error) {
callback(error); callback(error);
} else if(result.type !== NODE_TYPE_DIRECTORY) { } else if (result.type !== NODE_TYPE_DIRECTORY) {
callback(new Errors.ENOTDIR(null, path)); callback(new Errors.ENOTDIR(null, path));
} else { } else {
directoryNode = result; directoryNode = result;
@ -1150,6 +1177,17 @@ function read_directory(context, path, callback) {
find_node(context, path, read_directory_data); find_node(context, path, read_directory_data);
} }
function validate_directory_options(options, enc) {
if (!options) {
options = { encoding: enc };
} else if (typeof options === 'function') {
options = { encoding: enc };
} else if (typeof options === 'string') {
options = { encoding: options };
}
return options;
}
function make_symbolic_link(context, srcpath, dstpath, callback) { function make_symbolic_link(context, srcpath, dstpath, callback) {
dstpath = normalize(dstpath); dstpath = normalize(dstpath);
var name = basename(dstpath); var name = basename(dstpath);
@ -2163,8 +2201,8 @@ function lseek(context, fd, offset, whence, callback) {
} }
} }
function readdir(context, path, callback) { function readdir(context, path, options, callback) {
read_directory(context, path, callback); read_directory(context, path, options, callback);
} }
function toUnixTimestamp(time) { function toUnixTimestamp(time) {