From 04aa739146c8a926aab6da7a046f1844e414899f Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Sat, 18 Dec 1999 16:00:49 +0000 Subject: [PATCH] (wipename): When repeatedly renaming a file, making the name shorter and shorter, skip to the next shorter length length if a rename fails (e.g. due to permission denied). Otherwise, this loop would iterate for so long that shred would appear to be stuck in an infinite loop for any but the shortest file names. --- src/shred.c | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/src/shred.c b/src/shred.c index e8b7c8d080..d192a1a330 100644 --- a/src/shred.c +++ b/src/shred.c @@ -1589,22 +1589,34 @@ wipename (char *oldname, char const *qoldname, struct Options const *flags) do { struct stat st; - if (lstat (newname, &st) < 0 && rename (oldname, newname) == 0) + if (lstat (newname, &st) < 0) { - if (dir_fd < 0 - || (fdatasync (dir_fd) < 0 && fsync (dir_fd) < 0)) - sync (); /* Force directory out */ - if (flags->verbose) + if (rename (oldname, newname) == 0) { - /* - * People seem to understand this better than talking - * about renaming oldname. newname doesn't need - * quoting because we picked it. - */ - error (0, 0, _("%s: renamed to `%s'"), qoldname, newname); + if (dir_fd < 0 + || (fdatasync (dir_fd) < 0 && fsync (dir_fd) < 0)) + sync (); /* Force directory out */ + if (flags->verbose) + { + /* + * People seem to understand this better than talking + * about renaming oldname. newname doesn't need + * quoting because we picked it. + */ + error (0, 0, _("%s: renamed to `%s'"), qoldname, newname); + } + memcpy (oldname + (base - newname), base, len + 1); + break; } - memcpy (oldname + (base - newname), base, len + 1); - break; + else + { + /* The rename failed: give up on this length. */ + break; + } + } + else + { + /* newname exists, so increment BASE so we use another */ } } while (!incname (base, len)); -- 2.47.3