-
Notifications
You must be signed in to change notification settings - Fork 2
/
pelf-dwfs_extract
executable file
·85 lines (71 loc) · 2.18 KB
/
pelf-dwfs_extract
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
#!/bin/sh
set -e
# Function to display usage information
usage() {
echo "Usage: $0 [--base64] input_file output_directory"
}
# Parse command line options
RAW_MODE=true # Default mode is now raw mode
while [ "$#" -ne 0 ]; do
case "$1" in
--base64)
RAW_MODE=false # Switch to base64 mode if --base64 is specified
;;
*)
break
;;
esac
shift
done
# Check if the required arguments are provided
if [ $# -ne 2 ]; then
usage
exit 1
fi
INPUT_FILE="$1"
OUTPUT_DIR="$2"
# Create a temporary directory for the extraction process
_VAR_BUNDLE_DIR="${TMPDIR:-/tmp}/.pelf_extract"
_VAR_BWORK_DIR="${_VAR_BUNDLE_DIR}/pbundle_${INPUT_FILE##*/}_$(date '+%s%M%S')" # Temporary work directory
_VAR_ARCHIVE="$_VAR_BWORK_DIR/archive.dwfs" # Path to the DwarFS archive
mkdir -p "$_VAR_BWORK_DIR"
# Cleanup function to remove the temporary directories
cleanup() {
rm -rf "$_VAR_BUNDLE_DIR" 2>/dev/null || true
}
trap cleanup INT TERM HUP QUIT EXIT
# Find the line number where the archive starts
ARCHIVE_MARKER=$(awk '/^__ARCHIVE_MARKER__/ { print NR + 1; exit }' "$INPUT_FILE")
# Check if the archive marker was found
if [ -z "$ARCHIVE_MARKER" ]; then
echo "Error: Archive marker not found in the input file." >&2
exit 1
fi
# Ensure the output directory exists
mkdir -p "$OUTPUT_DIR"
# Extract the archive based on whether --base64 is used
if $RAW_MODE; then
# Extract the archive directly (no decoding)
tail -n +$ARCHIVE_MARKER "$INPUT_FILE" >"$_VAR_ARCHIVE" || {
echo "Error: Failed to extract raw DwarfFS archive." >&2
exit 1
}
else
# Decode the base64-encoded archive before extracting
tail -n +$ARCHIVE_MARKER "$INPUT_FILE" | base64 -d >"$_VAR_ARCHIVE" || {
echo "Error: Failed to decode and extract DwarFS archive." >&2
exit 1
}
fi
# Check if the archive was extracted and exists
if [ -f "$_VAR_ARCHIVE" ]; then
# Try extracting the DwarFS archive using dwarfsextract
dwarfsextract --input "$_VAR_ARCHIVE" --output "$OUTPUT_DIR" || {
echo "Error: Failed to extract the DwarFS archive. Please check if the file is valid." >&2
exit 1
}
else
echo "Error: DwarFS archive not found after extraction." >&2
exit 1
fi
echo "Files extracted successfully to $OUTPUT_DIR"