1use std::{
3 fs::{self, File, OpenOptions},
4 io::{self, Write},
5 path::{Path, PathBuf},
6 sync::{
7 Arc, Mutex, OnceLock,
8 atomic::{AtomicU64, Ordering},
9 },
10 time::{SystemTime, UNIX_EPOCH},
11};
12
13#[derive(Default)]
14struct CloneDurabilityStats {
15 barriers: AtomicU64,
16 skipped: AtomicU64,
17}
18
19#[derive(Clone)]
20struct CloneDurabilityEntry {
21 root: PathBuf,
22 stats: Arc<CloneDurabilityStats>,
23}
24
25fn clone_durability_entries() -> &'static Mutex<Vec<CloneDurabilityEntry>> {
26 static ENTRIES: OnceLock<Mutex<Vec<CloneDurabilityEntry>>> = OnceLock::new();
27 ENTRIES.get_or_init(|| Mutex::new(Vec::new()))
28}
29
30fn deferred_clone_stats(path: &Path) -> Option<Arc<CloneDurabilityStats>> {
31 clone_durability_entries()
32 .lock()
33 .ok()?
34 .iter()
35 .rev()
36 .find(|entry| path.starts_with(&entry.root))
37 .map(|entry| Arc::clone(&entry.stats))
38}
39
40pub fn clone_write_is_deferred(path: &Path) -> bool {
41 deferred_clone_stats(path).is_some()
42}
43
44pub fn record_deferred_clone_barrier(path: &Path) {
45 if let Some(stats) = deferred_clone_stats(path) {
46 stats.skipped.fetch_add(1, Ordering::Relaxed);
47 }
48}
49
50pub struct CloneDurabilityBatch {
56 root: PathBuf,
57 stats: Arc<CloneDurabilityStats>,
58}
59
60impl CloneDurabilityBatch {
61 pub fn begin(root: impl AsRef<Path>) -> Self {
62 let root = root.as_ref().to_path_buf();
63 let stats = Arc::new(CloneDurabilityStats::default());
64 clone_durability_entries()
65 .lock()
66 .unwrap_or_else(std::sync::PoisonError::into_inner)
67 .push(CloneDurabilityEntry {
68 root: root.clone(),
69 stats: Arc::clone(&stats),
70 });
71 Self { root, stats }
72 }
73
74 pub fn commit(&self) -> io::Result<()> {
77 sync_filesystem(&self.root)?;
78 self.stats.barriers.fetch_add(1, Ordering::Relaxed);
79 Ok(())
80 }
81
82 pub fn barrier_count(&self) -> u64 {
83 self.stats.barriers.load(Ordering::Relaxed)
84 }
85
86 pub fn skipped_barrier_count(&self) -> u64 {
87 self.stats.skipped.load(Ordering::Relaxed)
88 }
89}
90
91impl Drop for CloneDurabilityBatch {
92 fn drop(&mut self) {
93 let mut entries = clone_durability_entries()
94 .lock()
95 .unwrap_or_else(std::sync::PoisonError::into_inner);
96 if let Some(index) = entries
97 .iter()
98 .rposition(|entry| entry.root == self.root && Arc::ptr_eq(&entry.stats, &self.stats))
99 {
100 entries.remove(index);
101 }
102 }
103}
104
105#[derive(Clone, Copy)]
106enum AtomicWriteKind {
107 Normal,
108 Secret,
109}
110
111impl AtomicWriteKind {
112 fn open_tmp(self, tmp: &Path) -> io::Result<File> {
113 let mut options = OpenOptions::new();
114 options.create_new(true).write(true);
115
116 #[cfg(unix)]
117 if matches!(self, Self::Secret) {
118 use std::os::unix::fs::OpenOptionsExt;
119 options.mode(0o600);
120 }
121
122 options.open(tmp)
123 }
124
125 fn enforce_before_write(self, file: &File) -> io::Result<()> {
126 match self {
127 Self::Normal => Ok(()),
128 Self::Secret => enforce_secret_permissions_before_write(file),
129 }
130 }
131}
132
133#[cfg(unix)]
134fn enforce_secret_permissions_before_write(file: &File) -> io::Result<()> {
135 use std::os::unix::fs::PermissionsExt;
136
137 file.set_permissions(fs::Permissions::from_mode(0o600))?;
138 let mode = file.metadata()?.permissions().mode() & 0o777;
139 if mode != 0o600 {
140 return Err(io::Error::new(
141 io::ErrorKind::PermissionDenied,
142 format!("secret temp file permissions are {mode:o}, expected 600"),
143 ));
144 }
145 Ok(())
146}
147
148#[cfg(not(unix))]
149fn enforce_secret_permissions_before_write(_file: &File) -> io::Result<()> {
150 Ok(())
154}
155
156static TEMP_PATH_COUNTER: AtomicU64 = AtomicU64::new(0);
157
158const ENOSPC: i32 = 28;
163
164const ENOTEMPTY_LINUX: i32 = 39;
169const ENOTEMPTY_MACOS: i32 = 66;
170const ENOTEMPTY_WINDOWS: i32 = 145;
171
172const EACCES: i32 = 13;
175
176const ENOENT: i32 = 2;
179
180const EROFS: i32 = 30;
183
184const EXDEV: i32 = 18;
187
188pub fn is_out_of_space(err: &io::Error) -> bool {
193 if err.raw_os_error() == Some(ENOSPC) {
194 return true;
195 }
196 if err.kind() == io::ErrorKind::StorageFull {
200 return true;
201 }
202 if err.kind() == io::ErrorKind::WriteZero {
208 return true;
209 }
210 false
211}
212
213pub fn is_directory_not_empty(err: &io::Error) -> bool {
221 if err.kind() == io::ErrorKind::DirectoryNotEmpty {
222 return true;
223 }
224 matches!(
225 err.raw_os_error(),
226 Some(ENOTEMPTY_LINUX) | Some(ENOTEMPTY_MACOS) | Some(ENOTEMPTY_WINDOWS)
227 )
228}
229
230pub fn is_permission_denied(err: &io::Error) -> bool {
236 if err.kind() == io::ErrorKind::PermissionDenied {
237 return true;
238 }
239 err.raw_os_error() == Some(EACCES)
240}
241
242pub fn is_not_found(err: &io::Error) -> bool {
248 if err.kind() == io::ErrorKind::NotFound {
249 return true;
250 }
251 err.raw_os_error() == Some(ENOENT)
252}
253
254pub fn is_read_only_filesystem(err: &io::Error) -> bool {
260 if err.kind() == io::ErrorKind::ReadOnlyFilesystem {
261 return true;
262 }
263 err.raw_os_error() == Some(EROFS)
264}
265
266pub fn is_cross_device_link(err: &io::Error) -> bool {
273 if err.kind() == io::ErrorKind::CrossesDevices {
274 return true;
275 }
276 err.raw_os_error() == Some(EXDEV)
277}
278
279pub fn temp_path(path: &Path) -> PathBuf {
280 let parent = path.parent().unwrap_or_else(|| Path::new("."));
281 let file_name = path
282 .file_name()
283 .and_then(|s| s.to_str())
284 .filter(|s| !s.is_empty())
285 .unwrap_or("heddle-tmp");
286 let unique = SystemTime::now()
287 .duration_since(UNIX_EPOCH)
288 .map(|d| d.as_nanos())
289 .unwrap_or(0);
290 let counter = TEMP_PATH_COUNTER.fetch_add(1, Ordering::Relaxed);
291 let pid = std::process::id();
292 parent.join(format!(".{file_name}.tmp-{pid}-{unique}-{counter}"))
293}
294
295#[cfg(target_os = "linux")]
304fn kick_writeback(file: &File) {
305 use std::os::unix::io::AsRawFd;
306 const SYNC_FILE_RANGE_WRITE: libc::c_uint = 2;
310 unsafe {
311 libc::sync_file_range(file.as_raw_fd(), 0, 0, SYNC_FILE_RANGE_WRITE);
312 }
313}
314
315#[cfg(not(target_os = "linux"))]
316fn kick_writeback(_file: &File) {}
317
318pub fn stage_temp_files_durable(files: &[(PathBuf, Vec<u8>)]) -> io::Result<()> {
338 let mut handles: Vec<File> = Vec::with_capacity(files.len());
339 for (temp_path, bytes) in files {
340 let mut file = File::create(temp_path).map_err(|err| enrich_write_error(temp_path, err))?;
341 file.write_all(bytes)
342 .map_err(|err| enrich_write_error(temp_path, err))?;
343 kick_writeback(&file);
344 handles.push(file);
345 }
346 for (file, (temp_path, _)) in handles.iter().zip(files) {
349 sync_file(file, temp_path).map_err(|err| enrich_write_error(temp_path, err))?;
350 }
351 Ok(())
352}
353
354#[cfg(windows)]
378pub fn sync_directory(_path: &Path) -> io::Result<()> {
379 Ok(())
380}
381
382#[cfg(not(windows))]
383pub fn sync_directory(path: &Path) -> io::Result<()> {
384 if let Some(stats) = deferred_clone_stats(path) {
385 stats.skipped.fetch_add(1, Ordering::Relaxed);
386 return Ok(());
387 }
388 let dir = OpenOptions::new().read(true).open(path)?;
389 dir.sync_all()
390}
391
392pub fn sync_file(file: &File, path: &Path) -> io::Result<()> {
394 if let Some(stats) = deferred_clone_stats(path) {
395 stats.skipped.fetch_add(1, Ordering::Relaxed);
396 return Ok(());
397 }
398 file.sync_all()
399}
400
401pub fn sync_file_data(file: &File, path: &Path) -> io::Result<()> {
402 if let Some(stats) = deferred_clone_stats(path) {
403 stats.skipped.fetch_add(1, Ordering::Relaxed);
404 return Ok(());
405 }
406 file.sync_data()
407}
408
409#[cfg(any(target_os = "linux", target_os = "android"))]
410fn sync_filesystem(path: &Path) -> io::Result<()> {
411 use std::os::fd::AsRawFd;
412
413 let file = OpenOptions::new().read(true).open(path)?;
414 if unsafe { libc::syncfs(file.as_raw_fd()) } == 0 {
416 Ok(())
417 } else {
418 Err(io::Error::last_os_error())
419 }
420}
421
422#[cfg(all(unix, not(any(target_os = "linux", target_os = "android"))))]
423fn sync_filesystem(_path: &Path) -> io::Result<()> {
424 unsafe { libc::sync() };
427 Ok(())
428}
429
430#[cfg(windows)]
431fn sync_filesystem(path: &Path) -> io::Result<()> {
432 for entry in fs::read_dir(path)? {
436 let entry = entry?;
437 let file_type = entry.file_type()?;
438 if file_type.is_dir() {
439 sync_filesystem(&entry.path())?;
440 } else if file_type.is_file() {
441 OpenOptions::new()
442 .read(true)
443 .open(entry.path())?
444 .sync_all()?;
445 }
446 }
447 Ok(())
448}
449
450fn plan_missing_dirs(path: &Path) -> (Vec<PathBuf>, Option<PathBuf>) {
454 let mut missing: Vec<PathBuf> = Vec::new();
457 {
458 let mut cur = path;
459 loop {
460 match fs::metadata(cur) {
461 Ok(meta) if meta.is_dir() => break,
462 Ok(_) => {
463 break;
466 }
467 Err(e) if e.kind() == io::ErrorKind::NotFound => {
468 missing.push(cur.to_path_buf());
469 match cur.parent() {
470 Some(parent) if parent.as_os_str().is_empty() => break,
473 Some(parent) if parent != cur => cur = parent,
475 _ => break,
476 }
477 }
478 Err(_) => break,
481 }
482 }
483 }
484
485 let deepest_existing = missing
486 .last()
487 .and_then(|shallowest| match shallowest.parent() {
488 Some(parent) if parent.as_os_str().is_empty() => Some(PathBuf::from(".")),
489 Some(parent) => Some(parent.to_path_buf()),
490 None => None,
491 });
492
493 (missing, deepest_existing)
494}
495
496fn sync_new_dirents(missing: &[PathBuf], deepest_existing: Option<&Path>) -> io::Result<()> {
499 if missing.is_empty() {
500 return Ok(());
501 }
502 for dir in missing {
503 sync_directory(dir)?;
504 }
505 if let Some(existing) = deepest_existing {
508 sync_directory(existing)?;
509 }
510 Ok(())
511}
512
513pub fn create_dir_all_durable(path: &Path) -> io::Result<()> {
531 let (missing, deepest_existing) = plan_missing_dirs(path);
532 fs::create_dir_all(path)?;
533 sync_new_dirents(&missing, deepest_existing.as_deref())
534}
535
536fn enrich_write_error(path: &Path, err: io::Error) -> io::Error {
546 enrich_fs_error(path, "writing", err)
547}
548
549pub fn enrich_fs_error(path: &Path, op: &'static str, err: io::Error) -> io::Error {
578 if is_out_of_space(&err) {
579 let msg = format!(
580 "out of disk space {op} {}: free disk space and re-run the command — your working tree is unchanged",
581 path.display()
582 );
583 return io::Error::new(
584 io::ErrorKind::StorageFull,
585 EnrichedFsError { msg, source: err },
586 );
587 }
588 if is_directory_not_empty(&err) {
589 let msg = format!(
590 "could not remove directory `{}` because it contains content (heddle-ignored or otherwise) — leaving in place",
591 path.display()
592 );
593 return io::Error::new(
594 io::ErrorKind::DirectoryNotEmpty,
595 EnrichedFsError { msg, source: err },
596 );
597 }
598 if is_read_only_filesystem(&err) {
599 let msg = format!(
600 "filesystem is read-only — `{}` cannot be modified",
601 path.display()
602 );
603 return io::Error::new(
604 io::ErrorKind::ReadOnlyFilesystem,
605 EnrichedFsError { msg, source: err },
606 );
607 }
608 if is_permission_denied(&err) {
609 let msg = format!(
610 "permission denied {op} `{}` — check filesystem permissions",
611 path.display()
612 );
613 return io::Error::new(
614 io::ErrorKind::PermissionDenied,
615 EnrichedFsError { msg, source: err },
616 );
617 }
618 if is_not_found(&err) {
619 let msg = format!("could not find `{}` for {op}", path.display());
620 return io::Error::new(
621 io::ErrorKind::NotFound,
622 EnrichedFsError { msg, source: err },
623 );
624 }
625 if is_cross_device_link(&err) {
626 let msg = format!(
627 "cannot rename across filesystems — temp file for `{}` lives on a different mount; set TMPDIR to the same filesystem as the destination",
628 path.display()
629 );
630 return io::Error::new(
631 io::ErrorKind::CrossesDevices,
632 EnrichedFsError { msg, source: err },
633 );
634 }
635 err
636}
637
638pub fn enrich_rename_error(src: &Path, dst: &Path, err: io::Error) -> io::Error {
643 if is_cross_device_link(&err) {
644 let msg = format!(
645 "cannot rename across filesystems — temp file at `{}` cannot be renamed to `{}`; set TMPDIR to the same filesystem as the destination",
646 src.display(),
647 dst.display()
648 );
649 return io::Error::new(
650 io::ErrorKind::CrossesDevices,
651 EnrichedFsError { msg, source: err },
652 );
653 }
654 enrich_fs_error(dst, "renaming", err)
655}
656
657#[derive(Debug)]
658struct EnrichedFsError {
659 msg: String,
660 source: io::Error,
661}
662
663impl std::fmt::Display for EnrichedFsError {
664 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
665 f.write_str(&self.msg)
666 }
667}
668
669impl std::error::Error for EnrichedFsError {
670 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
671 Some(&self.source)
672 }
673}
674
675pub struct StagedAtomicWrite {
676 path: PathBuf,
677 parent: PathBuf,
678 tmp: PathBuf,
679 pending: bool,
680}
681
682impl StagedAtomicWrite {
683 pub fn publish(mut self) -> io::Result<()> {
684 fs::rename(&self.tmp, &self.path)
685 .map_err(|error| enrich_rename_error(&self.tmp, &self.path, error))?;
686 self.pending = false;
687 sync_directory(&self.parent)
688 .map_err(|error| enrich_fs_error(&self.parent, "syncing", error))
689 }
690}
691
692impl Drop for StagedAtomicWrite {
693 fn drop(&mut self) {
694 if self.pending {
695 let _ = fs::remove_file(&self.tmp);
696 }
697 }
698}
699
700fn stage_file_atomic_impl(
701 path: &Path,
702 bytes: &[u8],
703 kind: AtomicWriteKind,
704 before_write: impl FnOnce(&File, &Path) -> io::Result<()>,
705) -> io::Result<StagedAtomicWrite> {
706 let parent = path.parent().unwrap_or_else(|| Path::new("."));
707 create_dir_all_durable(parent).map_err(|e| enrich_fs_error(parent, "creating", e))?;
708
709 let tmp = temp_path(path);
710 let inner = (|| -> io::Result<()> {
711 let mut file = kind.open_tmp(&tmp)?;
712 kind.enforce_before_write(&file)?;
713 before_write(&file, &tmp)?;
714 file.write_all(bytes)?;
715 sync_file(&file, &tmp)?;
716 Ok(())
717 })();
718
719 if let Err(err) = inner {
720 let _ = fs::remove_file(&tmp);
724 return Err(enrich_write_error(path, err));
725 }
726
727 Ok(StagedAtomicWrite {
728 path: path.to_path_buf(),
729 parent: parent.to_path_buf(),
730 tmp,
731 pending: true,
732 })
733}
734
735fn write_file_atomic_impl(
736 path: &Path,
737 bytes: &[u8],
738 kind: AtomicWriteKind,
739 before_write: impl FnOnce(&File, &Path) -> io::Result<()>,
740) -> io::Result<()> {
741 stage_file_atomic_impl(path, bytes, kind, before_write)?.publish()
742}
743
744pub fn write_file_atomic(path: &Path, bytes: &[u8]) -> io::Result<()> {
745 write_file_atomic_impl(path, bytes, AtomicWriteKind::Normal, |_, _| Ok(()))
746}
747
748pub fn create_private_dir_all(path: &Path) -> io::Result<()> {
760 #[cfg(unix)]
761 {
762 use std::os::unix::fs::DirBuilderExt;
763 let (missing, deepest_existing) = plan_missing_dirs(path);
764 let mut builder = fs::DirBuilder::new();
765 builder.recursive(true).mode(0o700);
766 match builder.create(path) {
767 Ok(()) => {}
768 Err(e) if e.kind() == io::ErrorKind::AlreadyExists => {}
769 Err(e) => return Err(e),
770 }
771 sync_new_dirents(&missing, deepest_existing.as_deref())
772 }
773 #[cfg(not(unix))]
774 {
775 create_dir_all_durable(path)
777 }
778}
779
780pub fn write_file_atomic_secret(path: &Path, bytes: &[u8]) -> io::Result<()> {
790 write_file_atomic_impl(path, bytes, AtomicWriteKind::Secret, |_, _| Ok(()))
791}
792
793pub fn stage_file_atomic_secret(path: &Path, bytes: &[u8]) -> io::Result<StagedAtomicWrite> {
794 stage_file_atomic_impl(path, bytes, AtomicWriteKind::Secret, |_, _| Ok(()))
795}
796
797fn fsync_file_data(path: &Path) -> io::Result<()> {
824 let file = OpenOptions::new()
825 .read(true)
826 .write(true)
827 .open(path)
828 .map_err(|e| enrich_fs_error(path, "opening", e))?;
829 sync_file(&file, path).map_err(|e| enrich_fs_error(path, "syncing", e))
830}
831
832pub fn publish_file_durable(src: &Path, dst: &Path) -> io::Result<()> {
833 let parent = dst.parent().unwrap_or_else(|| Path::new("."));
834 create_dir_all_durable(parent).map_err(|e| enrich_fs_error(parent, "creating", e))?;
835
836 fsync_file_data(src)?;
841
842 match fs::rename(src, dst) {
843 Ok(()) => {}
844 Err(e) if e.kind() == io::ErrorKind::AlreadyExists => {
845 let _ = fs::remove_file(src);
847 }
848 Err(e) if is_cross_device_link(&e) => {
849 publish_file_via_copy_durable(src, dst)?;
850 }
851 Err(e) => return Err(enrich_rename_error(src, dst, e)),
852 }
853
854 sync_directory(parent).map_err(|e| enrich_fs_error(parent, "syncing", e))
855}
856
857fn publish_file_via_copy_durable(src: &Path, dst: &Path) -> io::Result<()> {
861 let parent = dst.parent().unwrap_or_else(|| Path::new("."));
862 create_dir_all_durable(parent).map_err(|e| enrich_fs_error(parent, "creating", e))?;
863
864 let tmp = temp_path(dst);
865 let result = (|| -> io::Result<()> {
866 fs::copy(src, &tmp).map_err(|e| enrich_fs_error(&tmp, "writing", e))?;
867 fsync_file_data(&tmp)?;
868 fs::rename(&tmp, dst).map_err(|e| enrich_rename_error(&tmp, dst, e))?;
869 let _ = fs::remove_file(src);
870 Ok(())
871 })();
872 if result.is_err() {
873 let _ = fs::remove_file(&tmp);
874 }
875 result
876}
877
878#[cfg(test)]
879mod tests {
880 use super::*;
881
882 fn enospc_io_error() -> io::Error {
883 io::Error::from_raw_os_error(ENOSPC)
884 }
885
886 #[test]
887 fn is_out_of_space_detects_enospc_raw() {
888 assert!(is_out_of_space(&enospc_io_error()));
889 }
890
891 #[test]
892 fn is_out_of_space_detects_storage_full_kind() {
893 let err = io::Error::new(io::ErrorKind::StorageFull, "mock disk full");
894 assert!(is_out_of_space(&err));
895 }
896
897 #[test]
898 fn is_out_of_space_detects_write_zero() {
899 let err = io::Error::new(io::ErrorKind::WriteZero, "short write");
900 assert!(is_out_of_space(&err));
901 }
902
903 #[test]
904 fn is_out_of_space_rejects_unrelated_errors() {
905 assert!(!is_out_of_space(&io::Error::new(
906 io::ErrorKind::NotFound,
907 "missing"
908 )));
909 assert!(!is_out_of_space(&io::Error::new(
910 io::ErrorKind::PermissionDenied,
911 "nope"
912 )));
913 assert!(!is_out_of_space(&io::Error::other("generic")));
914 }
915
916 #[test]
917 fn is_directory_not_empty_detects_kind() {
918 let err = io::Error::new(io::ErrorKind::DirectoryNotEmpty, "still has children");
919 assert!(is_directory_not_empty(&err));
920 }
921
922 #[test]
923 fn is_directory_not_empty_detects_raw_codes() {
924 for code in [ENOTEMPTY_LINUX, ENOTEMPTY_MACOS, ENOTEMPTY_WINDOWS] {
925 assert!(
926 is_directory_not_empty(&io::Error::from_raw_os_error(code)),
927 "expected raw OS error {code} to classify as ENOTEMPTY"
928 );
929 }
930 }
931
932 #[test]
933 fn is_directory_not_empty_rejects_unrelated() {
934 assert!(!is_directory_not_empty(&io::Error::new(
935 io::ErrorKind::NotFound,
936 "missing"
937 )));
938 assert!(!is_directory_not_empty(&enospc_io_error()));
939 }
940
941 #[test]
942 fn is_permission_denied_detects_kind_and_raw() {
943 assert!(is_permission_denied(&io::Error::new(
944 io::ErrorKind::PermissionDenied,
945 "nope"
946 )));
947 assert!(is_permission_denied(&io::Error::from_raw_os_error(EACCES)));
948 }
949
950 #[test]
951 fn is_not_found_detects_kind_and_raw() {
952 assert!(is_not_found(&io::Error::new(
953 io::ErrorKind::NotFound,
954 "missing"
955 )));
956 assert!(is_not_found(&io::Error::from_raw_os_error(ENOENT)));
957 }
958
959 #[test]
960 fn is_read_only_filesystem_detects_raw() {
961 assert!(is_read_only_filesystem(&io::Error::from_raw_os_error(
962 EROFS
963 )));
964 }
965
966 #[test]
967 fn is_cross_device_link_detects_raw() {
968 assert!(is_cross_device_link(&io::Error::from_raw_os_error(EXDEV)));
969 }
970
971 #[test]
972 fn enrich_fs_error_passes_through_unclassified() {
973 let path = Path::new("/tmp/example");
974 let original = io::Error::other("weird");
975 let wrapped = enrich_fs_error(path, "writing", original);
976 assert_eq!(wrapped.kind(), io::ErrorKind::Other);
978 assert_eq!(wrapped.to_string(), "weird");
979 }
980
981 #[test]
982 fn enrich_fs_error_wraps_enospc_with_path_and_recovery_hint() {
983 let path = Path::new("/repo/.heddle/state/abc.bin");
984 let wrapped = enrich_fs_error(path, "writing", enospc_io_error());
985
986 assert_eq!(wrapped.kind(), io::ErrorKind::StorageFull);
988 let msg = wrapped.to_string();
990 assert!(
991 msg.contains("out of disk space"),
992 "missing failure name: {msg}"
993 );
994 assert!(
995 msg.contains("/repo/.heddle/state/abc.bin"),
996 "missing path: {msg}"
997 );
998 assert!(
999 msg.contains("free disk space") && msg.contains("re-run"),
1000 "missing recovery hint: {msg}"
1001 );
1002 assert!(
1003 msg.contains("working tree is unchanged"),
1004 "missing reassurance: {msg}"
1005 );
1006 let src = std::error::Error::source(&wrapped as &dyn std::error::Error)
1009 .or_else(|| wrapped.get_ref().and_then(|e| e.source()))
1010 .expect("source preserved");
1011 assert!(src.to_string().to_lowercase().contains("space"));
1012 }
1013
1014 #[test]
1015 fn enrich_fs_error_wraps_enotempty_with_directory_message() {
1016 let path = Path::new("/repo/web");
1017 let wrapped = enrich_fs_error(
1018 path,
1019 "removing",
1020 io::Error::from_raw_os_error(ENOTEMPTY_MACOS),
1021 );
1022 assert_eq!(wrapped.kind(), io::ErrorKind::DirectoryNotEmpty);
1023 let msg = wrapped.to_string();
1024 assert!(
1025 msg.contains("could not remove directory"),
1026 "missing action: {msg}"
1027 );
1028 assert!(msg.contains("/repo/web"), "missing path: {msg}");
1029 assert!(
1030 msg.contains("heddle-ignored"),
1031 "missing heddle-ignored hint: {msg}"
1032 );
1033 assert!(
1034 msg.contains("leaving in place"),
1035 "missing reassurance: {msg}"
1036 );
1037 let src = wrapped.get_ref().and_then(|e| e.source()).expect("source");
1042 let original = src
1043 .downcast_ref::<io::Error>()
1044 .expect("original io::Error preserved");
1045 assert_eq!(original.raw_os_error(), Some(ENOTEMPTY_MACOS));
1046 }
1047
1048 #[test]
1049 fn enrich_fs_error_wraps_eacces_with_op_and_path() {
1050 let path = Path::new("/repo/.heddle/state/index.bin");
1051 let wrapped = enrich_fs_error(path, "writing", io::Error::from_raw_os_error(EACCES));
1052 assert_eq!(wrapped.kind(), io::ErrorKind::PermissionDenied);
1053 let msg = wrapped.to_string();
1054 assert!(msg.starts_with("permission denied writing"), "msg: {msg}");
1055 assert!(msg.contains("/repo/.heddle/state/index.bin"), "msg: {msg}");
1056 assert!(msg.contains("check filesystem permissions"), "msg: {msg}");
1057 }
1058
1059 #[test]
1060 fn enrich_fs_error_wraps_enoent_with_op_and_path() {
1061 let path = Path::new("/repo/.heddle");
1062 let wrapped = enrich_fs_error(path, "opening", io::Error::from_raw_os_error(ENOENT));
1063 assert_eq!(wrapped.kind(), io::ErrorKind::NotFound);
1064 let msg = wrapped.to_string();
1065 assert!(msg.contains("could not find"), "missing action: {msg}");
1066 assert!(msg.contains("/repo/.heddle"), "missing path: {msg}");
1067 assert!(msg.contains("for opening"), "missing op: {msg}");
1068 }
1069
1070 #[test]
1071 fn enrich_fs_error_wraps_erofs_with_path() {
1072 let path = Path::new("/mnt/readonly/.heddle/state/index.bin");
1073 let wrapped = enrich_fs_error(path, "writing", io::Error::from_raw_os_error(EROFS));
1074 assert_eq!(wrapped.kind(), io::ErrorKind::ReadOnlyFilesystem);
1075 let msg = wrapped.to_string();
1076 assert!(msg.contains("filesystem is read-only"), "msg: {msg}");
1077 assert!(
1078 msg.contains("/mnt/readonly/.heddle/state/index.bin"),
1079 "msg: {msg}"
1080 );
1081 assert!(msg.contains("cannot be modified"), "msg: {msg}");
1082 }
1083
1084 #[test]
1085 fn enrich_rename_error_wraps_exdev_with_src_and_dst() {
1086 let src = Path::new("/tmp-mount/.x.tmp-1234");
1087 let dst = Path::new("/repo/.heddle/state/index.bin");
1088 let wrapped = enrich_rename_error(src, dst, io::Error::from_raw_os_error(EXDEV));
1089 assert_eq!(wrapped.kind(), io::ErrorKind::CrossesDevices);
1090 let msg = wrapped.to_string();
1091 assert!(
1092 msg.contains("cannot rename across filesystems"),
1093 "msg: {msg}"
1094 );
1095 assert!(msg.contains("/tmp-mount/.x.tmp-1234"), "missing src: {msg}");
1096 assert!(
1097 msg.contains("/repo/.heddle/state/index.bin"),
1098 "missing dst: {msg}"
1099 );
1100 assert!(msg.contains("TMPDIR"), "missing recovery hint: {msg}");
1101 }
1102
1103 #[test]
1104 fn enrich_rename_error_falls_through_to_generic_for_other_kinds() {
1105 let src = Path::new("/tmp/.x.tmp");
1106 let dst = Path::new("/repo/file");
1107 let wrapped = enrich_rename_error(src, dst, io::Error::from_raw_os_error(EACCES));
1108 assert_eq!(wrapped.kind(), io::ErrorKind::PermissionDenied);
1111 let msg = wrapped.to_string();
1112 assert!(msg.starts_with("permission denied renaming"), "msg: {msg}");
1113 assert!(msg.contains("/repo/file"), "missing dst: {msg}");
1114 }
1115
1116 #[test]
1117 fn enrich_write_error_passes_through_non_enospc_unclassified() {
1118 let path = Path::new("/tmp/example");
1121 let original = io::Error::other("weird");
1122 let wrapped = enrich_write_error(path, original);
1123 assert_eq!(wrapped.kind(), io::ErrorKind::Other);
1124 assert_eq!(wrapped.to_string(), "weird");
1125 }
1126
1127 #[test]
1128 fn write_file_atomic_round_trip() {
1129 let dir = tempfile::TempDir::new().unwrap();
1130 let target = dir.path().join("nested/under/here/file.bin");
1131 write_file_atomic(&target, b"hello").unwrap();
1132 assert_eq!(fs::read(&target).unwrap(), b"hello");
1133 }
1134
1135 #[test]
1136 fn stage_temp_files_durable_writes_every_file_verbatim() {
1137 let dir = tempfile::TempDir::new().unwrap();
1141 let files: Vec<(PathBuf, Vec<u8>)> = (0..50)
1142 .map(|i| {
1143 (
1144 dir.path().join(format!("ref-{i}.tmp")),
1145 format!("change-id-{i}\n").into_bytes(),
1146 )
1147 })
1148 .collect();
1149
1150 stage_temp_files_durable(&files).unwrap();
1151
1152 for (path, bytes) in &files {
1153 assert_eq!(&fs::read(path).unwrap(), bytes, "mismatch at {path:?}");
1154 }
1155 }
1156
1157 #[test]
1158 fn stage_temp_files_durable_empty_batch_is_ok() {
1159 stage_temp_files_durable(&[]).unwrap();
1162 }
1163
1164 #[test]
1165 fn stage_temp_files_durable_errors_when_parent_missing() {
1166 let dir = tempfile::TempDir::new().unwrap();
1170 let files = vec![(dir.path().join("does/not/exist/ref.tmp"), b"x".to_vec())];
1171 assert!(stage_temp_files_durable(&files).is_err());
1172 }
1173
1174 #[cfg(unix)]
1175 #[test]
1176 fn create_private_dir_all_sets_0700() {
1177 use std::os::unix::fs::PermissionsExt;
1178
1179 let dir = tempfile::TempDir::new().unwrap();
1180 let target = dir.path().join("nested/private");
1181 create_private_dir_all(&target).expect("create private dir");
1182 let mode = fs::metadata(&target).unwrap().permissions().mode() & 0o777;
1183 assert_eq!(mode, 0o700, "new private dir must be 0700, got {mode:o}");
1184 let mid_mode = fs::metadata(dir.path().join("nested"))
1187 .unwrap()
1188 .permissions()
1189 .mode()
1190 & 0o777;
1191 assert_eq!(
1192 mid_mode, 0o700,
1193 "intermediate private ancestor must be 0700"
1194 );
1195 create_private_dir_all(&target).expect("idempotent private create");
1197 let mode_again = fs::metadata(&target).unwrap().permissions().mode() & 0o777;
1198 assert_eq!(mode_again, 0o700);
1199 }
1200
1201 #[cfg(unix)]
1202 #[test]
1203 fn write_file_atomic_secret_is_0600_before_write_and_after_rename() {
1204 use std::os::unix::fs::PermissionsExt;
1205
1206 let dir = tempfile::TempDir::new().unwrap();
1207 let target = dir.path().join("nested/secret.txt");
1208 let mut observed_tmp_mode = None;
1209
1210 write_file_atomic_impl(&target, b"secret", AtomicWriteKind::Secret, |file, tmp| {
1211 let fd_mode = file.metadata()?.permissions().mode() & 0o777;
1212 let path_mode = fs::metadata(tmp)?.permissions().mode() & 0o777;
1213 observed_tmp_mode = Some((fd_mode, path_mode));
1214 Ok(())
1215 })
1216 .unwrap();
1217
1218 assert_eq!(observed_tmp_mode, Some((0o600, 0o600)));
1219 let final_mode = fs::metadata(&target).unwrap().permissions().mode() & 0o777;
1220 assert_eq!(final_mode, 0o600);
1221 assert_eq!(fs::read(&target).unwrap(), b"secret");
1222 }
1223
1224 #[test]
1225 fn write_file_atomic_secret_cleans_up_when_pre_write_check_fails() {
1226 let dir = tempfile::TempDir::new().unwrap();
1227 let target = dir.path().join("secret.txt");
1228 let mut tmp_path = None;
1229
1230 let err = write_file_atomic_impl(&target, b"secret", AtomicWriteKind::Secret, |_, tmp| {
1231 tmp_path = Some(tmp.to_path_buf());
1232 Err(io::Error::new(
1233 io::ErrorKind::PermissionDenied,
1234 "injected permission failure",
1235 ))
1236 })
1237 .expect_err("permission failure should propagate");
1238
1239 assert!(is_permission_denied(&err), "unexpected error: {err}");
1240 assert!(!target.exists(), "secret write must not publish target");
1241 let tmp = tmp_path.expect("pre-write hook observed temp path");
1242 assert!(!tmp.exists(), "failed secret write should remove temp file");
1243 }
1244
1245 #[test]
1246 fn staged_secret_is_unpublished_until_publish() {
1247 let dir = tempfile::TempDir::new().unwrap();
1248 let target = dir.path().join("secret.txt");
1249 let staged = stage_file_atomic_secret(&target, b"secret").unwrap();
1250
1251 assert!(!target.exists());
1252 staged.publish().unwrap();
1253 assert_eq!(fs::read(target).unwrap(), b"secret");
1254 }
1255
1256 #[test]
1257 fn dropping_staged_secret_removes_temporary_file() {
1258 let dir = tempfile::TempDir::new().unwrap();
1259 let target = dir.path().join("secret.txt");
1260 let staged = stage_file_atomic_secret(&target, b"secret").unwrap();
1261 drop(staged);
1262
1263 assert!(!target.exists());
1264 assert_eq!(fs::read_dir(dir.path()).unwrap().count(), 0);
1265 }
1266
1267 #[test]
1276 fn sync_directory_succeeds_on_writable_tempdir() {
1277 let dir = tempfile::TempDir::new().unwrap();
1278 sync_directory(dir.path()).expect("sync_directory on writable tempdir");
1279 }
1280
1281 #[test]
1286 fn write_file_atomic_does_not_permission_deny_on_parent_sync() {
1287 let dir = tempfile::TempDir::new().unwrap();
1288 let target = dir.path().join("oplog/oplog.bin");
1289 let result = write_file_atomic(&target, b"hello");
1290 if let Err(e) = &result {
1291 assert!(
1292 !is_permission_denied(e),
1293 "write_file_atomic surfaced PermissionDenied on a writable \
1294 tempdir (heddle#105): {e}"
1295 );
1296 }
1297 result.expect("write_file_atomic");
1298 }
1299
1300 #[test]
1301 fn publish_file_durable_renames_and_removes_source() {
1302 let dir = tempfile::TempDir::new().unwrap();
1303 let src = dir.path().join("staged.pack");
1304 let dst = dir.path().join("objects/packs/final.pack");
1305 fs::write(&src, b"pack-bytes").unwrap();
1306
1307 publish_file_durable(&src, &dst).unwrap();
1308
1309 assert!(!src.exists(), "source must be consumed by publish");
1310 assert_eq!(fs::read(&dst).unwrap(), b"pack-bytes");
1311 }
1312
1313 #[test]
1317 fn publish_file_durable_syncs_source_without_permission_deny() {
1318 let dir = tempfile::TempDir::new().unwrap();
1319 let src = dir.path().join("staged.bin");
1320 let dst = dir.path().join("final.bin");
1321 fs::write(&src, b"need-fsync-before-rename").unwrap();
1322 let result = publish_file_durable(&src, &dst);
1323 if let Err(e) = &result {
1324 assert!(
1325 !is_permission_denied(e),
1326 "publish_file_durable PermissionDenied on source fsync: {e}"
1327 );
1328 }
1329 result.expect("publish_file_durable");
1330 assert_eq!(fs::read(&dst).unwrap(), b"need-fsync-before-rename");
1331 }
1332
1333 #[test]
1334 fn publish_file_via_copy_durable_never_writes_final_path_directly() {
1335 let dir = tempfile::TempDir::new().unwrap();
1341 let src = dir.path().join("staged.pack");
1342 let dst = dir.path().join("final.pack");
1343 fs::write(&dst, b"TORN-OLD-CONTENT!!!!!!!!!!!!!").unwrap();
1346 fs::write(&src, b"complete-new-pack-bytes").unwrap();
1347
1348 publish_file_via_copy_durable(&src, &dst).unwrap();
1349
1350 assert!(!src.exists(), "source must be removed after copy publish");
1351 assert_eq!(fs::read(&dst).unwrap(), b"complete-new-pack-bytes");
1352 let leftovers: Vec<_> = fs::read_dir(dir.path())
1354 .unwrap()
1355 .filter_map(|e| e.ok())
1356 .map(|e| e.file_name().to_string_lossy().into_owned())
1357 .filter(|name| name.contains(".tmp-"))
1358 .collect();
1359 assert!(
1360 leftovers.is_empty(),
1361 "durable copy must not leave temp siblings: {leftovers:?}"
1362 );
1363 }
1364
1365 #[test]
1366 fn publish_file_via_copy_durable_cleans_temp_when_rename_cannot_publish() {
1367 let dir = tempfile::TempDir::new().unwrap();
1372 let src = dir.path().join("staged.pack");
1373 let dst_dir = dir.path().join("final.pack");
1374 fs::write(&src, b"new-bytes").unwrap();
1375 fs::create_dir(&dst_dir).unwrap();
1379
1380 let err = publish_file_via_copy_durable(&src, &dst_dir).expect_err("rename over dir");
1381 assert!(
1382 err.kind() == io::ErrorKind::AlreadyExists
1383 || err.raw_os_error().is_some()
1384 || is_permission_denied(&err)
1385 || err.kind() == io::ErrorKind::Other
1386 || err.kind() == io::ErrorKind::IsADirectory
1387 || err.kind() == io::ErrorKind::DirectoryNotEmpty,
1388 "unexpected error kind for rename-over-dir: {err:?}"
1389 );
1390 assert!(src.exists(), "failed publish must leave source intact");
1391 assert!(dst_dir.is_dir(), "destination directory must be untouched");
1392 let leftovers: Vec<_> = fs::read_dir(dir.path())
1393 .unwrap()
1394 .filter_map(|e| e.ok())
1395 .map(|e| e.file_name().to_string_lossy().into_owned())
1396 .filter(|name| name.contains(".tmp-"))
1397 .collect();
1398 assert!(
1399 leftovers.is_empty(),
1400 "failed publish must clean temp siblings: {leftovers:?}"
1401 );
1402 }
1403
1404 #[test]
1405 fn publish_file_durable_propagates_non_exdev_rename_failures() {
1406 let dir = tempfile::TempDir::new().unwrap();
1411 let src = dir.path().join("staged.pack");
1412 let dst = dir.path().join("final.pack");
1413 fs::write(&src, b"pack-bytes").unwrap();
1414 fs::create_dir(&dst).unwrap();
1415
1416 let err = publish_file_durable(&src, &dst).expect_err("rename over directory");
1417 assert!(
1418 !is_cross_device_link(&err),
1419 "failure must not be misclassified as EXDEV: {err}"
1420 );
1421 assert!(src.exists());
1423 }
1424
1425 #[test]
1429 fn create_dir_all_durable_creates_nested_path() {
1430 let dir = tempfile::TempDir::new().unwrap();
1431 let shard = dir.path().join("blobs/ab");
1434 create_dir_all_durable(&shard).expect("create nested shard path");
1435 assert!(shard.is_dir(), "leaf shard directory must exist");
1436 assert!(
1437 dir.path().join("blobs").is_dir(),
1438 "intermediate grandparent must exist"
1439 );
1440 create_dir_all_durable(&shard).expect("idempotent durable create");
1442 assert!(shard.is_dir());
1443 }
1444
1445 #[test]
1449 fn write_file_atomic_creates_missing_shard_parents() {
1450 let dir = tempfile::TempDir::new().unwrap();
1451 let target = dir.path().join("blobs/ab/object.bin");
1452 write_file_atomic(&target, b"shard-bytes").unwrap();
1453 assert_eq!(fs::read(&target).unwrap(), b"shard-bytes");
1454 assert!(dir.path().join("blobs/ab").is_dir());
1455 }
1456
1457 #[test]
1459 fn create_dir_all_durable_ok_when_path_already_exists() {
1460 let dir = tempfile::TempDir::new().unwrap();
1461 let nested = dir.path().join("already/there");
1462 fs::create_dir_all(&nested).unwrap();
1463 create_dir_all_durable(&nested).expect("existing dir");
1464 assert!(nested.is_dir());
1465 }
1466}