@@ -36,33 +36,47 @@ export function getFileInfoType(fileInfo: Deno.FileInfo): PathType | undefined {
3636}
3737
3838// from https://jsr.io/@std /fs/1.0.3/_is_subdir.ts
39- // 2024-15-11: isSubDir("foo", "foo/bar") returns true, which gets src and dest exactly backwards?!
4039/**
41- * Checks whether `src ` is a sub-directory of `dest `.
40+ * Checks whether `path2 ` is a sub-directory of `path1 `.
4241 *
43- * @param src Source file path as a string or URL.
44- * @param dest Destination file path as a string or URL.
42+ * The original function uses bad parameter names which are misleading.
43+ *
44+ * This function is such that, for all paths p:
45+ *
46+ * isSubdir(p, join(p, "foo")) === true
47+ * isSubdir(p, p) === false
48+ * isSubdir(join(p, "foo"), p) === false
49+ *
50+ * @param path1 First path, as a string or URL.
51+ * @param path2 Second path, as a string or URL.
4552 * @param sep Path separator. Defaults to `\\` for Windows and `/` for other
4653 * platforms.
4754 *
48- * @returns `true` if `src ` is a sub-directory of `dest `, `false` otherwise.
55+ * @returns `true` if `path2 ` is a proper sub-directory of `path1 `, `false` otherwise.
4956 */
5057export function isSubdir (
51- src : string | URL ,
52- dest : string | URL ,
58+ path1 : string | URL ,
59+ path2 : string | URL ,
5360 sep = SEPARATOR ,
5461) : boolean {
55- src = toPathString ( src ) ;
56- dest = toPathString ( dest ) ;
62+ path1 = toPathString ( path1 ) ;
63+ path2 = toPathString ( path2 ) ;
5764
58- if ( resolve ( src ) === resolve ( dest ) ) {
65+ path1 = resolve ( path1 ) ;
66+ path2 = resolve ( path2 ) ;
67+
68+ if ( path1 === path2 ) {
5969 return false ;
6070 }
6171
62- const srcArray = src . split ( sep ) ;
63- const destArray = dest . split ( sep ) ;
72+ const path1Array = path1 . split ( sep ) ;
73+ const path2Array = path2 . split ( sep ) ;
74+
75+ // if path1Array is longer than path2Array, then at least one of the
76+ // comparisons will return false, because it will compare a string to
77+ // undefined
6478
65- return srcArray . every ( ( current , i ) => destArray [ i ] === current ) ;
79+ return path1Array . every ( ( current , i ) => path2Array [ i ] === current ) ;
6680}
6781
6882/**
@@ -118,8 +132,7 @@ export function safeRemoveDirSync(
118132 path : string ,
119133 boundary : string ,
120134) {
121- // note the comment above about isSubdir getting src and dest backwards
122- if ( path === boundary || isSubdir ( path , boundary ) ) {
135+ if ( path === boundary || ! isSubdir ( boundary , path ) ) {
123136 throw new UnsafeRemovalError (
124137 `Refusing to remove directory ${ path } that isn't a subdirectory of ${ boundary } ` ,
125138 ) ;
0 commit comments