src: add option '--force-overwrite'

This commit is contained in:
Markus F.X.J. Oberhumer
2022-11-16 12:52:53 +01:00
parent 627e733a30
commit 3cf102334f
4 changed files with 38 additions and 31 deletions
+10 -10
View File
@@ -205,16 +205,16 @@ if(NOT CMAKE_CROSSCOMPILING AND NOT UPX_CONFIG_DISABLE_SELF_PACK_TEST)
endfunction() endfunction()
set(exe ${CMAKE_EXECUTABLE_SUFFIX}) set(exe ${CMAKE_EXECUTABLE_SUFFIX})
set(upx_self_exe "$<TARGET_FILE:upx>") set(upx_self_exe "$<TARGET_FILE:upx>")
upx_add_test(upx-test-cleanup rm -f ./upx-packed${exe} ./upx-unpacked${exe}) set(fo "--force-overwrite")
upx_add_test(upx-self-pack upx -3 ${upx_self_exe} -o upx-packed${exe}) upx_add_test(upx-self-pack upx -3 ${upx_self_exe} ${fo} -o upx-packed${exe})
upx_add_test(upx-self-pack-n2b upx -3 --nrv2b ${upx_self_exe} -f -o upx-packed-n2b${exe}) upx_add_test(upx-self-pack-n2b upx -3 --nrv2b ${upx_self_exe} ${fo} -o upx-packed-n2b${exe})
upx_add_test(upx-self-pack-n2d upx -3 --nrv2d ${upx_self_exe} -f -o upx-packed-n2d${exe}) upx_add_test(upx-self-pack-n2d upx -3 --nrv2d ${upx_self_exe} ${fo} -o upx-packed-n2d${exe})
upx_add_test(upx-self-pack-n2e upx -3 --nrv2e ${upx_self_exe} -f -o upx-packed-n2e${exe}) upx_add_test(upx-self-pack-n2e upx -3 --nrv2e ${upx_self_exe} ${fo} -o upx-packed-n2e${exe})
upx_add_test(upx-self-pack-lzma upx -3 --lzma ${upx_self_exe} -f -o upx-packed-lzma${exe}) upx_add_test(upx-self-pack-lzma upx -3 --lzma ${upx_self_exe} ${fo} -o upx-packed-lzma${exe})
upx_add_test(upx-list upx -l upx-packed${exe}) upx_add_test(upx-list upx -l upx-packed${exe} upx-packed-n2b${exe} upx-packed-n2d${exe} upx-packed-n2e${exe} upx-packed-lzma${exe})
upx_add_test(upx-fileinfo upx --fileinfo upx-packed${exe}) upx_add_test(upx-fileinfo upx --fileinfo upx-packed${exe} upx-packed-n2b${exe} upx-packed-n2d${exe} upx-packed-n2e${exe} upx-packed-lzma${exe})
upx_add_test(upx-test upx -t upx-packed${exe}) upx_add_test(upx-test upx -t upx-packed${exe} upx-packed-n2b${exe} upx-packed-n2d${exe} upx-packed-n2e${exe} upx-packed-lzma${exe})
upx_add_test(upx-unpack upx -d upx-packed${exe} -o upx-unpacked${exe}) upx_add_test(upx-unpack upx -d upx-packed${exe} ${fo} -o upx-unpacked${exe})
upx_add_test(upx-run-unpacked ./upx-unpacked${exe} --version-short) upx_add_test(upx-run-unpacked ./upx-unpacked${exe} --version-short)
upx_add_test(upx-run-packed ./upx-packed${exe} --version-short) upx_add_test(upx-run-packed ./upx-packed${exe} --version-short)
endif() endif()
+6 -2
View File
@@ -375,6 +375,9 @@ static int do_option(int optc, const char *arg) {
case 'f': case 'f':
opt->force++; opt->force++;
break; break;
case 529:
opt->force_overwrite = true;
break;
case 909: case 909:
set_cmd(CMD_FILEINFO); set_cmd(CMD_FILEINFO);
break; break;
@@ -801,6 +804,7 @@ int main_get_options(int argc, char **argv) {
// options // options
{"force", 0, N, 'f'}, // force overwrite of output files {"force", 0, N, 'f'}, // force overwrite of output files
{"force-compress", 0, N, 'f'}, // and compression of suspicious files {"force-compress", 0, N, 'f'}, // and compression of suspicious files
{"force-overwrite", 0x90, N, 529}, // force overwrite of output files
{"info", 0, N, 'i'}, // info mode {"info", 0, N, 'i'}, // info mode
{"no-env", 0x10, N, 519}, // no environment var {"no-env", 0x10, N, 519}, // no environment var
{"no-mode", 0x10, N, 526}, // do not preserve mode (permissions) {"no-mode", 0x10, N, 526}, // do not preserve mode (permissions)
@@ -897,7 +901,7 @@ int main_get_options(int argc, char **argv) {
// dos/sys // dos/sys
// unix // unix
{"blocksize", 0x31, N, 660}, // --blocksize= {"blocksize", 0x31, N, 660}, // --blocksize=
{"force-execve", 0x10, N, 661}, // force linux/386 execve format {"force-execve", 0x90, N, 661}, // force linux/386 execve format
{"is_ptinterp", 0x10, N, 663}, // linux/elf386 PT_INTERP program {"is_ptinterp", 0x10, N, 663}, // linux/elf386 PT_INTERP program
{"use_ptinterp", 0x10, N, 664}, // linux/elf386 PT_INTERP program {"use_ptinterp", 0x10, N, 664}, // linux/elf386 PT_INTERP program
{"make_ptinterp", 0x10, N, 665}, // linux/elf386 PT_INTERP program {"make_ptinterp", 0x10, N, 665}, // linux/elf386 PT_INTERP program
@@ -912,7 +916,7 @@ int main_get_options(int argc, char **argv) {
{"unmap-all-pages", 0x10, N, 674}, // linux /proc/self/exe vanishes {"unmap-all-pages", 0x10, N, 674}, // linux /proc/self/exe vanishes
{"preserve-build-id", 0, N, 675}, {"preserve-build-id", 0, N, 675},
{"android-shlib", 0, N, 676}, {"android-shlib", 0, N, 676},
{"force-pie", 0, N, 677}, {"force-pie", 0x90, N, 677},
// watcom/le // watcom/le
{"le", 0x10, N, 620}, // produce LE output {"le", 0x10, N, 620}, // produce LE output
// win32/pe // win32/pe
+1
View File
@@ -69,6 +69,7 @@ struct options_t {
int backup; int backup;
int console; int console;
int force; int force;
bool force_overwrite;
int info_mode; int info_mode;
bool ignorewarn; bool ignorewarn;
bool no_env; bool no_env;
+9 -7
View File
@@ -68,6 +68,8 @@ void do_one_file(const char *iname, char *oname) {
else else
throwIOException(iname, errno); throwIOException(iname, errno);
} }
if (S_ISDIR(st.st_mode))
throwIOException("is a directory -- skipped");
if (!(S_ISREG(st.st_mode))) if (!(S_ISREG(st.st_mode)))
throwIOException("not a regular file -- skipped"); throwIOException("not a regular file -- skipped");
#if defined(__unix__) #if defined(__unix__)
@@ -114,13 +116,9 @@ void do_one_file(const char *iname, char *oname) {
throwIOException("data not written to a terminal; Use '-f' to force."); throwIOException("data not written to a terminal; Use '-f' to force.");
} else { } else {
char tname[ACC_FN_PATH_MAX + 1]; char tname[ACC_FN_PATH_MAX + 1];
if (opt->output_name) if (opt->output_name) {
strcpy(tname, opt->output_name); strcpy(tname, opt->output_name);
else { if (opt->force_overwrite || opt->force >= 2) {
if (!maketempname(tname, sizeof(tname), iname, ".upx"))
throwIOException("could not create a temporary file name");
}
if (opt->force >= 2) {
#if (HAVE_CHMOD) #if (HAVE_CHMOD)
r = chmod(tname, 0777); r = chmod(tname, 0777);
IGNORE_ERROR(r); IGNORE_ERROR(r);
@@ -128,8 +126,12 @@ void do_one_file(const char *iname, char *oname) {
r = unlink(tname); r = unlink(tname);
IGNORE_ERROR(r); IGNORE_ERROR(r);
} }
} else {
if (!maketempname(tname, sizeof(tname), iname, ".upx"))
throwIOException("could not create a temporary file name");
}
int flags = O_CREAT | O_WRONLY | O_BINARY; int flags = O_CREAT | O_WRONLY | O_BINARY;
if (opt->force) if (opt->force_overwrite || opt->force)
flags |= O_TRUNC; flags |= O_TRUNC;
else else
flags |= O_EXCL; flags |= O_EXCL;