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
use ssh2::{Error, File, OpenFlags, OpenType, Sftp};

use std::io::{Read, Seek, Write};
use std::ops::{Deref, DerefMut, Drop};
use std::path::{Path, PathBuf};
use std::rc::Rc;

type Result<T> = std::result::Result<T, Error>;

/// Wraps a remote file with its paired remote lock file
///
/// The lock file is deleted upon dropping
pub struct LockedFile {
    file: File,
    path: PathBuf,
    lock_file_path: PathBuf,
    sftp: Rc<Sftp>,
}

impl LockedFile {
    pub fn open_read_write<T: AsRef<Path>>(path: T, sftp: Rc<Sftp>) -> Result<Option<LockedFile>> {
        let path = path.as_ref().to_path_buf();
        let extension = if let Some(ext) = path.extension() {
            // FIXME: Really need to handle this in a way that doesn't panic on non unicode
            let mut ext = String::from(ext.to_string_lossy());
            ext.push_str(".lock");
            ext
        } else {
            "lock".to_string()
        };
        let lock_file_path = path.with_extension(extension);
        // Check if the lock file exsits, by attempting to open it in read only mode
        let lockfile_attempt = sftp.stat(&lock_file_path);
        if lockfile_attempt.is_ok() {
            return Ok(None);
        }
        // Lock file doesn't exist, try opening our main file
        let file = sftp.open_mode(
            &path,
            OpenFlags::READ | OpenFlags::WRITE | OpenFlags::CREATE,
            0o644,
            OpenType::File,
        )?;
        // Create the lock file
        let _lock_file = sftp.open_mode(
            &lock_file_path,
            OpenFlags::READ | OpenFlags::WRITE | OpenFlags::CREATE | OpenFlags::EXCLUSIVE,
            0o644,
            OpenType::File,
        )?;

        Ok(Some(LockedFile {
            file,
            path,
            lock_file_path,
            sftp,
        }))
    }
}

impl Drop for LockedFile {
    fn drop(&mut self) {
        // Check to see if the lock file exists before doing anything
        if self.sftp.stat(&self.lock_file_path).is_ok() {
            self.sftp.unlink(&self.lock_file_path).unwrap_or_else(|_| {
                panic!(
                    "Unable to delete lock file for {:?}, something went wrong",
                    self.path
                )
            });
        }
    }
}

impl Deref for LockedFile {
    type Target = File;
    fn deref(&self) -> &File {
        &self.file
    }
}

impl DerefMut for LockedFile {
    fn deref_mut(&mut self) -> &mut File {
        &mut self.file
    }
}

impl Read for LockedFile {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        self.file.read(buf)
    }
}

impl Write for LockedFile {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        self.file.write(buf)
    }
    fn flush(&mut self) -> std::io::Result<()> {
        self.file.flush()
    }
}

impl Seek for LockedFile {
    fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
        self.file.seek(pos)
    }
}

impl std::fmt::Debug for LockedFile {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("LockedFile")
            .field("path", &self.path)
            .field("lock_file_path", &self.lock_file_path)
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::repository::backend::sftp::*;
    use std::env;
    fn get_settings(path: String) -> SFTPSettings {
        let hostname = env::var_os("ASURAN_SFTP_HOSTNAME")
            .map(|x| x.into_string().unwrap())
            .expect("Server must be set");
        let username = env::var_os("ASURAN_SFTP_USER")
            .map(|x| x.into_string().unwrap())
            .unwrap_or("asuran".to_string());
        let password = env::var_os("ASURAN_SFTP_PASS")
            .map(|x| x.into_string().unwrap())
            .unwrap_or("asuran".to_string());
        let port = env::var_os("ASURAN_SFTP_PORT")
            .map(|x| x.into_string().unwrap())
            .unwrap_or("22".to_string())
            .parse::<u16>()
            .expect("Unable to parse port");

        SFTPSettings {
            hostname,
            username,
            port: Some(port),
            password: Some(password),
            path,
        }
    }

    #[test]
    fn lock_non_existant_file() {
        let mut connection: SFTPConnection =
            get_settings("asuran/lock_non_existant_file".to_string()).into();
        connection
            .connect()
            .expect("Unable to connect to sftp server");

        let locked_file =
            LockedFile::open_read_write("/asuran/non_existant_file", connection.sftp().unwrap())
                .expect("Unable to lock file");

        assert!(locked_file.is_some());
    }

    #[test]
    fn lock_existant_file() {
        let mut connection: SFTPConnection =
            get_settings("asuran/lock_existant_file".to_string()).into();
        connection
            .connect()
            .expect("Unable to connect to sftp server");

        let sftp = connection.sftp().unwrap();

        let path = PathBuf::from("asuran/lock_test_file");
        let _test_file = sftp.create(&path).expect("Unable to create testfile");

        let locked_file = LockedFile::open_read_write("asuran/lock_test_file", sftp)
            .expect("Unable to lock file");

        assert!(locked_file.is_some());
    }

    #[test]
    fn locking_locked_file_fails() {
        let mut connection: SFTPConnection =
            get_settings("asuran/lock_existant_file".to_string()).into();
        connection
            .connect()
            .expect("Unable to connect to sftp server");

        let sftp = connection.sftp().unwrap();

        let path = PathBuf::from("asuran/test_locked_file");
        let _test_file = sftp.create(&path).expect("Unable to create test file");
        let lock_path = PathBuf::from("asuran/test_locked_file.lock");
        let _test_file_lock = sftp.create(&lock_path).expect("Unable to create test lock");

        let locked_file = LockedFile::open_read_write("asuran/test_locked_file", sftp)
            .expect("I/O error attempting to lock file");

        assert!(locked_file.is_none());
    }
}