]> git.proxmox.com Git - rustc.git/blob - vendor/fs-err/src/file.rs
New upstream version 1.48.0~beta.8+dfsg1
[rustc.git] / vendor / fs-err / src / file.rs
1 use std::fs;
2 use std::io::{self, Read, Seek, Write};
3 use std::path::{Path, PathBuf};
4
5 use crate::errors::{Error, ErrorKind};
6
7 /// Wrapper around [`std::fs::File`][std::fs::File] which adds more helpful
8 /// information to all errors.
9 ///
10 /// [std::fs::File]: https://doc.rust-lang.org/stable/std/fs/struct.File.html
11 #[derive(Debug)]
12 pub struct File {
13 file: fs::File,
14 path: PathBuf,
15 }
16
17 /// Wrappers for methods from [`std::fs::File`][std::fs::File].
18 ///
19 /// [std::fs::File]: https://doc.rust-lang.org/stable/std/fs/struct.File.html
20 impl File {
21 /// Wrapper for [`File::open`](https://doc.rust-lang.org/stable/std/fs/struct.File.html#method.open).
22 pub fn open<P>(path: P) -> Result<Self, io::Error>
23 where
24 P: AsRef<Path> + Into<PathBuf>,
25 {
26 match fs::File::open(path.as_ref()) {
27 Ok(file) => Ok(File::from_parts(file, path.into())),
28 Err(source) => Err(Error::new(source, ErrorKind::OpenFile, path)),
29 }
30 }
31
32 /// Wrapper for [`File::create`](https://doc.rust-lang.org/stable/std/fs/struct.File.html#method.create).
33 pub fn create<P>(path: P) -> Result<Self, io::Error>
34 where
35 P: AsRef<Path> + Into<PathBuf>,
36 {
37 match fs::File::create(path.as_ref()) {
38 Ok(file) => Ok(File::from_parts(file, path.into())),
39 Err(source) => Err(Error::new(source, ErrorKind::CreateFile, path)),
40 }
41 }
42
43 /// Wrapper for [`OpenOptions::open`](https://doc.rust-lang.org/stable/std/fs/struct.OpenOptions.html#method.open).
44 pub fn from_options<P>(path: P, options: &fs::OpenOptions) -> Result<Self, io::Error>
45 where
46 P: AsRef<Path> + Into<PathBuf>,
47 {
48 match options.open(path.as_ref()) {
49 Ok(file) => Ok(File::from_parts(file, path.into())),
50 Err(source) => Err(Error::new(source, ErrorKind::OpenFile, path)),
51 }
52 }
53
54 /// Wrapper for [`File::sync_all`](https://doc.rust-lang.org/stable/std/fs/struct.File.html#method.sync_all).
55 pub fn sync_all(&self) -> Result<(), io::Error> {
56 self.file
57 .sync_all()
58 .map_err(|source| self.error(source, ErrorKind::SyncFile))
59 }
60
61 /// Wrapper for [`File::sync_data`](https://doc.rust-lang.org/stable/std/fs/struct.File.html#method.sync_data).
62 pub fn sync_data(&self) -> Result<(), io::Error> {
63 self.file
64 .sync_data()
65 .map_err(|source| self.error(source, ErrorKind::SyncFile))
66 }
67
68 /// Wrapper for [`File::set_len`](https://doc.rust-lang.org/stable/std/fs/struct.File.html#method.set_len).
69 pub fn set_len(&self, size: u64) -> Result<(), io::Error> {
70 self.file
71 .set_len(size)
72 .map_err(|source| self.error(source, ErrorKind::SetLen))
73 }
74
75 /// Wrapper for [`File::metadata`](https://doc.rust-lang.org/stable/std/fs/struct.File.html#method.metadata).
76 pub fn metadata(&self) -> Result<fs::Metadata, io::Error> {
77 self.file
78 .metadata()
79 .map_err(|source| self.error(source, ErrorKind::Metadata))
80 }
81
82 /// Wrapper for [`File::try_clone`](https://doc.rust-lang.org/stable/std/fs/struct.File.html#method.try_clone).
83 pub fn try_clone(&self) -> Result<Self, io::Error> {
84 self.file
85 .try_clone()
86 .map(|file| File {
87 file,
88 path: self.path.clone(),
89 })
90 .map_err(|source| self.error(source, ErrorKind::Clone))
91 }
92
93 /// Wrapper for [`File::set_permissions`](https://doc.rust-lang.org/stable/std/fs/struct.File.html#method.set_permissions).
94 pub fn set_permissions(&self, perm: fs::Permissions) -> Result<(), io::Error> {
95 self.file
96 .set_permissions(perm)
97 .map_err(|source| self.error(source, ErrorKind::SetPermissions))
98 }
99
100 /// Creates a [`File`](struct.File.html) from a raw file and its path.
101 pub fn from_parts<P>(file: fs::File, path: P) -> Self
102 where
103 P: Into<PathBuf>,
104 {
105 File {
106 file,
107 path: path.into(),
108 }
109 }
110 }
111
112 /// Methods added by fs-err that are not available on
113 /// [`std::fs::File`][std::fs::File].
114 ///
115 /// [std::fs::File]: https://doc.rust-lang.org/stable/std/fs/struct.File.html
116 impl File {
117 /// Returns a reference to the underlying [`std::fs::File`][std::fs::File].
118 ///
119 /// [std::fs::File]: https://doc.rust-lang.org/stable/std/fs/struct.File.html
120 pub fn file(&self) -> &fs::File {
121 &self.file
122 }
123
124 /// Returns a reference to the path that this file was created with.
125 pub fn path(&self) -> &Path {
126 &self.path
127 }
128
129 /// Wrap the error in information specific to this `File` object.
130 fn error(&self, source: io::Error, kind: ErrorKind) -> io::Error {
131 Error::new(source, kind, &self.path)
132 }
133 }
134
135 impl Read for File {
136 fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
137 self.file
138 .read(buf)
139 .map_err(|source| self.error(source, ErrorKind::Read))
140 }
141
142 fn read_vectored(&mut self, bufs: &mut [std::io::IoSliceMut<'_>]) -> std::io::Result<usize> {
143 self.file
144 .read_vectored(bufs)
145 .map_err(|source| self.error(source, ErrorKind::Read))
146 }
147 }
148
149 impl<'a> Read for &'a File {
150 fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
151 (&(**self).file)
152 .read(buf)
153 .map_err(|source| self.error(source, ErrorKind::Read))
154 }
155
156 fn read_vectored(&mut self, bufs: &mut [std::io::IoSliceMut<'_>]) -> std::io::Result<usize> {
157 (&(**self).file)
158 .read_vectored(bufs)
159 .map_err(|source| self.error(source, ErrorKind::Read))
160 }
161 }
162
163 impl Seek for File {
164 fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
165 self.file
166 .seek(pos)
167 .map_err(|source| self.error(source, ErrorKind::Seek))
168 }
169 }
170
171 impl<'a> Seek for &'a File {
172 fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
173 (&(**self).file)
174 .seek(pos)
175 .map_err(|source| self.error(source, ErrorKind::Seek))
176 }
177 }
178
179 impl Write for File {
180 fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
181 self.file
182 .write(buf)
183 .map_err(|source| self.error(source, ErrorKind::Write))
184 }
185
186 fn write_vectored(&mut self, bufs: &[std::io::IoSlice<'_>]) -> std::io::Result<usize> {
187 self.file
188 .write_vectored(bufs)
189 .map_err(|source| self.error(source, ErrorKind::Write))
190 }
191
192 fn flush(&mut self) -> std::io::Result<()> {
193 self.file
194 .flush()
195 .map_err(|source| self.error(source, ErrorKind::Flush))
196 }
197 }
198
199 impl<'a> Write for &'a File {
200 fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
201 (&(**self).file)
202 .write(buf)
203 .map_err(|source| self.error(source, ErrorKind::Write))
204 }
205
206 fn write_vectored(&mut self, bufs: &[std::io::IoSlice<'_>]) -> std::io::Result<usize> {
207 (&(**self).file)
208 .write_vectored(bufs)
209 .map_err(|source| self.error(source, ErrorKind::Write))
210 }
211
212 fn flush(&mut self) -> std::io::Result<()> {
213 (&(**self).file)
214 .flush()
215 .map_err(|source| self.error(source, ErrorKind::Flush))
216 }
217 }