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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#[cfg(not(any(unix, target_os = "windows", wasi_ext)))]
use std::sync::{Mutex, PoisonError};

use std::{
    fs::{self, File},
    io,
    path::Path,
    sync::Arc,
};

#[cfg(unix)]
use std::os::unix::prelude::*;
#[cfg(wasi_ext)]
use std::os::wasi::prelude::*;
#[cfg(target_os = "windows")]
use std::os::windows::prelude::*;

use super::{ReadAt, WriteAt};

#[cfg(any(unix, target_os = "windows", wasi_ext))]
type FileRepr = File;

// If no platform-specific extension is available, we use a mutex to make sure
// operations (seek + read) are atomic.
#[cfg(not(any(unix, target_os = "windows", wasi_ext)))]
type FileRepr = Mutex<File>;

/// A file with cross-platform positioned I/O.
#[derive(Debug)]
pub struct RandomAccessFile(FileRepr);

impl RandomAccessFile {
    /// Attempts to open a file in read-only mode.
    ///
    /// See [`File::open`] for details.
    #[inline]
    pub fn open<P: AsRef<Path>>(path: P) -> io::Result<RandomAccessFile> {
        let f = File::open(path.as_ref())?;
        Ok(RandomAccessFile::from(f))
    }

    /// Opens a file in write-only mode.
    ///
    /// See [`File::create`] for details.
    #[inline]
    pub fn create<P: AsRef<Path>>(path: P) -> io::Result<RandomAccessFile> {
        let f = File::create(path.as_ref())?;
        Ok(RandomAccessFile::from(f))
    }

    #[inline]
    pub(crate) fn with_file<T>(&self, f: impl FnOnce(&File) -> T) -> T {
        #[cfg(any(unix, target_os = "windows", wasi_ext))]
        {
            f(&self.0)
        }

        #[cfg(not(any(unix, target_os = "windows", wasi_ext)))]
        {
            f(&self.0.lock().unwrap_or_else(PoisonError::into_inner))
        }
    }

    /// Attempts to sync all OS-internal metadata to disk.
    ///
    /// See [`File::sync_all`] for details.
    #[inline]
    pub fn sync_all(&self) -> io::Result<()> {
        self.with_file(|f| f.sync_all())
    }

    /// This function is similar to `sync_all`, except that it may not
    /// synchronize file metadata to the filesystem.
    ///
    /// See [`File::sync_data`] for details.
    #[inline]
    pub fn sync_data(&self) -> io::Result<()> {
        self.with_file(|f| f.sync_data())
    }

    /// Truncates or extends the underlying file, updating the size of this file
    /// to become `size`.
    ///
    /// See [`File::set_len`] for details.
    #[inline]
    pub fn set_len(&self, size: u64) -> io::Result<()> {
        self.with_file(|f| f.set_len(size))
    }

    /// Queries metadata about the underlying file.
    ///
    /// See [`File::metadata`] for details.
    #[inline]
    pub fn metadata(&self) -> io::Result<fs::Metadata> {
        self.with_file(|f| f.metadata())
    }

    /// Changes the permissions on the underlying file.
    ///
    /// See [`File::set_permissions`] for details.
    #[inline]
    pub fn set_permissions(&self, perm: fs::Permissions) -> io::Result<()> {
        self.with_file(|f| f.set_permissions(perm))
    }

    /// Unwraps the inner [`File`].
    ///
    /// The file's cursor position is unspecified.
    #[inline]
    pub fn into_inner(self) -> File {
        #[cfg(any(unix, target_os = "windows", wasi_ext))]
        {
            self.0
        }

        #[cfg(not(any(unix, target_os = "windows", wasi_ext)))]
        {
            self.0.into_inner().unwrap_or_else(PoisonError::into_inner)
        }
    }
}

impl ReadAt for RandomAccessFile {
    fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize> {
        #[cfg(any(unix, wasi_ext))]
        {
            self.0.read_at(buf, offset)
        }

        #[cfg(target_os = "windows")]
        {
            self.0.seek_read(buf, offset)
        }

        #[cfg(not(any(unix, target_os = "windows", wasi_ext)))]
        {
            use io::{Read, Seek};

            let file = &mut *self.0.lock().unwrap_or_else(PoisonError::into_inner);
            file.seek(io::SeekFrom::Start(offset))?;
            file.read(buf)
        }
    }

    #[cfg(any(unix, wasi_ext))]
    fn read_exact_at(&self, buf: &mut [u8], offset: u64) -> io::Result<()> {
        self.0.read_exact_at(buf, offset)
    }

    #[cfg(not(any(unix, target_os = "windows", wasi_ext)))]
    fn read_exact_at(&self, buf: &mut [u8], offset: u64) -> io::Result<()> {
        use io::{Read, Seek};

        let file = &mut *self.0.lock().unwrap_or_else(PoisonError::into_inner);
        file.seek(io::SeekFrom::Start(offset))?;
        file.read_exact(buf)
    }
}

impl WriteAt for RandomAccessFile {
    fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize> {
        #[cfg(any(unix, wasi_ext))]
        {
            self.0.write_at(buf, offset)
        }

        #[cfg(target_os = "windows")]
        {
            self.0.seek_write(buf, offset)
        }

        #[cfg(not(any(unix, target_os = "windows", wasi_ext)))]
        {
            use io::{Seek, Write};

            let file = &mut *self.0.lock().unwrap_or_else(PoisonError::into_inner);
            file.seek(io::SeekFrom::Start(offset))?;
            file.write(buf)
        }
    }

    #[cfg(any(unix, wasi_ext))]
    fn write_all_at(&self, buf: &[u8], offset: u64) -> io::Result<()> {
        self.0.write_all_at(buf, offset)
    }

    #[cfg(not(any(unix, target_os = "windows", wasi_ext)))]
    fn write_all_at(&self, buf: &[u8], offset: u64) -> io::Result<()> {
        use io::{Seek, Write};

        let file = &mut *self.0.lock().unwrap_or_else(PoisonError::into_inner);
        file.seek(io::SeekFrom::Start(offset))?;
        file.write_all(buf)
    }
}

impl From<File> for RandomAccessFile {
    /// Creates a new `RandomAccessFile` from an open [`File`].
    #[inline]
    fn from(file: File) -> RandomAccessFile {
        #[cfg(not(any(unix, target_os = "windows", wasi_ext)))]
        let file = Mutex::new(file);

        RandomAccessFile(file)
    }
}

impl From<RandomAccessFile> for File {
    #[inline]
    fn from(file: RandomAccessFile) -> File {
        file.into_inner()
    }
}

#[cfg(any(unix, wasi_ext))]
impl AsRawFd for RandomAccessFile {
    #[inline]
    fn as_raw_fd(&self) -> RawFd {
        self.0.as_raw_fd()
    }
}

#[cfg(target_os = "windows")]
impl AsRawHandle for RandomAccessFile {
    #[inline]
    fn as_raw_handle(&self) -> RawHandle {
        self.0.as_raw_handle()
    }
}

#[cfg(any(unix, wasi_ext))]
impl FromRawFd for RandomAccessFile {
    #[inline]
    unsafe fn from_raw_fd(fd: RawFd) -> Self {
        Self::from(File::from_raw_fd(fd))
    }
}

#[cfg(target_os = "windows")]
impl FromRawHandle for RandomAccessFile {
    #[inline]
    unsafe fn from_raw_handle(handle: RawHandle) -> Self {
        Self::from(File::from_raw_handle(handle))
    }
}

#[cfg(any(unix, wasi_ext))]
impl IntoRawFd for RandomAccessFile {
    #[inline]
    fn into_raw_fd(self) -> RawFd {
        self.0.into_raw_fd()
    }
}

#[cfg(target_os = "windows")]
impl IntoRawHandle for RandomAccessFile {
    #[inline]
    fn into_raw_handle(self) -> RawHandle {
        self.0.into_raw_handle()
    }
}

/// A file wrapper that is safe to use concurrently.
///
/// This wrapper exists because [`std::fs::File`] uses a single cursor, so
/// reading from a file concurrently will likely produce race conditions.
///
/// `SyncFile`s are cheap to clone and clones use distinct cursors, so they can
/// be used concurrently without issues.
#[derive(Debug, Clone)]
pub struct SyncFile {
    file: Arc<RandomAccessFile>,
    offset: u64,
}

impl SyncFile {
    /// Attempts to open a file in read-only mode.
    ///
    /// See [`File::open`] for details.
    #[inline]
    pub fn open<P: AsRef<Path>>(path: P) -> io::Result<SyncFile> {
        let f = File::open(path.as_ref())?;
        Ok(SyncFile::from(f))
    }

    /// Opens a file in write-only mode.
    ///
    /// See [`File::create`] for details.
    pub fn create<P: AsRef<Path>>(path: P) -> io::Result<SyncFile> {
        let f = File::create(path.as_ref())?;
        Ok(SyncFile::from(f))
    }

    /// Returns the offset used when reading the file.
    ///
    /// This is equivalent to [`io::Seek::stream_position`] but does not use a
    /// fallible API nor require a mutable reference.
    #[must_use]
    pub fn offset(&self) -> u64 {
        self.offset
    }
}

impl std::ops::Deref for SyncFile {
    type Target = RandomAccessFile;

    #[inline]
    fn deref(&self) -> &RandomAccessFile {
        &self.file
    }
}

impl ReadAt for SyncFile {
    #[inline]
    fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize> {
        self.file.read_at(buf, offset)
    }

    #[inline]
    fn read_exact_at(&self, buf: &mut [u8], offset: u64) -> io::Result<()> {
        self.file.read_exact_at(buf, offset)
    }
}

impl WriteAt for SyncFile {
    #[inline]
    fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize> {
        self.file.write_at(buf, offset)
    }

    #[inline]
    fn write_all_at(&self, buf: &[u8], offset: u64) -> io::Result<()> {
        self.file.write_all_at(buf, offset)
    }
}

impl io::Read for SyncFile {
    #[inline]
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        let read = self.read_at(buf, self.offset)?;
        self.offset += read as u64;
        Ok(read)
    }

    #[inline]
    fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
        let ret = self.read_exact_at(buf, self.offset);
        if ret.is_ok() {
            self.offset += buf.len() as u64;
        }
        ret
    }
}

impl io::Seek for SyncFile {
    #[inline]
    fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
        self.offset = match pos {
            io::SeekFrom::Start(p) => p,
            io::SeekFrom::Current(p) => {
                let (offset, overflowed) = self.offset.overflowing_add(p as u64);
                if overflowed ^ (p < 0) {
                    return Err(invalid_seek());
                }
                offset
            }
            io::SeekFrom::End(_) => self.file.with_file(|mut f| f.seek(pos))?,
        };

        Ok(self.offset)
    }

    #[inline]
    fn stream_position(&mut self) -> io::Result<u64> {
        Ok(self.offset)
    }
}

impl io::Write for SyncFile {
    #[inline]
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        let written = self.write_at(buf, self.offset)?;
        self.offset += written as u64;
        Ok(written)
    }

    #[inline]
    fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
        let ret = self.write_all_at(buf, self.offset);
        if ret.is_ok() {
            self.offset += buf.len() as u64;
        }
        ret
    }

    #[inline]
    fn flush(&mut self) -> io::Result<()> {
        self.file.with_file(|mut f| f.flush())
    }
}

impl From<File> for SyncFile {
    /// Creates a new `SyncFile` from an open [`File`].
    ///
    /// The cursor starts at the beginning of the file.
    #[inline]
    fn from(file: File) -> SyncFile {
        SyncFile {
            file: Arc::new(RandomAccessFile::from(file)),
            offset: 0,
        }
    }
}

#[cfg(any(unix, wasi_ext))]
impl AsRawFd for SyncFile {
    #[inline]
    fn as_raw_fd(&self) -> RawFd {
        self.file.as_raw_fd()
    }
}

#[cfg(target_os = "windows")]
impl AsRawHandle for SyncFile {
    #[inline]
    fn as_raw_handle(&self) -> RawHandle {
        self.file.as_raw_handle()
    }
}

#[cfg(any(unix, wasi_ext))]
impl FromRawFd for SyncFile {
    #[inline]
    unsafe fn from_raw_fd(fd: RawFd) -> Self {
        Self::from(File::from_raw_fd(fd))
    }
}

#[cfg(target_os = "windows")]
impl FromRawHandle for SyncFile {
    #[inline]
    unsafe fn from_raw_handle(handle: RawHandle) -> Self {
        Self::from(File::from_raw_handle(handle))
    }
}

#[cold]
fn invalid_seek() -> io::Error {
    io::Error::new(
        io::ErrorKind::InvalidInput,
        "invalid seek to a negative or overflowing position",
    )
}