uv/crates/uv-fs/src/cachedir.rs
konsti 1344cfae4b
Use fs_err for cachedir errors (#3304)
When running

```
set UV_CACHE_DIR=%LOCALAPPDATA%\uv\cache-foo && uv venv venv
```

in windows CMD, the error would be just

```
error: The system cannot find the path specified. (os error 3)
```

The problem is that the first action in the cache dir is adding the tag,
and the `cachedir` crate is using `std::fs` instead of `fs_err`. I've
copied the two functions we use from the crate and changed the import
from `std::fs` to `fs_err`.

The new error is

```
error: failed to open file `C:\Users\Konstantin\AppData\Local\uv\cache-foo \CACHEDIR.TAG`
  Caused by: The system cannot find the path specified. (os error 3)
```

which correctly explains the problem.

Closes #3280
2024-04-29 16:33:10 +02:00

42 lines
1.4 KiB
Rust

//! Vendored from cachedir 0.3.1 to replace `std::fs` with `fs_err`.
use std::io::Write;
use std::{io, path};
use cachedir::HEADER;
/// Adds a tag to the specified `directory`.
///
/// Will return an error if:
///
/// * The `directory` exists and contains a `CACHEDIR.TAG` file, regardless of its content.
/// * The file can't be created for any reason (the `directory` doesn't exist, permission error,
/// can't write to the file etc.)
pub fn add_tag<P: AsRef<path::Path>>(directory: P) -> io::Result<()> {
let directory = directory.as_ref();
match fs_err::OpenOptions::new()
.write(true)
.create_new(true)
.open(directory.join("CACHEDIR.TAG"))
{
Ok(mut cachedir_tag) => cachedir_tag.write_all(HEADER),
Err(e) => Err(e),
}
}
/// Ensures the tag exists in `directory`.
///
/// This function considers the `CACHEDIR.TAG` file in `directory` existing, regardless of its
/// content, as a success.
///
/// Will return an error if The tag file doesn't exist and can't be created for any reason
/// (the `directory` doesn't exist, permission error, can't write to the file etc.).
pub fn ensure_tag<P: AsRef<path::Path>>(directory: P) -> io::Result<()> {
match add_tag(directory) {
Err(e) => match e.kind() {
io::ErrorKind::AlreadyExists => Ok(()),
_ => Err(e),
},
other => other,
}
}