|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +use std::collections::HashMap; |
| 10 | +use std::path::Path; |
| 11 | +use std::path::PathBuf; |
| 12 | +use std::time::Duration; |
| 13 | +use std::time::SystemTime; |
| 14 | +use std::time::UNIX_EPOCH; |
| 15 | + |
| 16 | +use anyhow::Context; |
| 17 | +use anyhow::Result; |
| 18 | +use anyhow::ensure; |
| 19 | +use chrono::DateTime; |
| 20 | +use chrono::Utc; |
| 21 | +use digest::Digest; |
| 22 | +use digest::Output; |
| 23 | +use rattler_conda_types::PrefixRecord; |
| 24 | +use rattler_conda_types::prefix_record::PathsEntry; |
| 25 | +use serde::Deserialize; |
| 26 | +use serde::Serialize; |
| 27 | +use serde_json; |
| 28 | +use sha2::Sha256; |
| 29 | +use tokio::fs; |
| 30 | +use walkdir::WalkDir; |
| 31 | + |
| 32 | +use crate::hash_utils; |
| 33 | +use crate::pack_meta::History; |
| 34 | +use crate::pack_meta::Offsets; |
| 35 | + |
| 36 | +/// Fingerprint of the conda-meta directory, used by `CondaFingerprint` below. |
| 37 | +#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] |
| 38 | +pub struct CondaMetaFingerprint { |
| 39 | + // TODO(agallagher): It might be worth storing more information of installed |
| 40 | + // packages, so that we could print better error messages when we detect two |
| 41 | + // envs are not equivalent. |
| 42 | + hash: Output<Sha256>, |
| 43 | +} |
| 44 | + |
| 45 | +impl CondaMetaFingerprint { |
| 46 | + async fn from_env(path: &Path) -> Result<Self> { |
| 47 | + let mut hasher = Sha256::new(); |
| 48 | + hash_utils::hash_directory_tree(&path.join("conda-meta"), &mut hasher).await?; |
| 49 | + Ok(Self { |
| 50 | + hash: hasher.finalize(), |
| 51 | + }) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +/// Fingerprint of the pack-meta directory, used by `CondaFingerprint` below. |
| 56 | +#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] |
| 57 | +pub struct PackMetaFingerprint { |
| 58 | + offsets: Output<Sha256>, |
| 59 | + pub history: History, |
| 60 | +} |
| 61 | + |
| 62 | +impl PackMetaFingerprint { |
| 63 | + async fn from_env(path: &Path) -> Result<Self> { |
| 64 | + let pack_meta = path.join("pack-meta"); |
| 65 | + |
| 66 | + // Read the fulle history.jsonl file. |
| 67 | + let contents = fs::read_to_string(pack_meta.join("history.jsonl")).await?; |
| 68 | + let history = History::from_contents(&contents)?; |
| 69 | + |
| 70 | + // Read entire offsets.jsonl file, but avoid hashing the offsets, which can change. |
| 71 | + let mut hasher = Sha256::new(); |
| 72 | + let contents = fs::read_to_string(pack_meta.join("offsets.jsonl")).await?; |
| 73 | + let offsets = Offsets::from_contents(&contents)?; |
| 74 | + for ent in offsets.entries { |
| 75 | + let contents = bincode::serialize(&(ent.path, ent.mode, ent.offsets.len()))?; |
| 76 | + hasher.update(contents.len().to_le_bytes()); |
| 77 | + hasher.update(&contents); |
| 78 | + } |
| 79 | + let offsets = hasher.finalize(); |
| 80 | + |
| 81 | + Ok(Self { history, offsets }) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +/// A fingerprint of a conda environment, used to detect if two envs are similar enough to |
| 86 | +/// facilitate mtime-based conda syncing. |
| 87 | +#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] |
| 88 | +pub struct CondaFingerprint { |
| 89 | + pub conda_meta: CondaMetaFingerprint, |
| 90 | + pub pack_meta: PackMetaFingerprint, |
| 91 | +} |
| 92 | + |
| 93 | +impl CondaFingerprint { |
| 94 | + pub async fn from_env(path: &Path) -> Result<Self> { |
| 95 | + Ok(Self { |
| 96 | + conda_meta: CondaMetaFingerprint::from_env(path).await?, |
| 97 | + pack_meta: PackMetaFingerprint::from_env(path).await?, |
| 98 | + }) |
| 99 | + } |
| 100 | + |
| 101 | + /// Create a comparator to compare the mtimes of files from two "equivalent" conda envs. |
| 102 | + /// In particular, thie comparator will be aware of spuriuos mtime changes that occurs from |
| 103 | + /// prefix replacement (via `meta-pack`), and will filter them out. |
| 104 | + pub fn mtime_comparator( |
| 105 | + a: &Self, |
| 106 | + b: &Self, |
| 107 | + ) -> Result<Box<dyn Fn(&SystemTime, &SystemTime) -> std::cmp::Ordering + Send + Sync>> { |
| 108 | + let (a_prefix, a_base) = a.pack_meta.history.first()?; |
| 109 | + let (b_prefix, b_base) = b.pack_meta.history.first()?; |
| 110 | + ensure!(a_prefix == b_prefix); |
| 111 | + |
| 112 | + // NOTE(agallagher): There appears to be some mtime drift on some files after fbpkg creation, |
| 113 | + // so acccount for that here. |
| 114 | + let slop = Duration::from_secs(5 * 60); |
| 115 | + |
| 116 | + // We load the timestamp from the first history entry, and use this to see if any |
| 117 | + // files have been updated since the env was created. |
| 118 | + let a_base = UNIX_EPOCH + Duration::from_secs(a_base) + slop; |
| 119 | + let b_base = UNIX_EPOCH + Duration::from_secs(b_base) + slop; |
| 120 | + |
| 121 | + // We also load the last prefix update window for each, as any mtimes from this window |
| 122 | + // should be ignored. |
| 123 | + let a_window = a |
| 124 | + .pack_meta |
| 125 | + .history |
| 126 | + .prefix_and_last_update_window()? |
| 127 | + .1 |
| 128 | + .map(|(s, e)| { |
| 129 | + ( |
| 130 | + UNIX_EPOCH + Duration::from_secs(s), |
| 131 | + UNIX_EPOCH + Duration::from_secs(e + 1), |
| 132 | + ) |
| 133 | + }); |
| 134 | + let b_window = b |
| 135 | + .pack_meta |
| 136 | + .history |
| 137 | + .prefix_and_last_update_window()? |
| 138 | + .1 |
| 139 | + .map(|(s, e)| { |
| 140 | + ( |
| 141 | + UNIX_EPOCH + Duration::from_secs(s), |
| 142 | + UNIX_EPOCH + Duration::from_secs(e + 1), |
| 143 | + ) |
| 144 | + }); |
| 145 | + |
| 146 | + Ok(Box::new(move |a: &SystemTime, b: &SystemTime| { |
| 147 | + match ( |
| 148 | + *a > a_base && a_window.is_none_or(|(s, e)| *a < s || *a > e), |
| 149 | + *b > b_base && b_window.is_none_or(|(s, e)| *b < s || *b > e), |
| 150 | + ) { |
| 151 | + (true, false) => std::cmp::Ordering::Greater, |
| 152 | + (false, true) => std::cmp::Ordering::Less, |
| 153 | + (false, false) => std::cmp::Ordering::Equal, |
| 154 | + (true, true) => a.cmp(b), |
| 155 | + } |
| 156 | + })) |
| 157 | + } |
| 158 | +} |
0 commit comments