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
//! Little library implementing a wrapper over a file that's locked on creation and unlocked when
//! it goes out of scope.

use ::{
        fs2::FileExt,
        std::{
            fs::File,
            io::{self, SeekFrom, prelude::*},
            ops::{Deref, DerefMut},
            thread::panicking,
        },
};

/// Wrapper over a file that calls [`FileExt::unlock`] at [dropping][`Drop`].
#[derive(Debug)]
pub struct FileLock<'a>(pub &'a File);

impl<'a> FileLock<'a> {
    /// Creates a `Self` instance calling [`FileExt::try_lock_shared`] on `f` and returning any
    /// error that could have caused.
    pub fn try_wrap_shared(f: &'a File) -> io::Result<Self> {
        f.try_lock_shared()?;
        Ok(Self(f))
    }

    /// Creates a `Self` instance calling [`FileExt::lock_shared`] on `f` and returning any
    /// error that could have caused.
    pub fn wrap_shared(f: &'a File) -> io::Result<Self> {
        f.lock_shared()?;
        Ok(Self(f))
    }

    /// Creates a `Self` instance calling [`FileExt::try_lock_exclusive`] on `f` and returning any
    /// error that could have caused.
    pub fn try_wrap_exclusive(f: &'a File) -> io::Result<Self> {
        f.try_lock_exclusive()?;
        Ok(Self(f))
    }

    /// Creates a `Self` instance calling [`FileExt::lock_exclusive`] on `f` and returning any
    /// error that could have caused.
    pub fn wrap_exclusive(f: &'a File) -> io::Result<Self> {
        f.lock_exclusive()?;
        Ok(Self(f))
    }
}

impl<'a> Write for FileLock<'a> {
    #[inline(always)]
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.0.write(buf)
    }

    #[inline(always)]
    fn flush(&mut self) -> io::Result<()> {
        self.0.flush()
    }
}

impl<'a> Read for FileLock<'a> {
    #[inline(always)]
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        self.0.read(buf)
    }
}

impl<'a> Seek for FileLock<'a> {
    #[inline(always)]
    fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
        self.0.seek(pos)
    }
}

impl<'a> Deref for FileLock<'a> {
    type Target = &'a File;

    #[inline(always)]
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<'a> DerefMut for FileLock<'a> {
    #[inline(always)]
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl<'a> Drop for FileLock<'a> {
    fn drop(&mut self) {
        if let Err(e) = self.0.unlock() {
            if panicking() {
                eprintln!("error unlocking file lock: {}", e)
            } else {
                panic!("error unlocking file lock: {}", e)
            }
        }
    }
}