Skip to content

Commit ba026da

Browse files
committed
update: simplify archive extraction for cross-platform compatibility in restore commands
- Replaced decompression commands with direct file reading for improved BSD/GNU tar support. - Adjusted handling of encrypted archives to decrypt into temporary files before extraction. - Enhanced logging for archive extraction steps.
1 parent 4bae5e7 commit ba026da

2 files changed

Lines changed: 30 additions & 21 deletions

File tree

commands/restore-full.cmd

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -860,27 +860,40 @@ function restoreSourceCode() {
860860

861861
mkdir -p "$target_dir"
862862

863-
local decompress_cmd="cat"
864-
case "$src_file" in
865-
*.tar.gz*) decompress_cmd="gzip -dc" ;;
866-
*.tar.xz*) decompress_cmd="xz -dc" ;;
867-
*.tar.lz4*) decompress_cmd="lz4 -dc" ;;
868-
esac
869-
863+
# Use direct file reading instead of piping for cross-platform compatibility
864+
# BSD tar (macOS) doesn't reliably handle piped stdin with -C flag
865+
# Both BSD and GNU tar auto-detect compression format with -xf
870866
if [[ $is_encrypted == true ]]; then
871867
if [[ -z "$RESTORE_DECRYPT" ]]; then
872868
logMessage ERROR "Encrypted source archive found but no decryption password provided"
873869
return 1
874870
fi
875-
if echo "$RESTORE_DECRYPT" | gpg --batch --yes --quiet --passphrase-fd 0 --decrypt "$src_file" | $decompress_cmd | tar -xf - -C "$target_dir" 2>/dev/null; then
876-
logMessage SUCCESS "Source code restored"
877-
return 0
871+
# Decrypt to temp file first, then extract directly
872+
local temp_file="$backup_path/source_decrypted.tar"
873+
case "$src_file" in
874+
*.tar.gz.gpg) temp_file="$backup_path/source_decrypted.tar.gz" ;;
875+
*.tar.xz.gpg) temp_file="$backup_path/source_decrypted.tar.xz" ;;
876+
*.tar.lz4.gpg) temp_file="$backup_path/source_decrypted.tar.lz4" ;;
877+
esac
878+
879+
if echo "$RESTORE_DECRYPT" | gpg --batch --yes --quiet --passphrase-fd 0 --decrypt "$src_file" > "$temp_file"; then
880+
if tar -xf "$temp_file" -C "$target_dir"; then
881+
rm -f "$temp_file"
882+
logMessage SUCCESS "Source code restored"
883+
return 0
884+
else
885+
rm -f "$temp_file"
886+
logMessage ERROR "Failed to extract source code"
887+
return 1
888+
fi
878889
else
879-
logMessage ERROR "Failed to restore source code"
890+
rm -f "$temp_file" 2>/dev/null
891+
logMessage ERROR "Failed to decrypt source archive"
880892
return 1
881893
fi
882894
else
883-
if $decompress_cmd "$src_file" | tar -xf - -C "$target_dir" 2>/dev/null; then
895+
# Direct extraction - works on both BSD tar (macOS) and GNU tar (Linux)
896+
if tar -xf "$src_file" -C "$target_dir"; then
884897
logMessage SUCCESS "Source code restored"
885898
return 0
886899
else

commands/restore.cmd

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -299,16 +299,12 @@ function extractBackupArchive() {
299299

300300
mkdir -p "$extract_dir"
301301

302-
# Determine decompression command based on file extension
303-
local decompress_cmd="cat"
304-
case "$archive_file" in
305-
*.tar.gz) decompress_cmd="gzip -d" ;;
306-
*.tar.xz) decompress_cmd="xz -d" ;;
307-
*.tar.lz4) decompress_cmd="lz4 -d" ;;
308-
esac
309-
logVerbose "Using decompression command: $decompress_cmd"
302+
# Use direct file reading for cross-platform compatibility
303+
# BSD tar (macOS) doesn't reliably handle piped stdin with -C flag
304+
# Both BSD and GNU tar auto-detect compression format with -xf
305+
logVerbose "Extracting archive using direct file reading"
310306

311-
if $decompress_cmd < "$archive_file" | tar -xf - -C "$extract_dir" --strip-components=1 2>/dev/null; then
307+
if tar -xf "$archive_file" -C "$extract_dir" --strip-components=1; then
312308
logVerbose "Successfully extracted archive to: $extract_dir"
313309
echo "$extract_dir"
314310
return 0

0 commit comments

Comments
 (0)