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

More careful permissions handling #4285

Closed
wants to merge 5 commits into from

Conversation

dubiousjim
Copy link
Contributor

This is part of a series of PRs towards #4017. It integrates commits from an earlier PR #4228.

This PR sets file permissions more hygienically in cli/fs.rs, and makes sure that the mode argument to op_chmod is masked by 0o777. It also improves debug! calls in the associated functions.

The previous implementation would clobber anything Rust calls
`permissions` that aren't visible to Unix chmod (for example,
`metadata.permissions().readonly()`).

Replaces with the method recommended at
https://doc.rust-lang.org/std/os/unix/fs/trait.PermissionsExt.html#tymethod.set_mode.
* denoland/master:
  use Object instead of Map for promise table (denoland#4309)
  reorg: move js runtime tests to cli/js/tests/ (denoland#4250)
  upgrade: dprint 0.8.0 (denoland#4308)
  reorg: move JS ops implementations to cli/js/ops/, part 3 (denoland#4302)
  test: add actual error class to fail message (denoland#4305)
  upgrade: typescript 3.8.3 (denoland#4301)
  reorg: move JS ops implementations to cli/js/ops/, part 2 (denoland#4283)
  feat(std/node) add appendFile and appendFileSync (denoland#4294)
  disable test_raw_tty (denoland#4282)
@dubiousjim
Copy link
Contributor Author

This still needs a review.

let metadata = file.metadata()?;
let mut permissions = metadata.permissions();
permissions.set_mode(mode);
file.set_permissions(permissions)
Copy link
Member

@ry ry Mar 10, 2020

Choose a reason for hiding this comment

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

Why is this better? It looks like might make more syscalls.

If it's fixing a bug, there should be a test. If not, it shouldn't be changed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As I understand the Rust docs, the previous behavior was buggy. If a file was marked as "read-only" (this is exposed in the platform-neutral std::fs API, but I'm not sure what it amounts to on Unix, and I have minimal Windows experience,...) and it's run through the previous implementation of this function, then those read-only bits get erased and instead the mode bits are written to the file with no additional metadata. Same if the file has any metadata that the Rust API doesn't now expose. However, we can't make a test for this in TypeScript because we don't now have an API for creating or querying such additional metadata.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Check out std::fs::Permissions and std::os::unix::fs::PermissionsExt. The old method creates a new permissions structure containing only the desired mode bits. The new method proposed here (and at the second link) reads the existing permissions structure, overwrites just its mode bits with the desired new ones, and then writes that permissions structure back to the file.

Copy link
Member

Choose a reason for hiding this comment

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

I don't think there's a bug here. This isn't an op, just an internal function so it can be tested with a Rust unit test. If you can demonstrate a bug I would happily accept it. As this is now, as far as I can see, it's just moving code around. Thank you for the effort but I'm closing this PR without merge.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, forgot we can make a Rust unit test. I'll see if I can demonstrate a bug doing that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ry, it looks like you were correct. On Unix, the permissions().readonly/set_readonly only affect the chmod bits, and on Windows, the set_permissions function is a noop anyway, so it doesn't interfere with any readonly bits that may have been externally applied to a file. So the "fix" I proposed was unnecessary. Sorry for the noise.

@ry ry closed this Mar 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants