Debian Codegraph @

third_party/rust/fs-err/src/tokio/file.rs

raw
1 use crate::errors::{Error, ErrorKind};
2 use std::fs::{Metadata, Permissions};
3 use std::io;
4 use std::io::{IoSlice, SeekFrom};
5 use std::path::{Path, PathBuf};
6 use std::pin::Pin;
7 use std::task::{ready, Context, Poll};
8 use tokio::fs;
9 use tokio::fs::File as TokioFile;
10 use tokio::io::{AsyncRead, AsyncSeek, AsyncWrite, ReadBuf};
11
12 /// Wrapper around [`tokio::fs::File`] which adds more helpful
13 /// information to all errors.
14 #[derive(Debug)]
15 #[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
16 pub struct File {
17 tokio: fs::File,
18 path: PathBuf,
19 }
20
21 impl File {
22 /// Wrapper for [`tokio::fs::File::open`].
23 pub async fn open(path: impl Into<PathBuf>) -> io::Result<File> {
24 let path = path.into();
25 let f = TokioFile::open(&path)
26 .await
27 .map_err(|err| Error::build(err, ErrorKind::OpenFile, &path))?;
28 Ok(File::from_parts(f, path))
29 }
30
31 /// Wrapper for [`tokio::fs::File::create`].
32 pub async fn create(path: impl Into<PathBuf>) -> io::Result<File> {
33 let path = path.into();
34 match TokioFile::create(&path).await {
35 Ok(f) => Ok(File::from_parts(f, path)),
36 Err(err) => Err(Error::build(err, ErrorKind::CreateFile, &path)),
37 }
38 }
39
40 /// Wrapper for [`tokio::fs::File::from_std`].
41 pub fn from_std(std: crate::File) -> File {
42 let (std, path) = std.into_parts();
43 File::from_parts(TokioFile::from_std(std), path)
44 }
45
46 /// Wrapper for [`tokio::fs::File::sync_all`].
47 pub async fn sync_all(&self) -> io::Result<()> {
48 self.tokio
49 .sync_all()
50 .await
51 .map_err(|err| self.error(err, ErrorKind::SyncFile))
52 }
53
54 /// Wrapper for [`tokio::fs::File::sync_data`].
55 pub async fn sync_data(&self) -> io::Result<()> {
56 self.tokio
57 .sync_data()
58 .await
59 .map_err(|err| self.error(err, ErrorKind::SyncFile))
60 }
61
62 /// Wrapper for [`tokio::fs::File::set_len`].
63 pub async fn set_len(&self, size: u64) -> io::Result<()> {
64 self.tokio
65 .set_len(size)
66 .await
67 .map_err(|err| self.error(err, ErrorKind::SetLen))
68 }
69
70 /// Wrapper for [`tokio::fs::File::metadata`].
71 pub async fn metadata(&self) -> io::Result<Metadata> {
72 self.tokio
73 .metadata()
74 .await
75 .map_err(|err| self.error(err, ErrorKind::Metadata))
76 }
77
78 /// Wrapper for [`tokio::fs::File::try_clone`].
79 pub async fn try_clone(&self) -> io::Result<File> {
80 match self.tokio.try_clone().await {
81 Ok(file) => Ok(File::from_parts(file, self.path.clone())),
82 Err(err) => Err(self.error(err, ErrorKind::Clone)),
83 }
84 }
85
86 /// Wrapper for [`tokio::fs::File::into_std`].
87 pub async fn into_std(self) -> crate::File {
88 crate::File::from_parts(self.tokio.into_std().await, self.path)
89 }
90
91 /// Wrapper for [`tokio::fs::File::try_into_std`].
92 pub fn try_into_std(self) -> Result<crate::File, File> {
93 match self.tokio.try_into_std() {
94 Ok(f) => Ok(crate::File::from_parts(f, self.path)),
95 Err(f) => Err(File::from_parts(f, self.path)),
96 }
97 }
98
99 /// Wrapper for [`tokio::fs::File::set_permissions`].
100 pub async fn set_permissions(&self, perm: Permissions) -> io::Result<()> {
101 self.tokio
102 .set_permissions(perm)
103 .await
104 .map_err(|err| self.error(err, ErrorKind::SetPermissions))
105 }
106 }
107
108 /// Methods added by fs-err that are not available on
109 /// [`tokio::fs::File`].
110 impl File {
111 /// Creates a [`File`](struct.File.html) from a raw file and its path.
112 pub fn from_parts<P>(file: TokioFile, path: P) -> Self
113 where
114 P: Into<PathBuf>,
115 {
116 File {
117 tokio: file,
118 path: path.into(),
119 }
120 }
121
122 /// Extract the raw file and its path from this [`File`](struct.File.html).
123 pub fn into_parts(self) -> (TokioFile, PathBuf) {
124 (self.tokio, self.path)
125 }
126
127 /// Returns a reference to the underlying [`tokio::fs::File`].
128 pub fn file(&self) -> &TokioFile {
129 &self.tokio
130 }
131
132 /// Returns a mutable reference to the underlying [`tokio::fs::File`].
133 pub fn file_mut(&mut self) -> &mut TokioFile {
134 &mut self.tokio
135 }
136
137 /// Returns a reference to the path that this file was created with.
138 pub fn path(&self) -> &Path {
139 &self.path
140 }
141
142 /// Wrap the error in information specific to this `File` object.
143 fn error(&self, source: io::Error, kind: ErrorKind) -> io::Error {
144 Error::build(source, kind, &self.path)
145 }
146 }
147
148 impl From<crate::File> for File {
149 fn from(f: crate::File) -> Self {
150 let (f, path) = f.into_parts();
151 File::from_parts(f.into(), path)
152 }
153 }
154
155 impl From<File> for TokioFile {
156 fn from(f: File) -> Self {
157 f.into_parts().0
158 }
159 }
160
161 #[cfg(unix)]
162 impl std::os::unix::io::AsRawFd for File {
163 fn as_raw_fd(&self) -> std::os::unix::io::RawFd {
164 self.tokio.as_raw_fd()
165 }
166 }
167
168 #[cfg(windows)]
169 impl std::os::windows::io::AsRawHandle for File {
170 fn as_raw_handle(&self) -> std::os::windows::io::RawHandle {
171 self.tokio.as_raw_handle()
172 }
173 }
174
175 impl AsyncRead for File {
176 fn poll_read(
177 mut self: Pin<&mut Self>,
178 cx: &mut Context<'_>,
179 buf: &mut ReadBuf<'_>,
180 ) -> Poll<io::Result<()>> {
181 Poll::Ready(
182 ready!(Pin::new(&mut self.tokio).poll_read(cx, buf))
183 .map_err(|err| self.error(err, ErrorKind::Read)),
184 )
185 }
186 }
187
188 impl AsyncSeek for File {
189 fn start_seek(mut self: Pin<&mut Self>, position: SeekFrom) -> io::Result<()> {
190 Pin::new(&mut self.tokio)
191 .start_seek(position)
192 .map_err(|err| self.error(err, ErrorKind::Seek))
193 }
194
195 fn poll_complete(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<u64>> {
196 Poll::Ready(
197 ready!(Pin::new(&mut self.tokio).poll_complete(cx))
198 .map_err(|err| self.error(err, ErrorKind::Seek)),
199 )
200 }
201 }
202
203 impl AsyncWrite for File {
204 fn poll_write(
205 mut self: Pin<&mut Self>,
206 cx: &mut Context<'_>,
207 buf: &[u8],
208 ) -> Poll<io::Result<usize>> {
209 Poll::Ready(
210 ready!(Pin::new(&mut self.tokio).poll_write(cx, buf))
211 .map_err(|err| self.error(err, ErrorKind::Write)),
212 )
213 }
214
215 fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
216 Poll::Ready(
217 ready!(Pin::new(&mut self.tokio).poll_flush(cx))
218 .map_err(|err| self.error(err, ErrorKind::Flush)),
219 )
220 }
221
222 fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
223 Poll::Ready(
224 ready!(Pin::new(&mut self.tokio).poll_shutdown(cx))
225 .map_err(|err| self.error(err, ErrorKind::Flush)),
226 )
227 }
228
229 fn poll_write_vectored(
230 mut self: Pin<&mut Self>,
231 cx: &mut Context<'_>,
232 bufs: &[IoSlice<'_>],
233 ) -> Poll<io::Result<usize>> {
234 Poll::Ready(
235 ready!(Pin::new(&mut self.tokio).poll_write_vectored(cx, bufs))
236 .map_err(|err| self.error(err, ErrorKind::Write)),
237 )
238 }
239
240 fn is_write_vectored(&self) -> bool {
241 self.tokio.is_write_vectored()
242 }
243 }