@@ -36,33 +36,44 @@ export function getFileInfoType(fileInfo: Deno.FileInfo): PathType | undefined {
36
36
}
37
37
38
38
// 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?!
40
39
/**
41
- * Checks whether `src ` is a sub-directory of `dest `.
40
+ * Checks whether `path2 ` is a sub-directory of `path1 `.
42
41
*
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.
45
52
* @param sep Path separator. Defaults to `\\` for Windows and `/` for other
46
53
* platforms.
47
54
*
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.
49
56
*/
50
57
export function isSubdir (
51
- src : string | URL ,
52
- dest : string | URL ,
58
+ path1 : string | URL ,
59
+ path2 : string | URL ,
53
60
sep = SEPARATOR ,
54
61
) : boolean {
55
- src = toPathString ( src ) ;
56
- dest = toPathString ( dest ) ;
62
+ path1 = toPathString ( path1 ) ;
63
+ path2 = toPathString ( path2 ) ;
57
64
58
- if ( resolve ( src ) === resolve ( dest ) ) {
65
+ if ( resolve ( path1 ) === resolve ( path2 ) ) {
59
66
return false ;
60
67
}
61
68
62
- const srcArray = src . split ( sep ) ;
63
- const destArray = dest . split ( sep ) ;
69
+ const path1Array = path1 . split ( sep ) ;
70
+ const path2Array = path2 . split ( sep ) ;
71
+
72
+ // if path1Array is longer than path2Array, then at least one of the
73
+ // comparisons will return false, because it will compare a string to
74
+ // undefined
64
75
65
- return srcArray . every ( ( current , i ) => destArray [ i ] === current ) ;
76
+ return path1Array . every ( ( current , i ) => path2Array [ i ] === current ) ;
66
77
}
67
78
68
79
/**
@@ -118,8 +129,7 @@ export function safeRemoveDirSync(
118
129
path : string ,
119
130
boundary : string ,
120
131
) {
121
- // note the comment above about isSubdir getting src and dest backwards
122
- if ( path === boundary || isSubdir ( path , boundary ) ) {
132
+ if ( path === boundary || ! isSubdir ( boundary , path ) ) {
123
133
throw new UnsafeRemovalError (
124
134
`Refusing to remove directory ${ path } that isn't a subdirectory of ${ boundary } ` ,
125
135
) ;
0 commit comments