Repository navigation
node:fs: cp keeps a <symlink>/.. root as given, and does not copy a directory into its own output
#47102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
node:fs: cp keeps a <symlink>/.. root as given, and does not copy a directory into its own output
#47102
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fad719f
node:fs cp: do not descend into a directory that the copy writes into
robobun ec70bdc
node:fs cp: address entries through the root as given when its `..` c…
robobun 66dd63e
node:fs cp: say each new comment in one line
robobun cf04c4b
node:fs cp: resolve the text of the destination parent before the nat…
robobun ce17f7f
node:fs cp: keep the lists of cancelled names off Array.prototype
robobun 9db0bbb
node:fs cp: let a filter into the output of the copy again after the …
robobun f2afda1
test: match the cp filter against the path below the fixture
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| // For a cp root with `..`: if app/cur links to releases/v2, "app/cur/../x" is releases/x, and path.join() gives app/x. | ||
| import type { KeptRoot } from "./cp-sync"; | ||
|
|
||
| const { lstatSync, realpathSync, statSync } = require("node:fs"); | ||
| const { lstat, realpath, stat } = require("node:fs/promises"); | ||
| const { basename, dirname, join, normalize } = require("node:path"); | ||
|
|
||
| const realpathNativeSync = realpathSync.native; | ||
|
|
||
| const StringPrototypeIndexOf = String.prototype.indexOf; | ||
| const StringPrototypeSlice = String.prototype.slice; | ||
|
|
||
| // The names of `path` that a later `..` cancels: where each one ends in `path`, in the order of the `..`. | ||
| function cancelledNames(path: string) { | ||
| const names: number[] = []; | ||
| const cancelled: number[] = []; | ||
| for (let start = 0; start < path.length;) { | ||
| let end = StringPrototypeIndexOf.$call(path, "/", start); | ||
| if (end === -1) end = path.length; | ||
| const length = end - start; | ||
| if (length === 2 && path[start] === "." && path[start + 1] === ".") { | ||
| if (names.length) cancelled.push(names.pop()!); | ||
| } else if (length > 1 || (length === 1 && path[start] !== ".")) { | ||
| names.push(end); | ||
| } | ||
| start = end + 1; | ||
| } | ||
| return cancelled; | ||
| } | ||
|
|
||
| // True when the system and path.join() give `path` two directories. That takes a `..` that cancels a symbolic link. | ||
| function keepsPathSync(path: string) { | ||
| const cancelled = cancelledNames(path); | ||
| for (let i = 0; i < cancelled.length; i++) { | ||
| let isLink: boolean; | ||
| try { | ||
| // No name that a `..` cancelled before this one is a link, so the text up to here is right. | ||
| isLink = lstatSync(normalize(StringPrototypeSlice.$call(path, 0, cancelled[i]))).isSymbolicLink(); | ||
| } catch { | ||
| // A name that the copy makes. Any other failure is for the calls of the copy to report. | ||
| continue; | ||
| } | ||
| if (!isLink) continue; | ||
| try { | ||
| // The names at the end that do not exist yet are the same for the system and for the text. | ||
| while (statSync(path, { throwIfNoEntry: false }) === undefined) { | ||
| const parent = dirname(path); | ||
| // After `new/..` the system resolves the rest only once the copy made `new`. | ||
| if (parent === path || basename(path) === "..") return true; | ||
| path = parent; | ||
| } | ||
| return realpathNativeSync(path) !== realpathNativeSync(normalize(path)); | ||
| } catch { | ||
| // Nothing shows that the text is right. | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| async function keepsPath(path: string) { | ||
| const cancelled = cancelledNames(path); | ||
| for (let i = 0; i < cancelled.length; i++) { | ||
| let isLink: boolean; | ||
| try { | ||
| isLink = (await lstat(normalize(StringPrototypeSlice.$call(path, 0, cancelled[i])))).isSymbolicLink(); | ||
| } catch { | ||
| continue; | ||
| } | ||
| if (!isLink) continue; | ||
| try { | ||
| while (!(await exists(path))) { | ||
| const parent = dirname(path); | ||
| if (parent === path || basename(path) === "..") return true; | ||
| path = parent; | ||
| } | ||
| return (await realpath(path)) !== (await realpath(normalize(path))); | ||
| } catch { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| async function exists(path: string) { | ||
| try { | ||
| await stat(path); | ||
| return true; | ||
| } catch (err: any) { | ||
| if (err.code === "ENOENT") return false; | ||
| throw err; | ||
| } | ||
| } | ||
|
|
||
| // The real directory of `link` below a kept root. The walk follows no link below a root, so the rest is text. | ||
| function linkDirSync(link: string, kept: KeptRoot) { | ||
| try { | ||
| kept.real ??= realpathNativeSync(kept.path); | ||
| } catch { | ||
| // What node's JS walk takes for every link. | ||
| return dirname(link); | ||
| } | ||
| return dirname(join(kept.real, StringPrototypeSlice.$call(link, kept.path.length))); | ||
| } | ||
|
|
||
| async function linkDir(link: string, kept: KeptRoot) { | ||
| try { | ||
| kept.real ??= await realpath(kept.path); | ||
| } catch { | ||
| return dirname(link); | ||
| } | ||
| return dirname(join(kept.real, StringPrototypeSlice.$call(link, kept.path.length))); | ||
| } | ||
|
|
||
| export default { keepsPathSync, keepsPath, linkDirSync, linkDir }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.