Fix: download_telemt extract - use dedicated dir instead of find -newer

This commit is contained in:
anten-ka
2026-04-06 22:11:46 +03:00
parent cc3f547273
commit 1293b04430

View File

@@ -65,6 +65,7 @@ download_telemt() {
fi
local tmp_file="/tmp/telemt_download_$$"
local extract_dir="/tmp/telemt_extract_$$"
log_info "Скачивание: $url"
if ! curl -L -s --max-time 120 -o "$tmp_file" "$url"; then
@@ -73,42 +74,58 @@ download_telemt() {
return 1
fi
# Проверяем что файл не пустой и не HTML
local file_size
file_size=$(stat -c%s "$tmp_file" 2>/dev/null || echo 0)
if [ "$file_size" -lt 1000 ]; then
log_error "Скачанный файл слишком маленький ($file_size байт) — возможна ошибка сети"
rm -f "$tmp_file"
return 1
fi
# Определяем тип файла и распаковываем
local mime
local mime extracted=""
mime=$(file -b --mime-type "$tmp_file" 2>/dev/null)
rm -rf "$extract_dir"
mkdir -p "$extract_dir"
case "$mime" in
application/gzip|application/x-gzip)
tar xzf "$tmp_file" -C /tmp/ 2>/dev/null
local extracted
extracted=$(find /tmp -maxdepth 2 -name "telemt" -type f -newer "$tmp_file" 2>/dev/null | head -1)
tar xzf "$tmp_file" -C "$extract_dir" 2>/dev/null
extracted=$(find "$extract_dir" -name "telemt" -type f 2>/dev/null | head -1)
if [ -z "$extracted" ]; then
# Может быть просто gzip без tar
gunzip -c "$tmp_file" > /tmp/telemt_bin_$$ 2>/dev/null
extracted="/tmp/telemt_bin_$$"
gunzip -c "$tmp_file" > "$extract_dir/telemt_bin" 2>/dev/null
extracted="$extract_dir/telemt_bin"
fi
;;
application/x-tar)
tar xf "$tmp_file" -C /tmp/ 2>/dev/null
extracted=$(find /tmp -maxdepth 2 -name "telemt" -type f -newer "$tmp_file" 2>/dev/null | head -1)
tar xf "$tmp_file" -C "$extract_dir" 2>/dev/null
extracted=$(find "$extract_dir" -name "telemt" -type f 2>/dev/null | head -1)
;;
application/zip)
unzip -o "$tmp_file" -d /tmp/telemt_extract_$$ 2>/dev/null
extracted=$(find /tmp/telemt_extract_$$ -name "telemt" -type f 2>/dev/null | head -1)
unzip -o "$tmp_file" -d "$extract_dir" 2>/dev/null
extracted=$(find "$extract_dir" -name "telemt" -type f 2>/dev/null | head -1)
;;
application/octet-stream|application/x-executable)
# Уже бинарник
extracted="$tmp_file"
;;
*)
# Пробуем как бинарник
extracted="$tmp_file"
# Пробуем определить по содержимому
if file "$tmp_file" 2>/dev/null | grep -q "ELF"; then
extracted="$tmp_file"
else
# Пробуем как tar.gz
tar xzf "$tmp_file" -C "$extract_dir" 2>/dev/null
extracted=$(find "$extract_dir" -name "telemt" -type f 2>/dev/null | head -1)
fi
;;
esac
if [ -z "$extracted" ] || [ ! -f "$extracted" ]; then
log_error "Не удалось извлечь бинарник telemt"
log_error "Не удалось извлечь бинарник telemt (mime: $mime)"
rm -f "$tmp_file"
rm -rf "$extract_dir"
return 1
fi
@@ -116,14 +133,14 @@ download_telemt() {
cp "$extracted" "$TELEMT_BIN"
chmod 755 "$TELEMT_BIN"
rm -f "$tmp_file"
rm -rf /tmp/telemt_extract_$$ /tmp/telemt_bin_$$
rm -rf "$extract_dir"
# Проверяем
if "$TELEMT_BIN" --version &>/dev/null; then
log_success "telemt $(get_installed_telemt_version) установлен в $TELEMT_BIN"
return 0
else
log_error "Бинарник telemt не запускается"
log_error "Бинарник telemt не запускается ($(file -b "$TELEMT_BIN" 2>/dev/null))"
return 1
fi
}