#!/usr/bin/env bash # # extract_all.sh — Rebuild the original HERCULES dataset tree from its .tar.zst archives. # # Each per-robot / per-result folder in this dataset is stored as a .tar.zst archive # (raw loose files would exceed the Hugging Face 10,000-files-per-folder limit). # This script extracts every archive *in place*, reproducing the exact original # directory layout, with original permissions and timestamps preserved. # # It is self-contained: it finds archives relative to its own location, so just run it # from inside your downloaded copy of the dataset. # # Usage: # ./extract_all.sh # extract everything; SKIP archives already extracted # ./extract_all.sh --force # re-extract even if the target folder already exists # ./extract_all.sh --clean # delete each .tar.zst after it extracts successfully # ./extract_all.sh --dry-run # show what would happen, change nothing # ./extract_all.sh --help # # Requirements: tar + zstd (Debian/Ubuntu: sudo apt install zstd) # set -u FORCE=0; CLEAN=0; DRY=0 for a in "$@"; do case "$a" in --force) FORCE=1 ;; --clean) CLEAN=1 ;; --dry-run) DRY=1 ;; -h|--help) grep '^#' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'; exit 0 ;; *) echo "ERROR: unknown argument: $a (use --help)"; exit 2 ;; esac done command -v tar >/dev/null 2>&1 || { echo "ERROR: 'tar' not found"; exit 3; } command -v zstd >/dev/null 2>&1 || { echo "ERROR: 'zstd' not found (sudo apt install zstd)"; exit 3; } ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" echo "=== HERCULES extract_all ===" echo "Dataset root : $ROOT" echo "Mode : $([ $DRY -eq 1 ] && echo 'DRY-RUN ')$([ $FORCE -eq 1 ] && echo 'FORCE ')$([ $CLEAN -eq 1 ] && echo 'CLEAN ')$([ $FORCE -eq 0 ] && echo '(skip already-extracted)')" echo OK=0; SKIP=0; FAIL=0 # Find every .tar.zst under the dataset root (handles spaces in folder names). while IFS= read -r -d '' arc; do dir="$(dirname "$arc")" # extract here -> reproduces original tree base="$(basename "$arc")" target_name="${base%.tar.zst}" # archive Drone1.tar.zst -> folder Drone1 target="$dir/$target_name" rel="${arc#$ROOT/}" # skip if already extracted (unless --force) if [ "$FORCE" -eq 0 ] && [ -d "$target" ] && [ -n "$(find "$target" -mindepth 1 -print -quit 2>/dev/null)" ]; then echo " [SKIP ] already extracted: $rel" SKIP=$((SKIP+1)); continue fi if [ "$DRY" -eq 1 ]; then echo " [DRY ] would extract $rel -> $dir/" OK=$((OK+1)); continue fi # verify integrity before touching anything (real run only) if ! zstd -t "$arc" >/dev/null 2>&1; then echo " [FAIL ] integrity check failed, NOT extracting: $rel" FAIL=$((FAIL+1)); continue fi # extract preserving permissions + timestamps (no -m: keep stored mtimes) if tar --use-compress-program="zstd -d" -xpf "$arc" -C "$dir"; then echo " [ OK ] $rel" OK=$((OK+1)) [ "$CLEAN" -eq 1 ] && rm -f "$arc" && echo " (removed archive)" else echo " [FAIL ] extraction error: $rel" FAIL=$((FAIL+1)) fi done < <(find "$ROOT" -type f -name '*.tar.zst' -print0 | sort -z) echo echo "=== Summary ===" echo "Extracted : $OK" echo "Skipped : $SKIP (already present; use --force to re-extract)" echo "Failed : $FAIL" [ "$FAIL" -gt 0 ] && exit 1 echo "Done. The original folder structure has been restored." exit 0