From: Alexander F Rødseth Date: Mon, 13 Feb 2017 12:44:38 +0000 (+0100) Subject: rename: add --no-act option X-Git-Tag: v2.30-rc1~244 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=990bf1f04837d5010629b3e12925842d25dec9d4;p=thirdparty%2Futil-linux.git rename: add --no-act option [kzak@redhat.com: - rename --dry-run to --no-act] Signed-off-by: Alexander F Rødseth Signed-off-by: Karel Zak --- diff --git a/misc-utils/rename.1 b/misc-utils/rename.1 index 165d61effa..ebb2fd40d0 100644 --- a/misc-utils/rename.1 +++ b/misc-utils/rename.1 @@ -22,6 +22,9 @@ Do not rename a symlink but its target. .BR \-v , " \-\-verbose" Show which files where renamed, if any. .TP +.BR \-n , " \-\-no-act" +Do not make any changes. +.TP .BR \-V , " \-\-version" Display version information and exit. .TP @@ -60,11 +63,13 @@ rename '_with_long_name' '' file_with_long_name.* .RE will remove the substring in the filenames. .SH WARNING -The renaming has no safeguards. If the user has permission to rewrite file names, -the command will perform the action without any questions. For example, the -result can be quite drastic when the command is run as root in the /lib -directory. Always make a backup before running the command, unless you truly -know what you are doing. +The renaming has no safeguards except the +.B \-\-no\-act +option. If the user has +permission to rewrite file names, the command will perform the action without +any questions. For example, the result can be quite drastic when the command +is run as root in the /lib directory. Always make a backup before running the +command, unless you truly know what you are doing. .SH "EXIT STATUS" .RS .PD 0 diff --git a/misc-utils/rename.c b/misc-utils/rename.c index f53b236e28..c1b9d4a0eb 100644 --- a/misc-utils/rename.c +++ b/misc-utils/rename.c @@ -53,7 +53,7 @@ static int string_replace(char *from, char *to, char *s, char *orig, char **newn return 0; } -static int do_symlink(char *from, char *to, char *s, int verbose) +static int do_symlink(char *from, char *to, char *s, int verbose, int noact) { char *newname = NULL, *target = NULL; int ret = 1; @@ -76,21 +76,21 @@ static int do_symlink(char *from, char *to, char *s, int verbose) target[sb.st_size] = '\0'; if (string_replace(from, to, target, target, &newname)) ret = 0; - else if (0 > unlink(s)) { + else if (!noact && 0 > unlink(s)) { warn(_("%s: unlink failed"), s); ret = 2; - } else if (symlink(newname, s) != 0) { + } else if (!noact && symlink(newname, s) != 0) { warn(_("%s: symlinking to %s failed"), s, newname); ret = 2; } - if (verbose && ret == 1) + if (verbose && (noact || ret == 1)) printf("%s: `%s' -> `%s'\n", s, target, newname); free(newname); free(target); return ret; } -static int do_file(char *from, char *to, char *s, int verbose) +static int do_file(char *from, char *to, char *s, int verbose, int noact) { char *newname = NULL, *file=NULL; int ret = 1; @@ -101,11 +101,11 @@ static int do_file(char *from, char *to, char *s, int verbose) file = s; if (string_replace(from, to, file, s, &newname)) return 0; - else if (rename(s, newname) != 0) { + else if (!noact && rename(s, newname) != 0) { warn(_("%s: rename to %s failed"), s, newname); ret = 2; } - if (verbose && ret == 1) + if (verbose && (noact || ret == 1)) printf("`%s' -> `%s'\n", s, newname); free(newname); return ret; @@ -124,6 +124,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE * out) fputs(USAGE_OPTIONS, out); fputs(_(" -v, --verbose explain what is being done\n"), out); fputs(_(" -s, --symlink act on the target of symlinks\n"), out); + fputs(_(" -n, --no-act do not make any changes\n"), out); fputs(USAGE_SEPARATOR, out); fputs(USAGE_HELP, out); fputs(USAGE_VERSION, out); @@ -134,13 +135,14 @@ static void __attribute__ ((__noreturn__)) usage(FILE * out) int main(int argc, char **argv) { char *from, *to; - int i, c, ret = 0, verbose = 0; - int (*do_rename)(char *from, char *to, char *s, int verbose) = do_file; + int i, c, ret = 0, verbose = 0, noact = 0; + int (*do_rename)(char *from, char *to, char *s, int verbose, int noact) = do_file; static const struct option longopts[] = { {"verbose", no_argument, NULL, 'v'}, {"version", no_argument, NULL, 'V'}, {"help", no_argument, NULL, 'h'}, + {"no-act", no_argument, NULL, 'n'}, {"symlink", no_argument, NULL, 's'}, {NULL, 0, NULL, 0} }; @@ -150,11 +152,14 @@ int main(int argc, char **argv) textdomain(PACKAGE); atexit(close_stdout); - while ((c = getopt_long(argc, argv, "vsVh", longopts, NULL)) != -1) + while ((c = getopt_long(argc, argv, "vsVhn", longopts, NULL)) != -1) switch (c) { case 'v': verbose = 1; break; + case 'n': + noact = 1; + break; case 's': do_rename = do_symlink; break; @@ -163,6 +168,7 @@ int main(int argc, char **argv) return EXIT_SUCCESS; case 'h': usage(stdout); + /* fallthrough */ default: errtryhelp(EXIT_FAILURE); } @@ -179,7 +185,7 @@ int main(int argc, char **argv) to = argv[1]; for (i = 2; i < argc; i++) - ret |= do_rename(from, to, argv[i], verbose); + ret |= do_rename(from, to, argv[i], verbose, noact); switch (ret) { case 0: