virtual_fs/
special_file.rs

1//! Used for /dev/stdin, /dev/stdout, dev/stderr - returns a
2//! static file descriptor (0, 1, 2)
3
4use std::io::{self, *};
5use std::pin::Pin;
6use std::task::{Context, Poll};
7
8use crate::VirtualFile;
9use tokio::io::{AsyncRead, AsyncSeek, AsyncWrite};
10
11pub type Fd = u32;
12
13/// A "special" file is a file that is locked
14/// to one file descriptor (i.e. stdout => 0, stdin => 1), etc.
15#[derive(Debug)]
16pub struct DeviceFile {
17    fd: Fd,
18}
19
20impl DeviceFile {
21    pub const STDIN: Fd = 0;
22    pub const STDOUT: Fd = 1;
23    pub const STDERR: Fd = 2;
24
25    pub fn new(fd: Fd) -> Self {
26        Self { fd }
27    }
28}
29
30impl AsyncSeek for DeviceFile {
31    fn start_seek(self: Pin<&mut Self>, _position: SeekFrom) -> io::Result<()> {
32        Ok(())
33    }
34
35    fn poll_complete(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<u64>> {
36        Poll::Ready(Ok(0))
37    }
38}
39
40impl AsyncWrite for DeviceFile {
41    fn poll_write(
42        self: Pin<&mut Self>,
43        _cx: &mut Context<'_>,
44        buf: &[u8],
45    ) -> Poll<io::Result<usize>> {
46        Poll::Ready(Ok(buf.len()))
47    }
48
49    fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
50        Poll::Ready(Ok(()))
51    }
52
53    fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
54        Poll::Ready(Ok(()))
55    }
56
57    fn poll_write_vectored(
58        self: Pin<&mut Self>,
59        _cx: &mut Context<'_>,
60        bufs: &[IoSlice<'_>],
61    ) -> Poll<io::Result<usize>> {
62        Poll::Ready(Ok(bufs.len()))
63    }
64
65    fn is_write_vectored(&self) -> bool {
66        false
67    }
68}
69
70impl AsyncRead for DeviceFile {
71    fn poll_read(
72        self: Pin<&mut Self>,
73        _cx: &mut Context<'_>,
74        _buf: &mut tokio::io::ReadBuf<'_>,
75    ) -> Poll<io::Result<()>> {
76        Poll::Ready(Ok(()))
77    }
78}
79
80impl VirtualFile for DeviceFile {
81    fn last_accessed(&self) -> u64 {
82        0
83    }
84    fn last_modified(&self) -> u64 {
85        0
86    }
87    fn created_time(&self) -> u64 {
88        0
89    }
90    fn size(&self) -> u64 {
91        0
92    }
93    fn set_len(&mut self, _new_size: u64) -> crate::Result<()> {
94        Ok(())
95    }
96    fn unlink(&mut self) -> crate::Result<()> {
97        Ok(())
98    }
99    fn get_special_fd(&self) -> Option<u32> {
100        Some(self.fd)
101    }
102    fn poll_read_ready(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
103        Poll::Ready(Ok(0))
104    }
105    fn poll_write_ready(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
106        Poll::Ready(Ok(0))
107    }
108}