[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: cp -iu always prompts
From: |
Evan Hunt |
Subject: |
Re: cp -iu always prompts |
Date: |
Wed, 27 Dec 2006 21:54:19 -0800 |
User-agent: |
Mutt/1.5.10i |
> Indeed, without the -u, this behavior of cp -i is required by POSIX. But
> since -u is a GNU extension, you are correct that it may be worth changing
> the behavior in this area to suppress the -i prompt if -u determines the
> dates imply no copy is needed. Now all that is needed is for someone with
> enough time to write a patch.
Well, I have some time. Patch below.
All it does is move the check for update mode ahead of the check for
interactivity. So far I haven't thought of any adverse side-effects of
this change, and it passes all automated tests.
While I was in there, I noticed a similar bug with the -l/--link option:
$ touch foo
$ ln foo bar
$ cp -li foo bar
cp: overwrite `bar'?
There's no need at all to ask this question, since either answer will
result in the same outcome--bar will *not* be overwritten, and *will*
still be a hard link to foo. So I moved that test to be before the
test for interactive mode, too.
Evan Hunt
----------------
*** copy.c.00 2006-12-27 21:31:38.000000000 -0800
--- copy.c 2006-12-27 21:48:58.000000000 -0800
***************
*** 1079,1084 ****
--- 1079,1131 ----
return false;
}
+
+ if (return_now)
+ return true;
+
+ if (!S_ISDIR (src_mode))
+ {
+ if (S_ISDIR (dst_sb.st_mode))
+ {
+ if (x->move_mode && x->backup_type != no_backups)
+ {
+ /* Moving a non-directory onto an existing
+ directory is ok only with --backup. */
+ }
+ else
+ {
+ error (0, 0,
+ _("cannot overwrite directory %s with non-directory"),
+ quote (dst_name));
+ return false;
+ }
+ }
+
+ if (x->update)
+ {
+ /* When preserving time stamps (but not moving within a file
+ system), don't worry if the destination time stamp is
+ less than the source merely because of time stamp
+ truncation. */
+ int options = ((x->preserve_timestamps
+ && ! (x->move_mode
+ && dst_sb.st_dev == src_sb.st_dev))
+ ? UTIMECMP_TRUNCATE_SOURCE
+ : 0);
+
+ if (0 <= utimecmp (dst_name, &dst_sb, &src_sb, options))
+ {
+ /* We're using --update and the destination is not older
+ than the source, so do not copy or move. Pretend the
+ rename succeeded, so the caller (if it's mv) doesn't
+ end up removing the source file. */
+ if (rename_succeeded)
+ *rename_succeeded = true;
+ return true;
+ }
+ }
+ }
+
/* When there is an existing destination file, we may end up
returning early, and hence not copying/moving the file.
This may be due to an interactive `negative' reply to the
***************
*** 1115,1123 ****
return true;
}
- if (return_now)
- return true;
-
if (!S_ISDIR (dst_sb.st_mode))
{
if (S_ISDIR (src_mode))
--- 1162,1167 ----
***************
*** 1154,1202 ****
}
}
- if (!S_ISDIR (src_mode))
- {
- if (S_ISDIR (dst_sb.st_mode))
- {
- if (x->move_mode && x->backup_type != no_backups)
- {
- /* Moving a non-directory onto an existing
- directory is ok only with --backup. */
- }
- else
- {
- error (0, 0,
- _("cannot overwrite directory %s with non-directory"),
- quote (dst_name));
- return false;
- }
- }
-
- if (x->update)
- {
- /* When preserving time stamps (but not moving within a file
- system), don't worry if the destination time stamp is
- less than the source merely because of time stamp
- truncation. */
- int options = ((x->preserve_timestamps
- && ! (x->move_mode
- && dst_sb.st_dev == src_sb.st_dev))
- ? UTIMECMP_TRUNCATE_SOURCE
- : 0);
-
- if (0 <= utimecmp (dst_name, &dst_sb, &src_sb, options))
- {
- /* We're using --update and the destination is not older
- than the source, so do not copy or move. Pretend the
- rename succeeded, so the caller (if it's mv) doesn't
- end up removing the source file. */
- if (rename_succeeded)
- *rename_succeeded = true;
- return true;
- }
- }
- }
-
if (x->move_mode)
{
/* Don't allow user to move a directory onto a non-directory. */
--- 1198,1203 ----