]> git.scottworley.com Git - annex-ec/blob - annex-ec-recover
tests: Factor out has_been_deleted()
[annex-ec] / annex-ec-recover
1 #!/usr/bin/env bash
2
3 # annex-ec: Use erasure codes for more efficient storage use in git-annex
4 # Copyright (C) 2026 Scott Worley
5
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Affero General Public License as
8 # published by the Free Software Foundation, either version 3 of the
9 # License, or (at your option) any later version.
10
11 set -euo pipefail
12
13 die() {
14 echo "$*" >&2
15 exit 1
16 }
17
18 find_recovery_set_files() {
19 local found
20 found=false
21 recovery_set_files=()
22 while IFS= read -r line;do
23 if [[ "$line" == ' '* ]];then
24 recovery_set_files+=( "${line# }" )
25 if [[ "${line# }" == "$1" ]];then
26 found=true
27 fi
28 else
29 if $found;then
30 break
31 fi
32 recovery_set_name=$line
33 recovery_set_files=()
34 fi
35 done < ec/.meta
36 if ! $found;then
37 die "Could not find a recovery set containing file $1"
38 fi
39 }
40
41 while [[ "$1" == -- ]];do shift; done
42
43 for target;do
44 if [[ -e "$target" ]];then continue; fi
45 set +e
46 git annex get "$target"
47 set -e
48 if [[ -e "$target" ]];then continue; fi
49
50 find_recovery_set_files "$target"
51
52 set +e
53 git annex get "${recovery_set_files[@]}" ec/"$recovery_set_name"*
54 set -e
55
56 # par2 doesn't like directory structure, so we work from a temp dir where we can flatten everything
57 srcdir=$PWD
58 flat="$(mktemp -d)"
59 pushd "$flat"
60
61 for f in "${recovery_set_files[@]}";do
62 ln -s "$srcdir"/"$f" .
63 done
64 ln -s "$srcdir"/ec/"$recovery_set_name"* .
65
66 # We have to remove the broken symlinks or par2 gets confused trying to write to them
67 find -L . -type l -exec rm -v {} +
68
69 par2 r -- *.par2
70
71 popd
72 for f in "${recovery_set_files[@]}";do
73 git annex reinject "$flat"/"$(basename "$f")" "$f"
74 done
75 done