diff --git a/lib/binding.js b/lib/binding.js index d259599..3870308 100644 --- a/lib/binding.js +++ b/lib/binding.js @@ -844,28 +844,43 @@ Binding.prototype.readdir = function ( }); }; -Binding.prototype.readFile = function (filepath, options, callback, ctx) { - markSyscall(ctx, 'readFile'); - - return maybeCallback(normalizeCallback(callback), ctx, this, function () { - filepath = deBuffer(filepath); - const item = this._system.getItem(filepath); - - return item.getContent(); - }); -}; +/** + * Read file as utf8 string. + * @param {string} name file to write. + * @param {number} flags Flags. + * @return {string} the file content. + */ +Binding.prototype.readFileUtf8 = function (name, flags) { + const fd = this.open(name, flags); + const descriptor = this.getDescriptorById(fd); -Binding.prototype.readFileSync = function (filepath, options, ctx) { - return this.readFile(filepath, options, undefined, ctx); -}; -Binding.prototype.readFileUtf8 = function (filepath, options, ctx) { - return this.readFile(filepath, options, undefined, ctx).toString('utf-8'); + if (!descriptor.isRead()) { + throw new FSError('EBADF'); + } + const file = descriptor.getItem(); + if (file instanceof Directory) { + throw new FSError('EISDIR'); + } + if (!(file instanceof File)) { + // deleted or not a regular file + throw new FSError('EBADF'); + } + const content = file.getContent(); + return content.toString('utf8'); }; +/** + * Write a utf8 string. + * @param {string} filepath file to write. + * @param {string} data data to write to filepath. + * @param {number} flags Flags. + * @param {number} mode Mode. + */ Binding.prototype.writeFileUtf8 = function (filepath, data, flags, mode) { const destFd = this.open(filepath, flags, mode); this.writeBuffer(destFd, data, 0, data.length); }; + /** * Create a directory. * @param {string} pathname Path to new directory.