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
454
455
use super::{Backend, Lock, LockBackend};
use crate::processor::{ArgumentParser, BatchItem, Mode, Oid, Status};
use bytes::{Bytes, BytesMut};
use chrono::{TimeZone, Utc};
use scutiger_core::errors::{Error, ErrorKind};
use sha2::{Digest, Sha256};
use std::any::Any;
use std::collections::BTreeMap;
use std::fs;
use std::io;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::time::SystemTime;
use tempfile::{Builder, NamedTempFile};

pub struct LocalBackend<'a> {
    lfs_path: &'a Path,
    umask: u32,
    timestamp: Option<i64>,
}

impl<'a> LocalBackend<'a> {
    pub fn new(lfs_path: &'a Path, umask: u32, timestamp: Option<i64>) -> Self {
        LocalBackend {
            lfs_path,
            umask,
            timestamp,
        }
    }

    #[cfg(unix)]
    fn fix_permissions(&self, path: &Path) -> Result<Status, Error> {
        use std::os::unix::fs::PermissionsExt;

        let mut perms = fs::metadata(path)?.permissions();
        perms.set_mode(0o777 & !self.umask);
        fs::set_permissions(path, perms)?;
        Ok(Status::success())
    }

    #[cfg(not(unix))]
    fn fix_permissions(&self, path: &Path) -> Result<Status, Error> {
        Ok(Status::success())
    }
}

pub struct UploadState {
    oid: Oid,
    tempfile: Option<NamedTempFile>,
}

impl<'a> Backend for LocalBackend<'a> {
    fn batch(&mut self, _mode: Mode, oids: &[(Oid, u64)]) -> Result<Vec<BatchItem>, Error> {
        oids.iter()
            .map(|(oid, batch_size)| {
                let maybe_size = oid.size_at_path(self.lfs_path);
                Ok(BatchItem {
                    oid: oid.clone(),
                    size: maybe_size.unwrap_or(*batch_size),
                    present: maybe_size.is_some(),
                })
            })
            .collect()
    }

    fn start_upload(
        &mut self,
        oid: &Oid,
        rdr: &mut dyn io::Read,
        _args: &BTreeMap<Bytes, Bytes>,
    ) -> Result<Box<dyn Any>, Error> {
        let mut tempdir: PathBuf = self.lfs_path.into();
        tempdir.push("incomplete");
        let mut tempfile = Builder::new()
            .prefix(oid.as_str())
            .rand_bytes(12)
            .tempfile_in(&tempdir)?;
        io::copy(rdr, tempfile.as_file_mut())?;
        Ok(Box::new(UploadState {
            oid: oid.clone(),
            tempfile: Some(tempfile),
        }))
    }

    fn finish_upload(&mut self, mut state: Box<dyn Any>) -> Result<(), Error> {
        let state = match state.downcast_mut::<UploadState>() {
            Some(state) => state,
            None => return Err(Error::new_simple(ErrorKind::DowncastError)),
        };
        // This is a valid file.  Let's create any missing directories and then rename it.  This
        // uses an atomic rename, which should work
        // on all platforms.
        let dest_path = state.oid.expected_path(self.lfs_path);
        if let Some(parent) = dest_path.parent() {
            fs::create_dir_all(parent)?;
        }
        state
            .tempfile
            .take()
            .unwrap()
            .into_temp_path()
            .persist(&dest_path)
            .map_err(|e| Error::new(ErrorKind::IOError, Some(e)))?;
        self.fix_permissions(&dest_path)?;
        Ok(())
    }

    fn verify(&mut self, oid: &Oid, args: &BTreeMap<Bytes, Bytes>) -> Result<Status, Error> {
        let expected_size: u64 = ArgumentParser::parse_value_as_integer(args, b"size")?;
        let path = oid.expected_path(self.lfs_path);
        let metadata = match fs::metadata(path) {
            Ok(m) => m,
            Err(e) if e.kind() == io::ErrorKind::NotFound => {
                return Ok(Status::new_failure(404, "not found".as_bytes()))
            }
            Err(e) => return Err(e.into()),
        };
        let actual_size = metadata.len();
        if actual_size == expected_size {
            Ok(Status::success())
        } else {
            Ok(Status::new_failure(
                409,
                "mismatched size or cryptographic collision".as_bytes(),
            ))
        }
    }

    fn download(
        &mut self,
        oid: &Oid,
        _args: &BTreeMap<Bytes, Bytes>,
    ) -> Result<(Box<dyn io::Read>, Option<u64>), Error> {
        let path = oid.expected_path(self.lfs_path);
        let file = fs::File::open(path)?;
        let metadata = file.metadata()?;
        Ok((Box::new(file), Some(metadata.len())))
    }

    fn lock_backend<'b>(&'b self) -> Box<dyn LockBackend + 'b> {
        let mut buf = PathBuf::from(self.lfs_path);
        buf.push("locks");
        Box::new(LocalLockBackend {
            backend: self,
            lock_path: buf,
        })
    }
}

struct LockFile {
    path: PathBuf,
    temp: PathBuf,
}

impl LockFile {
    fn new(path: &Path) -> Result<LockFile, Error> {
        let mut temp = path.to_owned();
        temp.set_extension("lock");
        Ok(LockFile {
            path: path.to_owned(),
            temp,
        })
    }

    fn write(&self, data: &[u8]) -> Result<(), Error> {
        let mut f = match fs::OpenOptions::new()
            .write(true)
            .create_new(true)
            .open(&self.temp)
        {
            Ok(f) => f,
            Err(e) if e.kind() == io::ErrorKind::AlreadyExists => {
                return Err(Error::new_simple(ErrorKind::Conflict))
            }
            Err(e) => return Err(e.into()),
        };
        f.write_all(data)?;
        f.flush()?;
        drop(f);
        Ok(())
    }

    #[allow(unused_must_use)]
    fn persist(self) -> Result<(), Error> {
        match fs::hard_link(&self.temp, &self.path) {
            Ok(()) => Ok(()),
            Err(e) if e.kind() == io::ErrorKind::AlreadyExists => {
                Err(Error::new_simple(ErrorKind::Conflict))
            }
            Err(e) => Err(e.into()),
        }
    }
}

impl Drop for LockFile {
    #[allow(unused_must_use)]
    fn drop(&mut self) {
        // We don't care if the file removal failed.  We did the best we could.
        fs::remove_file(&self.temp);
    }
}

struct LocalBackendLock {
    root: PathBuf,
    path_name: Bytes,
    time: i64,
    ownername: String,
}

impl LocalBackendLock {
    const VERSION: &'static str = "v1";

    fn hash_for(path: &Bytes) -> String {
        let mut hash = Sha256::new();
        hash.update(Self::VERSION.as_bytes());
        hash.update(b":");
        hash.update(path);
        hex::encode(hash.finalize())
    }

    fn parse(data: Vec<u8>) -> Option<(i64, Bytes)> {
        let v: Vec<Vec<u8>> = data.splitn(3, |&x| x == b':').map(|x| x.into()).collect();
        if v.len() != 3 || v[0] != Self::VERSION.as_bytes() {
            return None;
        }
        let s: String = String::from_utf8(v[1].clone()).ok()?;
        let time = s.parse().ok()?;
        Some((time, v[2].clone().into()))
    }

    #[cfg(windows)]
    fn current_user(&self) -> Result<String, Error> {
        Ok("unknown".into())
    }

    #[cfg(unix)]
    fn current_user(&self) -> Result<String, Error> {
        let uid = unsafe { libc::getuid() } as u32;
        match passwd::Passwd::from_uid(uid) {
            Some(pwd) => Ok(pwd.name),
            None => Ok(format!("uid {}", uid)),
        }
    }
}

impl Lock for LocalBackendLock {
    fn unlock(&self) -> Result<(), Error> {
        let id = Self::hash_for(&self.path_name);
        let mut filename = self.root.clone();
        filename.push(id);
        fs::remove_file(&filename)?;
        Ok(())
    }

    fn id(&self) -> String {
        Self::hash_for(&self.path_name)
    }

    fn path(&self) -> &Bytes {
        &self.path_name
    }

    fn formatted_timestamp(&self) -> String {
        Utc.timestamp(self.time, 0).to_rfc3339()
    }

    fn ownername(&self) -> &str {
        &self.ownername
    }

    fn as_lock_spec(&self, owner_id: bool) -> Result<Vec<Bytes>, Error> {
        let id = self.id();
        let mut v = vec![
            format!("lock {}\n", id).as_bytes().into(),
            ([b"path ", id.as_bytes(), b" ", self.path(), b"\n"])
                .join(b"" as &[u8])
                .into(),
            format!("locked-at {} {}\n", id, self.formatted_timestamp())
                .as_bytes()
                .into(),
            format!("ownername {} {}\n", id, self.ownername())
                .as_bytes()
                .into(),
        ];
        if owner_id {
            let user = self.current_user()?;
            let who = if user == self.ownername() {
                "ours"
            } else {
                "theirs"
            };
            v.push(format!("owner {} {}\n", id, who).as_bytes().into());
        }
        Ok(v)
    }
}

struct LocalLockBackend<'a> {
    backend: &'a LocalBackend<'a>,
    lock_path: PathBuf,
}

impl<'a> LocalLockBackend<'a> {
    fn timestamp(&self) -> i64 {
        self.backend.timestamp.unwrap_or_else(|| {
            match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
                Ok(d) => d.as_secs() as i64,
                Err(e) => -(e.duration().as_secs() as i64),
            }
        })
    }

    #[cfg(windows)]
    fn user_for_file(path: &Path) -> Result<String, Error> {
        Ok("unknown".into())
    }

    #[cfg(unix)]
    fn user_for_file(path: &Path) -> Result<String, Error> {
        use std::os::unix::fs::MetadataExt;
        let st = fs::metadata(path)?;
        match passwd::Passwd::from_uid(st.uid()) {
            Some(pwd) => Ok(pwd.name),
            None => Ok(format!("uid {}", st.uid())),
        }
    }
}

impl<'a> LockBackend for LocalLockBackend<'a> {
    fn create(&self, path: &Bytes) -> Result<Box<dyn Lock>, Error> {
        let id = LocalBackendLock::hash_for(path);
        let mut b: BytesMut = format!("{}:{}:", LocalBackendLock::VERSION, self.timestamp()).into();
        b.extend_from_slice(path);
        let mut filename = self.lock_path.clone();
        filename.push(id);
        let lock = LockFile::new(&filename)?;
        lock.write(&b)?;
        lock.persist()?;
        let user = Self::user_for_file(&filename).unwrap_or_else(|_| "unknown".into());
        Ok(Box::new(LocalBackendLock {
            root: self.lock_path.clone(),
            path_name: path.clone(),
            time: self.timestamp(),
            ownername: user,
        }))
    }

    fn from_path(&self, path: &Bytes) -> Result<Option<Box<dyn Lock>>, Error> {
        let id = LocalBackendLock::hash_for(path);
        match self.from_id(&id) {
            Ok(None) => Ok(None),
            Ok(Some(l)) if l.path() != path => {
                // This should never happen except with corruption, since otherwise we'd need a
                // collision of SHA-256.
                Err(Error::from_message(
                    ErrorKind::CorruptData,
                    "unexpected filename in parsed lock",
                ))
            }
            Ok(Some(l)) => Ok(Some(l)),
            Err(e) => Err(e),
        }
    }

    fn from_id(&self, id: &str) -> Result<Option<Box<dyn Lock>>, Error> {
        let mut filename = self.lock_path.clone();
        filename.push(id);
        let mut f = match fs::OpenOptions::new().read(true).open(&filename) {
            Ok(f) => f,
            Err(e) if e.kind() == io::ErrorKind::NotFound => return Ok(None),
            Err(e) => return Err(e.into()),
        };
        let mut v = Vec::new();
        f.read_to_end(&mut v)?;
        let user = Self::user_for_file(&filename).unwrap_or_else(|_| "unknown".into());
        let (time, parsed_path) = match LocalBackendLock::parse(v) {
            Some(x) => x,
            None => {
                return Err(Error::from_message(
                    ErrorKind::CorruptData,
                    "invalid parsed lock",
                ))
            }
        };
        Ok(Some(Box::new(LocalBackendLock {
            root: self.lock_path.clone(),
            path_name: parsed_path,
            time,
            ownername: user,
        })))
    }

    fn iter<'b>(&'b self) -> Box<dyn Iterator<Item = Result<Box<dyn Lock>, Error>> + 'b> {
        Box::new(LockSetIterator::new(self, &self.lock_path))
    }
}

struct LockSetIterator<'a> {
    data: Vec<fs::DirEntry>,
    err: Option<Error>,
    item: usize,
    done: bool,
    backend: &'a LocalLockBackend<'a>,
}

impl<'a> LockSetIterator<'a> {
    fn new(backend: &'a LocalLockBackend, path: &Path) -> LockSetIterator<'a> {
        let data: Result<Vec<fs::DirEntry>, io::Error> = match fs::read_dir(path) {
            Ok(iter) => iter.collect(),
            Err(e) => Err(e),
        };
        let (data, err) = match data {
            Ok(mut v) => {
                v.sort_by_key(fs::DirEntry::file_name);
                (v, None)
            }
            Err(e) => (vec![], Some(e.into())),
        };
        LockSetIterator {
            data,
            err,
            item: 0,
            done: false,
            backend,
        }
    }
}

impl<'a> Iterator for LockSetIterator<'a> {
    type Item = Result<Box<dyn Lock>, Error>;

    fn next(&mut self) -> Option<Self::Item> {
        match (self.err.take(), self.done) {
            (_, true) => None,
            (Some(e), false) => {
                self.done = true;
                Some(Err(e))
            }
            (None, false) => {
                while self.item < self.data.len() {
                    let pos = self.item;
                    self.item += 1;
                    let item = &self.data[pos];
                    let filename = item.file_name();
                    let filename = filename.to_string_lossy();
                    match self.backend.from_id(&filename) {
                        Ok(Some(l)) => return Some(Ok(l)),
                        _ => continue,
                    };
                }
                None
            }
        }
    }
}