1 /* mv -- move or rename files
2 Copyright (C) 86, 89, 90, 91, 1995-2000 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 -f, --force Assume a 'y' answer to all questions it would
20 normally ask, and not ask the questions.
22 -i, --interactive Require confirmation from the user before
23 performing any move that would destroy an
26 -u, --update Do not move a nondirectory that has an
27 existing destination with the same or newer
30 -v, --verbose List the name of each file as it is moved, and
31 the name it is moved to.
38 Written by Mike Parker, David MacKenzie, and Jim Meyering */
47 #include <sys/types.h>
51 #include "backupfile.h"
55 #include "path-concat.h"
58 /* The official name of this program (e.g., no `g' prefix). */
59 #define PROGRAM_NAME "mv"
61 #define AUTHORS "Mike Parker, David MacKenzie, and Jim Meyering"
63 /* Initial number of entries in each hash table entry's table of inodes. */
64 #define INITIAL_HASH_MODULE 100
66 /* Initial number of entries in the inode hash table. */
67 #define INITIAL_ENTRY_TAB_SIZE 70
69 /* For long options that have no equivalent short option, use a
70 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
73 TARGET_DIRECTORY_OPTION = CHAR_MAX + 1,
74 STRIP_TRAILING_SLASHES_OPTION
83 /* The name this program was run with. */
86 static int remove_trailing_slashes;
88 static struct option const long_options[] =
90 {"backup", optional_argument, NULL, 'b'},
91 {"force", no_argument, NULL, 'f'},
92 {"interactive", no_argument, NULL, 'i'},
93 {"strip-trailing-slash", no_argument, NULL, STRIP_TRAILING_SLASHES_OPTION},
94 {"suffix", required_argument, NULL, 'S'},
95 {"target-directory", required_argument, NULL, TARGET_DIRECTORY_OPTION},
96 {"update", no_argument, NULL, 'u'},
97 {"verbose", no_argument, NULL, 'v'},
98 {"version-control", required_argument, NULL, 'V'},
99 {GETOPT_HELP_OPTION_DECL},
100 {GETOPT_VERSION_OPTION_DECL},
105 rm_option_init (struct rm_options *x)
109 /* FIXME: maybe this should be 1. The POSIX spec doesn't specify. */
110 x->ignore_missing_files = 0;
114 /* Should we prompt for removal, too? No. Prompting for the `move'
115 part is enough. It implies removal. */
123 cp_option_init (struct cp_options *x)
125 x->copy_as_regular = 0; /* FIXME: maybe make this an option */
128 x->failed_unlink_is_fatal = 1;
132 x->myeuid = geteuid ();
133 x->one_file_system = 0;
134 x->preserve_owner_and_group = 1;
135 x->preserve_chmod_bits = 1;
136 x->preserve_timestamps = 1;
137 x->require_preserve = 0; /* FIXME: maybe make this an option */
139 x->sparse_mode = SPARSE_AUTO; /* FIXME: maybe make this an option */
140 x->symbolic_link = 0;
144 /* Find out the current file creation mask, to knock the right bits
145 when using chmod. The creation mask is set to be liberal, so
146 that created directories can be written, even if it would not
147 have been allowed with the mask this process was started with. */
148 x->umask_kill = ~ umask (0);
155 /* If PATH is an existing directory, return nonzero, else 0. */
158 is_real_dir (const char *path)
162 return lstat (path, &stats) == 0 && S_ISDIR (stats.st_mode);
165 /* Move SOURCE onto DEST. Handles cross-filesystem moves.
166 If SOURCE is a directory, DEST must not exist.
167 Return 0 if successful, non-zero if an error occurred. */
170 do_move (const char *source, const char *dest, const struct cp_options *x)
172 static int first = 1;
174 int rename_succeeded;
181 /* Allocate space for remembering copied and created files. */
182 hash_init (INITIAL_HASH_MODULE, INITIAL_ENTRY_TAB_SIZE);
185 fail = copy (source, dest, 0, x, ©_into_self, &rename_succeeded);
189 const char *dir_to_remove;
192 /* In general, when copy returns with copy_into_self set, SOURCE is
193 the same as, or a parent of DEST. In this case we know it's a
194 parent. It doesn't make sense to move a directory into itself, and
195 besides in some situations doing so would give highly nonintuitive
196 results. Run this `mkdir b; touch a c; mv * b' in an empty
197 directory. Here's the result of running echo `find b -print`:
198 b b/a b/b b/b/a b/c. Notice that only file `a' was copied
199 into b/b. Handle this by giving a diagnostic, removing the
200 copied-into-self directory, DEST (`b/b' in the example),
203 dir_to_remove = NULL;
205 _("cannot move `%s' to a subdirectory of itself, `%s'"),
208 else if (rename_succeeded)
210 /* No need to remove anything. SOURCE was successfully
212 dir_to_remove = NULL;
216 /* This may mean SOURCE and DEST referred to different devices.
217 It may also conceivably mean that even though they referred
218 to the same device, rename wasn't implemented for that device.
220 E.g., (from Joel N. Weber),
221 [...] there might someday be cases where you can't rename
222 but you can copy where the device name is the same, especially
223 on Hurd. Consider an ftpfs with a primitive ftp server that
224 supports uploading, downloading and deleting, but not renaming.
226 Also, note that comparing device numbers is not a reliable
227 check for `can-rename'. Some systems can be set up so that
228 files from many different physical devices all have the same
229 st_dev field. This is a feature of some NFS mounting
232 We reach this point if SOURCE has been successfully copied
233 to DEST. Now we have to remove SOURCE.
235 This function used to resort to copying only when rename
236 failed and set errno to EXDEV. */
238 dir_to_remove = source;
241 if (dir_to_remove != NULL)
243 struct rm_options rm_options;
245 enum RM_status status;
247 rm_option_init (&rm_options);
248 rm_options.verbose = x->verbose;
252 fspec_init_file (&fs, dir_to_remove);
253 status = rm (&fs, 1, &rm_options);
254 assert (VALID_STATUS (status));
255 if (status == RM_ERROR)
261 error (0, errno, _("cannot remove `%s'"), dir_to_remove);
272 strip_trailing_slashes_2 (char *path)
274 char *end_p = path + strlen (path) - 1;
277 while (slash > path && *slash == '/')
280 return slash < end_p;
283 /* Move file SOURCE onto DEST. Handles the case when DEST is a directory.
284 DEST_IS_DIR must be nonzero when DEST is a directory or a symlink to a
285 directory and zero otherwise.
286 Return 0 if successful, non-zero if an error occurred. */
289 movefile (char *source, char *dest, int dest_is_dir,
290 const struct cp_options *x)
292 int dest_had_trailing_slash = strip_trailing_slashes_2 (dest);
295 /* This code was introduced to handle the ambiguity in the semantics
296 of mv that is induced by the varying semantics of the rename function.
297 Some systems (e.g., Linux) have a rename function that honors a
298 trailing slash, while others (like Solaris 5,6,7) have a rename
299 function that ignores a trailing slash. I believe the Linux
300 rename semantics are POSIX and susv2 compliant. */
302 if (remove_trailing_slashes)
303 strip_trailing_slashes_2 (source);
305 /* In addition to when DEST is a directory, if DEST has a trailing
306 slash and neither SOURCE nor DEST is a directory, presume the target
307 is DEST/`basename source`. This converts `mv x y/' to `mv x y/x'.
308 This change means that the command `mv any file/' will now fail
309 rather than performing the move. The case when SOURCE is a
310 directory and DEST is not is properly diagnosed by do_move. */
312 if (dest_is_dir || (dest_had_trailing_slash && !is_real_dir (source)))
314 /* DEST is a directory; build full target filename. */
318 /* Remove trailing slashes before taking base_name.
319 Otherwise, base_name ("a/") returns "". */
320 strip_trailing_slashes_2 (source);
322 src_basename = base_name (source);
323 new_dest = path_concat (dest, src_basename, NULL);
324 if (new_dest == NULL)
325 error (1, 0, _("virtual memory exhausted"));
326 fail = do_move (source, new_dest, x);
328 /* Do not free new_dest. It may have been squirelled away by
329 the remember_copied function. */
333 fail = do_move (source, dest, x);
343 fprintf (stderr, _("Try `%s --help' for more information.\n"),
348 Usage: %s [OPTION]... SOURCE DEST\n\
349 or: %s [OPTION]... SOURCE... DIRECTORY\n\
350 or: %s [OPTION]... --target-directory=DIRECTORY SOURCE...\n\
352 program_name, program_name, program_name);
354 Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n\
356 -b, --backup[=CONTROL] make backup before removal\n\
357 -f, --force remove existing destinations, never prompt\n\
358 -i, --interactive prompt before overwrite\n\
359 --strip-trailing-slashes remove any trailing slashes from each SOURCE\n\
361 -S, --suffix=SUFFIX override the usual backup suffix\n\
362 --target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY\n\
363 -u, --update move only older or brand new non-directories\n\
364 -v, --verbose explain what is being done\n\
365 --help display this help and exit\n\
366 --version output version information and exit\n\
370 The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
371 The version control method may be selected via the --backup option or through\n\
372 the VERSION_CONTROL environment variable. Here are the values:\n\
374 none, off never make backups (even if --backup is given)\n\
375 numbered, t make numbered backups\n\
376 existing, nil numbered if numbered backups exist, simple otherwise\n\
377 simple, never always make simple backups\n\
379 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
386 main (int argc, char **argv)
390 int make_backups = 0;
392 char *backup_suffix_string;
393 char *version_control_string = NULL;
395 char *target_directory = NULL;
396 int target_directory_specified;
397 unsigned int n_files;
400 program_name = argv[0];
401 setlocale (LC_ALL, "");
402 bindtextdomain (PACKAGE, LOCALEDIR);
403 textdomain (PACKAGE);
407 /* FIXME: consider not calling getenv for SIMPLE_BACKUP_SUFFIX unless
408 we'll actually use backup_suffix_string. */
409 backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
413 while ((c = getopt_long (argc, argv, "bfiuvS:V:", long_options, NULL)) != -1)
420 case 'V': /* FIXME: this is deprecated. Remove it in 2001. */
422 _("warning: --version-control (-V) is obsolete; support for\
423 it\nwill be removed in some future release. Use --backup=%s instead."
430 version_control_string = optarg;
440 case STRIP_TRAILING_SLASHES_OPTION:
441 remove_trailing_slashes = 1;
443 case TARGET_DIRECTORY_OPTION:
444 target_directory = optarg;
454 backup_suffix_string = optarg;
456 case_GETOPT_HELP_CHAR;
457 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
463 n_files = argc - optind;
464 file = argv + optind;
466 target_directory_specified = (target_directory != NULL);
467 if (target_directory == NULL)
469 /* Be careful below. When n_files is 0, this value will be
470 argv[0], `--', or an option string. */
471 target_directory = file[n_files - 1];
474 dest_is_dir = (n_files > 0 && isdir (target_directory));
476 if (target_directory_specified)
480 error (0, 0, _("specified target, `%s' is not a directory"),
487 error (0, 0, "%s", _("missing file argument"));
495 error (0, 0, "%s", (n_files == 0
496 ? _("missing file arguments")
497 : _("missing file argument")));
501 if (n_files > 2 && !dest_is_dir)
504 _("when moving multiple files, last argument must be a directory"));
509 if (backup_suffix_string)
510 simple_backup_suffix = xstrdup (backup_suffix_string);
512 x.backup_type = (make_backups
513 ? xget_version (_("--version-control"),
514 version_control_string)
517 /* Move each arg but the last into the target_directory. */
519 unsigned int last_file_idx = (target_directory_specified
523 for (i = 0; i <= last_file_idx; ++i)
524 errors |= movefile (file[i], target_directory, dest_is_dir, &x);