Fix bug in helpers::create_if_not_exist

Returned `File` must be both readable and writable.

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-07-22 22:38:10 +10:00
parent 09d210bf62
commit d9fe7bfaf4
No known key found for this signature in database
GPG key ID: 591C0B03040416D6

View file

@ -53,13 +53,18 @@ pub fn cargo_home() -> Result<&'static Path, io::Error> {
.map(ops::Deref::deref)
}
/// Returned file is readable and writable.
pub fn create_if_not_exist(path: impl AsRef<Path>) -> io::Result<fs::File> {
let path = path.as_ref();
fs::File::options()
let mut options = fs::File::options();
options.read(true).write(true);
options
.clone()
.create_new(true)
.open(path)
.or_else(|_| fs::File::open(path))
.or_else(|_| options.open(path))
}
pub async fn await_task<T>(task: tokio::task::JoinHandle<miette::Result<T>>) -> miette::Result<T> {