1 /* mv -- move or rename files
2 Copyright (C) 86, 89, 90, 91, 1995-2003 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. */
18 /* Written by Mike Parker, David MacKenzie, and Jim Meyering */
23 #include <sys/types.h>
28 #include "backupfile.h"
33 #include "path-concat.h"
37 /* The official name of this program (e.g., no `g' prefix). */
38 #define PROGRAM_NAME "mv"
40 #define WRITTEN_BY _("Written by Mike Parker, David MacKenzie, and Jim Meyering.")
42 /* Initial number of entries in each hash table entry's table of inodes. */
43 #define INITIAL_HASH_MODULE 100
45 /* Initial number of entries in the inode hash table. */
46 #define INITIAL_ENTRY_TAB_SIZE 70
48 /* For long options that have no equivalent short option, use a
49 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
52 TARGET_DIRECTORY_OPTION = CHAR_MAX + 1,
53 STRIP_TRAILING_SLASHES_OPTION,
60 /* The name this program was run with. */
63 /* Remove any trailing slashes from each SOURCE argument. */
64 static int remove_trailing_slashes;
66 /* Valid arguments to the `--reply' option. */
67 static char const* const reply_args[] =
69 "yes", "no", "query", 0
72 /* The values that correspond to the above strings. */
73 static int const reply_vals[] =
75 I_ALWAYS_YES, I_ALWAYS_NO, I_ASK_USER
78 static struct option const long_options[] =
80 {"backup", optional_argument, NULL, 'b'},
81 {"force", no_argument, NULL, 'f'},
82 {"interactive", no_argument, NULL, 'i'},
83 {"reply", required_argument, NULL, REPLY_OPTION},
84 {"strip-trailing-slashes", no_argument, NULL, STRIP_TRAILING_SLASHES_OPTION},
85 {"suffix", required_argument, NULL, 'S'},
86 {"target-directory", required_argument, NULL, TARGET_DIRECTORY_OPTION},
87 {"update", no_argument, NULL, 'u'},
88 {"verbose", no_argument, NULL, 'v'},
89 {"version-control", required_argument, NULL, 'V'},
90 {GETOPT_HELP_OPTION_DECL},
91 {GETOPT_VERSION_OPTION_DECL},
96 rm_option_init (struct rm_options *x)
100 x->ignore_missing_files = 0;
104 /* Should we prompt for removal, too? No. Prompting for the `move'
105 part is enough. It implies removal. */
113 cp_option_init (struct cp_options *x)
115 x->copy_as_regular = 0; /* FIXME: maybe make this an option */
116 x->dereference = DEREF_NEVER;
117 x->unlink_dest_before_opening = 0;
118 x->unlink_dest_after_failed_open = 0;
120 x->interactive = I_UNSPECIFIED;
122 x->myeuid = geteuid ();
123 x->one_file_system = 0;
124 x->preserve_ownership = 1;
125 x->preserve_links = 1;
126 x->preserve_mode = 1;
127 x->preserve_timestamps = 1;
128 x->require_preserve = 0; /* FIXME: maybe make this an option */
130 x->sparse_mode = SPARSE_AUTO; /* FIXME: maybe make this an option */
131 x->symbolic_link = 0;
134 x->stdin_tty = isatty (STDIN_FILENO);
136 /* Find out the current file creation mask, to knock the right bits
137 when using chmod. The creation mask is set to be liberal, so
138 that created directories can be written, even if it would not
139 have been allowed with the mask this process was started with. */
140 x->umask_kill = ~ umask (0);
149 /* If PATH is an existing directory, return nonzero, else 0. */
152 is_real_dir (const char *path)
156 return lstat (path, &stats) == 0 && S_ISDIR (stats.st_mode);
159 /* Move SOURCE onto DEST. Handles cross-filesystem moves.
160 If SOURCE is a directory, DEST must not exist.
161 Return 0 if successful, non-zero if an error occurred. */
164 do_move (const char *source, const char *dest, const struct cp_options *x)
166 static int first = 1;
168 int rename_succeeded;
175 /* Allocate space for remembering copied and created files. */
179 fail = copy (source, dest, 0, x, ©_into_self, &rename_succeeded);
183 char const *dir_to_remove;
186 /* In general, when copy returns with copy_into_self set, SOURCE is
187 the same as, or a parent of DEST. In this case we know it's a
188 parent. It doesn't make sense to move a directory into itself, and
189 besides in some situations doing so would give highly nonintuitive
190 results. Run this `mkdir b; touch a c; mv * b' in an empty
191 directory. Here's the result of running echo `find b -print`:
192 b b/a b/b b/b/a b/c. Notice that only file `a' was copied
193 into b/b. Handle this by giving a diagnostic, removing the
194 copied-into-self directory, DEST (`b/b' in the example),
197 dir_to_remove = NULL;
200 else if (rename_succeeded)
202 /* No need to remove anything. SOURCE was successfully
203 renamed to DEST. Or the user declined to rename a file. */
204 dir_to_remove = NULL;
208 /* This may mean SOURCE and DEST referred to different devices.
209 It may also conceivably mean that even though they referred
210 to the same device, rename wasn't implemented for that device.
212 E.g., (from Joel N. Weber),
213 [...] there might someday be cases where you can't rename
214 but you can copy where the device name is the same, especially
215 on Hurd. Consider an ftpfs with a primitive ftp server that
216 supports uploading, downloading and deleting, but not renaming.
218 Also, note that comparing device numbers is not a reliable
219 check for `can-rename'. Some systems can be set up so that
220 files from many different physical devices all have the same
221 st_dev field. This is a feature of some NFS mounting
224 We reach this point if SOURCE has been successfully copied
225 to DEST. Now we have to remove SOURCE.
227 This function used to resort to copying only when rename
228 failed and set errno to EXDEV. */
230 dir_to_remove = source;
233 if (dir_to_remove != NULL)
235 struct rm_options rm_options;
236 enum RM_status status;
238 rm_option_init (&rm_options);
239 rm_options.verbose = x->verbose;
241 status = rm (1, &dir_to_remove, &rm_options);
242 assert (VALID_STATUS (status));
243 if (status == RM_ERROR)
251 /* Move file SOURCE onto DEST. Handles the case when DEST is a directory.
252 DEST_IS_DIR must be nonzero when DEST is a directory or a symlink to a
253 directory and zero otherwise.
254 Return 0 if successful, non-zero if an error occurred. */
257 movefile (char *source, char *dest, int dest_is_dir,
258 const struct cp_options *x)
260 int dest_had_trailing_slash = strip_trailing_slashes (dest);
263 /* This code was introduced to handle the ambiguity in the semantics
264 of mv that is induced by the varying semantics of the rename function.
265 Some systems (e.g., Linux) have a rename function that honors a
266 trailing slash, while others (like Solaris 5,6,7) have a rename
267 function that ignores a trailing slash. I believe the Linux
268 rename semantics are POSIX and susv2 compliant. */
270 if (remove_trailing_slashes)
271 strip_trailing_slashes (source);
273 /* In addition to when DEST is a directory, if DEST has a trailing
274 slash and neither SOURCE nor DEST is a directory, presume the target
275 is DEST/`basename source`. This converts `mv x y/' to `mv x y/x'.
276 This change means that the command `mv any file/' will now fail
277 rather than performing the move. The case when SOURCE is a
278 directory and DEST is not is properly diagnosed by do_move. */
280 if (dest_is_dir || (dest_had_trailing_slash && !is_real_dir (source)))
282 /* DEST is a directory; build full target filename. */
283 char const *src_basename = base_name (source);
284 char *new_dest = path_concat (dest, src_basename, NULL);
285 if (new_dest == NULL)
287 strip_trailing_slashes (new_dest);
288 fail = do_move (source, new_dest, x);
293 fail = do_move (source, dest, x);
303 fprintf (stderr, _("Try `%s --help' for more information.\n"),
308 Usage: %s [OPTION]... SOURCE DEST\n\
309 or: %s [OPTION]... SOURCE... DIRECTORY\n\
310 or: %s [OPTION]... --target-directory=DIRECTORY SOURCE...\n\
312 program_name, program_name, program_name);
314 Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n\
318 Mandatory arguments to long options are mandatory for short options too.\n\
321 --backup[=CONTROL] make a backup of each existing destination file\n\
322 -b like --backup but does not accept an argument\n\
323 -f, --force do not prompt before overwriting\n\
324 (equivalent to --reply=yes)\n\
325 -i, --interactive prompt before overwrite\n\
326 (equivalent to --reply=query)\n\
329 --reply={yes,no,query} specify how to handle the prompt about an\n\
330 existing destination file\n\
331 --strip-trailing-slashes remove any trailing slashes from each SOURCE\n\
333 -S, --suffix=SUFFIX override the usual backup suffix\n\
336 --target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY\n\
337 -u, --update move only when the SOURCE file is newer\n\
338 than the destination file or when the\n\
339 destination file is missing\n\
340 -v, --verbose explain what is being done\n\
342 fputs (HELP_OPTION_DESCRIPTION, stdout);
343 fputs (VERSION_OPTION_DESCRIPTION, stdout);
346 The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
347 The version control method may be selected via the --backup option or through\n\
348 the VERSION_CONTROL environment variable. Here are the values:\n\
352 none, off never make backups (even if --backup is given)\n\
353 numbered, t make numbered backups\n\
354 existing, nil numbered if numbered backups exist, simple otherwise\n\
355 simple, never always make simple backups\n\
357 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
363 main (int argc, char **argv)
367 int make_backups = 0;
369 char *backup_suffix_string;
370 char *version_control_string = NULL;
372 char *target_directory = NULL;
373 int target_directory_specified;
374 unsigned int n_files;
377 initialize_main (&argc, &argv);
378 program_name = argv[0];
379 setlocale (LC_ALL, "");
380 bindtextdomain (PACKAGE, LOCALEDIR);
381 textdomain (PACKAGE);
383 atexit (close_stdout);
387 /* FIXME: consider not calling getenv for SIMPLE_BACKUP_SUFFIX unless
388 we'll actually use backup_suffix_string. */
389 backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
393 while ((c = getopt_long (argc, argv, "bfiuvS:V:", long_options, NULL)) != -1)
400 case 'V': /* FIXME: this is deprecated. Remove it in 2001. */
402 _("warning: --version-control (-V) is obsolete; support for\
403 it\nwill be removed in some future release. Use --backup=%s instead."
410 version_control_string = optarg;
413 x.interactive = I_ALWAYS_YES;
416 x.interactive = I_ASK_USER;
419 x.interactive = XARGMATCH ("--reply", optarg,
420 reply_args, reply_vals);
422 case STRIP_TRAILING_SLASHES_OPTION:
423 remove_trailing_slashes = 1;
425 case TARGET_DIRECTORY_OPTION:
426 target_directory = optarg;
436 backup_suffix_string = optarg;
438 case_GETOPT_HELP_CHAR;
439 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, WRITTEN_BY);
441 usage (EXIT_FAILURE);
445 n_files = (optind < argc ? argc - optind : 0);
446 file = argv + optind;
448 target_directory_specified = (target_directory != NULL);
449 if (target_directory == NULL && n_files != 0)
450 target_directory = file[n_files - 1];
452 dest_is_dir = (n_files > 0 && isdir (target_directory));
454 if (n_files == 0 || (n_files == 1 && !target_directory_specified))
456 error (0, 0, _("missing file argument"));
457 usage (EXIT_FAILURE);
460 if (target_directory_specified)
464 error (0, 0, _("specified target, %s is not a directory"),
465 quote (target_directory));
466 usage (EXIT_FAILURE);
469 else if (n_files > 2 && !dest_is_dir)
472 _("when moving multiple files, last argument must be a directory"));
473 usage (EXIT_FAILURE);
476 if (backup_suffix_string)
477 simple_backup_suffix = xstrdup (backup_suffix_string);
479 x.backup_type = (make_backups
480 ? xget_version (_("backup type"),
481 version_control_string)
484 /* Move each arg but the last into the target_directory. */
486 unsigned int last_file_idx = (target_directory_specified
491 /* Initialize the hash table only if we'll need it.
492 The problem it is used to detect can arise only if there are
493 two or more files to move. */
497 for (i = 0; i <= last_file_idx; ++i)
498 errors |= movefile (file[i], target_directory, dest_is_dir, &x);