| 1 | |
|
| 2 | |
|
| 3 | | use super::*; |
| 4 | |
|
| 5 | | #[derive(Debug, Default, Clone, PartialEq, Eq)] |
| 6 | | pub struct CheckoutReport { |
| 7 | | pub written: usize, |
| 8 | | pub removed: usize, |
| 9 | | pub conflicted: bool, |
| 10 | | |
| 11 | | pub notes: Vec<String>, |
| 12 | | |
| 13 | | pub lacks: usize, |
| 14 | | } |
| 15 | |
|
| 16 | |
|
| 17 | | const CHECKOUT_BATCH: usize = 2048; |
| 18 | |
|
| 19 | | impl Repo { |
| 20 | | |
| 21 | | |
| 22 | | |
| 23 | 456× | pub fn checkout( |
| 24 | 456× | &self, |
| 25 | 456× | heads: &[ChangeHash], |
| 26 | 456× | discard: bool, |
| 27 | 456× | command: &str, |
| 28 | 456× | ) -> Result<CheckoutReport> { |
| 29 | 456× | self.checkout_keeping(heads, discard, command, &BTreeSet::new()) |
| 30 | 456× | } |
| 31 | |
|
| 32 | | |
| 33 | | |
| 34 | 460× | pub(crate) fn checkout_keeping( |
| 35 | 460× | &self, |
| 36 | 460× | heads: &[ChangeHash], |
| 37 | 460× | discard: bool, |
| 38 | 460× | command: &str, |
| 39 | 460× | keep_subrepos: &BTreeSet<String>, |
| 40 | 460× | ) -> Result<CheckoutReport> { |
| 41 | 460× | let _wc = self.lock_working_copy()?; |
| 42 | 460× | let _all = crate::timing::phase("checkout"); |
| 43 | | |
| 44 | | |
| 45 | | |
| 46 | | |
| 47 | | |
| 48 | 460× | let changed_paths: Vec<String> = if self.clean_by_manifest()? { |
| 49 | 191× | Vec::new() |
| 50 | | } else { |
| 51 | 269× | let snap = self.snapshot_status(command)?; |
| 52 | 269× | self.refuse_dirty(&snap, discard)?; |
| 53 | 267× | snap.statuses.iter().map(|s| s.path.clone()).collect() |
| 54 | | }; |
| 55 | 458× | let (_, wc_now) = self.working_copy()?; |
| 56 | 458× | let old_manifest = self.read_manifest()?.map(|(_, _, m)| m).unwrap_or_default(); |
| 57 | 458× | let changed: HashSet<&str> = changed_paths.iter().map(String::as_str).collect(); |
| 58 | | |
| 59 | | |
| 60 | | |
| 61 | 458× | let mut target = self.load_state_indexed(heads)?; |
| 62 | 458× | let heads = target.maximal(heads); |
| 63 | 458× | let tv = crate::timing::phase("checkout: target view"); |
| 64 | 458× | let tview = target.tree().view(); |
| 65 | 458× | drop(tv); |
| 66 | 458× | let mut report = CheckoutReport::default(); |
| 67 | | |
| 68 | 458× | report.notes.extend( |
| 69 | 458× | self.platform |
| 70 | 458× | .problems(tview.entries.keys().map(String::as_str)), |
| 71 | | ); |
| 72 | 458× | report.conflicted = tview.is_conflicted(); |
| 73 | 458× | report.lacks = self.count_lacking(&wc_now.deps, &target)?; |
| 74 | |
|
| 75 | | |
| 76 | | |
| 77 | 458× | let mut dirs_to_prune: BTreeSet<PathBuf> = BTreeSet::new(); |
| 78 | 458× | let gone: BTreeSet<&str> = old_manifest |
| 79 | 458× | .entries |
| 80 | 458× | .keys() |
| 81 | 458× | .map(String::as_str) |
| 82 | 458× | .chain(changed.iter().copied()) |
| 83 | 7703× | .filter(|p| !tview.entries.contains_key(*p)) |
| 84 | 458× | .collect(); |
| 85 | 3078× | for path in gone { |
| 86 | 3078× | let dir = self.root.join(path); |
| 87 | 3078× | if dir.join(OXY_DIR).exists() || dir.join(".git").exists() { |
| 88 | 1× | report |
| 89 | 1× | .notes |
| 90 | 1× | .push(format!("{path}: nested repository left in place")); |
| 91 | 1× | continue; |
| 92 | 3077× | } |
| 93 | 3077× | let full = self.safe_path(path)?; |
| 94 | 3077× | if fs::symlink_metadata(&full).is_err() { |
| 95 | 0× | continue; |
| 96 | 3077× | } |
| 97 | 3077× | fs::remove_file(&full)?; |
| 98 | 3077× | report.removed += 1; |
| 99 | 3077× | if let Some(parent) = full.parent() { |
| 100 | 3077× | dirs_to_prune.insert(parent.to_path_buf()); |
| 101 | 3077× | } |
| 102 | | } |
| 103 | |
|
| 104 | | |
| 105 | 458× | let plan = crate::timing::phase("checkout: plan"); |
| 106 | 458× | let mut entries: BTreeMap<String, FsEntry> = BTreeMap::new(); |
| 107 | 5872× | let hex = |b: &[u8]| *blake3::hash(b).as_bytes(); |
| 108 | | |
| 109 | 458× | let mut texts: Vec<(String, NodeId, Mode)> = Vec::new(); |
| 110 | 10524× | for (path, e) in tview.entries.iter() { |
| 111 | 10524× | if e.kind == NodeKind::Dir || !self.in_scope(path) { |
| 112 | 219× | continue; |
| 113 | 10305× | } |
| 114 | | |
| 115 | | |
| 116 | 10305× | let exec_ok = |
| 117 | 4588× | |m: &prompt::ManifestEntry| !self.platform.exec_bits || m.exec == e.mode.exec; |
| 118 | 10305× | let entry = old_manifest |
| 119 | 10305× | .entries |
| 120 | 10305× | .get(path) |
| 121 | 10305× | .filter(|m| !changed.contains(path.as_str()) && exec_ok(m)); |
| 122 | 10305× | if e.kind == NodeKind::File && !e.mode.opaque { |
| 123 | | |
| 124 | | |
| 125 | | |
| 126 | 10267× | let key = target.graph_key(&e.node); |
| 127 | 10267× | let proven = |m: &&prompt::ManifestEntry| { |
| 128 | 4564× | !is_attributes(path) && key.is_some() && m.graph == key |
| 129 | 4564× | }; |
| 130 | 10267× | match entry.filter(proven) { |
| 131 | 4408× | Some(m) => { |
| 132 | 4408× | entries.insert( |
| 133 | 4408× | path.clone(), |
| 134 | 4408× | FsEntry { |
| 135 | 4408× | content_hash: Some(m.hash), |
| 136 | 4408× | graph_key: m.graph, |
| 137 | 4408× | unchanged: true, |
| 138 | 4408× | stat: Some((m.size, m.mtime_ns)), |
| 139 | 4408× | ..FsEntry::new(NodeKind::File, e.mode.exec, Vec::new()) |
| 140 | 4408× | }, |
| 141 | 4408× | ); |
| 142 | 4408× | } |
| 143 | 5859× | None => texts.push((path.clone(), e.node, e.mode)), |
| 144 | | } |
| 145 | 10267× | continue; |
| 146 | 38× | } |
| 147 | | |
| 148 | 38× | let blobs = target.live_blobs(&e.node); |
| 149 | 38× | if blobs.len() > 1 { |
| 150 | 1× | report.conflicted = true; |
| 151 | 37× | } |
| 152 | 38× | let Some(blob) = blobs.first().copied() else { |
| 153 | 0× | continue; |
| 154 | | }; |
| 155 | 38× | if e.kind == NodeKind::Subrepo { |
| 156 | 8× | if keep_subrepos.contains(path) { |
| 157 | 1× | continue; |
| 158 | 7× | } |
| 159 | 7× | let pin = crate::subrepo::Pin::decode(&self.read_blob(&blob)?)?; |
| 160 | 7× | let sync = self.sync_subrepo(path, &pin, false)?; |
| 161 | 6× | if matches!( |
| 162 | 7× | sync, |
| 163 | | SubrepoSync::Updated | SubrepoSync::KeptBookmark { .. } |
| 164 | 1× | ) { |
| 165 | 1× | report.written += 1; |
| 166 | 6× | } |
| 167 | 7× | report.notes.extend(sync.note(path)); |
| 168 | 7× | continue; |
| 169 | 30× | } |
| 170 | 30× | if let Some(m) = entry |
| 171 | 30× | .filter(|m| e.kind == NodeKind::File && blobs.len() == 1 && m.blob == Some(blob)) |
| 172 | | { |
| 173 | 15× | entries.insert( |
| 174 | 15× | path.clone(), |
| 175 | 15× | FsEntry { |
| 176 | 15× | content_hash: Some(m.hash), |
| 177 | 15× | blob: (e.kind == NodeKind::File).then_some(blob), |
| 178 | 15× | stat: Some((m.size, m.mtime_ns)), |
| 179 | 15× | ..FsEntry::new(e.kind, e.mode.exec, Vec::new()) |
| 180 | 15× | }, |
| 181 | | ); |
| 182 | 15× | continue; |
| 183 | 15× | } |
| 184 | 15× | let bytes = self.read_blob(&blob)?; |
| 185 | 15× | let full = self.root.join(path); |
| 186 | 15× | if e.kind == NodeKind::Symlink { |
| 187 | 0× | let same = fs::read_link(&full) |
| 188 | 0× | .is_ok_and(|t| t.to_string_lossy().as_bytes() == bytes.as_slice()); |
| 189 | 0× | if !same { |
| 190 | 0× | self.write_symlink(path, &bytes)?; |
| 191 | 0× | report.written += 1; |
| 192 | 0× | } |
| 193 | 0× | entries.insert( |
| 194 | 0× | path.clone(), |
| 195 | 0× | FsEntry { |
| 196 | 0× | content_hash: Some(hex(&bytes)), |
| 197 | 0× | ..FsEntry::new(NodeKind::Symlink, false, Vec::new()) |
| 198 | 0× | }, |
| 199 | | ); |
| 200 | | } else { |
| 201 | 15× | if !self.same_on_disk(&full, &bytes, e.mode.exec) { |
| 202 | 14× | self.write_file(path, &bytes, e.mode.exec)?; |
| 203 | 14× | report.written += 1; |
| 204 | 1× | } |
| 205 | 15× | entries.insert( |
| 206 | 15× | path.clone(), |
| 207 | 15× | FsEntry { |
| 208 | 15× | content_hash: Some(hex(&bytes)), |
| 209 | 15× | blob: (blobs.len() == 1).then_some(blob), |
| 210 | 15× | ..FsEntry::new(NodeKind::File, e.mode.exec, Vec::new()) |
| 211 | 15× | }, |
| 212 | | ); |
| 213 | | } |
| 214 | 15× | for (i, b) in blobs.iter().enumerate().skip(1) { |
| 215 | 1× | self.write_file( |
| 216 | 1× | &format!("{path}.oxy-conflict-{}", i + 1), |
| 217 | 1× | &self.read_blob(b)?, |
| 218 | 1× | e.mode.exec, |
| 219 | 0× | )?; |
| 220 | | } |
| 221 | | } |
| 222 | |
|
| 223 | 458× | drop(plan); |
| 224 | 458× | let _render = crate::timing::phase("checkout: render and write"); |
| 225 | | |
| 226 | | |
| 227 | 11028× | texts.sort_by_key(|(p, _, _)| !is_attributes(p)); |
| 228 | 458× | let keys: HashMap<NodeId, [u8; 32]> = texts |
| 229 | 458× | .iter() |
| 230 | 5859× | .filter_map(|(_, n, _)| Some((*n, target.graph_key(n)?))) |
| 231 | 458× | .collect(); |
| 232 | 458× | let mut attrs = crate::attributes::Attributes::default(); |
| 233 | 5859× | let attr_count = texts.iter().filter(|(p, _, _)| is_attributes(p)).count(); |
| 234 | 458× | let mut done = 0usize; |
| 235 | 458× | let mut batches: Vec<&[(String, NodeId, Mode)]> = Vec::new(); |
| 236 | 458× | if attr_count > 0 { |
| 237 | 2× | batches.push(&texts[..attr_count]); |
| 238 | 456× | } |
| 239 | 458× | batches.extend(texts[attr_count..].chunks(CHECKOUT_BATCH)); |
| 240 | 458× | for batch in batches { |
| 241 | | use rayon::prelude::*; |
| 242 | 348× | let nodes: Vec<NodeId> = batch.iter().map(|(_, n, _)| *n).collect(); |
| 243 | | |
| 244 | 348× | let single: Vec<Option<crate::objects::TextRef>> = |
| 245 | 5859× | nodes.iter().map(|n| target.single_text(n)).collect(); |
| 246 | 348× | let graphs: Vec<NodeId> = nodes |
| 247 | 348× | .iter() |
| 248 | 348× | .zip(&single) |
| 249 | 5859× | .filter(|(_, s)| s.is_none()) |
| 250 | 348× | .map(|(n, _)| *n) |
| 251 | 348× | .collect(); |
| 252 | 348× | let mut texts = { |
| 253 | 348× | let _t = crate::timing::phase("checkout: read single texts"); |
| 254 | 348× | let wanted: Vec<crate::objects::TextRef> = |
| 255 | 348× | single.iter().flatten().copied().collect(); |
| 256 | 348× | self.single_texts(&wanted)?.into_iter() |
| 257 | | }; |
| 258 | | { |
| 259 | 348× | let _t = crate::timing::phase("checkout: build graphs"); |
| 260 | 348× | self.load_graphs_of(&mut target, &graphs)?; |
| 261 | | } |
| 262 | 348× | let rendered: Vec<(Vec<u8>, bool)> = { |
| 263 | 348× | let _t = crate::timing::phase("checkout: render"); |
| 264 | 348× | let mut out: Vec<Option<(Vec<u8>, bool)>> = single |
| 265 | 348× | .iter() |
| 266 | 5859× | .map(|s| s.as_ref().and_then(|_| texts.next().map(|t| (t, false)))) |
| 267 | 348× | .collect(); |
| 268 | 348× | let from_graphs: Vec<(Vec<u8>, bool)> = graphs |
| 269 | 348× | .par_iter() |
| 270 | 348× | .map(|n| { |
| 271 | 320× | let (bytes, m) = target.render_file(n); |
| 272 | 320× | (bytes, m.is_conflicted()) |
| 273 | 320× | }) |
| 274 | 348× | .collect(); |
| 275 | 348× | let mut from_graphs = from_graphs.into_iter(); |
| 276 | 5859× | for o in out.iter_mut().filter(|o| o.is_none()) { |
| 277 | 320× | *o = from_graphs.next(); |
| 278 | 320× | } |
| 279 | 348× | out.into_iter() |
| 280 | 5859× | .map(|o| o.expect("every file rendered")) |
| 281 | 348× | .collect() |
| 282 | | }; |
| 283 | | #[cfg(test)] |
| 284 | | { |
| 285 | 142× | let singles: Vec<NodeId> = nodes |
| 286 | 142× | .iter() |
| 287 | 142× | .zip(&single) |
| 288 | 5273× | .filter(|(_, s)| s.is_some()) |
| 289 | 142× | .map(|(n, _)| *n) |
| 290 | 142× | .collect(); |
| 291 | 142× | self.load_graphs_of(&mut target, &singles)?; |
| 292 | 5273× | for (n, (bytes, _)) in nodes.iter().zip(&rendered) { |
| 293 | 5273× | assert_eq!( |
| 294 | | *bytes, |
| 295 | 5273× | target.render_file(n).0, |
| 296 | | "a single text is its file's rendering" |
| 297 | | ); |
| 298 | | } |
| 299 | | } |
| 300 | 348× | target.drop_graphs(&nodes); |
| 301 | 348× | let _t = crate::timing::phase("checkout: write"); |
| 302 | 348× | if done == 0 && attr_count > 0 { |
| 303 | 2× | attrs = crate::attributes::Attributes::from_files( |
| 304 | 2× | batch |
| 305 | 2× | .iter() |
| 306 | 2× | .zip(&rendered) |
| 307 | 2× | .map(|((p, _, _), (b, _))| (p.as_str(), b.as_slice())), |
| 308 | | ); |
| 309 | 346× | } |
| 310 | 348× | done += batch.len(); |
| 311 | 348× | let on_disk: Vec<Vec<u8>> = batch |
| 312 | 348× | .iter() |
| 313 | 348× | .zip(rendered.iter()) |
| 314 | 5859× | .map(|((path, _, _), (bytes, _))| { |
| 315 | 5859× | if attrs.lookup(path).eol == Some(crate::attributes::Eol::Crlf) { |
| 316 | 2× | crate::attributes::to_crlf(bytes) |
| 317 | | } else { |
| 318 | 5857× | bytes.clone() |
| 319 | | } |
| 320 | 5859× | }) |
| 321 | 348× | .collect(); |
| 322 | | |
| 323 | | |
| 324 | 348× | let mut dirs: BTreeSet<&str> = BTreeSet::new(); |
| 325 | 5859× | for (path, _, _) in batch { |
| 326 | 5859× | if let Some((dir, _)) = path.rsplit_once('/') { |
| 327 | 5271× | dirs.insert(dir); |
| 328 | 5271× | } |
| 329 | | } |
| 330 | 348× | for dir in dirs { |
| 331 | 180× | let full = self.safe_path(dir)?; |
| 332 | 179× | if !full.is_dir() { |
| 333 | 134× | if fs::symlink_metadata(&full).is_ok() { |
| 334 | 0× | remove_any(&full)?; |
| 335 | 134× | } |
| 336 | 134× | fs::create_dir_all(&full)?; |
| 337 | 45× | } |
| 338 | | } |
| 339 | 5857× | for (path, _, _) in batch { |
| 340 | 5857× | check_path_name(path)?; |
| 341 | | } |
| 342 | 347× | let (root, exec_bits) = (&self.root, self.platform.exec_bits); |
| 343 | | |
| 344 | | |
| 345 | | |
| 346 | | |
| 347 | 347× | let plans: Vec<Disk> = batch |
| 348 | 347× | .par_iter() |
| 349 | 347× | .zip(on_disk.par_iter()) |
| 350 | 5857× | .map(|((path, _, mode), bytes)| { |
| 351 | 5857× | compare_on_disk(&root.join(path), bytes, mode.exec, exec_bits) |
| 352 | 5857× | }) |
| 353 | 347× | .collect(); |
| 354 | 347× | let fresh: Vec<usize> = (0..batch.len()) |
| 355 | 5857× | .filter(|&i| plans[i] == Disk::Absent) |
| 356 | 347× | .collect(); |
| 357 | 347× | fresh |
| 358 | 347× | .par_iter() |
| 359 | 5695× | .map(|&i| write_file_at(&root.join(&batch[i].0), &on_disk[i], batch[i].2.exec)) |
| 360 | 347× | .collect::<std::io::Result<()>>()?; |
| 361 | 5857× | for (i, plan) in plans.iter().enumerate() { |
| 362 | 5857× | if *plan == Disk::Differs { |
| 363 | 152× | write_file_at(&root.join(&batch[i].0), &on_disk[i], batch[i].2.exec)?; |
| 364 | 5705× | } |
| 365 | | } |
| 366 | 5857× | report.written += plans.iter().filter(|p| **p != Disk::Same).count(); |
| 367 | 5857× | for (((path, node, mode), (_, conflicted)), on_disk) in |
| 368 | 347× | batch.iter().zip(rendered).zip(on_disk) |
| 369 | | { |
| 370 | 5857× | let a = attrs.lookup(path); |
| 371 | 5857× | report.conflicted |= conflicted; |
| 372 | 5857× | entries.insert( |
| 373 | 5857× | path.clone(), |
| 374 | | FsEntry { |
| 375 | 5857× | content_hash: Some(hex(&on_disk)), |
| 376 | 5857× | graph_key: (!conflicted && a.eol.is_none()) |
| 377 | 5857× | .then(|| keys.get(node).copied()) |
| 378 | 5857× | .flatten(), |
| 379 | 5857× | ..FsEntry::new(NodeKind::File, mode.exec, Vec::new()) |
| 380 | | }, |
| 381 | | ); |
| 382 | | } |
| 383 | | } |
| 384 | | |
| 385 | | |
| 386 | 10295× | for (path, e) in &entries { |
| 387 | 10295× | if e.kind == NodeKind::File && attrs.lookup(path).lockable && !self.is_locked(path) { |
| 388 | 2× | let _ = set_readonly(&self.root.join(path), true); |
| 389 | 10293× | } |
| 390 | | } |
| 391 | | |
| 392 | 457× | for d in dirs_to_prune.iter().rev() { |
| 393 | 74× | let mut d = d.clone(); |
| 394 | 96× | while d != self.root |
| 395 | 25× | && fs::read_dir(&d) |
| 396 | 25× | .map(|mut i| i.next().is_none()) |
| 397 | 25× | .unwrap_or(false) |
| 398 | | { |
| 399 | 22× | fs::remove_dir(&d)?; |
| 400 | 22× | let Some(p) = d.parent() else { break }; |
| 401 | 22× | d = p.to_path_buf(); |
| 402 | | } |
| 403 | | } |
| 404 | |
|
| 405 | 457× | drop(_render); |
| 406 | 457× | let recording = crate::timing::phase("checkout: record"); |
| 407 | 457× | let (op_ref, op) = self.head_op()?; |
| 408 | 457× | let wc = self.new_working_copy(heads.clone(), ""); |
| 409 | 457× | let wc_hash = self.store.put_change(&wc)?; |
| 410 | 457× | let mut view = op.view.clone(); |
| 411 | 457× | view.workspaces.insert(self.workspace, wc_hash); |
| 412 | 457× | self.record_op(&[op_ref], command, view)?; |
| 413 | 457× | drop(recording); |
| 414 | 457× | let _t = crate::timing::phase("checkout: manifest"); |
| 415 | | |
| 416 | | |
| 417 | | |
| 418 | | |
| 419 | | |
| 420 | 457× | if !old_manifest.dirs.is_empty() { |
| 421 | 457× | let mut dirs: BTreeSet<String> = |
| 422 | 556× | old_manifest.dirs.iter().map(|(d, _)| d.clone()).collect(); |
| 423 | 457× | dirs.insert(String::new()); |
| 424 | 10295× | for p in entries.keys() { |
| 425 | 10295× | let mut at = p.as_str(); |
| 426 | 10430× | while let Some((parent, _)) = at.rsplit_once('/') { |
| 427 | 9501× | if !dirs.insert(parent.to_string()) { |
| 428 | 9366× | break; |
| 429 | 135× | } |
| 430 | 135× | at = parent; |
| 431 | | } |
| 432 | | } |
| 433 | | use rayon::prelude::*; |
| 434 | 457× | let root = &self.root; |
| 435 | 457× | let dirs: Vec<(String, u128)> = dirs |
| 436 | 457× | .into_par_iter() |
| 437 | 691× | .filter_map(|d| { |
| 438 | 691× | let m = fs::metadata(root.join(&d)).ok().filter(|m| m.is_dir())?; |
| 439 | 669× | Some((d, prompt::mtime_ns(&m))) |
| 440 | 691× | }) |
| 441 | 457× | .collect(); |
| 442 | 457× | let mut dirs = dirs; |
| 443 | 457× | dirs.sort_unstable(); |
| 444 | 457× | *self.walked_dirs.borrow_mut() = Some((dirs, old_manifest.nested.clone())); |
| 445 | 0× | } |
| 446 | 457× | self.write_manifest_entries(report.conflicted, false, &entries, &HashMap::new())?; |
| 447 | 457× | let _ = self.run_hook( |
| 448 | 457× | "post-checkout", |
| 449 | 457× | &format!( |
| 450 | | "{{\"event\":\"post-checkout\",\"heads\":{},\"written\":{},\"removed\":{}}}", |
| 451 | 789× | json_array(heads.iter().map(|h| json_str(&h.to_hex()))), |
| 452 | | report.written, |
| 453 | | report.removed |
| 454 | | ), |
| 455 | | ); |
| 456 | 457× | Ok(report) |
| 457 | 460× | } |
| 458 | |
|
| 459 | | |
| 460 | | |
| 461 | 458× | fn count_lacking(&self, from: &[ChangeHash], to: &State) -> Result<usize> { |
| 462 | 458× | let mut seen: HashSet<ChangeHash> = HashSet::new(); |
| 463 | 458× | let mut stack: Vec<ChangeHash> = from.to_vec(); |
| 464 | 871× | while let Some(h) = stack.pop() { |
| 465 | 413× | if to.contains(&h) || !seen.insert(h) { |
| 466 | 340× | continue; |
| 467 | 73× | } |
| 468 | 73× | stack.extend(self.store.get_change_lazy(&h)?.0.deps); |
| 469 | | } |
| 470 | 458× | Ok(seen.len()) |
| 471 | 458× | } |
| 472 | |
|
| 473 | | |
| 474 | 269× | pub(crate) fn refuse_dirty(&self, snap: &SnapshotResult, discard: bool) -> Result<()> { |
| 475 | 269× | if discard || (snap.wc.tree_ops.is_empty() && snap.wc.edits.is_empty()) { |
| 476 | 267× | return Ok(()); |
| 477 | 2× | } |
| 478 | 2× | let list: Vec<String> = snap |
| 479 | 2× | .statuses |
| 480 | 2× | .iter() |
| 481 | 2× | .take(8) |
| 482 | 3× | .map(|s| format!("{} {}", status_word(s.status), s.path)) |
| 483 | 2× | .collect(); |
| 484 | 2× | fail(format!( |
| 485 | | "the working copy has changes ({}{}); commit them, take them along with --carry, or drop them with --discard", |
| 486 | 2× | list.join(", "), |
| 487 | 2× | if snap.statuses.len() > 8 { ", …" } else { "" } |
| 488 | | )) |
| 489 | 269× | } |
| 490 | |
|
| 491 | | |
| 492 | | |
| 493 | 15× | fn same_on_disk(&self, full: &Path, bytes: &[u8], exec: bool) -> bool { |
| 494 | 15× | match fs::symlink_metadata(full) { |
| 495 | 5× | Ok(m) if m.is_file() && m.len() == bytes.len() as u64 => { |
| 496 | 4× | (!self.platform.exec_bits || is_executable(&m) == exec) |
| 497 | 4× | && fs::read(full).is_ok_and(|b| b == bytes) |
| 498 | | } |
| 499 | 11× | _ => false, |
| 500 | | } |
| 501 | 15× | } |
| 502 | |
|
| 503 | | |
| 504 | | |
| 505 | 3278× | fn safe_path(&self, rel: &str) -> Result<PathBuf> { |
| 506 | 3278× | let mut full = self.root.clone(); |
| 507 | 3278× | let comps: Vec<&str> = rel.split('/').collect(); |
| 508 | 6297× | for (i, comp) in comps.iter().enumerate() { |
| 509 | 6297× | if comp.is_empty() || *comp == "." || *comp == ".." || comp.contains('\\') { |
| 510 | 0× | return fail(format!("refusing unsafe path {rel:?}")); |
| 511 | 6297× | } |
| 512 | 6297× | if i == 0 && *comp == OXY_DIR { |
| 513 | 0× | return fail(format!("refusing to write inside {OXY_DIR}: {rel:?}")); |
| 514 | 6297× | } |
| 515 | 6297× | if i + 1 < comps.len() { |
| 516 | 3020× | full.push(comp); |
| 517 | 3020× | if let Ok(m) = fs::symlink_metadata(&full) { |
| 518 | 3019× | if m.file_type().is_symlink() { |
| 519 | 1× | return fail(format!( |
| 520 | | "refusing to write through symlink {}", |
| 521 | 1× | full.display() |
| 522 | | )); |
| 523 | 3018× | } |
| 524 | 1× | } |
| 525 | 3277× | } else { |
| 526 | 3277× | full.push(comp); |
| 527 | 3277× | } |
| 528 | | } |
| 529 | 3277× | Ok(full) |
| 530 | 3278× | } |
| 531 | |
|
| 532 | 21× | pub(crate) fn write_file(&self, rel: &str, bytes: &[u8], exec: bool) -> Result<()> { |
| 533 | 21× | let full = self.safe_path(rel)?; |
| 534 | 21× | if let Some(parent) = full.parent() { |
| 535 | 21× | fs::create_dir_all(parent)?; |
| 536 | 0× | } |
| 537 | 21× | if fs::symlink_metadata(&full).is_ok_and(|m| m.file_type().is_symlink() || m.is_dir()) { |
| 538 | 0× | remove_any(&full)?; |
| 539 | 21× | } |
| 540 | 21× | if fs::metadata(&full).is_ok_and(|m| m.permissions().readonly()) { |
| 541 | 1× | set_readonly(&full, false)?; |
| 542 | 20× | } |
| 543 | 21× | fs::write(&full, bytes)?; |
| 544 | 21× | set_executable(&full, exec)?; |
| 545 | 21× | Ok(()) |
| 546 | 21× | } |
| 547 | |
|
| 548 | 0× | pub(crate) fn write_symlink(&self, rel: &str, target: &[u8]) -> Result<()> { |
| 549 | 0× | let full = self.safe_path(rel)?; |
| 550 | 0× | if let Some(parent) = full.parent() { |
| 551 | 0× | fs::create_dir_all(parent)?; |
| 552 | 0× | } |
| 553 | 0× | if fs::symlink_metadata(&full).is_ok() { |
| 554 | 0× | remove_any(&full)?; |
| 555 | 0× | } |
| 556 | | #[cfg(unix)] |
| 557 | | { |
| 558 | 0× | std::os::unix::fs::symlink(String::from_utf8_lossy(target).as_ref(), &full)?; |
| 559 | 0× | Ok(()) |
| 560 | | } |
| 561 | | #[cfg(not(unix))] |
| 562 | | { |
| 563 | | |
| 564 | | fs::write(&full, target)?; |
| 565 | | Ok(()) |
| 566 | | } |
| 567 | 0× | } |
| 568 | | } |
| 569 | |
|
| 570 | |
|
| 571 | |
|
| 572 | 3× | fn status_word(s: ChangeStatus) -> &'static str { |
| 573 | 3× | match s { |
| 574 | 0× | ChangeStatus::Added => "added", |
| 575 | 2× | ChangeStatus::Modified => "modified", |
| 576 | 1× | ChangeStatus::Removed => "removed", |
| 577 | 0× | ChangeStatus::Renamed => "renamed", |
| 578 | 0× | ChangeStatus::ModeChanged => "mode changed", |
| 579 | 0× | ChangeStatus::ConflictEdited => "conflict edited", |
| 580 | 0× | ChangeStatus::Resolved => "resolved", |
| 581 | | } |
| 582 | 3× | } |
| 583 | |
|
| 584 | |
|
| 585 | |
|
| 586 | 5857× | fn check_path_name(rel: &str) -> Result<()> { |
| 587 | 11128× | for (i, comp) in rel.split('/').enumerate() { |
| 588 | 11128× | if comp.is_empty() || comp == "." || comp == ".." || comp.contains('\\') { |
| 589 | 0× | return fail(format!("refusing unsafe path {rel:?}")); |
| 590 | 11128× | } |
| 591 | 11128× | if i == 0 && comp == OXY_DIR { |
| 592 | 0× | return fail(format!("refusing to write inside {OXY_DIR}: {rel:?}")); |
| 593 | 11128× | } |
| 594 | | } |
| 595 | 5857× | Ok(()) |
| 596 | 5857× | } |
| 597 | |
|
| 598 | |
|
| 599 | | #[derive(Clone, Copy, PartialEq, Eq)] |
| 600 | | enum Disk { |
| 601 | | Same, |
| 602 | | Differs, |
| 603 | | Absent, |
| 604 | | } |
| 605 | |
|
| 606 | |
|
| 607 | |
|
| 608 | 5857× | fn compare_on_disk(full: &Path, bytes: &[u8], exec: bool, exec_bits: bool) -> Disk { |
| 609 | 5857× | match fs::symlink_metadata(full) { |
| 610 | 10× | Ok(m) |
| 611 | 162× | if m.is_file() |
| 612 | 162× | && m.len() == bytes.len() as u64 |
| 613 | 24× | && (!exec_bits || is_executable(&m) == exec) |
| 614 | 24× | && fs::read(full).is_ok_and(|b| b == bytes) => |
| 615 | | { |
| 616 | 10× | Disk::Same |
| 617 | | } |
| 618 | 152× | Ok(_) => Disk::Differs, |
| 619 | 5695× | Err(_) => Disk::Absent, |
| 620 | | } |
| 621 | 5857× | } |
| 622 | |
|
| 623 | |
|
| 624 | 5847× | fn write_file_at(full: &Path, bytes: &[u8], exec: bool) -> std::io::Result<()> { |
| 625 | 5847× | match fs::symlink_metadata(full) { |
| 626 | 152× | Ok(m) if m.is_file() => { |
| 627 | 152× | if m.permissions().readonly() { |
| 628 | 0× | set_readonly(full, false)?; |
| 629 | 152× | } |
| 630 | | } |
| 631 | 0× | Ok(_) => remove_any(full)?, |
| 632 | 5695× | Err(_) => {} |
| 633 | | } |
| 634 | 5847× | fs::write(full, bytes)?; |
| 635 | 5847× | set_executable(full, exec) |
| 636 | 5847× | } |