| 1 |
//! Tokio-specific wrappers that use `fs_err` error messages. |
| 2 |
|
| 3 |
use crate::errors::{Error, ErrorKind, SourceDestError, SourceDestErrorKind}; |
| 4 |
use std::fs::{Metadata, Permissions}; |
| 5 |
use std::path::{Path, PathBuf}; |
| 6 |
use tokio::io; |
| 7 |
mod dir_builder; |
| 8 |
mod file; |
| 9 |
mod open_options; |
| 10 |
mod read_dir; |
| 11 |
|
| 12 |
pub use self::open_options::OpenOptions; |
| 13 |
pub use self::read_dir::{read_dir, DirEntry, ReadDir}; |
| 14 |
pub use dir_builder::DirBuilder; |
| 15 |
pub use file::File; |
| 16 |
|
| 17 |
/// Wrapper for [`tokio::fs::canonicalize`]. |
| 18 |
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))] |
| 19 |
pub async fn canonicalize(path: impl AsRef<Path>) -> io::Result<PathBuf> { |
| 20 |
let path = path.as_ref(); |
| 21 |
tokio::fs::canonicalize(path) |
| 22 |
.await |
| 23 |
.map_err(|err| Error::build(err, ErrorKind::Canonicalize, path)) |
| 24 |
} |
| 25 |
|
| 26 |
/// Wrapper for [`tokio::fs::copy`]. |
| 27 |
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))] |
| 28 |
pub async fn copy(from: impl AsRef<Path>, to: impl AsRef<Path>) -> Result<u64, io::Error> { |
| 29 |
let (from, to) = (from.as_ref(), to.as_ref()); |
| 30 |
tokio::fs::copy(from, to) |
| 31 |
.await |
| 32 |
.map_err(|err| SourceDestError::build(err, SourceDestErrorKind::Copy, from, to)) |
| 33 |
} |
| 34 |
|
| 35 |
/// Wrapper for [`tokio::fs::create_dir`]. |
| 36 |
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))] |
| 37 |
pub async fn create_dir(path: impl AsRef<Path>) -> io::Result<()> { |
| 38 |
let path = path.as_ref(); |
| 39 |
tokio::fs::create_dir(path) |
| 40 |
.await |
| 41 |
.map_err(|err| Error::build(err, ErrorKind::CreateDir, path)) |
| 42 |
} |
| 43 |
|
| 44 |
/// Wrapper for [`tokio::fs::create_dir_all`]. |
| 45 |
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))] |
| 46 |
pub async fn create_dir_all(path: impl AsRef<Path>) -> io::Result<()> { |
| 47 |
let path = path.as_ref(); |
| 48 |
tokio::fs::create_dir_all(path) |
| 49 |
.await |
| 50 |
.map_err(|err| Error::build(err, ErrorKind::CreateDir, path)) |
| 51 |
} |
| 52 |
|
| 53 |
/// Wrapper for [`tokio::fs::hard_link`]. |
| 54 |
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))] |
| 55 |
pub async fn hard_link(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> { |
| 56 |
let (src, dst) = (src.as_ref(), dst.as_ref()); |
| 57 |
tokio::fs::hard_link(src, dst) |
| 58 |
.await |
| 59 |
.map_err(|err| SourceDestError::build(err, SourceDestErrorKind::HardLink, src, dst)) |
| 60 |
} |
| 61 |
|
| 62 |
/// Wrapper for [`tokio::fs::metadata`]. |
| 63 |
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))] |
| 64 |
pub async fn metadata(path: impl AsRef<Path>) -> io::Result<Metadata> { |
| 65 |
let path = path.as_ref(); |
| 66 |
tokio::fs::metadata(path) |
| 67 |
.await |
| 68 |
.map_err(|err| Error::build(err, ErrorKind::Metadata, path)) |
| 69 |
} |
| 70 |
|
| 71 |
/// Wrapper for [`tokio::fs::read`]. |
| 72 |
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))] |
| 73 |
pub async fn read(path: impl AsRef<Path>) -> io::Result<Vec<u8>> { |
| 74 |
let path = path.as_ref(); |
| 75 |
tokio::fs::read(path) |
| 76 |
.await |
| 77 |
.map_err(|err| Error::build(err, ErrorKind::Read, path)) |
| 78 |
} |
| 79 |
|
| 80 |
/// Wrapper for [`tokio::fs::read_link`]. |
| 81 |
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))] |
| 82 |
pub async fn read_link(path: impl AsRef<Path>) -> io::Result<PathBuf> { |
| 83 |
let path = path.as_ref(); |
| 84 |
tokio::fs::read_link(path) |
| 85 |
.await |
| 86 |
.map_err(|err| Error::build(err, ErrorKind::ReadLink, path)) |
| 87 |
} |
| 88 |
|
| 89 |
/// Wrapper for [`tokio::fs::read_to_string`]. |
| 90 |
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))] |
| 91 |
pub async fn read_to_string(path: impl AsRef<Path>) -> io::Result<String> { |
| 92 |
let path = path.as_ref(); |
| 93 |
tokio::fs::read_to_string(path) |
| 94 |
.await |
| 95 |
.map_err(|err| Error::build(err, ErrorKind::Read, path)) |
| 96 |
} |
| 97 |
|
| 98 |
/// Wrapper for [`tokio::fs::remove_dir`]. |
| 99 |
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))] |
| 100 |
pub async fn remove_dir(path: impl AsRef<Path>) -> io::Result<()> { |
| 101 |
let path = path.as_ref(); |
| 102 |
tokio::fs::remove_dir(path) |
| 103 |
.await |
| 104 |
.map_err(|err| Error::build(err, ErrorKind::RemoveDir, path)) |
| 105 |
} |
| 106 |
|
| 107 |
/// Wrapper for [`tokio::fs::remove_dir_all`]. |
| 108 |
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))] |
| 109 |
pub async fn remove_dir_all(path: impl AsRef<Path>) -> io::Result<()> { |
| 110 |
let path = path.as_ref(); |
| 111 |
tokio::fs::remove_dir_all(path) |
| 112 |
.await |
| 113 |
.map_err(|err| Error::build(err, ErrorKind::RemoveDir, path)) |
| 114 |
} |
| 115 |
|
| 116 |
/// Wrapper for [`tokio::fs::remove_file`]. |
| 117 |
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))] |
| 118 |
pub async fn remove_file(path: impl AsRef<Path>) -> io::Result<()> { |
| 119 |
let path = path.as_ref(); |
| 120 |
tokio::fs::remove_file(path) |
| 121 |
.await |
| 122 |
.map_err(|err| Error::build(err, ErrorKind::RemoveFile, path)) |
| 123 |
} |
| 124 |
|
| 125 |
/// Wrapper for [`tokio::fs::rename`]. |
| 126 |
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))] |
| 127 |
pub async fn rename(from: impl AsRef<Path>, to: impl AsRef<Path>) -> io::Result<()> { |
| 128 |
let (from, to) = (from.as_ref(), to.as_ref()); |
| 129 |
tokio::fs::rename(from, to) |
| 130 |
.await |
| 131 |
.map_err(|err| SourceDestError::build(err, SourceDestErrorKind::Rename, from, to)) |
| 132 |
} |
| 133 |
|
| 134 |
/// Wrapper for [`tokio::fs::set_permissions`]. |
| 135 |
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))] |
| 136 |
pub async fn set_permissions(path: impl AsRef<Path>, perm: Permissions) -> io::Result<()> { |
| 137 |
let path = path.as_ref(); |
| 138 |
tokio::fs::set_permissions(path, perm) |
| 139 |
.await |
| 140 |
.map_err(|err| Error::build(err, ErrorKind::SetPermissions, path)) |
| 141 |
} |
| 142 |
|
| 143 |
/// Wrapper for [`tokio::fs::symlink_metadata`]. |
| 144 |
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))] |
| 145 |
pub async fn symlink_metadata(path: impl AsRef<Path>) -> io::Result<Metadata> { |
| 146 |
let path = path.as_ref(); |
| 147 |
tokio::fs::symlink_metadata(path) |
| 148 |
.await |
| 149 |
.map_err(|err| Error::build(err, ErrorKind::SymlinkMetadata, path)) |
| 150 |
} |
| 151 |
|
| 152 |
/// Wrapper for [`tokio::fs::symlink`]. |
| 153 |
#[cfg(unix)] |
| 154 |
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))] |
| 155 |
pub async fn symlink(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> { |
| 156 |
let (src, dst) = (src.as_ref(), dst.as_ref()); |
| 157 |
tokio::fs::symlink(src, dst) |
| 158 |
.await |
| 159 |
.map_err(|err| SourceDestError::build(err, SourceDestErrorKind::Symlink, src, dst)) |
| 160 |
} |
| 161 |
|
| 162 |
/// Wrapper for [`tokio::fs::symlink_dir`]. |
| 163 |
#[cfg(windows)] |
| 164 |
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))] |
| 165 |
pub async fn symlink(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> { |
| 166 |
let (src, dst) = (src.as_ref(), dst.as_ref()); |
| 167 |
tokio::fs::symlink_dir(src, dst) |
| 168 |
.await |
| 169 |
.map_err(|err| SourceDestError::build(err, SourceDestErrorKind::SymlinkDir, src, dst)) |
| 170 |
} |
| 171 |
|
| 172 |
/// Wrapper for [`tokio::fs::symlink_file`]. |
| 173 |
#[cfg(windows)] |
| 174 |
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))] |
| 175 |
pub async fn symlink_file(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> { |
| 176 |
let (src, dst) = (src.as_ref(), dst.as_ref()); |
| 177 |
tokio::fs::symlink_file(src, dst) |
| 178 |
.await |
| 179 |
.map_err(|err| SourceDestError::build(err, SourceDestErrorKind::SymlinkFile, src, dst)) |
| 180 |
} |
| 181 |
|
| 182 |
/// Wrapper for [`tokio::fs::write`]. |
| 183 |
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))] |
| 184 |
pub async fn write(path: impl AsRef<Path>, contents: impl AsRef<[u8]>) -> io::Result<()> { |
| 185 |
let (path, contents) = (path.as_ref(), contents.as_ref()); |
| 186 |
tokio::fs::write(path, contents) |
| 187 |
.await |
| 188 |
.map_err(|err| Error::build(err, ErrorKind::Write, path)) |
| 189 |
} |