Skip to content

[fs] Path::new(r#"C:\hiberfil.sys"#).exists() returns false, despite the file existing #96980

Closed
@strega-nil-ms

Description

@strega-nil-ms

I noticed this bug while researching the same bug in https://github.com/microsoft/STL.

// test.rs
use std::{env, fs};
use std::path::Path;

fn main() {
  for p in env::args().skip(1) {
    println!("{:?}.exists() = {}", p, Path::new(p).exists());
    println!("  => (metadata = {})", if fs::metadata(p).is_err() { "error" } else { "ok" });
  }
}

Given test.exe C:\hiberfil.sys, I expected:

"C:\\hiberfil.sys".exists() = true
  => (metadata = ok)

Instead, this happened:

"C:\\hiberfil.sys".exists() = false
  => (metadata = error)

Solution

The related issue in the STL is microsoft/STL#2370; the PR to fix this in the STL is microsoft/STL#2715.

The problem is that fs::metadata opens the file; this is not allowed for the following four files (not sure if there are additional files which act similarly):

  • C:\DumpStack.log.tmp
  • C:\hiberfil.sys
  • C:\pagefile.sys
  • C:\swapfile.sys

We can get around this by using FindFirstFileW instead of open(); is that something that's desired, or is this considered a weird enough use case that we aren't interested in fixing it?

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.O-windowsOperating system: WindowsT-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions