12345678module snapshot910import os11import src.storage1213141516const restoring_key = 'restoring'1718pub const restore_unfinished = 'an earlier restore stopped before all its files were written — run koh revert to finish it (it records the tree as it is in a checkpoint first)'1920pub fn restore_pending(store &storage.Store) bool {21 return store.state(restoring_key) != none22}23242526pub fn mark_restore(store &storage.Store) ! {27 store.set_state(restoring_key, 'yes')!28}2930pub fn clear_restore_mark(store &storage.Store) ! {31 store.remove_state(restoring_key)!32}3334pub struct RestoreReport {35pub:36 written int37 removed int38}3940414243444546pub fn restore_files(store &storage.Store, root string, ig Ignore, target []FileEntry, remove []string) !RestoreReport {47 48 in_repo := root == store.root49 report := write_tree(store, root, ig, target, remove) or {50 51 if in_repo {52 return error('${err.msg()} — the restore stopped part way; fix that, then run koh revert to finish it')53 }54 return err55 }56 if in_repo {57 clear_restore_mark(store)!58 }59 return report60}6162fn write_tree(store &storage.Store, root string, ig Ignore, target []FileEntry, remove []string) !RestoreReport {63 mut wanted := map[string]bool{}64 for entry in target {65 wanted[entry.path] = true66 }67 mut removed := 068 for path in remove {69 full := os.join_path(root, path)70 if !os.is_file(full) || ig.matches(path, false) || path in wanted {71 continue72 }73 os.rm(full)!74 removed++75 prune_empty_dirs(root, os.dir(full))76 }77 78 79 80 for entry in target {81 full := os.join_path(root, entry.path)82 if os.is_dir(full) {83 os.rmdir(full) or {84 return error('${entry.path} is a folder of ignored files where a saved file belongs — move it')85 }86 }87 }88 mut written := 089 cache := store.load_cache()!90 for entry in target {91 if ig.matches(entry.path, false) && os.exists(os.join_path(root, entry.path)) {92 continue93 }94 95 96 if already_there(root, entry, cache) {97 continue98 }99 data := store.get_object(entry.hash)!100 write_file(os.join_path(root, entry.path), data, entry.executable)!101 written++102 }103 return RestoreReport{104 written: written105 removed: removed106 }107}108109110111112fn already_there(root string, entry FileEntry, cache map[string]storage.CacheRow) bool {113 full := os.join_path(root, entry.path)114 st := lstat(full) or { return false }115 if st.kind != .file || st.size != entry.size || is_executable(st.mode) != entry.executable {116 return false117 }118 if row := cache[entry.path] {119 if row.size == st.size && row.mtime_ns == st.mtime_ns && row.ctime_ns == st.ctime_ns {120 return row.hash == entry.hash121 }122 }123 data := os.read_bytes(full) or { return false }124 return storage.hash_hex(data) == entry.hash125}126127128129130131pub fn check_links(root string, files []FileEntry) ! {132 for file in files {133 mut folder := ''134 for part in file.path.split('/')#[..-1] {135 folder = if folder == '' { part } else { '${folder}/${part}' }136 st := lstat(os.join_path(root, folder)) or { break }137 if st.kind == .symlink {138 return error('${folder} is a symbolic link where a saved folder belongs — koh will not write through it; move it, then run the command again')139 }140 }141 }142}143144fn write_file(path string, data []u8, executable bool) ! {145 os.mkdir_all(os.dir(path), os.MkdirParams{})!146 tmp := '${path}.koh-tmp-${os.getpid()}'147 os.write_file_array(tmp, data)!148 mode := if executable { 0o755 } else { 0o644 }149 os.chmod(tmp, mode)!150 os.rename(tmp, path)!151}152153154fn prune_empty_dirs(root string, dir string) {155 mut current := dir156 for current.len > root.len && current.starts_with(root) {157 if (os.ls(current) or { return }).len > 0 {158 return159 }160 os.rmdir(current) or { return }161 current = os.dir(current)162 }163}