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

Windows: Allow File::create to work on hidden files #116438

Merged
merged 1 commit into from
Dec 13, 2023

Conversation

ChrisDenton
Copy link
Contributor

This makes OpenOptions::new().write(true).create(true).truncate(true).open(&path) work if the path exists and is a hidden file. Previously it would fail with access denied.

This makes it consistent with OpenOptions::new().write(true).truncate(true).open(&path) (note the lack of create) which does not have this restriction. It's also more consistent with other platforms.

Fixes #115745 (see that issue for more details).

@rustbot
Copy link
Collaborator

rustbot commented Oct 5, 2023

r? @thomcc

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added O-windows Operating system: Windows S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 5, 2023
Comment on lines 314 to 320
let alloc = c::FILE_ALLOCATION_INFO { AllocationSize: 0 };
let result = c::SetFileInformationByHandle(
handle.as_raw_handle(),
c::FileAllocationInfo,
ptr::addr_of!(alloc).cast::<c_void>(),
mem::size_of::<c::FILE_ALLOCATION_INFO>() as u32,
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh exciting.

Copy link
Contributor Author

@ChrisDenton ChrisDenton Oct 5, 2023

Choose a reason for hiding this comment

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

Just a fancy way to pass an i64 to a function 😇

@bors
Copy link
Contributor

bors commented Oct 15, 2023

☔ The latest upstream changes (presumably #116683) made this pull request unmergeable. Please resolve the merge conflicts.

@rust-log-analyzer

This comment has been minimized.

Previously it failed on Windows if the file had the `FILE_ATTRIBUTE_HIDDEN` attribute set. This was inconsistent with `OpenOptions::new().write(true).truncate(true)` which can truncate an existing hidden file.
@thomcc
Copy link
Member

thomcc commented Oct 23, 2023

I'm a little worried about the possible race here (between file creation and truncation) but I suppose there's no avoiding it, and it's not like we make any guarantee about atomicity anyway.

@bors r

@bors
Copy link
Contributor

bors commented Oct 23, 2023

📌 Commit c6f7aa0 has been approved by thomcc

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 23, 2023
@ChrisDenton
Copy link
Contributor Author

I'm a little worried about the possible race here (between file creation and truncation) but I suppose there's no avoiding it, and it's not like we make any guarantee about atomicity anyway.

Indeed. Also it should be noted that this is essentially how TRUNCATE_EXISTING is currently implemented under the hood (well, using the NT equivalent) so we've already unwittingly crossed that bridge,

@bors
Copy link
Contributor

bors commented Oct 24, 2023

⌛ Testing commit c6f7aa0 with merge bd5d524...

bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 24, 2023
Windows: Allow `File::create` to work on hidden files

This makes `OpenOptions::new().write(true).create(true).truncate(true).open(&path)` work if the path exists and is a hidden file. Previously it would fail with access denied.

This makes it consistent with `OpenOptions::new().write(true).truncate(true).open(&path)` (note the lack of `create`) which does not have this restriction. It's also more consistent with other platforms.

Fixes rust-lang#115745 (see that issue for more details).
@rust-log-analyzer
Copy link
Collaborator

The job x86_64-msvc-ext failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)

@bors
Copy link
Contributor

bors commented Oct 24, 2023

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Oct 24, 2023
@ChrisDenton
Copy link
Contributor Author

Hm, a few cargo tests failing. It could be because file metadata is affected differently. Will need to investigate.

@ChrisDenton
Copy link
Contributor Author

Ok... so I think the issue is that a few cargo tests use fs::write(path, "") to update the last write time. That has a few problems:

  • With this PR, setting the file size to zero when it's already zero does not update the last write time
  • Even without this PR, writing an empty string to the file also does not update the last write time

Looking at the docs for OpenOptions::create and truncate I think the new behaviour is arguably within spec, if only by omission.

The easiest fix in this PR would be to set the last write time explicitly. The other option is to say this is fine and Cargo's tests are relying on something we never promised (and I don't think this introduces any actual bug to Cargo as a zero length file that remains zero length is indeed unchanged). I might need to think about this some more.

cc @epage maybe, for if Cargo has a view on this. Also just in case my explanation is missing anything.

@epage
Copy link
Contributor

epage commented Oct 24, 2023

Looking at this, while we do write(p, "") in production code, it doesn't seem to be for updating the mtime. It looks like we only do that in test code. Whether that is a justifiable change, I leave to libs.

@ChrisDenton
Copy link
Contributor Author

Thanks! I'm kinda leaning towards breaking Cargo's tests here. Especially as we're about to stabilise an API for setting mtimes so it won't be necessary to rely on side-effects.

But I'm not 100% sure yet.

@ChrisDenton ChrisDenton added the S-blocked Status: Marked as blocked ❌ on something else such as an RFC or other implementation work. label Oct 25, 2023
@ChrisDenton
Copy link
Contributor Author

Marking this as blocked on the stabilization of File::set_times. It'll be good to have a stable and guaranteed way of updating the last write time before this change lands.

TaKO8Ki added a commit to TaKO8Ki/rust that referenced this pull request Nov 9, 2023
Windows: Allow `File::create` to work on hidden files

This makes `OpenOptions::new().write(true).create(true).truncate(true).open(&path)` work if the path exists and is a hidden file. Previously it would fail with access denied.

This makes it consistent with `OpenOptions::new().write(true).truncate(true).open(&path)` (note the lack of `create`) which does not have this restriction. It's also more consistent with other platforms.

Fixes rust-lang#115745 (see that issue for more details).
@bors
Copy link
Contributor

bors commented Dec 9, 2023

⌛ Testing commit c6f7aa0 with merge 0182cc6...

bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 9, 2023
Windows: Allow `File::create` to work on hidden files

This makes `OpenOptions::new().write(true).create(true).truncate(true).open(&path)` work if the path exists and is a hidden file. Previously it would fail with access denied.

This makes it consistent with `OpenOptions::new().write(true).truncate(true).open(&path)` (note the lack of `create`) which does not have this restriction. It's also more consistent with other platforms.

Fixes rust-lang#115745 (see that issue for more details).
@bors
Copy link
Contributor

bors commented Dec 9, 2023

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-blocked Status: Marked as blocked ❌ on something else such as an RFC or other implementation work. labels Dec 9, 2023
@rust-log-analyzer
Copy link
Collaborator

A job failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)

@ChrisDenton
Copy link
Contributor Author

Not sure why this was in the queue again so soon. File::set_times is now stable (in beta) 🎉 but we'll need to wait for a cargo update before this can be retried.

@workingjubilee
Copy link
Contributor

A sync.
@bors r-

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 9, 2023
@ChrisDenton
Copy link
Contributor Author

#118765 updated cargo so let's try this again

@bors r=thomcc

@bors
Copy link
Contributor

bors commented Dec 13, 2023

📌 Commit c6f7aa0 has been approved by thomcc

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Dec 13, 2023
@bors
Copy link
Contributor

bors commented Dec 13, 2023

⌛ Testing commit c6f7aa0 with merge 77d1699...

@bors
Copy link
Contributor

bors commented Dec 13, 2023

☀️ Test successful - checks-actions
Approved by: thomcc
Pushing 77d1699 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Dec 13, 2023
@bors bors merged commit 77d1699 into rust-lang:master Dec 13, 2023
12 checks passed
@rustbot rustbot added this to the 1.76.0 milestone Dec 13, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (77d1699): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.9% [-0.9%, -0.9%] 1
All ❌✅ (primary) - - 0

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.2% [-2.2%, -2.1%] 2
All ❌✅ (primary) - - 0

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 672.799s -> 671.844s (-0.14%)
Artifact size: 312.41 MiB -> 312.44 MiB (0.01%)

@ChrisDenton ChrisDenton deleted the truncate branch December 13, 2023 06:00
@Emilgardis
Copy link
Contributor

This somehow broke something in wine, not sure how broad the issue is, see cross-rs/cross#1386 (reply in thread)

@ChrisDenton
Copy link
Contributor Author

Hm, I tend toward thinking that wine should support Windows applications rather than Windows applications should support wine but in this case I think there's a simple fix: we can fallback to a possibly less efficient function if wine tells us its unsupported.

Btw, if anyone does want to have wine support this then head over to NtSetInformationFile and write a quick implementation for FileAllocationInformation (we don't use this low level interface in Rust's std but it has to be implemented before the higher level one can be). See the win api docs for info on the behaviour. It's more or less ftruncate if AllocationSize <= st_size (though it also moves the file pointer) or fallocate (with FALLOC_FL_KEEP_SIZE) otherwise.

@ChrisDenton
Copy link
Contributor Author

I created #119051 to fix this issue. I ended up going the simpler route of always using something that is supported by WINE instead of only as a fallback.

wip-sync pushed a commit to NetBSD/pkgsrc-wip that referenced this pull request Feb 18, 2024
Pkgsrc changes:
 * Adapt checksums and patches.

Upstream chnages:

Version 1.76.0 (2024-02-08)
==========================

Language
--------
- [Document Rust ABI compatibility between various types]
  (rust-lang/rust#115476)
- [Also: guarantee that char and u32 are ABI-compatible]
  (rust-lang/rust#118032)
- [Warn against ambiguous wide pointer comparisons]
  (rust-lang/rust#117758)

Compiler
--------
- [Lint pinned `#[must_use]` pointers (in particular, `Box<T>`
  where `T` is `#[must_use]`) in `unused_must_use`.]
  (rust-lang/rust#118054)
- [Soundness fix: fix computing the offset of an unsized field in
  a packed struct]
  (rust-lang/rust#118540)
- [Soundness fix: fix dynamic size/align computation logic for
  packed types with dyn Trait tail]
  (rust-lang/rust#118538)
- [Add `$message_type` field to distinguish json diagnostic outputs]
  (rust-lang/rust#115691)
- [Enable Rust to use the EHCont security feature of Windows]
  (rust-lang/rust#118013)
- [Add tier 3 {x86_64,i686}-win7-windows-msvc targets]
  (rust-lang/rust#118150)
- [Add tier 3 aarch64-apple-watchos target]
  (rust-lang/rust#119074)
- [Add tier 3 arm64e-apple-ios & arm64e-apple-darwin targets]
  (rust-lang/rust#115526)

Refer to Rust's [platform support page][platform-support-doc]
for more information on Rust's tiered platform support.

Libraries
---------
- [Add a column number to `dbg!()`]
  (rust-lang/rust#114962)
- [Add `std::hash::{DefaultHasher, RandomState}` exports]
  (rust-lang/rust#115694)
- [Fix rounding issue with exponents in fmt]
  (rust-lang/rust#116301)
- [Add T: ?Sized to `RwLockReadGuard` and `RwLockWriteGuard`'s Debug impls.]
  (rust-lang/rust#117138)
- [Windows: Allow `File::create` to work on hidden files]
  (rust-lang/rust#116438)

Stabilized APIs
---------------
- [`Arc::unwrap_or_clone`]
  (https://doc.rust-lang.org/stable/std/sync/struct.Arc.html#method.unwrap_or_clone)
- [`Rc::unwrap_or_clone`]
  (https://doc.rust-lang.org/stable/std/rc/struct.Rc.html#method.unwrap_or_clone)
- [`Result::inspect`]
  (https://doc.rust-lang.org/stable/std/result/enum.Result.html#method.inspect)
- [`Result::inspect_err`]
  (https://doc.rust-lang.org/stable/std/result/enum.Result.html#method.inspect_err)
- [`Option::inspect`]
  (https://doc.rust-lang.org/stable/std/option/enum.Option.html#method.inspect)
- [`type_name_of_val`]
  (https://doc.rust-lang.org/stable/std/any/fn.type_name_of_val.html)
- [`std::hash::{DefaultHasher, RandomState}`]
  (https://doc.rust-lang.org/stable/std/hash/index.html#structs)
  These were previously available only through `std::collections::hash_map`.
- [`ptr::{from_ref, from_mut}`]
  (https://doc.rust-lang.org/stable/std/ptr/fn.from_ref.html)
- [`ptr::addr_eq`](https://doc.rust-lang.org/stable/std/ptr/fn.addr_eq.html)

Cargo
-----

See [Cargo release notes]
(https://github.com/rust-lang/cargo/blob/master/CHANGELOG.md#cargo-176-2024-02-08).

Rustdoc
-------
- [Don't merge cfg and doc(cfg) attributes for re-exports]
  (rust-lang/rust#113091)
- [rustdoc: allow resizing the sidebar / hiding the top bar]
  (rust-lang/rust#115660)
- [rustdoc-search: add support for traits and associated types]
  (rust-lang/rust#116085)
- [rustdoc: Add highlighting for comments in items declaration]
  (rust-lang/rust#117869)

Compatibility Notes
-------------------
- [Add allow-by-default lint for unit bindings]
  (rust-lang/rust#112380)
  This is expected to be upgraded to a warning by default in a future Rust
  release. Some macros emit bindings with type `()` with user-provided spans,
  which means that this lint will warn for user code.
- [Remove x86_64-sun-solaris target.]
  (rust-lang/rust#118091)
- [Remove asmjs-unknown-emscripten target]
  (rust-lang/rust#117338)
- [Report errors in jobserver inherited through environment variables]
  (rust-lang/rust#113730)
  This [may warn](rust-lang/rust#120515)
  on benign problems too.
- [Update the minimum external LLVM to 16.]
  (rust-lang/rust#117947)
- [Improve `print_tts`](rust-lang/rust#114571)
  This change can break some naive manual parsing of token trees
  in proc macro code which expect a particular structure after
  `.to_string()`, rather than just arbitrary Rust code.
- [Make `IMPLIED_BOUNDS_ENTAILMENT` into a hard error from a lint]
  (rust-lang/rust#117984)
- [Vec's allocation behavior was changed when collecting some iterators]
  (rust-lang/rust#110353)
  Allocation behavior is currently not specified, nevertheless
  changes can be surprising.
  See [`impl FromIterator for Vec`]
  (https://doc.rust-lang.org/nightly/std/vec/struct.Vec.html#impl-FromIterator-for-Vec)
  for more details.
- [Properly reject `default` on free const items]
  (rust-lang/rust#117818)
celinval added a commit to celinval/rust-dev that referenced this pull request Jun 4, 2024
Update Rust toolchain from nightly-2023-12-13 to nightly-2023-12-14
without any other source changes.
This is an automatically generated pull request. If any of the CI checks
fail, manual intervention is required. In such a case, review the
changes at https://github.com/rust-lang/rust from
rust-lang@3340d49
up to
rust-lang@eeff92a.
The log for this commit range is:
rust-lang@eeff92ad32 Auto merge of
rust-lang#118402 - notriddle:notriddle/ranking-and-filtering, r=GuillaumeGomez
rust-lang@a90372c6e8 Auto merge of
rust-lang#118213 - Urgau:check-cfg-diagnostics-rustc-cargo, r=petrochenkov
rust-lang@2862500152 Auto merge of
rust-lang#118919 - matthiaskrgr:rollup-02udckl, r=matthiaskrgr
rust-lang@bec6672984 rustdoc-search:
clean up handleSingleArg type handling
rust-lang@9dfcf131b3 rustdoc-search:
better hashing, faster unification
rust-lang@9a9695a052 rustdoc-search: use
set ops for ranking and filtering
rust-lang@fd1d256d61 rustdoc-search:
remove the now-redundant `validateResult`
rust-lang@251d1af0d2 Rollup merge of
rust-lang#118906 - Kobzol:bootstrap-is-windows, r=petrochenkov
rust-lang@666353e7ba Rollup merge of
rust-lang#118883 - HosseinAssaran:patch-1, r=fmease
rust-lang@1dd36119d0 Rollup merge of
rust-lang#118871 - tmiasko:coroutine-maybe-uninit-fields, r=compiler-errors
rust-lang@dbc6ec6636 Rollup merge of
rust-lang#118759 - compiler-errors:bare-unit-structs, r=petrochenkov
rust-lang@f6617d050d Remove dangling
check-cfg ui tests files
rust-lang@5345a166fe Add more suggestion
to unexpected cfg names and values
rust-lang@7176b8babd Auto merge of
rust-lang#118894 - dtolnay:bootstrapwrite, r=onur-ozkan
rust-lang@c3def263a4 Auto merge of
rust-lang#118870 - Enselic:rustc_passes-query-stability, r=compiler-errors
rust-lang@56d25ba5ea Auto merge of
rust-lang#118500 - ZetaNumbers:tcx_hir_refactor, r=petrochenkov
rust-lang@2fdd9eda0c Auto merge of
rust-lang#118534 - RalfJung:extern-type-size-of-val, r=WaffleLapkin
rust-lang@066e6ffa02 Fix LLD thread flag
selection for Windows targets
rust-lang@c5208518fa Add
`TargetSelection::is_windows` method
rust-lang@f651b436ce Auto merge of
rust-lang#117050 - c410-f3r:here-we-go-again, r=petrochenkov
rust-lang@9f1bfe53b6 Auto merge of
rust-lang#118900 - workingjubilee:rollup-wkv9hq1, r=workingjubilee
rust-lang@f9078a40ee Rollup merge of
rust-lang#118891 - compiler-errors:async-gen-blocks, r=eholk
rust-lang@4583a0134f Rollup merge of
rust-lang#118889 - matthiaskrgr:compl_2023_2, r=WaffleLapkin
rust-lang@df0686b629 Rollup merge of
rust-lang#118887 - smoelius:patch-1, r=Nilstrieb
rust-lang@2f937c720d Rollup merge of
rust-lang#118886 - GuillaumeGomez:clean-up-search-vars, r=notriddle
rust-lang@5308733112 Rollup merge of
rust-lang#118885 - matthiaskrgr:compl_2023, r=compiler-errors
rust-lang@89d4a9bee9 Rollup merge of
rust-lang#118884 - matthiaskrgr:auszweimacheins, r=Nadrieril
rust-lang@18e0966f39 Rollup merge of
rust-lang#118873 - lukas-code:fix_waker_getter_tracking_issue_number,
r=workingjubilee
rust-lang@0430782d1d Rollup merge of
rust-lang#118872 - GuillaumeGomez:codeblock-attr-lint, r=notriddle
rust-lang@a33f1a3d3a Rollup merge of
rust-lang#118864 - farnoy:masked-load-store-fixes, r=workingjubilee
rust-lang@2d1d443d7f Rollup merge of
rust-lang#118858 - mu001999:dead_code/clean, r=cuviper
rust-lang@77d1699756 Auto merge of
rust-lang#116438 - ChrisDenton:truncate, r=thomcc
rust-lang@b30e94b7bb Unbreak non-unix
non-windows bootstrap
rust-lang@1d78ce681e Actually parse async
gen blocks correctly
rust-lang@2a1acc26a0 Update
compiler/rustc_pattern_analysis/src/constructor.rs
rust-lang@3795cc8eb0 more
clippy::complexity fixes
rust-lang@046f2dea33 Typo
rust-lang@58327c10c5 Add a test for a
codeblock with multiple invalid attributes
rust-lang@f1342f30a5 Clean up variables
in `search.js`
rust-lang@d707461a1a clippy::complexity
fixes
rust-lang@6892fcd690 simplify merging of
two vecs
rust-lang@a2ffff0708 Change a typo
mistake in the-doc-attribute.md
rust-lang@f813ccd784 also add a Miri test
rust-lang@edcb7aba6b also test projecting
to some sized fields at non-zero offset in structs with an extern type
tail
rust-lang@a47416beb5 test that both
size_of_val and align_of_val panic
rust-lang@bb0fd665a8 Follow guidelines
for lint suggestions
rust-lang@98aa20b0a7 Add test for `rustX`
codeblock attribute
rust-lang@d3cb25f4cf Add `rustX` check to
codeblock attributes lint
rust-lang@24f009c5e5 Move some methods
from `tcx.hir()` to `tcx`
rust-lang@04f3adb4a7 fix `waker_getters`
tracking issue number
rust-lang@e9b16cc2c5 rustc_passes:
Enforce `rustc::potential_query_instability` lint
rust-lang@95b5a80f47 Fix alignment passed
down to LLVM for simd_masked_load
rust-lang@fb32eb3529 Clean up
CodeBlocks::next code
rust-lang@df227f78c6 make it more clear
what comments refer to; avoid dangling unaligned references
rust-lang@b9c9b3e7a2 remove a cranelift
test that doesn't make sense any more
rust-lang@9ef1e35166 reject projecting to
fields whose offset we cannot compute
rust-lang@b1613ebc43 codegen: panic when
trying to compute size/align of extern type
rust-lang@6c0dbb8cc6 Remove dead codes in
core
rust-lang@a48cebc4b8 Coroutine variant
fields can be uninitialized
rust-lang@d473bdfdc3 Support bare unit
structs in destructuring assignments
rust-lang@0278505691 Attempt to try to
resolve blocking concerns
rust-lang@c6f7aa0eea Make File::create
work on Windows hidden files

Co-authored-by: celinval <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. O-windows Operating system: Windows S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

File::create fails on hidden files on Windows
10 participants