1
0
mirror of https://github.com/tw93/Mole.git synced 2026-02-04 13:16:47 +00:00

fix: address mo update hanging and imporve temporary file reliability

This commit is contained in:
Tw93
2026-01-12 14:55:42 +08:00
parent add3cca6ef
commit 5d5056fc9e
4 changed files with 20 additions and 16 deletions

View File

@@ -13,6 +13,9 @@ LIB_DIR="$(cd "$SCRIPT_DIR/../lib" && pwd)"
# shellcheck source=../lib/core/common.sh # shellcheck source=../lib/core/common.sh
source "$LIB_DIR/core/common.sh" source "$LIB_DIR/core/common.sh"
# Set up global cleanup trap
trap cleanup_temp_files EXIT INT TERM
readonly PAM_SUDO_FILE="${MOLE_PAM_SUDO_FILE:-/etc/pam.d/sudo}" readonly PAM_SUDO_FILE="${MOLE_PAM_SUDO_FILE:-/etc/pam.d/sudo}"
readonly PAM_SUDO_LOCAL_FILE="${MOLE_PAM_SUDO_LOCAL_FILE:-/etc/pam.d/sudo_local}" readonly PAM_SUDO_LOCAL_FILE="${MOLE_PAM_SUDO_LOCAL_FILE:-/etc/pam.d/sudo_local}"
readonly PAM_TID_LINE="auth sufficient pam_tid.so" readonly PAM_TID_LINE="auth sufficient pam_tid.so"
@@ -66,9 +69,8 @@ show_status() {
# Enable Touch ID for sudo # Enable Touch ID for sudo
enable_touchid() { enable_touchid() {
# Cleanup trap # Cleanup trap handled by global EXIT trap
local temp_file="" local temp_file=""
trap '[[ -n "${temp_file:-}" ]] && rm -f "${temp_file:-}"' EXIT
# First check if system supports Touch ID # First check if system supports Touch ID
if ! supports_touchid; then if ! supports_touchid; then
@@ -88,7 +90,7 @@ enable_touchid() {
# It is in sudo_local, but let's check if it's ALSO in sudo (incomplete migration) # It is in sudo_local, but let's check if it's ALSO in sudo (incomplete migration)
if grep -q "pam_tid.so" "$PAM_SUDO_FILE"; then if grep -q "pam_tid.so" "$PAM_SUDO_FILE"; then
# Clean up legacy config # Clean up legacy config
temp_file=$(mktemp) temp_file=$(create_temp_file)
grep -v "pam_tid.so" "$PAM_SUDO_FILE" > "$temp_file" grep -v "pam_tid.so" "$PAM_SUDO_FILE" > "$temp_file"
if sudo mv "$temp_file" "$PAM_SUDO_FILE" 2> /dev/null; then if sudo mv "$temp_file" "$PAM_SUDO_FILE" 2> /dev/null; then
echo -e "${GREEN}${ICON_SUCCESS} Cleanup legacy configuration${NC}" echo -e "${GREEN}${ICON_SUCCESS} Cleanup legacy configuration${NC}"
@@ -117,7 +119,7 @@ enable_touchid() {
else else
# Append if not present # Append if not present
if ! grep -q "pam_tid.so" "$PAM_SUDO_LOCAL_FILE"; then if ! grep -q "pam_tid.so" "$PAM_SUDO_LOCAL_FILE"; then
temp_file=$(mktemp) temp_file=$(create_temp_file)
cp "$PAM_SUDO_LOCAL_FILE" "$temp_file" cp "$PAM_SUDO_LOCAL_FILE" "$temp_file"
echo "$PAM_TID_LINE" >> "$temp_file" echo "$PAM_TID_LINE" >> "$temp_file"
sudo mv "$temp_file" "$PAM_SUDO_LOCAL_FILE" sudo mv "$temp_file" "$PAM_SUDO_LOCAL_FILE"
@@ -132,7 +134,7 @@ enable_touchid() {
if $write_success; then if $write_success; then
# If we migrated from legacy, clean it up now # If we migrated from legacy, clean it up now
if $is_legacy_configured; then if $is_legacy_configured; then
temp_file=$(mktemp) temp_file=$(create_temp_file)
grep -v "pam_tid.so" "$PAM_SUDO_FILE" > "$temp_file" grep -v "pam_tid.so" "$PAM_SUDO_FILE" > "$temp_file"
sudo mv "$temp_file" "$PAM_SUDO_FILE" sudo mv "$temp_file" "$PAM_SUDO_FILE"
log_success "Touch ID migrated to sudo_local" log_success "Touch ID migrated to sudo_local"
@@ -163,7 +165,7 @@ enable_touchid() {
fi fi
# Create temp file # Create temp file
temp_file=$(mktemp) temp_file=$(create_temp_file)
# Insert pam_tid.so after the first comment block # Insert pam_tid.so after the first comment block
awk ' awk '
@@ -194,9 +196,8 @@ enable_touchid() {
# Disable Touch ID for sudo # Disable Touch ID for sudo
disable_touchid() { disable_touchid() {
# Cleanup trap # Cleanup trap handled by global EXIT trap
local temp_file="" local temp_file=""
trap '[[ -n "${temp_file:-}" ]] && rm -f "${temp_file:-}"' EXIT
if ! is_touchid_configured; then if ! is_touchid_configured; then
echo -e "${YELLOW}Touch ID is not currently enabled${NC}" echo -e "${YELLOW}Touch ID is not currently enabled${NC}"
@@ -206,13 +207,13 @@ disable_touchid() {
# Check sudo_local first # Check sudo_local first
if [[ -f "$PAM_SUDO_LOCAL_FILE" ]] && grep -q "pam_tid.so" "$PAM_SUDO_LOCAL_FILE"; then if [[ -f "$PAM_SUDO_LOCAL_FILE" ]] && grep -q "pam_tid.so" "$PAM_SUDO_LOCAL_FILE"; then
# Remove from sudo_local # Remove from sudo_local
temp_file=$(mktemp) temp_file=$(create_temp_file)
grep -v "pam_tid.so" "$PAM_SUDO_LOCAL_FILE" > "$temp_file" grep -v "pam_tid.so" "$PAM_SUDO_LOCAL_FILE" > "$temp_file"
if sudo mv "$temp_file" "$PAM_SUDO_LOCAL_FILE" 2> /dev/null; then if sudo mv "$temp_file" "$PAM_SUDO_LOCAL_FILE" 2> /dev/null; then
# Since we modified sudo_local, we should also check if it's in sudo file (legacy cleanup) # Since we modified sudo_local, we should also check if it's in sudo file (legacy cleanup)
if grep -q "pam_tid.so" "$PAM_SUDO_FILE"; then if grep -q "pam_tid.so" "$PAM_SUDO_FILE"; then
temp_file=$(mktemp) temp_file=$(create_temp_file)
grep -v "pam_tid.so" "$PAM_SUDO_FILE" > "$temp_file" grep -v "pam_tid.so" "$PAM_SUDO_FILE" > "$temp_file"
sudo mv "$temp_file" "$PAM_SUDO_FILE" sudo mv "$temp_file" "$PAM_SUDO_FILE"
fi fi
@@ -236,7 +237,7 @@ disable_touchid() {
fi fi
# Remove pam_tid.so line # Remove pam_tid.so line
temp_file=$(mktemp) temp_file=$(create_temp_file)
grep -v "pam_tid.so" "$PAM_SUDO_FILE" > "$temp_file" grep -v "pam_tid.so" "$PAM_SUDO_FILE" > "$temp_file"
if sudo mv "$temp_file" "$PAM_SUDO_FILE" 2> /dev/null; then if sudo mv "$temp_file" "$PAM_SUDO_FILE" 2> /dev/null; then

View File

@@ -100,7 +100,7 @@ resolve_source_dir() {
local tmp local tmp
tmp="$(mktemp -d)" tmp="$(mktemp -d)"
trap 'stop_line_spinner 2>/dev/null; rm -rf "$tmp"' EXIT trap "stop_line_spinner 2>/dev/null; rm -rf \"$tmp\"" EXIT
local branch="${MOLE_VERSION:-}" local branch="${MOLE_VERSION:-}"
if [[ -z "$branch" ]]; then if [[ -z "$branch" ]]; then

View File

@@ -512,7 +512,7 @@ declare -a MOLE_TEMP_DIRS=()
create_temp_file() { create_temp_file() {
local temp local temp
temp=$(mktemp) || return 1 temp=$(mktemp) || return 1
MOLE_TEMP_FILES+=("$temp") register_temp_file "$temp"
echo "$temp" echo "$temp"
} }
@@ -520,7 +520,7 @@ create_temp_file() {
create_temp_dir() { create_temp_dir() {
local temp local temp
temp=$(mktemp -d) || return 1 temp=$(mktemp -d) || return 1
MOLE_TEMP_DIRS+=("$temp") register_temp_dir "$temp"
echo "$temp" echo "$temp"
} }
@@ -538,9 +538,12 @@ register_temp_dir() {
# Compatible with both BSD mktemp (macOS default) and GNU mktemp (coreutils) # Compatible with both BSD mktemp (macOS default) and GNU mktemp (coreutils)
mktemp_file() { mktemp_file() {
local prefix="${1:-mole}" local prefix="${1:-mole}"
local temp
# Use TMPDIR if set, otherwise /tmp # Use TMPDIR if set, otherwise /tmp
# Add .XXXXXX suffix to work with both BSD and GNU mktemp # Add .XXXXXX suffix to work with both BSD and GNU mktemp
mktemp "${TMPDIR:-/tmp}/${prefix}.XXXXXX" temp=$(mktemp "${TMPDIR:-/tmp}/${prefix}.XXXXXX") || return 1
register_temp_file "$temp"
echo "$temp"
} }
# Cleanup all tracked temp files and directories # Cleanup all tracked temp files and directories

View File

@@ -34,7 +34,7 @@ update_via_homebrew() {
temp_upgrade=$(mktemp_file "brew_upgrade") temp_upgrade=$(mktemp_file "brew_upgrade")
# Set up trap for interruption (Ctrl+C) with inline cleanup # Set up trap for interruption (Ctrl+C) with inline cleanup
trap 'stop_inline_spinner 2>/dev/null; rm -f "$temp_update" "$temp_upgrade" 2>/dev/null; echo ""; exit 130' INT TERM trap "stop_inline_spinner 2>/dev/null; rm -f \"$temp_update\" \"$temp_upgrade\" 2>/dev/null; echo \"\"; exit 130" INT TERM
# Update Homebrew # Update Homebrew
if [[ -t 1 ]]; then if [[ -t 1 ]]; then