Skip to content
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

fs: move ToNamespacedPath to c++ #52135

Merged
merged 1 commit into from
Jun 20, 2024
Merged

Conversation

anonrig
Copy link
Member

@anonrig anonrig commented Mar 18, 2024

This is a step forward to move all node:fs implementation to C++. Since we now have support for win32.pathResolve() in C++, we can move forward with moving ToNamespacedPath functions to C++.

In a follow up pull-request, I'll move FromNamespacedPath() from node_url.cc to path.cc

cc @nodejs/fs @RafaelGSS

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/url

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. labels Mar 18, 2024
@anonrig anonrig force-pushed the simplify-fs branch 2 times, most recently from 003590b to 2d120ca Compare March 18, 2024 15:16
@richardlau richardlau added fs Issues and PRs related to the fs subsystem / file system. windows Issues and PRs related to the Windows platform. labels Mar 18, 2024
@anonrig
Copy link
Member Author

anonrig commented Mar 18, 2024

cc @nodejs/platform-windows Any idea where this error/warning is thrown from?

src/path.cc Outdated Show resolved Hide resolved
src/path.cc Outdated Show resolved Hide resolved
@LiviaMedeiros
Copy link
Contributor

AFAICT the changes are implying that getValidatedPath() should do path.ToNamespacedPath() internally, but there's no changes to getValidatedPath() itself in this PR. Does it happen implicitly? If yes, on which step?

src/path.cc Outdated Show resolved Hide resolved
src/path.h Outdated Show resolved Hide resolved
src/path.cc Show resolved Hide resolved
@anonrig
Copy link
Member Author

anonrig commented Mar 18, 2024

AFAICT the changes are implying that getValidatedPath() should do path.ToNamespacedPath() internally, but there's no changes to getValidatedPath() itself in this PR. Does it happen implicitly? If yes, on which step?

getValidatedPath() result is called with toNamespacedPath() to ensure Windows long paths are handled correctly. I didn't quite understand your comment, but this pull request basically replaces toNamespacedPath(getValidatedPath(input)) with getValidatedPath(input) and calls toNamespacedPath from C++

@anonrig anonrig force-pushed the simplify-fs branch 2 times, most recently from d93f610 to 5c62bf2 Compare March 18, 2024 18:08
@LiviaMedeiros
Copy link
Contributor

getValidatedPath() result is called with toNamespacedPath() to ensure Windows long paths are handled correctly. I didn't quite understand your comment, but this pull request basically replaces toNamespacedPath(getValidatedPath(input)) with getValidatedPath(input) and calls toNamespacedPath from C++

Sorry, I read the changes wrong. It doesn't affect js-side toNamespacedPath() itself but adds cpp-side version that is used in cpp implementations for each fs method.
We might need to doublecheck that every method uses this. Of course, it's non-blocking concern: I'm just pointing out that after this change we'll have to check cpp side instead of js.
Right now I'm only seeing that Mkdtemp lacks it here:

node/src/node_file.cc

Lines 2820 to 2823 in d93f610

THROW_IF_INSUFFICIENT_PERMISSIONS(
env, permission::PermissionScope::kFileSystemWrite, tmpl.ToStringView());
const enum encoding encoding = ParseEncoding(isolate, args[1], UTF8);

Which mirrors current (might be bugged on Windows if called with long prefix) js implementation.

@anonrig anonrig added the request-ci Add this label to start a Jenkins CI on a PR. label Mar 18, 2024
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Mar 18, 2024
@nodejs-github-bot
Copy link
Collaborator

@RafaelGSS
Copy link
Member

This should land first #52148.

Copy link
Member

@RafaelGSS RafaelGSS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As @joyeecheung suggested on my initial PR for node::pathResolve, I suggest you do the same for path.toNamespacePath. Change the bindings in a different PR and run the tests to see if it works.

Example: #51295

@anonrig
Copy link
Member Author

anonrig commented Mar 19, 2024

@RafaelGSS the function signature is not 1 to 1. We're always using BufferValue in node:fs.

@joyeecheung
Copy link
Member

joyeecheung commented Mar 19, 2024

I think this is different from #51295 in that, the other PR only used the C++ version in a not very well-tested branch (only when permission is enabled) so if it's broken, we may not be able to know due to the low test coverage. This PR however changed most fs bindings so if it's broken, it's probably more likely that some tests would fail on Windows.

That said I think we already don't have enough test cases to check the kind of paths that are need to be handled specially by toNamespacedPath(). Perhaps it would be useful to check the cases in #51097 too

@anonrig anonrig force-pushed the simplify-fs branch 2 times, most recently from 8ab9cf7 to a611c98 Compare March 21, 2024 14:57
@anonrig
Copy link
Member Author

anonrig commented Jun 12, 2024

hey @huseyinacacak-janea, sorry for the late response. I've updated the pull-request with your recommendations.

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

Co-Authored-By: Daniel Lemire <daniel@lemire.me>
@anonrig
Copy link
Member Author

anonrig commented Jun 17, 2024

@RafaelGSS can you look into the permission model changes? I removed an unnecessary test since we don't use path.toNamespacedPath anymore in JS world, and updated several paths and surrounded them with path.toNamespacedPath()

@anonrig anonrig requested a review from RafaelGSS June 17, 2024 23:51
@nodejs-github-bot
Copy link
Collaborator

}));
}

// Monkey-patching path module should also not allow path traversal.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regardless if we are moving the implementation to C++ I believe path.toNamespacedPath will always exist through an API right? If so, I don't think we should remove this test, it won't fail anyway.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think its not needed but on top of that It fails because win32 path resolves return "." for cwd, but previously it didn't.

@@ -316,7 +316,7 @@ if (common.isMainThread && (common.isLinux || common.isOSX)) {
const p = fs.mkdirSync(pathname, common.mustNotMutateObjectDeep({ recursive: true }));
assert.strictEqual(fs.existsSync(pathname), true);
assert.strictEqual(fs.statSync(pathname).isDirectory(), true);
assert.strictEqual(p, firstPathCreated);
assert.strictEqual(p, path.toNamespacedPath(firstPathCreated));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why we are changing the comparison to include the path.toNamespacedPath? Did tmpdir.resolve API change somehow? If so, isn't it a breaking change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically it isn't since both paths are the same and from Windows perspective having a path with namespaced value does not really have any difference with without the prefix.

PS: Namespaced paths are used to avoid "path is too long" errors on Windows. If you look into this file, you'll see that we already check the value surrounded by toNamespacedPath in several places already. I feel like this is more complete.

src/node_file.cc Show resolved Hide resolved
@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@anonrig anonrig requested review from jasnell and mcollina June 19, 2024 22:49
@anonrig anonrig added the commit-queue Add this label to land a pull request using GitHub Actions. label Jun 20, 2024
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Jun 20, 2024
@nodejs-github-bot nodejs-github-bot merged commit 399eb33 into nodejs:main Jun 20, 2024
51 checks passed
@nodejs-github-bot
Copy link
Collaborator

Landed in 399eb33

targos pushed a commit that referenced this pull request Jun 20, 2024
Co-Authored-By: Daniel Lemire <daniel@lemire.me>
PR-URL: #52135
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
Reviewed-By: Daniel Lemire <daniel@lemire.me>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
sophoniie pushed a commit to sophoniie/node that referenced this pull request Jun 20, 2024
Co-Authored-By: Daniel Lemire <daniel@lemire.me>
PR-URL: nodejs#52135
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
Reviewed-By: Daniel Lemire <daniel@lemire.me>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
bmeck pushed a commit to bmeck/node that referenced this pull request Jun 22, 2024
Co-Authored-By: Daniel Lemire <daniel@lemire.me>
PR-URL: nodejs#52135
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
Reviewed-By: Daniel Lemire <daniel@lemire.me>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
@tniessen
Copy link
Member

tniessen commented Aug 5, 2024

@anonrig This PR might have broken a few things, PTAL at #54161.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
baking-for-lts PRs that need to wait before landing in a LTS release. c++ Issues and PRs that require attention from people who are familiar with C++. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. fs Issues and PRs related to the fs subsystem / file system. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. windows Issues and PRs related to the Windows platform.
Projects
None yet
Development

Successfully merging this pull request may close these issues.