Skip to content

Commit c0da8a3

Browse files
committed
fix patch function
1 parent dcc54f7 commit c0da8a3

File tree

2 files changed

+48
-100
lines changed

2 files changed

+48
-100
lines changed

qbittorrent-nox-static.sh

Lines changed: 24 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,45 +1709,26 @@ _apply_patches() {
17091709
patch_dir="${qbt_install_dir}/patches/${app_name}/${app_version[${app_name}]}"
17101710
patch_file="${patch_dir}/patch"
17111711

1712-
# Helper function to check if patch_dir has valid patch files (non-zero bytes)
1713-
_has_valid_patch_files() {
1714-
local dir="${1}"
1715-
[[ ! -d ${dir} ]] && return 1
1716-
1717-
# Check for valid patch files: patch, url, *.patch, *.diff
1718-
local found_valid=false
1719-
1720-
# Check main patch file
1721-
[[ -f "${dir}/patch" && -s "${dir}/patch" ]] && found_valid=true
1722-
1723-
# Check url file
1724-
[[ -f "${dir}/url" && -s "${dir}/url" ]] && found_valid=true
1725-
1726-
# Check *.patch and *.diff files
1727-
for file in "${dir}"/*.{patch,diff}; do
1728-
[[ -f ${file} && -s ${file} ]] && found_valid=true && break
1729-
done
1730-
1731-
[[ ${found_valid} == true ]]
1732-
}
1733-
1734-
# Helper function to check if patch_dir exists but is empty or has only 0-byte files
1735-
_is_patch_dir_empty() {
1736-
local dir="${1}"
1737-
[[ ! -d ${dir} ]] && return 0 # Non-existent = empty
1738-
1739-
# Directory exists, check if it has any non-zero files we care about
1740-
local has_content=false
1712+
# Helper function to check patch directory status
1713+
_check_patch_files() {
1714+
local dir="${1}" mode="${2:-has_valid}"
1715+
[[ ! -d ${dir} ]] && { [[ ${mode} == "is_empty" ]] && return 0 || return 1; }
17411716

17421717
# Check for any valid patch files
17431718
for file in "${dir}/patch" "${dir}/url" "${dir}"/*.{patch,diff}; do
1744-
[[ -f ${file} && -s ${file} ]] && has_content=true && break
1719+
if [[ -f ${file} && -s ${file} ]]; then
1720+
[[ ${mode} == "has_valid" ]] && return 0 || return 1
1721+
fi
17451722
done
17461723

1747-
# Return 0 (true) if empty, 1 (false) if has content
1748-
[[ ${has_content} == false ]]
1724+
# No valid files found
1725+
[[ ${mode} == "is_empty" ]] && return 0 || return 1
17491726
}
17501727

1728+
# Wrapper functions for clarity
1729+
_has_valid_patch_files() { _check_patch_files "${1}" "has_valid"; }
1730+
_is_patch_dir_empty() { _check_patch_files "${1}" "is_empty"; }
1731+
17511732
# Process local patches - never overwrites/updates files, only user does this
17521733
_process_local_patches() {
17531734
local has_content=false
@@ -1768,18 +1749,13 @@ _apply_patches() {
17681749
patch_url="$(< "${patch_dir}/url")"
17691750

17701751
if _curl "${patch_url}" -o "${tmp_patch}"; then
1771-
if [[ ${has_content} == true ]]; then
1772-
printf '\n\n# Merged from URL: %s\n' "${patch_url}" >> "${temp_patch}"
1773-
else
1774-
printf '# Downloaded from URL: %s\n' "${patch_url}" > "${temp_patch}"
1775-
fi
1752+
[[ ${has_content} == true ]] && printf '\n\n# Merged from URL: %s\n' "${patch_url}" >> "${temp_patch}" || printf '# Downloaded from URL: %s\n' "${patch_url}" > "${temp_patch}"
17761753
cat "${tmp_patch}" >> "${temp_patch}"
1777-
rm -f "${tmp_patch}"
17781754
has_content=true
17791755
else
17801756
printf '%b\n' " ${unicode_yellow_circle} Failed to download from URL: ${patch_url}"
1781-
rm -f "${tmp_patch}"
17821757
fi
1758+
rm -f "${tmp_patch}"
17831759
fi
17841760

17851761
# Step 3: Merge any *.patch or *.diff files last
@@ -1788,17 +1764,15 @@ _apply_patches() {
17881764
[[ -f ${patch_src} && -s ${patch_src} && ${patch_src} != "${patch_file}" ]] && additional_patches+=("${patch_src}")
17891765
done
17901766

1791-
if [[ ${#additional_patches[@]} -gt 0 ]]; then
1792-
for patch_src in "${additional_patches[@]}"; do
1793-
if [[ ${has_content} == true ]]; then
1794-
printf '\n\n# Merged from: %s\n' "${patch_src##*/}" >> "${temp_patch}"
1795-
else
1796-
printf '# From: %s\n' "${patch_src##*/}" > "${temp_patch}"
1797-
has_content=true
1798-
fi
1799-
cat "${patch_src}" >> "${temp_patch}"
1800-
done
1801-
fi
1767+
for patch_src in "${additional_patches[@]}"; do
1768+
if [[ ${has_content} == true ]]; then
1769+
printf '\n\n# Merged from: %s\n' "${patch_src##*/}" >> "${temp_patch}"
1770+
else
1771+
printf '# From: %s\n' "${patch_src##*/}" > "${temp_patch}"
1772+
has_content=true
1773+
fi
1774+
cat "${patch_src}" >> "${temp_patch}"
1775+
done
18021776

18031777
# Final validation and atomic move
18041778
if [[ ${has_content} == true && -s ${temp_patch} ]]; then

qbt-nox-static.bash

Lines changed: 24 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,45 +1709,26 @@ _apply_patches() {
17091709
patch_dir="${qbt_install_dir}/patches/${app_name}/${app_version[${app_name}]}"
17101710
patch_file="${patch_dir}/patch"
17111711

1712-
# Helper function to check if patch_dir has valid patch files (non-zero bytes)
1713-
_has_valid_patch_files() {
1714-
local dir="${1}"
1715-
[[ ! -d ${dir} ]] && return 1
1716-
1717-
# Check for valid patch files: patch, url, *.patch, *.diff
1718-
local found_valid=false
1719-
1720-
# Check main patch file
1721-
[[ -f "${dir}/patch" && -s "${dir}/patch" ]] && found_valid=true
1722-
1723-
# Check url file
1724-
[[ -f "${dir}/url" && -s "${dir}/url" ]] && found_valid=true
1725-
1726-
# Check *.patch and *.diff files
1727-
for file in "${dir}"/*.{patch,diff}; do
1728-
[[ -f ${file} && -s ${file} ]] && found_valid=true && break
1729-
done
1730-
1731-
[[ ${found_valid} == true ]]
1732-
}
1733-
1734-
# Helper function to check if patch_dir exists but is empty or has only 0-byte files
1735-
_is_patch_dir_empty() {
1736-
local dir="${1}"
1737-
[[ ! -d ${dir} ]] && return 0 # Non-existent = empty
1738-
1739-
# Directory exists, check if it has any non-zero files we care about
1740-
local has_content=false
1712+
# Helper function to check patch directory status
1713+
_check_patch_files() {
1714+
local dir="${1}" mode="${2:-has_valid}"
1715+
[[ ! -d ${dir} ]] && { [[ ${mode} == "is_empty" ]] && return 0 || return 1; }
17411716

17421717
# Check for any valid patch files
17431718
for file in "${dir}/patch" "${dir}/url" "${dir}"/*.{patch,diff}; do
1744-
[[ -f ${file} && -s ${file} ]] && has_content=true && break
1719+
if [[ -f ${file} && -s ${file} ]]; then
1720+
[[ ${mode} == "has_valid" ]] && return 0 || return 1
1721+
fi
17451722
done
17461723

1747-
# Return 0 (true) if empty, 1 (false) if has content
1748-
[[ ${has_content} == false ]]
1724+
# No valid files found
1725+
[[ ${mode} == "is_empty" ]] && return 0 || return 1
17491726
}
17501727

1728+
# Wrapper functions for clarity
1729+
_has_valid_patch_files() { _check_patch_files "${1}" "has_valid"; }
1730+
_is_patch_dir_empty() { _check_patch_files "${1}" "is_empty"; }
1731+
17511732
# Process local patches - never overwrites/updates files, only user does this
17521733
_process_local_patches() {
17531734
local has_content=false
@@ -1768,18 +1749,13 @@ _apply_patches() {
17681749
patch_url="$(< "${patch_dir}/url")"
17691750

17701751
if _curl "${patch_url}" -o "${tmp_patch}"; then
1771-
if [[ ${has_content} == true ]]; then
1772-
printf '\n\n# Merged from URL: %s\n' "${patch_url}" >> "${temp_patch}"
1773-
else
1774-
printf '# Downloaded from URL: %s\n' "${patch_url}" > "${temp_patch}"
1775-
fi
1752+
[[ ${has_content} == true ]] && printf '\n\n# Merged from URL: %s\n' "${patch_url}" >> "${temp_patch}" || printf '# Downloaded from URL: %s\n' "${patch_url}" > "${temp_patch}"
17761753
cat "${tmp_patch}" >> "${temp_patch}"
1777-
rm -f "${tmp_patch}"
17781754
has_content=true
17791755
else
17801756
printf '%b\n' " ${unicode_yellow_circle} Failed to download from URL: ${patch_url}"
1781-
rm -f "${tmp_patch}"
17821757
fi
1758+
rm -f "${tmp_patch}"
17831759
fi
17841760

17851761
# Step 3: Merge any *.patch or *.diff files last
@@ -1788,17 +1764,15 @@ _apply_patches() {
17881764
[[ -f ${patch_src} && -s ${patch_src} && ${patch_src} != "${patch_file}" ]] && additional_patches+=("${patch_src}")
17891765
done
17901766

1791-
if [[ ${#additional_patches[@]} -gt 0 ]]; then
1792-
for patch_src in "${additional_patches[@]}"; do
1793-
if [[ ${has_content} == true ]]; then
1794-
printf '\n\n# Merged from: %s\n' "${patch_src##*/}" >> "${temp_patch}"
1795-
else
1796-
printf '# From: %s\n' "${patch_src##*/}" > "${temp_patch}"
1797-
has_content=true
1798-
fi
1799-
cat "${patch_src}" >> "${temp_patch}"
1800-
done
1801-
fi
1767+
for patch_src in "${additional_patches[@]}"; do
1768+
if [[ ${has_content} == true ]]; then
1769+
printf '\n\n# Merged from: %s\n' "${patch_src##*/}" >> "${temp_patch}"
1770+
else
1771+
printf '# From: %s\n' "${patch_src##*/}" > "${temp_patch}"
1772+
has_content=true
1773+
fi
1774+
cat "${patch_src}" >> "${temp_patch}"
1775+
done
18021776

18031777
# Final validation and atomic move
18041778
if [[ ${has_content} == true && -s ${temp_patch} ]]; then

0 commit comments

Comments
 (0)