On Fri, Aug 31, 2018 at 09:40:19AM +0200, Martijn van Duren wrote:
> Hello tech@,
>
> While working on fixing the -iv behaviour yesterday I noticed that the
> -i verification isn't implemented for the special copies and thus
> allowing to overwrite a file.
> I did some archaeology and found it was introduced by bostic in 1990,
> so the behaviour is here for over 28 years!
>
> Since this is cp I split the diff in the smallest possible chunks for
> easier review. These diffs go on top of my diff send in yesterday[0].
>
> 1) Implements the copy_overwrite function so we can reuse it in all
> functions.
> 2) Add the copy_overwrite function to copy_{link,fifo,special}
> 3) Change dne variable to exists for copy_file to be consistent with
> the naming in the other copy_* functions.
>
> OK?
ok stsp@
>
> martijn@
>
> [0] https://marc.info/?l=openbsd-tech&m=153565590700590&w=2
>
> diff --git utils.c utils.c
> index fdbf5a0..3a8d6b4 100644
> --- utils.c
> +++ utils.c
> @@ -47,13 +47,15 @@
>
> #include "extern.h"
>
> +int copy_overwrite(void);
> +
> int
> copy_file(FTSENT *entp, int dne)
> {
> static char *buf;
> static char *zeroes;
> struct stat to_stat, *fs;
> - int ch, checkch, from_fd, rcount, rval, to_fd, wcount;
> + int from_fd, rcount, rval, to_fd, wcount;
> #ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
> char *p;
> #endif
> @@ -84,7 +86,6 @@ copy_file(FTSENT *entp, int dne)
> (void)unlink(to.p_path);
>
> /*
> - * If the file exists and we're interactive, verify with the user.
> * If the file DNE, set the mode to be the from file, minus setuid
> * bits, modified by the umask; arguably wrong, but it makes copying
> * executables work right and it's been that way forever. (The
> @@ -92,16 +93,10 @@ copy_file(FTSENT *entp, int dne)
> * modified by the umask.)
> */
> if (!dne && !fflag) {
> - if (iflag) {
> - (void)fprintf(stderr, "overwrite %s? ", to.p_path);
> - checkch = ch = getchar();
> - while (ch != '\n' && ch != EOF)
> - ch = getchar();
> - if (checkch != 'y' && checkch != 'Y') {
> - (void)close(from_fd);
> - return (2);
> - }
> - }
> + if (!copy_overwrite()) {
> + (void)close(from_fd);
> + return 2;
> + }
> to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0);
> } else
> to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
> @@ -248,6 +243,24 @@ copy_special(struct stat *from_stat, int exists)
> return (pflag ? setfile(from_stat, -1) : 0);
> }
>
> +/*
> + * If the file exists and we're interactive, verify with the user.
> + */
> +int
> +copy_overwrite(void)
> +{
> + int ch, checkch;
> +
> + if (iflag) {
> + (void)fprintf(stderr, "overwrite %s? ", to.p_path);
> + checkch = ch = getchar();
> + while (ch != '\n' && ch != EOF)
> + ch = getchar();
> + if (checkch != 'y' && checkch != 'Y')
> + return (0);
> + }
> + return 1;
> +}
>
> int
> setfile(struct stat *fs, int fd)
>