]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/solid/fs.rs
Rollup merge of #93613 - crlf0710:rename_to_async_iter, r=yaahc
[rust.git] / library / std / src / sys / solid / fs.rs
1 use super::{abi, error};
2 use crate::{
3     ffi::{CStr, CString, OsStr, OsString},
4     fmt,
5     io::{self, IoSlice, IoSliceMut, ReadBuf, SeekFrom},
6     mem::MaybeUninit,
7     os::raw::{c_int, c_short},
8     os::solid::ffi::OsStrExt,
9     path::{Path, PathBuf},
10     sync::Arc,
11     sys::time::SystemTime,
12     sys::unsupported,
13 };
14
15 pub use crate::sys_common::fs::try_exists;
16
17 /// A file descriptor.
18 #[derive(Clone, Copy)]
19 #[rustc_layout_scalar_valid_range_start(0)]
20 // libstd/os/raw/mod.rs assures me that every libstd-supported platform has a
21 // 32-bit c_int. Below is -2, in two's complement, but that only works out
22 // because c_int is 32 bits.
23 #[rustc_layout_scalar_valid_range_end(0xFF_FF_FF_FE)]
24 struct FileDesc {
25     fd: c_int,
26 }
27
28 impl FileDesc {
29     #[inline]
30     fn new(fd: c_int) -> FileDesc {
31         assert_ne!(fd, -1i32);
32         // Safety: we just asserted that the value is in the valid range and
33         // isn't `-1` (the only value bigger than `0xFF_FF_FF_FE` unsigned)
34         unsafe { FileDesc { fd } }
35     }
36
37     #[inline]
38     fn raw(&self) -> c_int {
39         self.fd
40     }
41 }
42
43 pub struct File {
44     fd: FileDesc,
45 }
46
47 #[derive(Clone)]
48 pub struct FileAttr {
49     stat: abi::stat,
50 }
51
52 // all DirEntry's will have a reference to this struct
53 struct InnerReadDir {
54     dirp: abi::S_DIR,
55     root: PathBuf,
56 }
57
58 pub struct ReadDir {
59     inner: Arc<InnerReadDir>,
60 }
61
62 pub struct DirEntry {
63     entry: abi::dirent,
64     inner: Arc<InnerReadDir>,
65 }
66
67 #[derive(Clone, Debug)]
68 pub struct OpenOptions {
69     // generic
70     read: bool,
71     write: bool,
72     append: bool,
73     truncate: bool,
74     create: bool,
75     create_new: bool,
76     // system-specific
77     custom_flags: i32,
78 }
79
80 #[derive(Clone, PartialEq, Eq, Debug)]
81 pub struct FilePermissions(c_short);
82
83 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
84 pub struct FileType(c_short);
85
86 #[derive(Debug)]
87 pub struct DirBuilder {}
88
89 impl FileAttr {
90     pub fn size(&self) -> u64 {
91         self.stat.st_size as u64
92     }
93
94     pub fn perm(&self) -> FilePermissions {
95         FilePermissions(self.stat.st_mode)
96     }
97
98     pub fn file_type(&self) -> FileType {
99         FileType(self.stat.st_mode)
100     }
101
102     pub fn modified(&self) -> io::Result<SystemTime> {
103         Ok(SystemTime::from_time_t(self.stat.st_mtime))
104     }
105
106     pub fn accessed(&self) -> io::Result<SystemTime> {
107         Ok(SystemTime::from_time_t(self.stat.st_atime))
108     }
109
110     pub fn created(&self) -> io::Result<SystemTime> {
111         Ok(SystemTime::from_time_t(self.stat.st_ctime))
112     }
113 }
114
115 impl FilePermissions {
116     pub fn readonly(&self) -> bool {
117         (self.0 & abi::S_IWRITE) == 0
118     }
119
120     pub fn set_readonly(&mut self, readonly: bool) {
121         if readonly {
122             self.0 &= !abi::S_IWRITE;
123         } else {
124             self.0 |= abi::S_IWRITE;
125         }
126     }
127 }
128
129 impl FileType {
130     pub fn is_dir(&self) -> bool {
131         self.is(abi::S_IFDIR)
132     }
133     pub fn is_file(&self) -> bool {
134         self.is(abi::S_IFREG)
135     }
136     pub fn is_symlink(&self) -> bool {
137         false
138     }
139
140     pub fn is(&self, mode: c_short) -> bool {
141         self.0 & abi::S_IFMT == mode
142     }
143 }
144
145 pub fn readdir(p: &Path) -> io::Result<ReadDir> {
146     unsafe {
147         let mut dir = MaybeUninit::uninit();
148         error::SolidError::err_if_negative(abi::SOLID_FS_OpenDir(
149             cstr(p)?.as_ptr(),
150             dir.as_mut_ptr(),
151         ))
152         .map_err(|e| e.as_io_error())?;
153         let inner = Arc::new(InnerReadDir { dirp: dir.assume_init(), root: p.to_owned() });
154         Ok(ReadDir { inner })
155     }
156 }
157
158 impl fmt::Debug for ReadDir {
159     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
160         // This will only be called from std::fs::ReadDir, which will add a "ReadDir()" frame.
161         // Thus the result will be e g 'ReadDir("/home")'
162         fmt::Debug::fmt(&*self.inner.root, f)
163     }
164 }
165
166 impl Iterator for ReadDir {
167     type Item = io::Result<DirEntry>;
168
169     fn next(&mut self) -> Option<io::Result<DirEntry>> {
170         unsafe {
171             let mut out_dirent = MaybeUninit::uninit();
172             error::SolidError::err_if_negative(abi::SOLID_FS_ReadDir(
173                 self.inner.dirp,
174                 out_dirent.as_mut_ptr(),
175             ))
176             .ok()?;
177             Some(Ok(DirEntry { entry: out_dirent.assume_init(), inner: Arc::clone(&self.inner) }))
178         }
179     }
180 }
181
182 impl Drop for InnerReadDir {
183     fn drop(&mut self) {
184         unsafe { abi::SOLID_FS_CloseDir(self.dirp) };
185     }
186 }
187
188 impl DirEntry {
189     pub fn path(&self) -> PathBuf {
190         self.inner.root.join(OsStr::from_bytes(
191             unsafe { CStr::from_ptr(self.entry.d_name.as_ptr()) }.to_bytes(),
192         ))
193     }
194
195     pub fn file_name(&self) -> OsString {
196         OsStr::from_bytes(unsafe { CStr::from_ptr(self.entry.d_name.as_ptr()) }.to_bytes())
197             .to_os_string()
198     }
199
200     pub fn metadata(&self) -> io::Result<FileAttr> {
201         lstat(&self.path())
202     }
203
204     pub fn file_type(&self) -> io::Result<FileType> {
205         match self.entry.d_type {
206             abi::DT_CHR => Ok(FileType(abi::S_IFCHR)),
207             abi::DT_FIFO => Ok(FileType(abi::S_IFIFO)),
208             abi::DT_REG => Ok(FileType(abi::S_IFREG)),
209             abi::DT_DIR => Ok(FileType(abi::S_IFDIR)),
210             abi::DT_BLK => Ok(FileType(abi::S_IFBLK)),
211             _ => lstat(&self.path()).map(|m| m.file_type()),
212         }
213     }
214 }
215
216 impl OpenOptions {
217     pub fn new() -> OpenOptions {
218         OpenOptions {
219             // generic
220             read: false,
221             write: false,
222             append: false,
223             truncate: false,
224             create: false,
225             create_new: false,
226             // system-specific
227             custom_flags: 0,
228         }
229     }
230
231     pub fn read(&mut self, read: bool) {
232         self.read = read;
233     }
234     pub fn write(&mut self, write: bool) {
235         self.write = write;
236     }
237     pub fn append(&mut self, append: bool) {
238         self.append = append;
239     }
240     pub fn truncate(&mut self, truncate: bool) {
241         self.truncate = truncate;
242     }
243     pub fn create(&mut self, create: bool) {
244         self.create = create;
245     }
246     pub fn create_new(&mut self, create_new: bool) {
247         self.create_new = create_new;
248     }
249
250     pub fn custom_flags(&mut self, flags: i32) {
251         self.custom_flags = flags;
252     }
253     pub fn mode(&mut self, _mode: u32) {}
254
255     fn get_access_mode(&self) -> io::Result<c_int> {
256         match (self.read, self.write, self.append) {
257             (true, false, false) => Ok(abi::O_RDONLY),
258             (false, true, false) => Ok(abi::O_WRONLY),
259             (true, true, false) => Ok(abi::O_RDWR),
260             (false, _, true) => Ok(abi::O_WRONLY | abi::O_APPEND),
261             (true, _, true) => Ok(abi::O_RDWR | abi::O_APPEND),
262             (false, false, false) => Err(io::Error::from_raw_os_error(libc::EINVAL)),
263         }
264     }
265
266     fn get_creation_mode(&self) -> io::Result<c_int> {
267         match (self.write, self.append) {
268             (true, false) => {}
269             (false, false) => {
270                 if self.truncate || self.create || self.create_new {
271                     return Err(io::Error::from_raw_os_error(libc::EINVAL));
272                 }
273             }
274             (_, true) => {
275                 if self.truncate && !self.create_new {
276                     return Err(io::Error::from_raw_os_error(libc::EINVAL));
277                 }
278             }
279         }
280
281         Ok(match (self.create, self.truncate, self.create_new) {
282             (false, false, false) => 0,
283             (true, false, false) => abi::O_CREAT,
284             (false, true, false) => abi::O_TRUNC,
285             (true, true, false) => abi::O_CREAT | abi::O_TRUNC,
286             (_, _, true) => abi::O_CREAT | abi::O_EXCL,
287         })
288     }
289 }
290
291 fn cstr(path: &Path) -> io::Result<CString> {
292     Ok(CString::new(path.as_os_str().as_bytes())?)
293 }
294
295 impl File {
296     pub fn open(path: &Path, opts: &OpenOptions) -> io::Result<File> {
297         let flags = opts.get_access_mode()?
298             | opts.get_creation_mode()?
299             | (opts.custom_flags as c_int & !abi::O_ACCMODE);
300         unsafe {
301             let mut fd = MaybeUninit::uninit();
302             error::SolidError::err_if_negative(abi::SOLID_FS_Open(
303                 fd.as_mut_ptr(),
304                 cstr(path)?.as_ptr(),
305                 flags,
306             ))
307             .map_err(|e| e.as_io_error())?;
308             Ok(File { fd: FileDesc::new(fd.assume_init()) })
309         }
310     }
311
312     pub fn file_attr(&self) -> io::Result<FileAttr> {
313         unsupported()
314     }
315
316     pub fn fsync(&self) -> io::Result<()> {
317         self.flush()
318     }
319
320     pub fn datasync(&self) -> io::Result<()> {
321         self.flush()
322     }
323
324     pub fn truncate(&self, _size: u64) -> io::Result<()> {
325         unsupported()
326     }
327
328     pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
329         unsafe {
330             let mut out_num_bytes = MaybeUninit::uninit();
331             error::SolidError::err_if_negative(abi::SOLID_FS_Read(
332                 self.fd.raw(),
333                 buf.as_mut_ptr(),
334                 buf.len(),
335                 out_num_bytes.as_mut_ptr(),
336             ))
337             .map_err(|e| e.as_io_error())?;
338             Ok(out_num_bytes.assume_init())
339         }
340     }
341
342     pub fn read_buf(&self, buf: &mut ReadBuf<'_>) -> io::Result<()> {
343         unsafe {
344             let len = buf.remaining();
345             let mut out_num_bytes = MaybeUninit::uninit();
346             error::SolidError::err_if_negative(abi::SOLID_FS_Read(
347                 self.fd.raw(),
348                 buf.unfilled_mut().as_mut_ptr() as *mut u8,
349                 len,
350                 out_num_bytes.as_mut_ptr(),
351             ))
352             .map_err(|e| e.as_io_error())?;
353
354             // Safety: `out_num_bytes` is filled by the successful call to
355             // `SOLID_FS_Read`
356             let num_bytes_read = out_num_bytes.assume_init();
357
358             // Safety: `num_bytes_read` bytes were written to the unfilled
359             // portion of the buffer
360             buf.assume_init(num_bytes_read);
361
362             buf.add_filled(num_bytes_read);
363
364             Ok(())
365         }
366     }
367
368     pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
369         crate::io::default_read_vectored(|buf| self.read(buf), bufs)
370     }
371
372     pub fn is_read_vectored(&self) -> bool {
373         false
374     }
375
376     pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
377         unsafe {
378             let mut out_num_bytes = MaybeUninit::uninit();
379             error::SolidError::err_if_negative(abi::SOLID_FS_Write(
380                 self.fd.raw(),
381                 buf.as_ptr(),
382                 buf.len(),
383                 out_num_bytes.as_mut_ptr(),
384             ))
385             .map_err(|e| e.as_io_error())?;
386             Ok(out_num_bytes.assume_init())
387         }
388     }
389
390     pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
391         crate::io::default_write_vectored(|buf| self.write(buf), bufs)
392     }
393
394     pub fn is_write_vectored(&self) -> bool {
395         false
396     }
397
398     pub fn flush(&self) -> io::Result<()> {
399         error::SolidError::err_if_negative(unsafe { abi::SOLID_FS_Sync(self.fd.raw()) })
400             .map_err(|e| e.as_io_error())?;
401         Ok(())
402     }
403
404     pub fn seek(&self, pos: SeekFrom) -> io::Result<u64> {
405         let (whence, pos) = match pos {
406             // Casting to `i64` is fine, too large values will end up as
407             // negative which will cause an error in `SOLID_FS_Lseek`.
408             SeekFrom::Start(off) => (abi::SEEK_SET, off as i64),
409             SeekFrom::End(off) => (abi::SEEK_END, off),
410             SeekFrom::Current(off) => (abi::SEEK_CUR, off),
411         };
412         error::SolidError::err_if_negative(unsafe {
413             abi::SOLID_FS_Lseek(self.fd.raw(), pos, whence)
414         })
415         .map_err(|e| e.as_io_error())?;
416
417         // Get the new offset
418         unsafe {
419             let mut out_offset = MaybeUninit::uninit();
420             error::SolidError::err_if_negative(abi::SOLID_FS_Ftell(
421                 self.fd.raw(),
422                 out_offset.as_mut_ptr(),
423             ))
424             .map_err(|e| e.as_io_error())?;
425             Ok(out_offset.assume_init() as u64)
426         }
427     }
428
429     pub fn duplicate(&self) -> io::Result<File> {
430         unsupported()
431     }
432
433     pub fn set_permissions(&self, _perm: FilePermissions) -> io::Result<()> {
434         unsupported()
435     }
436 }
437
438 impl Drop for File {
439     fn drop(&mut self) {
440         unsafe { abi::SOLID_FS_Close(self.fd.raw()) };
441     }
442 }
443
444 impl DirBuilder {
445     pub fn new() -> DirBuilder {
446         DirBuilder {}
447     }
448
449     pub fn mkdir(&self, p: &Path) -> io::Result<()> {
450         error::SolidError::err_if_negative(unsafe { abi::SOLID_FS_Mkdir(cstr(p)?.as_ptr()) })
451             .map_err(|e| e.as_io_error())?;
452         Ok(())
453     }
454 }
455
456 impl fmt::Debug for File {
457     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
458         f.debug_struct("File").field("fd", &self.fd.raw()).finish()
459     }
460 }
461
462 pub fn unlink(p: &Path) -> io::Result<()> {
463     if stat(p)?.file_type().is_dir() {
464         Err(io::const_io_error!(io::ErrorKind::IsADirectory, "is a directory"))
465     } else {
466         error::SolidError::err_if_negative(unsafe { abi::SOLID_FS_Unlink(cstr(p)?.as_ptr()) })
467             .map_err(|e| e.as_io_error())?;
468         Ok(())
469     }
470 }
471
472 pub fn rename(old: &Path, new: &Path) -> io::Result<()> {
473     error::SolidError::err_if_negative(unsafe {
474         abi::SOLID_FS_Rename(cstr(old)?.as_ptr(), cstr(new)?.as_ptr())
475     })
476     .map_err(|e| e.as_io_error())?;
477     Ok(())
478 }
479
480 pub fn set_perm(p: &Path, perm: FilePermissions) -> io::Result<()> {
481     error::SolidError::err_if_negative(unsafe {
482         abi::SOLID_FS_Chmod(cstr(p)?.as_ptr(), perm.0.into())
483     })
484     .map_err(|e| e.as_io_error())?;
485     Ok(())
486 }
487
488 pub fn rmdir(p: &Path) -> io::Result<()> {
489     if stat(p)?.file_type().is_dir() {
490         error::SolidError::err_if_negative(unsafe { abi::SOLID_FS_Unlink(cstr(p)?.as_ptr()) })
491             .map_err(|e| e.as_io_error())?;
492         Ok(())
493     } else {
494         Err(io::const_io_error!(io::ErrorKind::NotADirectory, "not a directory"))
495     }
496 }
497
498 pub fn remove_dir_all(path: &Path) -> io::Result<()> {
499     for child in readdir(path)? {
500         let child = child?;
501         let child_type = child.file_type()?;
502         if child_type.is_dir() {
503             remove_dir_all(&child.path())?;
504         } else {
505             unlink(&child.path())?;
506         }
507     }
508     rmdir(path)
509 }
510
511 pub fn readlink(p: &Path) -> io::Result<PathBuf> {
512     // This target doesn't support symlinks
513     stat(p)?;
514     Err(io::const_io_error!(io::ErrorKind::InvalidInput, "not a symbolic link"))
515 }
516
517 pub fn symlink(_original: &Path, _link: &Path) -> io::Result<()> {
518     // This target doesn't support symlinks
519     unsupported()
520 }
521
522 pub fn link(_src: &Path, _dst: &Path) -> io::Result<()> {
523     // This target doesn't support symlinks
524     unsupported()
525 }
526
527 pub fn stat(p: &Path) -> io::Result<FileAttr> {
528     // This target doesn't support symlinks
529     lstat(p)
530 }
531
532 pub fn lstat(p: &Path) -> io::Result<FileAttr> {
533     unsafe {
534         let mut out_stat = MaybeUninit::uninit();
535         error::SolidError::err_if_negative(abi::SOLID_FS_Stat(
536             cstr(p)?.as_ptr(),
537             out_stat.as_mut_ptr(),
538         ))
539         .map_err(|e| e.as_io_error())?;
540         Ok(FileAttr { stat: out_stat.assume_init() })
541     }
542 }
543
544 pub fn canonicalize(_p: &Path) -> io::Result<PathBuf> {
545     unsupported()
546 }
547
548 pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
549     use crate::fs::File;
550
551     let mut reader = File::open(from)?;
552     let mut writer = File::create(to)?;
553
554     io::copy(&mut reader, &mut writer)
555 }