Prompt user before truncating a file to zero bytes. Bug #922.

This commit is contained in:
Todd C. Miller
2020-07-22 07:42:40 -06:00
parent bcf96c153f
commit 6ee98cf453
3 changed files with 17 additions and 2 deletions

View File

@@ -86,6 +86,17 @@ sudo_copy_file(const char *src, int src_fd, off_t src_len, const char *dst,
ssize_t nwritten, nread;
debug_decl(sudo_copy_file, SUDO_DEBUG_UTIL);
/* Prompt the user before zeroing out an existing file. */
if (dst_len > 0 && src_len == 0) {
fprintf(stderr, U_("%s: truncate %s to zero bytes? (y/n) [n] "),
getprogname(), dst);
if (fgets(buf, sizeof(buf), stdin) == NULL ||
(buf[0] != 'y' && buf[0] != 'Y')) {
sudo_warnx(U_("not overwriting %s"), dst);
debug_return_int(0);
}
}
/* Extend the file to the new size if larger before copying. */
if (dst_len > 0 && src_len > dst_len) {
if (sudo_extend_file(dst_fd, dst, src_len) == -1)