Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions scripts/file_utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ FETCH_FILE() {
fi

LOG_INFO "Fetching $target_file from $(basename "$container") (Depth: $depth)"
LOG_DEBUG "Container: $container, Output dir: $out_dir"

local file_list
file_list="$(7z l "$container" 2>/dev/null)" || return 1
LOG_DEBUG "Files in container: $(echo "$file_list" | wc -l) entries"


if echo "$file_list" | awk '{print $NF}' | grep -Fxq "$target_file"; then
Expand All @@ -79,18 +81,53 @@ FETCH_FILE() {


if echo "$file_list" | awk '{print $NF}' | grep -Fxq "$target_file.lz4"; then
LOG_DEBUG "Found $target_file.lz4 in container, will extract and decompress"

# Check disk space before decompression
# Samsung firmware super.img files are typically 1-2GB compressed, expanding to 6-8GB uncompressed
# We check for 3GB because lz4 uses streaming decompression (doesn't need full output size at once)
# The decompressed data is written incrementally, so we need space for:
# - The compressed input being extracted from 7z (1-2GB)
# - A buffer for lz4 streaming decompression (~1-2GB)
# Total: ~3GB minimum to safely decompress
if ! CHECK_DISK_SPACE "$out_dir" 3; then
LOG_WARN "Low disk space before lz4 decompression. Attempting cleanup..."
LOG_DEBUG "Disk space check failed, cleaning up temporary files..."
# Try to free up space - only clean up script-created temporary files
find /tmp -type f -name "tmp_*" -user "$(whoami)" -delete 2>/dev/null || true
find "$out_dir" -type f -name "tmp_*" -delete 2>/dev/null || true

# Check again
if ! CHECK_DISK_SPACE "$out_dir" 3; then
LOG_ERROR "Insufficient disk space for lz4 decompression of $target_file.lz4"
return 1
fi
LOG_DEBUG "Cleanup successful, proceeding with extraction"
fi

local err_file
err_file="$(mktemp)"
LOG_DEBUG "Starting 7z extraction and lz4 decompression of $target_file.lz4"
if 7z x "$container" "$target_file.lz4" -so 2>"$err_file" \
| lz4 -d -c 2>>"$err_file" > "$out_path"; then
rm -f "$err_file"
LOG_DEBUG "Successfully extracted and decompressed $target_file.lz4"
[[ -s "$out_path" ]] && return 0
fi

# Show error if extraction failed
LOG_DEBUG "Extraction/decompression failed for $target_file.lz4"
if [[ -s "$err_file" ]]; then
LOG_WARN "7z/lz4 extraction error for $target_file.lz4:"
cat "$err_file" >&2
# Check if it's a disk space issue
# lz4 specific error format: "Error 70 : Write error : cannot write decoded block"
# Generic filesystem errors: "No space left on device"
# Pattern matches both lz4's specific format and generic disk errors
if grep -qiE "(Error [0-9]+ : Write error|cannot write decoded|no space left)" "$err_file"; then
LOG_ERROR "Disk space issue detected during lz4 decompression"
CHECK_DISK_SPACE "$out_dir" 1 || true
fi
fi
rm -f "$err_file" "$out_path"
fi
Expand Down
15 changes: 15 additions & 0 deletions scripts/logs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,21 @@ LOG_WARN() {
echo -e "${GRAY}[$(_TIMESTAMP)]${NC} ${YELLOW}[WARN]${NC} $msg"
}

LOG_ERROR() {
local msg="$1"
# Non-fatal error logging - logs error but doesn't terminate execution
# Use ERROR_EXIT for fatal errors that should stop the script
echo -e "${GRAY}[$(_TIMESTAMP)]${NC} ${RED}[ERROR]${NC} $msg" >&2
}

LOG_DEBUG() {
local msg="$1"
# Only log if DEBUG_BUILD is enabled
if [[ "${DEBUG_BUILD:-false}" == "true" ]]; then
echo -e "${GRAY}[$(_TIMESTAMP)]${NC} ${MAGENTA}[DEBUG]${NC} $msg"
fi
}


LOG() {
local msg="$1"
Expand Down
27 changes: 25 additions & 2 deletions scripts/unpack_fw.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ EXTRACT_ROM() {

EXTRACT_FIRMWARE "$m" "$c" "$type" || return 1
processed+="$fw_id "

# Clean up temporary files after each firmware extraction to free disk space
if IS_GITHUB_ACTIONS; then
LOG_INFO "Cleaning up temporary files after $type firmware extraction..."
# Remove any temporary extraction files
find "$WORKDIR" -type f -name "tmp_*" -delete 2>/dev/null || true
# Clean up old temporary files in /tmp (older than 5 minutes, owned by current user)
# 5 minutes threshold ensures files from current extraction are kept while old ones are removed
# User filter ensures we only clean up our own files, not affecting other processes
find /tmp -type f -name "tmp_*" -user "$(whoami)" -mmin +5 -delete 2>/dev/null || true
# Show disk space after cleanup
CHECK_DISK_SPACE "$WORKDIR" 1 || true
fi
done

return 0
Expand All @@ -56,6 +69,8 @@ EXTRACT_FIRMWARE() {
UNPACK_CONF="${work_model}/unpack.conf"
local target_partitions=(system product system_ext odm vendor_dlkm odm_dlkm system_dlkm vendor)

LOG_DEBUG "Starting EXTRACT_FIRMWARE for model=$model, csc=$csc, type=$fw_type"
LOG_DEBUG "odin_dir=$odin_dir, work_model=$work_model"

LOG "Checking $fw_type firmware.."

Expand Down Expand Up @@ -86,6 +101,11 @@ EXTRACT_FIRMWARE() {


LOG_INFO "Unpacking $model firmware.."

# Show current disk space status
if IS_GITHUB_ACTIONS; then
CHECK_DISK_SPACE "$work_model" 1 || true
fi

# Check disk space before extraction (need at least 6GB for firmware extraction)
if ! CHECK_DISK_SPACE "$work_model" 10; then
Expand All @@ -96,8 +116,8 @@ EXTRACT_FIRMWARE() {
LOG_WARN "Insufficient disk space for firmware extraction"
if IS_GITHUB_ACTIONS; then
LOG_INFO "Attempting to free up space..."
# Clean up any temporary files
rm -rf /tmp/* 2>/dev/null || true
# Clean up any temporary files - only script-created files
find /tmp -type f -name "tmp_*" -user "$(whoami)" -delete 2>/dev/null || true
find "$WORKDIR" -type f -name "tmp_*" -delete 2>/dev/null || true
# Try again
if ! CHECK_DISK_SPACE "$work_model" 6; then
Expand All @@ -111,9 +131,12 @@ EXTRACT_FIRMWARE() {
mkdir -p "$work_model"

local super_img="${work_model}/super.img"

LOG_DEBUG "Extracting super.img from $ap_file to $work_model"

FETCH_FILE "$ap_file" "super.img" "$work_model" >/dev/null || {
rm -f "$UNPACK_CONF" "${work_model}/.extraction_complete"
LOG_DEBUG "FETCH_FILE failed for super.img"
ERROR_EXIT "Failed to extract super.img from $ap_file"
return 1
}
Expand Down