Skip to content

Commit dd9b683

Browse files
rvaggaduh95
authored andcommitted
Revert "fs,win: fix bug in paths with trailing slashes"
This reverts commit 00b2f07. PR-URL: #55527 Fixes: #17801 Reviewed-By: Rafael Gonzaga <[email protected]> Reviewed-By: Juan José Arboleda <[email protected]> Reviewed-By: Marco Ippolito <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Stefan Stojanovic <[email protected]>
1 parent d0ea981 commit dd9b683

File tree

5 files changed

+20
-237
lines changed

5 files changed

+20
-237
lines changed

lib/fs.js

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -385,11 +385,7 @@ function readFile(path, options, callback) {
385385
const req = new FSReqCallback();
386386
req.context = context;
387387
req.oncomplete = readFileAfterOpen;
388-
binding.open(
389-
getValidatedPath(path, 'path', { expectFile: true, syscall: 'read' }),
390-
flagsNumber,
391-
0o666,
392-
req);
388+
binding.open(getValidatedPath(path), flagsNumber, 0o666, req);
393389
}
394390

395391
function tryStatSync(fd, isUserFd) {
@@ -441,9 +437,7 @@ function readFileSync(path, options) {
441437

442438
if (options.encoding === 'utf8' || options.encoding === 'utf-8') {
443439
if (!isInt32(path)) {
444-
path = getValidatedPath(path,
445-
'path',
446-
{ expectFile: true, syscall: 'read' });
440+
path = getValidatedPath(path);
447441
}
448442
return binding.readFileUtf8(path, stringToFlags(options.flag));
449443
}
@@ -537,7 +531,7 @@ function closeSync(fd) {
537531
* @returns {void}
538532
*/
539533
function open(path, flags, mode, callback) {
540-
path = getValidatedPath(path, 'path', { expectFile: true, syscall: 'open' });
534+
path = getValidatedPath(path);
541535
if (arguments.length < 3) {
542536
callback = flags;
543537
flags = 'r';
@@ -566,7 +560,7 @@ function open(path, flags, mode, callback) {
566560
*/
567561
function openSync(path, flags, mode) {
568562
return binding.open(
569-
getValidatedPath(path, 'path', { expectFile: true, syscall: 'open' }),
563+
getValidatedPath(path),
570564
stringToFlags(flags),
571565
parseFileMode(mode, 'mode', 0o666),
572566
);
@@ -2345,9 +2339,7 @@ function writeFileSync(path, data, options) {
23452339
// C++ fast path for string data and UTF8 encoding
23462340
if (typeof data === 'string' && (options.encoding === 'utf8' || options.encoding === 'utf-8')) {
23472341
if (!isInt32(path)) {
2348-
path = getValidatedPath(path,
2349-
'path',
2350-
{ expectFile: true, syscall: 'write' });
2342+
path = getValidatedPath(path);
23512343
}
23522344

23532345
return binding.writeFileUtf8(
@@ -2992,8 +2984,8 @@ function copyFile(src, dest, mode, callback) {
29922984
mode = 0;
29932985
}
29942986

2995-
src = getValidatedPath(src, 'src', { expectFile: true, syscall: 'cp' });
2996-
dest = getValidatedPath(dest, 'dest', { expectFile: true, syscall: 'cp' });
2987+
src = getValidatedPath(src, 'src');
2988+
dest = getValidatedPath(dest, 'dest');
29972989
callback = makeCallback(callback);
29982990

29992991
const req = new FSReqCallback();
@@ -3011,8 +3003,8 @@ function copyFile(src, dest, mode, callback) {
30113003
*/
30123004
function copyFileSync(src, dest, mode) {
30133005
binding.copyFile(
3014-
getValidatedPath(src, 'src', { expectFile: true, syscall: 'cp' }),
3015-
getValidatedPath(dest, 'dest', { expectFile: true, syscall: 'cp' }),
3006+
getValidatedPath(src, 'src'),
3007+
getValidatedPath(dest, 'dest'),
30163008
mode,
30173009
);
30183010
}

lib/internal/fs/promises.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -620,8 +620,8 @@ async function cp(src, dest, options) {
620620
async function copyFile(src, dest, mode) {
621621
return await PromisePrototypeThen(
622622
binding.copyFile(
623-
getValidatedPath(src, 'src', { expectFile: true, syscall: 'cp' }),
624-
getValidatedPath(dest, 'dest', { expectFile: true, syscall: 'cp' }),
623+
getValidatedPath(src, 'src'),
624+
getValidatedPath(dest, 'dest'),
625625
mode,
626626
kUsePromises,
627627
),
@@ -633,7 +633,7 @@ async function copyFile(src, dest, mode) {
633633
// Note that unlike fs.open() which uses numeric file descriptors,
634634
// fsPromises.open() uses the fs.FileHandle class.
635635
async function open(path, flags, mode) {
636-
path = getValidatedPath(path, 'path', { expectFile: true, syscall: 'open' });
636+
path = getValidatedPath(path);
637637
const flagsNumber = stringToFlags(flags);
638638
mode = parseFileMode(mode, 'mode', 0o666);
639639
return new FileHandle(await PromisePrototypeThen(

lib/internal/fs/streams.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ function ReadStream(path, options) {
182182
this.flags = options.flags === undefined ? 'r' : options.flags;
183183
this.mode = options.mode === undefined ? 0o666 : options.mode;
184184

185-
validatePath(this.path, 'path', { expectFile: true, syscall: 'read' });
185+
validatePath(this.path);
186186
} else {
187187
this.fd = getValidatedFd(importFd(this, options));
188188
}
@@ -337,7 +337,7 @@ function WriteStream(path, options) {
337337
this.flags = options.flags === undefined ? 'w' : options.flags;
338338
this.mode = options.mode === undefined ? 0o666 : options.mode;
339339

340-
validatePath(this.path, 'path', { expectFile: true, syscall: 'write' });
340+
validatePath(this.path);
341341
} else {
342342
this.fd = getValidatedFd(importFd(this, options));
343343
}

lib/internal/fs/utils.js

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -710,38 +710,13 @@ const validateOffsetLengthWrite = hideStackFrames(
710710
},
711711
);
712712

713-
/**
714-
* Validates the given path based on the expected type (file or directory).
715-
* @param {string} path - The path to validate.
716-
* @param {string} [propName='path'] - The name of the property being validated.
717-
* @param {object} options - Additional options for validation.
718-
* @param {boolean} [options.expectFile] - If true, expects the path to be a file.
719-
* @param {string} [options.syscall] - The name of the syscall.
720-
* @throws {TypeError} Throws if the path is not a string, Uint8Array, or URL without null bytes.
721-
* @throws {Error} Throws if the path does not meet the expected type (file).
722-
*/
723-
const validatePath = hideStackFrames((path, propName = 'path', options) => {
713+
const validatePath = hideStackFrames((path, propName = 'path') => {
724714
if (typeof path !== 'string' && !isUint8Array(path)) {
725715
throw new ERR_INVALID_ARG_TYPE.HideStackFramesError(propName, ['string', 'Buffer', 'URL'], path);
726716
}
727717

728718
const pathIsString = typeof path === 'string';
729719
const pathIsUint8Array = isUint8Array(path);
730-
if (options?.expectFile) {
731-
const lastCharacter = path[path.length - 1];
732-
if (
733-
lastCharacter === '/' || lastCharacter === 47 ||
734-
(isWindows && (lastCharacter === '\\' || lastCharacter === 92))
735-
) {
736-
throw new ERR_FS_EISDIR({
737-
code: 'ERR_FS_EISDIR',
738-
message: 'is a directory',
739-
path,
740-
syscall: options.syscall,
741-
errno: ERR_FS_EISDIR,
742-
});
743-
}
744-
}
745720

746721
// We can only perform meaningful checks on strings and Uint8Arrays.
747722
if ((!pathIsString && !pathIsUint8Array) ||
@@ -757,21 +732,11 @@ const validatePath = hideStackFrames((path, propName = 'path', options) => {
757732
);
758733
});
759734

760-
/**
761-
* Validates and returns the given file URL or path based on the expected type (file or directory).
762-
* @param {string|URL} fileURLOrPath - The file URL or path to validate.
763-
* @param {string} [propName='path'] - The name of the property being validated.
764-
* @param {object} options - Additional options for validation.
765-
* @param {boolean} [options.expectFile] - If true, expects the path to be a file.
766-
* @param {string} [options.syscall] - The name of the syscall.
767-
* @returns {string} The validated path.
768-
*/
769-
const getValidatedPath =
770-
hideStackFrames((fileURLOrPath, propName = 'path', options) => {
771-
const path = toPathIfFileURL(fileURLOrPath);
772-
validatePath(path, propName, options);
773-
return path;
774-
});
735+
const getValidatedPath = hideStackFrames((fileURLOrPath, propName = 'path') => {
736+
const path = toPathIfFileURL(fileURLOrPath);
737+
validatePath(path, propName);
738+
return path;
739+
});
775740

776741
const getValidatedFd = hideStackFrames((fd, propName = 'fd') => {
777742
if (ObjectIs(fd, -0)) {

test/sequential/test-fs-path-dir.js

Lines changed: 0 additions & 174 deletions
This file was deleted.

0 commit comments

Comments
 (0)