File size: 3,221 Bytes
6a65016
 
 
 
 
 
 
 
 
 
2db6e5a
6a65016
 
 
 
2db6e5a
 
 
 
 
 
 
 
 
6a65016
 
 
 
 
 
 
 
 
 
 
 
2db6e5a
6a65016
2db6e5a
 
 
 
 
 
 
 
 
 
 
 
6a65016
 
 
 
2db6e5a
 
 
 
 
 
6a65016
2db6e5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a65016
2db6e5a
6a65016
 
2db6e5a
6a65016
 
2db6e5a
6a65016
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env bash
set -euo pipefail

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
ARCHIVE_DIR="${ROOT_DIR}/data"
DATA_ROOT="${1:-${ROOT_DIR}/data}"

mkdir -p "${DATA_ROOT}"

declare -A selected_archives=()
declare -A selected_archive_parts=()

while IFS= read -r archive; do
  filename="$(basename "${archive}")"
  base="${filename}"
  if [[ "${filename}" == *.tar.zst.part-* ]]; then
    base="${filename%%.part-*}.tar.zst"
    selected_archive_parts["${base}"]+="${archive}"$'\n'
    continue
  elif [[ "${filename}" == *.tar.part-* ]]; then
    base="${filename%%.part-*}.tar"
    selected_archive_parts["${base}"]+="${archive}"$'\n'
    continue
  elif [[ "${filename}" == *.tar.zst ]]; then
    base="${filename%.tar.zst}"
  elif [[ "${filename}" == *.tar ]]; then
    base="${filename%.tar}"
  else
    continue
  fi

  if [[ -n "${selected_archives[${base}]:-}" ]]; then
    echo "found multiple archive variants for ${base} under ${ARCHIVE_DIR}; keep only one of .tar or .tar.zst" >&2
    exit 1
  fi
  selected_archives["${base}"]="${archive}"
done < <(find "${ARCHIVE_DIR}" -maxdepth 1 \( -type f -o -type l \) \( -name '*.tar' -o -name '*.tar.zst' \) | sort)

while IFS= read -r archive; do
  filename="$(basename "${archive}")"
  if [[ "${filename}" == *.tar.zst.part-* ]]; then
    base="${filename%%.part-*}.tar.zst"
    selected_archive_parts["${base}"]+="${archive}"$'\n'
  elif [[ "${filename}" == *.tar.part-* ]]; then
    base="${filename%%.part-*}.tar"
    selected_archive_parts["${base}"]+="${archive}"$'\n'
  fi
done < <(find "${ARCHIVE_DIR}" -maxdepth 1 \( -type f -o -type l \) \( -name '*.tar.zst.part-*' -o -name '*.tar.part-*' \) | sort)

if [[ "${#selected_archives[@]}" -eq 0 && "${#selected_archive_parts[@]}" -eq 0 ]]; then
  echo "no archives found under ${ARCHIVE_DIR}" >&2
  exit 1
fi

mapfile -t archive_bases < <(
  {
    printf '%s\n' "${!selected_archives[@]}"
    printf '%s\n' "${!selected_archive_parts[@]}"
  } | awk 'NF' | sort -u
)
for base in "${archive_bases[@]}"; do
  archive="${selected_archives[${base}]:-}"
  parts_blob="${selected_archive_parts[${base}]:-}"
  if [[ -n "${archive}" && -n "${parts_blob}" ]]; then
    echo "found both full archive and split parts for ${base} under ${ARCHIVE_DIR}; keep only one form" >&2
    exit 1
  fi
  if [[ -n "${archive}" ]]; then
    echo "extracting ${archive}"
    case "${archive}" in
      *.tar.zst)
        tar --zstd -xf "${archive}" -C "${DATA_ROOT}"
        ;;
      *.tar)
        tar -xf "${archive}" -C "${DATA_ROOT}"
        ;;
      *)
        echo "unsupported archive format: ${archive}" >&2
        exit 1
        ;;
    esac
    continue
  fi

  if [[ -z "${parts_blob}" ]]; then
    echo "missing archive content for ${base}" >&2
    exit 1
  fi
  mapfile -t parts < <(printf '%s' "${parts_blob}" | awk 'NF' | sort)
  echo "extracting ${base} from ${#parts[@]} parts"
  case "${base}" in
    *.tar.zst)
      cat "${parts[@]}" | tar --zstd -xf - -C "${DATA_ROOT}"
      ;;
    *.tar)
      cat "${parts[@]}" | tar -xf - -C "${DATA_ROOT}"
      ;;
    *)
      echo "unsupported split archive format: ${base}" >&2
      exit 1
      ;;
  esac
done

echo "raw data extracted into ${DATA_ROOT}"