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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#[cfg(feature = "allocator_api")]
use std::alloc::Allocator;
use std::{fs::Metadata, io, path::Path};

use compio_driver::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
#[cfg(feature = "runtime")]
use {
    compio_buf::{buf_try, vec_alloc, BufResult, IntoInner, IoBuf, IoBufMut},
    compio_driver::op::{BufResultExt, ReadAt, Sync, WriteAt},
    compio_runtime::{submit, Attacher},
};

use crate::OpenOptions;

/// A reference to an open file on the filesystem.
///
/// An instance of a `File` can be read and/or written depending on what options
/// it was opened with. The `File` type provides **positional** read and write
/// operations. The file does not maintain an internal cursor. The caller is
/// required to specify an offset when issuing an operation.
#[derive(Debug)]
pub struct File {
    inner: std::fs::File,
    #[cfg(feature = "runtime")]
    attacher: Attacher,
}

#[cfg(target_os = "windows")]
fn file_with_options(
    path: impl AsRef<Path>,
    mut options: std::fs::OpenOptions,
) -> io::Result<std::fs::File> {
    use std::os::windows::prelude::OpenOptionsExt;

    use windows_sys::Win32::Storage::FileSystem::FILE_FLAG_OVERLAPPED;

    options.custom_flags(FILE_FLAG_OVERLAPPED);
    options.open(path)
}

#[cfg(not(target_os = "windows"))]
fn file_with_options(
    path: impl AsRef<Path>,
    mut options: std::fs::OpenOptions,
) -> io::Result<std::fs::File> {
    use std::os::unix::prelude::OpenOptionsExt;

    // Don't set nonblocking with epoll.
    if cfg!(not(any(target_os = "linux", target_os = "android"))) {
        options.custom_flags(libc::O_NONBLOCK);
    }
    options.open(path)
}

impl File {
    pub(crate) fn with_options(path: impl AsRef<Path>, options: OpenOptions) -> io::Result<Self> {
        let this = Self {
            inner: file_with_options(path, options.0)?,
            #[cfg(feature = "runtime")]
            attacher: Attacher::new(),
        };
        Ok(this)
    }

    /// Attempts to open a file in read-only mode.
    ///
    /// See the [`OpenOptions::open`] method for more details.
    pub fn open(path: impl AsRef<Path>) -> io::Result<Self> {
        OpenOptions::new().read(true).open(path)
    }

    /// 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 the [`OpenOptions::open`] function for more details.
    pub fn create(path: impl AsRef<Path>) -> io::Result<Self> {
        OpenOptions::new()
            .create(true)
            .write(true)
            .truncate(true)
            .open(path)
    }

    #[cfg(feature = "runtime")]
    pub(crate) fn attach(&self) -> io::Result<()> {
        self.attacher.attach(self)
    }

    /// Creates a new `File` instance that shares the same underlying file
    /// handle as the existing `File` instance.
    ///
    /// It does not clear the attach state.
    pub fn try_clone(&self) -> io::Result<Self> {
        let inner = self.inner.try_clone()?;
        Ok(Self {
            #[cfg(feature = "runtime")]
            attacher: self.attacher.try_clone(&inner)?,
            inner,
        })
    }

    /// Queries metadata about the underlying file.
    pub fn metadata(&self) -> io::Result<Metadata> {
        self.inner.metadata()
    }

    /// Read some bytes at the specified offset from the file into the specified
    /// buffer, returning how many bytes were read.
    ///
    /// # Return
    ///
    /// The method returns the operation result and the same buffer value passed
    /// as an argument.
    ///
    /// If the method returns [`Ok(n)`], then the read was successful. A nonzero
    /// `n` value indicates that the buffer has been filled with `n` bytes of
    /// data from the file. If `n` is `0`, then one of the following happened:
    ///
    /// 1. The specified offset is the end of the file.
    /// 2. The buffer specified was 0 bytes in capacity.
    ///
    /// It is not an error if the returned value `n` is smaller than the buffer
    /// size, even when the file contains enough data to fill the buffer.
    ///
    /// # Errors
    ///
    /// If this function encounters any form of I/O or other error, an error
    /// variant will be returned. The buffer is returned on error.
    #[cfg(feature = "runtime")]
    pub async fn read_at<T: IoBufMut>(&self, buffer: T, pos: usize) -> BufResult<usize, T> {
        let ((), buffer) = buf_try!(self.attach(), buffer);
        let op = ReadAt::new(self.as_raw_fd(), pos, buffer);
        submit(op).await.into_inner().map_advanced()
    }

    /// Read the exact number of bytes required to fill `buffer`.
    ///
    /// This function reads as many bytes as necessary to completely fill the
    /// uninitialized space of specified `buffer`.
    ///
    /// # Errors
    ///
    /// If this function encounters an "end of file" before completely filling
    /// the buffer, it returns an error of the kind
    /// [`ErrorKind::UnexpectedEof`]. The contents of `buffer` are unspecified
    /// in this case.
    ///
    /// If any other read error is encountered then this function immediately
    /// returns. The contents of `buffer` are unspecified in this case.
    ///
    /// If this function returns an error, it is unspecified how many bytes it
    /// has read, but it will never read more than would be necessary to
    /// completely fill the buffer.
    ///
    /// [`ErrorKind::UnexpectedEof`]: io::ErrorKind::UnexpectedEof
    #[cfg(feature = "runtime")]
    pub async fn read_exact_at<T: IoBufMut>(
        &self,
        mut buffer: T,
        pos: usize,
    ) -> BufResult<usize, T> {
        let need = buffer.as_uninit_slice().len();
        let mut total_read = 0;
        let mut read;
        while total_read < need {
            (read, buffer) = buf_try!(self.read_at(buffer, pos + total_read).await);
            if read == 0 {
                break;
            } else {
                total_read += read;
            }
        }
        let res = if total_read < need {
            Err(io::Error::new(
                io::ErrorKind::UnexpectedEof,
                "failed to fill whole buffer",
            ))
        } else {
            Ok(total_read)
        };
        BufResult(res, buffer)
    }

    /// Read all bytes until EOF in this source, placing them into `buffer`.
    ///
    /// All bytes read from this source will be appended to the specified buffer
    /// `buffer`. This function will continuously call [`read_at()`] to append
    /// more data to `buffer` until [`read_at()`] returns [`Ok(0)`].
    ///
    /// If successful, this function will return the total number of bytes read.
    ///
    /// [`read_at()`]: File::read_at
    #[cfg(feature = "runtime")]
    pub async fn read_to_end_at<
        #[cfg(feature = "allocator_api")] A: Allocator + Unpin + 'static,
    >(
        &self,
        mut buffer: vec_alloc!(u8, A),
        pos: usize,
    ) -> BufResult<usize, vec_alloc!(u8, A)> {
        let mut total_read = 0;
        let mut read;
        loop {
            (read, buffer) = buf_try!(self.read_at(buffer, pos + total_read).await);
            if read == 0 {
                break;
            } else {
                total_read += read;
                if buffer.len() == buffer.capacity() {
                    buffer.reserve(32);
                }
            }
        }
        BufResult(Ok(total_read), buffer)
    }

    /// Write a buffer into this file at the specified offset, returning how
    /// many bytes were written.
    ///
    /// This function will attempt to write the entire contents of `buf`, but
    /// the entire write may not succeed, or the write may also generate an
    /// error. The bytes will be written starting at the specified offset.
    ///
    /// # Return
    ///
    /// The method returns the operation result and the same buffer value passed
    /// in as an argument. A return value of `0` typically means that the
    /// underlying file is no longer able to accept bytes and will likely not be
    /// able to in the future as well, or that the buffer provided is empty.
    ///
    /// # Errors
    ///
    /// Each call to `write_at` may generate an I/O error indicating that the
    /// operation could not be completed. If an error is returned then no bytes
    /// in the buffer were written to this writer.
    ///
    /// It is **not** considered an error if the entire buffer could not be
    /// written to this writer.
    #[cfg(feature = "runtime")]
    pub async fn write_at<T: IoBuf>(&self, buffer: T, pos: usize) -> BufResult<usize, T> {
        let ((), buffer) = buf_try!(self.attach(), buffer);
        let op = WriteAt::new(self.as_raw_fd(), pos, buffer);
        submit(op).await.into_inner()
    }

    /// Attempts to write an entire buffer into this writer.
    ///
    /// This method will continuously call [`write_at`] until there is no more
    /// data to be written. This method will not return until the entire
    /// buffer has been successfully written or such an error occurs.
    ///
    /// If the buffer contains no data, this will never call [`write_at`].
    ///
    /// [`write_at`]: File::write_at
    #[cfg(feature = "runtime")]
    pub async fn write_all_at<T: IoBuf>(&self, mut buffer: T, pos: usize) -> BufResult<usize, T> {
        let buf_len = buffer.buf_len();
        let mut total_written = 0;
        let mut written;
        while total_written < buf_len {
            (written, buffer) = buf_try!(
                self.write_at(buffer.slice(total_written..), pos + total_written)
                    .await
                    .into_inner()
            );
            total_written += written;
        }
        BufResult(Ok(total_written), buffer)
    }

    #[cfg(feature = "runtime")]
    async fn sync_impl(&self, datasync: bool) -> io::Result<()> {
        self.attach()?;
        let op = Sync::new(self.as_raw_fd(), datasync);
        submit(op).await.0?;
        Ok(())
    }

    /// Attempts to sync all OS-internal metadata to disk.
    ///
    /// This function will attempt to ensure that all in-memory data reaches the
    /// filesystem before returning.
    #[cfg(feature = "runtime")]
    pub async fn sync_all(&self) -> io::Result<()> {
        self.sync_impl(false).await
    }

    /// This function is similar to [`sync_all`], except that it might not
    /// synchronize file metadata to the filesystem.
    ///
    /// This is intended for use cases that must synchronize content, but don't
    /// need the metadata on disk. The goal of this method is to reduce disk
    /// operations.
    ///
    /// Note that some platforms may simply implement this in terms of
    /// [`sync_all`].
    ///
    /// [`sync_all`]: File::sync_all
    #[cfg(feature = "runtime")]
    pub async fn sync_data(&self) -> io::Result<()> {
        self.sync_impl(true).await
    }
}

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

impl FromRawFd for File {
    unsafe fn from_raw_fd(fd: RawFd) -> Self {
        Self {
            inner: FromRawFd::from_raw_fd(fd),
            #[cfg(feature = "runtime")]
            attacher: compio_runtime::Attacher::new(),
        }
    }
}

impl IntoRawFd for File {
    fn into_raw_fd(self) -> RawFd {
        self.inner.into_raw_fd()
    }
}