#[cfg(all(
not(test),
not(all(target_arch = "wasm32", target_os = "unknown")),
not(feature = "mem-fs"),
not(target_os = "windows"),
not(target_os = "ios"),
not(target_os = "android"),
))]
mod sys {
use async_fd_lock::{LockWrite, RwLockWriteGuard};
use std::path::Path;
use tokio::fs::{File, OpenOptions};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
pub async fn write_exclusive(
path: impl AsRef<Path>,
buf: impl AsRef<[u8]>,
) -> std::io::Result<()> {
let file = OpenOptions::new()
.create(true)
.truncate(true)
.read(true)
.write(true)
.open(path.as_ref())
.await?;
let mut guard = lock_write(file).await?;
guard.write_all(buf.as_ref()).await?;
guard.flush().await?;
Ok(())
}
pub async fn lock_write(
file: File,
) -> std::io::Result<RwLockWriteGuard<File>> {
Ok(file.lock_write().await?)
}
pub async fn read_exclusive(
path: impl AsRef<Path>,
) -> std::io::Result<Vec<u8>> {
let mut guard = File::open(path.as_ref()).await?.lock_write().await?;
let mut out = Vec::new();
guard.read_to_end(&mut out).await?;
Ok(out)
}
}
#[cfg(all(
not(test),
not(all(target_arch = "wasm32", target_os = "unknown")),
not(feature = "mem-fs"),
not(target_os = "windows"),
not(target_os = "ios"),
not(target_os = "android"),
))]
pub use sys::*;
#[cfg(any(
feature = "mem-fs",
all(target_arch = "wasm32", target_os = "unknown"),
target_os = "windows",
target_os = "ios",
target_os = "android",
))]
mod noop {
use crate::{read, write, File};
use std::path::Path;
pub async fn write_exclusive(
path: impl AsRef<Path>,
buf: impl AsRef<[u8]>,
) -> std::io::Result<()> {
write(path, buf).await
}
pub async fn lock_write(file: File) -> std::io::Result<File> {
Ok(file)
}
pub async fn read_exclusive(
path: impl AsRef<Path>,
) -> std::io::Result<Vec<u8>> {
read(path).await
}
}
#[cfg(any(
feature = "mem-fs",
all(target_arch = "wasm32", target_os = "unknown"),
target_os = "windows",
target_os = "ios",
target_os = "android",
))]
pub use noop::*;