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

View File

@ -205,16 +205,16 @@ if(NOT CMAKE_CROSSCOMPILING AND NOT UPX_CONFIG_DISABLE_SELF_PACK_TEST)
endfunction()
set(exe ${CMAKE_EXECUTABLE_SUFFIX})
set(upx_self_exe "$<TARGET_FILE:upx>")
upx_add_test(upx-test-cleanup rm -f ./upx-packed${exe} ./upx-unpacked${exe})
upx_add_test(upx-self-pack upx -3 ${upx_self_exe} -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-n2d upx -3 --nrv2d ${upx_self_exe} -f -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-lzma upx -3 --lzma ${upx_self_exe} -f -o upx-packed-lzma${exe})
upx_add_test(upx-list upx -l upx-packed${exe})
upx_add_test(upx-fileinfo upx --fileinfo upx-packed${exe})
upx_add_test(upx-test upx -t upx-packed${exe})
upx_add_test(upx-unpack upx -d upx-packed${exe} -o upx-unpacked${exe})
set(fo "--force-overwrite")
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} ${fo} -o upx-packed-n2b${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} ${fo} -o upx-packed-n2e${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-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-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-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} ${fo} -o upx-unpacked${exe})
upx_add_test(upx-run-unpacked ./upx-unpacked${exe} --version-short)
upx_add_test(upx-run-packed ./upx-packed${exe} --version-short)
endif()

View File

@ -375,6 +375,9 @@ static int do_option(int optc, const char *arg) {
case 'f':
opt->force++;
break;
case 529:
opt->force_overwrite = true;
break;
case 909:
set_cmd(CMD_FILEINFO);
break;
@ -799,14 +802,15 @@ int main_get_options(int argc, char **argv) {
{"version", 0, N, 'V' + 256}, // display version number
// options
{"force", 0, N, 'f'}, // force overwrite of output files
{"force-compress", 0, N, 'f'}, // and compression of suspicious files
{"info", 0, N, 'i'}, // info mode
{"no-env", 0x10, N, 519}, // no environment var
{"no-mode", 0x10, N, 526}, // do not preserve mode (permissions)
{"no-owner", 0x10, N, 527}, // do not preserve ownership
{"no-progress", 0, N, 516}, // no progress bar
{"no-time", 0x10, N, 528}, // do not preserve timestamp
{"force", 0, N, 'f'}, // force overwrite of output 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
{"no-env", 0x10, N, 519}, // no environment var
{"no-mode", 0x10, N, 526}, // do not preserve mode (permissions)
{"no-owner", 0x10, N, 527}, // do not preserve ownership
{"no-progress", 0, N, 516}, // no progress bar
{"no-time", 0x10, N, 528}, // do not preserve timestamp
{"output", 0x21, N, 'o'},
{"quiet", 0, N, 'q'}, // quiet mode
{"silent", 0, N, 'q'}, // quiet mode
@ -897,7 +901,7 @@ int main_get_options(int argc, char **argv) {
// dos/sys
// unix
{"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
{"use_ptinterp", 0x10, N, 664}, // 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
{"preserve-build-id", 0, N, 675},
{"android-shlib", 0, N, 676},
{"force-pie", 0, N, 677},
{"force-pie", 0x90, N, 677},
// watcom/le
{"le", 0x10, N, 620}, // produce LE output
// win32/pe

View File

@ -69,6 +69,7 @@ struct options_t {
int backup;
int console;
int force;
bool force_overwrite;
int info_mode;
bool ignorewarn;
bool no_env;

View File

@ -68,6 +68,8 @@ void do_one_file(const char *iname, char *oname) {
else
throwIOException(iname, errno);
}
if (S_ISDIR(st.st_mode))
throwIOException("is a directory -- skipped");
if (!(S_ISREG(st.st_mode)))
throwIOException("not a regular file -- skipped");
#if defined(__unix__)
@ -114,22 +116,22 @@ void do_one_file(const char *iname, char *oname) {
throwIOException("data not written to a terminal; Use '-f' to force.");
} else {
char tname[ACC_FN_PATH_MAX + 1];
if (opt->output_name)
if (opt->output_name) {
strcpy(tname, opt->output_name);
else {
if (opt->force_overwrite || opt->force >= 2) {
#if (HAVE_CHMOD)
r = chmod(tname, 0777);
IGNORE_ERROR(r);
#endif
r = unlink(tname);
IGNORE_ERROR(r);
}
} else {
if (!maketempname(tname, sizeof(tname), iname, ".upx"))
throwIOException("could not create a temporary file name");
}
if (opt->force >= 2) {
#if (HAVE_CHMOD)
r = chmod(tname, 0777);
IGNORE_ERROR(r);
#endif
r = unlink(tname);
IGNORE_ERROR(r);
}
int flags = O_CREAT | O_WRONLY | O_BINARY;
if (opt->force)
if (opt->force_overwrite || opt->force)
flags |= O_TRUNC;
else
flags |= O_EXCL;