1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use std::{
    future::{ready, Future},
    io::{Error, ErrorKind, Result, Seek as _},
    mem::ManuallyDrop,
    os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, OwnedFd, RawFd},
    path::Path,
};

use super::{Metadata, OpenOptions};
use crate::{
    io::{Read, ReadAt, Seek, SeekFrom, Write, WriteAt},
    runtime::syscall,
};

/// A reference to an open file.
///
/// This type is an async version of [`std::fs::File`].
#[derive(Debug)]
pub struct File(OwnedFd);

impl File {
    /// Opens a file in read-only mode.
    ///
    /// See also [`std::fs::File::open`].
    pub async fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
        let path = path.as_ref();
        OpenOptions::new().read(true).open(path).await
    }

    /// Opens a file in write-only mode.
    ///
    /// This function will create a file if it does not exist, and will truncate
    /// it if it does.
    ///
    /// See also [`std::fs::File::create`].
    pub async fn create<P: AsRef<Path>>(path: P) -> Result<Self> {
        OpenOptions::new()
            .write(true)
            .create(true)
            .truncate(true)
            .open(path)
            .await
    }

    /// Returns the metadata about this file.
    ///
    /// See also [`std::fs::File::metadata`].
    pub async fn metadata(&self) -> Result<Metadata> {
        syscall::fstat(self.as_fd()).await.map(Metadata::from)
    }

    /// Truncates or extends the size of this file.
    ///
    /// See also [`std::fs::File::set_len`].
    pub async fn set_len(&self, size: u64) -> Result<()> {
        self.as_std(|file| file.set_len(size))
    }

    /// Synchronizes all modified data of this file to disk.
    ///
    /// See also [`std::fs::File::sync_all`].
    pub async fn sync_all(&self) -> Result<()> {
        syscall::fsync(self.as_fd()).await
    }

    /// This function is similiar to [`Self::sync_all`], except that it might
    /// not synchronize metadata.
    ///
    /// See also [`std::fs::File::sync_data`].
    pub async fn sync_data(&self) -> Result<()> {
        syscall::fdatasync(self.as_fd()).await
    }
}

impl File {
    fn as_std<F, R>(&self, f: F) -> R
    where
        F: Fn(&mut std::fs::File) -> R,
    {
        // Convert the file to a `std::fs::File` without taking its ownership.
        let fd = self.0.as_raw_fd();
        let mut file = unsafe { ManuallyDrop::new(std::fs::File::from_raw_fd(fd)) };
        f(&mut file)
    }
}

#[doc(hidden)]
impl From<OwnedFd> for File {
    fn from(fd: OwnedFd) -> Self {
        Self(fd)
    }
}

#[doc(hidden)]
impl AsFd for File {
    fn as_fd(&self) -> BorrowedFd<'_> {
        self.0.as_fd()
    }
}

impl AsRawFd for File {
    fn as_raw_fd(&self) -> RawFd {
        self.0.as_raw_fd()
    }
}

impl FromRawFd for File {
    unsafe fn from_raw_fd(fd: RawFd) -> Self {
        Self(OwnedFd::from_raw_fd(fd))
    }
}

impl Seek for File {
    type Seek = impl Future<Output = Result<u64>>;

    fn seek(&mut self, pos: SeekFrom) -> Self::Seek {
        ready(self.as_std(|file| file.seek(pos)))
    }
}

impl Read for File {
    type Read<'a> = impl Future<Output = Result<usize>> + 'a;

    fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::Read<'a> {
        syscall::read(self.0.as_fd(), buf)
    }
}

impl ReadAt for File {
    type ReadAt<'a> = impl Future<Output = Result<usize>> + 'a;

    fn read_at<'a>(&'a self, buf: &'a mut [u8], pos: u64) -> Self::ReadAt<'a> {
        async move {
            let pos = pos
                .try_into()
                .map_err(|e| Error::new(ErrorKind::InvalidInput, e))?;
            syscall::pread(self.0.as_fd(), buf, pos).await
        }
    }
}

impl Write for File {
    type Write<'a> = impl Future<Output = Result<usize>> + 'a;

    fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::Write<'a> {
        syscall::write(self.0.as_fd(), buf)
    }
}

impl WriteAt for File {
    type WriteAt<'a> = impl Future<Output = Result<usize>> + 'a;

    fn write_at<'a>(&'a self, buf: &'a [u8], pos: u64) -> Self::WriteAt<'a> {
        async move {
            let pos = pos
                .try_into()
                .map_err(|e| Error::new(ErrorKind::InvalidInput, e))?;
            syscall::pwrite(self.0.as_fd(), buf, pos).await
        }
    }
}