bazaar 3.5.2

Rust implementation of the Bazaar formats and protocols
Documentation
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
use crate::filters::{ContentFilter, ContentFilterProvider, ContentFilterStack};
use crate::osutils::sha::sha_string;
use log::{debug, info};
use std::collections::HashMap;
use std::fs;
use std::fs::{File, Metadata, Permissions};
use std::io;
use std::io::prelude::*;
use std::io::BufReader;
#[cfg(unix)]
use std::os::unix::fs::MetadataExt;
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};
use tempfile::NamedTempFile;

/// TODO: Up-front, stat all files in order and remove those which are deleted or
/// out-of-date.  Don't actually re-read them until they're needed.  That ought
/// to bring all the inodes into core so that future stats to them are fast, and
/// it preserves the nice property that any caller will always get up-to-date
/// data except in unavoidable cases.

/// TODO: Perhaps return more details on the file to avoid statting it
/// again: nonexistent, file type, size, etc

const CACHE_HEADER: &[u8] = b"### bzr hashcache v5\n";

enum FileKind {
    Regular,
    Symlink,
    Other,
}

fn file_kind(path: &Path) -> FileKind {
    match fs::symlink_metadata(path) {
        Ok(meta) => {
            if meta.is_symlink() {
                FileKind::Symlink
            } else if meta.is_file() {
                FileKind::Regular
            } else {
                FileKind::Other
            }
        }
        Err(_) => FileKind::Other,
    }
}

/// Cache for looking up file SHA-1.
///
/// Files are considered to match the cached value if the fingerprint
/// of the file has not changed.  This includes its mtime, ctime,
/// device number, inode number, and size.  This should catch
/// modifications or replacement of the file by a new one.
///
/// This may not catch modifications that do not change the file's
/// size and that occur within the resolution window of the
/// timestamps.  To handle this we specifically do not cache files
/// which have changed since the start of the present second, since
/// they could undetectably change again.
///
/// This scheme may fail if the machine's clock steps backwards.
/// Don't do that.
///
/// This does not canonicalize the paths passed in; that should be
/// done by the caller.
///
/// _cache
///     Indexed by path, points to a two-tuple of the SHA-1 of the file.
///     and its fingerprint.
///
/// stat_count
///     number of times files have been statted
///
/// hit_count
///     number of times files have been retrieved from the cache, avoiding a
///     re-read
///
/// miss_count
///     number of misses (times files have been completely re-read)

#[derive(Debug, PartialEq, Default, Clone)]
pub struct Fingerprint {
    pub size: u64,
    pub mtime: i64,
    pub ctime: i64,
    pub ino: u64,
    pub dev: u64,
    pub mode: u32,
}

#[cfg(unix)]
impl From<Metadata> for Fingerprint {
    fn from(meta: Metadata) -> Fingerprint {
        Fingerprint {
            size: meta.size(),
            mtime: meta.mtime(),
            ctime: meta.ctime(),
            ino: meta.ino(),
            dev: meta.dev(),
            mode: meta.mode(),
        }
    }
}

#[cfg(windows)]
impl From<Metadata> for Fingerprint {
    fn from(meta: Metadata) -> Fingerprint {
        use std::os::windows::fs::MetadataExt;
        let mtime = meta
            .modified()
            .ok()
            .and_then(|t| t.duration_since(UNIX_EPOCH).ok())
            .map(|d| d.as_secs() as i64)
            .unwrap_or(0);
        let ctime = meta
            .created()
            .ok()
            .and_then(|t| t.duration_since(UNIX_EPOCH).ok())
            .map(|d| d.as_secs() as i64)
            .unwrap_or(0);
        Fingerprint {
            size: meta.file_size(),
            mtime,
            ctime,
            ino: 0,
            dev: 0,
            mode: 0,
        }
    }
}

const DEFAULT_CUTOFF_OFFSET: i64 = -3;

pub struct HashCache {
    root: PathBuf,
    hit_count: u32,
    miss_count: u32,
    stat_count: u32,
    danger_count: u32,
    removed_count: u32,
    update_count: u32,
    cache: HashMap<PathBuf, (String, Fingerprint)>,
    needs_write: bool,
    permissions: Option<Permissions>,
    cache_file_name: PathBuf,
    filter_provider: Option<Box<ContentFilterProvider>>,
    cutoff_offset: i64,
}

impl HashCache {
    /// Create a hash cache in base dir, and set the file mode to mode.
    ///
    /// Args:
    ///    content_filter_provider: a function that takes a
    ///       path (relative to the top of the tree) and a file-id as
    ///       parameters and returns a stack of ContentFilters.
    ///       If None, no content filtering is performed.
    pub fn new(
        root: &Path,
        cache_file_name: &Path,
        permissions: Option<Permissions>,
        content_filter_provider: Option<Box<ContentFilterProvider>>,
    ) -> Self {
        HashCache {
            root: root.to_path_buf(),
            hit_count: 0,
            miss_count: 0,
            stat_count: 0,
            danger_count: 0,
            removed_count: 0,
            update_count: 0,
            cache: HashMap::new(),
            needs_write: false,
            permissions,
            cache_file_name: cache_file_name.to_path_buf(),
            filter_provider: content_filter_provider,
            cutoff_offset: DEFAULT_CUTOFF_OFFSET,
        }
    }

    pub fn cache_file_name(&self) -> &Path {
        self.cache_file_name.as_path()
    }

    pub fn hit_count(&self) -> u32 {
        self.hit_count
    }

    pub fn miss_count(&self) -> u32 {
        self.miss_count
    }

    pub fn set_cutoff_offset(&mut self, offset: i64) {
        self.cutoff_offset = offset;
    }

    /// Discard all cached information.
    ///
    /// This does not reset the counters.
    pub fn clear(&mut self) {
        if !self.cache.is_empty() {
            self.needs_write = true;
            self.cache.clear();
        }
    }

    /// Scan all files and remove entries where the cache entry is obsolete.
    ///
    /// Obsolete entries are those where the file has been modified or deleted
    /// since the entry was inserted.
    pub fn scan(&mut self) {
        let mut keys_to_remove = Vec::new();
        let mut by_inode = self
            .cache
            .iter()
            .map(|(k, v)| (v.1.ino, k, v))
            .collect::<Vec<_>>();
        by_inode.sort_by_key(|x| x.0);
        for (_inode, path, cache_val) in by_inode {
            let abspath = self.root.join(path);
            let fp = self.fingerprint(abspath.as_ref(), None);
            self.stat_count += 1;

            if fp.is_none() || cache_val.1 != fp.unwrap() {
                // not here or not a regular file anymore
                self.removed_count += 1;
                self.needs_write = true;
                keys_to_remove.push(path.clone());
            }
        }
        for path in keys_to_remove {
            self.cache.remove(&path);
        }
    }

    pub fn get_sha1_by_fingerprint(
        &mut self,
        path: &Path,
        file_fp: &Fingerprint,
    ) -> io::Result<String> {
        let abspath = self.root.join(path);

        let (cache_sha1, cache_fp) = self
            .cache
            .get(path)
            .cloned()
            .unwrap_or((Default::default(), Default::default()));

        if cache_fp == *file_fp {
            self.hit_count += 1;
            Ok(cache_sha1)
        } else {
            self.miss_count += 1;

            match file_kind(&abspath) {
                FileKind::Regular => {
                    let filters: Box<dyn ContentFilter> =
                        if let Some(filter_provider) = self.filter_provider.as_ref() {
                            filter_provider(path, file_fp.ctime as u64)
                        } else {
                            Box::new(ContentFilterStack::new())
                        };
                    let digest = filters.sha1_file(&abspath)?;

                    // window of 3 seconds to allow for 2s resolution on windows,
                    // unsynchronized file servers, etc.
                    let cutoff = self.cutoff_time();
                    if file_fp.mtime >= cutoff || file_fp.ctime >= cutoff {
                        // changed too recently; can't be cached. we can
                        // return the result and it could possibly be cached
                        // next time.
                        //
                        // the point is that we only want to cache when we are sure that any
                        // subsequent modifications of the file can be detected. If a
                        // modification neither changes the inode, the device, the size, nor
                        // the mode, then we can only distinguish it by time; therefore we
                        // need to let sufficient time elapse before we may cache this entry
                        // again. If we didn't do this, then, for example, a very quick 1
                        // byte replacement in the file might go undetected.
                        self.danger_count += 1;
                        if self.cache.remove(path).is_some() {
                            self.removed_count += 1;
                            self.needs_write = true;
                        }
                    } else {
                        self.update_count += 1;
                        self.needs_write = true;
                        self.cache
                            .insert(path.to_owned(), (digest.clone(), file_fp.clone()));
                    }

                    Ok(digest)
                }
                FileKind::Symlink => {
                    let target = fs::read_link(&abspath)?;
                    let digest = sha_string(target.to_string_lossy().as_bytes());
                    self.cache
                        .insert(path.to_owned(), (digest.clone(), file_fp.clone()));
                    self.update_count += 1;
                    self.needs_write = true;
                    Ok(digest)
                }
                _ => Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    format!("unknown file stat mode: {:o}", file_fp.mode),
                )),
            }
        }
    }

    /// Return the SHA-1 of the file at path.
    pub fn get_sha1(
        &mut self,
        path: &Path,
        stat_value: Option<Metadata>,
    ) -> io::Result<Option<String>> {
        let abspath = self.root.join(path);
        self.stat_count += 1;
        let file_fp = self.fingerprint(abspath.as_ref(), stat_value);

        if file_fp.is_none() {
            // not a regular file or not existing
            if self.cache.remove(path).is_some() {
                self.removed_count += 1;
                self.needs_write = true;
            }

            Ok(None)
        } else {
            Ok(Some(self.get_sha1_by_fingerprint(path, &file_fp.unwrap())?))
        }
    }

    /// Write contents of cache to file.
    pub fn write(&mut self) -> Result<(), std::io::Error> {
        let mut outf = NamedTempFile::new_in(self.cache_file_name.parent().unwrap())?;
        if let Some(permissions) = self.permissions.clone() {
            outf.as_file().set_permissions(permissions)?;
        }
        outf.write_all(CACHE_HEADER)?;
        for (path, c) in &self.cache {
            let mut line_info: Vec<u8> = Vec::new();
            line_info.extend_from_slice(path.to_str().unwrap().as_bytes());
            line_info.extend_from_slice(b"// ");
            line_info.extend_from_slice(c.0.as_bytes());
            line_info.push(b' ');
            let fp = &c.1;
            write!(
                &mut line_info,
                "{} {} {} {} {} {}",
                fp.size, fp.mtime, fp.ctime, fp.ino, fp.dev, fp.mode
            )?;
            line_info.push(b'\n');
            outf.write_all(&line_info)?;
        }
        outf.persist(self.cache_file_name())?;
        self.needs_write = false;
        debug!(
            "write hash cache: {} hits={} misses={} stat={} recent={} updates={}",
            self.cache_file_name().display(),
            self.hit_count,
            self.miss_count,
            self.stat_count,
            self.danger_count,
            self.update_count
        );
        Ok(())
    }

    /// Reinstate cache from file.
    ///
    /// Overwrites existing cache.
    ///
    /// If the cache file has the wrong version marker, this just clears
    /// the cache.
    pub fn read(&mut self) -> Result<(), std::io::Error> {
        self.cache = HashMap::new();
        let file = File::open(self.cache_file_name());
        if file.is_err() {
            debug!(
                "failed to open {}: {}",
                self.cache_file_name().display(),
                file.err().unwrap()
            );
            self.needs_write = true;
            return Ok(());
        }
        let file = file.unwrap();
        let reader = BufReader::with_capacity(65000, file);
        let mut lines = reader.lines();
        if let Some(header) = lines.next() {
            if header?.as_bytes().eq(CACHE_HEADER) {
                self.needs_write = true;
                return Err(std::io::Error::new(
                    std::io::ErrorKind::InvalidData,
                    format!(
                        "cache header marker not found at top of {}; discarding cache",
                        self.cache_file_name().display()
                    ),
                ));
            }
        } else {
            self.needs_write = true;
            return Err(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "error reading cache file header".to_string(),
            ));
        }
        for line in lines {
            let line = line?;
            let pos = line.find("// ").unwrap();
            let path = PathBuf::from(&line[..pos]);
            if self.cache.contains_key(&path) {
                info!("duplicated path {} in cache", path.display());
                continue;
            }
            let pos = pos + 3;
            let fields = line[pos..].split(' ').collect::<Vec<_>>();
            if fields.len() != 7 {
                info!("bad line in hashcache: {}", line);
                continue;
            }
            let sha1 = fields[0].to_owned();
            if sha1.len() != 40 {
                info!("bad sha1 in hashcache: {}", sha1);
                continue;
            }
            let fp = Fingerprint {
                size: fields[1].parse::<u64>().unwrap(),
                mtime: fields[2].parse::<i64>().unwrap(),
                ctime: fields[3].parse::<i64>().unwrap(),
                ino: fields[4].parse::<u64>().unwrap(),
                dev: fields[5].parse::<u64>().unwrap(),
                mode: fields[6].parse::<u32>().unwrap(),
            };
            self.cache.insert(path, (sha1, fp));
        }
        self.needs_write = false;
        Ok(())
    }

    pub fn needs_write(&self) -> bool {
        self.needs_write
    }

    /// Return cutoff time.
    ///
    /// Files modified more recently than this time are at risk of being
    /// undetectably modified and so can't be cached.
    pub fn cutoff_time(&self) -> i64 {
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs() as i64
            + self.cutoff_offset
    }

    pub fn fingerprint(&self, abspath: &Path, stat_value: Option<Metadata>) -> Option<Fingerprint> {
        let stat_value = match stat_value {
            Some(s) => s,
            None => match fs::symlink_metadata(abspath) {
                Ok(s) => s,
                Err(_) => return None,
            },
        };
        if stat_value.is_dir() {
            return None;
        }
        Some(stat_value.into())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::osutils::sha::sha_string;
    use tempfile::TempDir;

    fn empty_sha1(input: &[u8]) -> String {
        sha_string(input)
    }

    fn make_cache(tmp: &TempDir) -> HashCache {
        let root = tmp.path().to_path_buf();
        std::fs::create_dir(root.join(".bzr")).unwrap();
        HashCache::new(&root, &root.join(".bzr/stat-cache"), None, None)
    }

    fn write_file(root: &Path, name: &str, contents: &[u8]) {
        std::fs::write(root.join(name), contents).unwrap();
    }

    #[test]
    fn initial_miss_returns_correct_hash() {
        let tmp = TempDir::new().unwrap();
        let mut hc = make_cache(&tmp);
        write_file(tmp.path(), "foo", b"hello");
        assert_eq!(
            hc.get_sha1(Path::new("foo"), None).unwrap(),
            Some("aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d".to_string())
        );
        assert_eq!(hc.miss_count(), 1);
        assert_eq!(hc.hit_count(), 0);
    }

    #[test]
    fn new_file_still_hashed_even_if_too_recent_to_cache() {
        let tmp = TempDir::new().unwrap();
        let mut hc = make_cache(&tmp);
        write_file(tmp.path(), "foo", b"goodbye");
        assert_eq!(
            hc.get_sha1(Path::new("foo"), None).unwrap(),
            Some(empty_sha1(b"goodbye"))
        );
    }

    #[test]
    fn nonexistent_file_returns_none() {
        let tmp = TempDir::new().unwrap();
        let mut hc = make_cache(&tmp);
        assert_eq!(hc.get_sha1(Path::new("no-name-yet"), None).unwrap(), None);
    }

    #[test]
    fn replaced_file_is_rehashed() {
        let tmp = TempDir::new().unwrap();
        let mut hc = make_cache(&tmp);
        write_file(tmp.path(), "foo", b"goodbye");
        assert_eq!(
            hc.get_sha1(Path::new("foo"), None).unwrap(),
            Some(empty_sha1(b"goodbye"))
        );
        std::fs::remove_file(tmp.path().join("foo")).unwrap();
        assert_eq!(hc.get_sha1(Path::new("foo"), None).unwrap(), None);
        write_file(tmp.path(), "foo", b"new content");
        assert_eq!(
            hc.get_sha1(Path::new("foo"), None).unwrap(),
            Some(empty_sha1(b"new content"))
        );
    }

    #[test]
    fn directory_returns_none() {
        let tmp = TempDir::new().unwrap();
        let mut hc = make_cache(&tmp);
        std::fs::create_dir(tmp.path().join("subdir")).unwrap();
        assert_eq!(hc.get_sha1(Path::new("subdir"), None).unwrap(), None);
    }

    #[test]
    fn cache_round_trips_through_disk_with_hit_after_reload() {
        let tmp = TempDir::new().unwrap();
        let mut hc = make_cache(&tmp);
        write_file(tmp.path(), "foo", b"contents");
        // Push the cutoff into the future so the file is considered old
        // enough to cache without having to sleep for several seconds.
        hc.set_cutoff_offset(10);
        assert_eq!(
            hc.get_sha1(Path::new("foo"), None).unwrap(),
            Some(empty_sha1(b"contents"))
        );
        hc.write().unwrap();

        let mut hc2 = HashCache::new(tmp.path(), &tmp.path().join(".bzr/stat-cache"), None, None);
        hc2.read().unwrap();
        assert_eq!(
            hc2.get_sha1(Path::new("foo"), None).unwrap(),
            Some(empty_sha1(b"contents"))
        );
        assert_eq!(hc2.hit_count(), 1);
    }
}