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. */
18 /* Written by Mike Parker, David MacKenzie, and Jim Meyering */
27 #include <sys/types.h>
31 #include "backupfile.h"
35 #include "path-concat.h"
39 /* The official name of this program (e.g., no `g' prefix). */
40 #define PROGRAM_NAME "mv"
42 #define AUTHORS "Mike Parker, David MacKenzie, and Jim Meyering"
44 /* Initial number of entries in each hash table entry's table of inodes. */
45 #define INITIAL_HASH_MODULE 100
47 /* Initial number of entries in the inode hash table. */
48 #define INITIAL_ENTRY_TAB_SIZE 70
50 /* For long options that have no equivalent short option, use a
51 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
54 TARGET_DIRECTORY_OPTION = CHAR_MAX + 1,
55 STRIP_TRAILING_SLASHES_OPTION
64 /* The name this program was run with. */
67 /* Remove any trailing slashes from each SOURCE argument. */
68 static int remove_trailing_slashes;
70 static struct option const long_options[] =
72 {"backup", optional_argument, NULL, 'b'},
73 {"force", no_argument, NULL, 'f'},
74 {"interactive", no_argument, NULL, 'i'},
75 {"strip-trailing-slash", no_argument, NULL, STRIP_TRAILING_SLASHES_OPTION},
76 {"suffix", required_argument, NULL, 'S'},
77 {"target-directory", required_argument, NULL, TARGET_DIRECTORY_OPTION},
78 {"update", no_argument, NULL, 'u'},
79 {"verbose", no_argument, NULL, 'v'},
80 {"version-control", required_argument, NULL, 'V'},
81 {GETOPT_HELP_OPTION_DECL},
82 {GETOPT_VERSION_OPTION_DECL},
87 rm_option_init (struct rm_options *x)
91 /* FIXME: maybe this should be 1. The POSIX spec doesn't specify. */
92 x->ignore_missing_files = 0;
96 /* Should we prompt for removal, too? No. Prompting for the `move'
97 part is enough. It implies removal. */
105 cp_option_init (struct cp_options *x)
107 x->copy_as_regular = 0; /* FIXME: maybe make this an option */
108 x->dereference = DEREF_NEVER;
109 x->unlink_dest_before_opening = 0;
110 x->unlink_dest_after_failed_open = 0;
111 x->failed_unlink_is_fatal = 1;
115 x->myeuid = geteuid ();
116 x->one_file_system = 0;
117 x->preserve_owner_and_group = 1;
118 x->preserve_chmod_bits = 1;
119 x->preserve_timestamps = 1;
120 x->require_preserve = 0; /* FIXME: maybe make this an option */
122 x->sparse_mode = SPARSE_AUTO; /* FIXME: maybe make this an option */
123 x->symbolic_link = 0;
127 /* Find out the current file creation mask, to knock the right bits
128 when using chmod. The creation mask is set to be liberal, so
129 that created directories can be written, even if it would not
130 have been allowed with the mask this process was started with. */
131 x->umask_kill = ~ umask (0);
138 /* If PATH is an existing directory, return nonzero, else 0. */
141 is_real_dir (const char *path)
145 return lstat (path, &stats) == 0 && S_ISDIR (stats.st_mode);
149 strip_trailing_slashes_2 (char *path)
151 char *end_p = path + strlen (path) - 1;
154 while (slash > path && *slash == '/')
157 return slash < end_p;
160 /* Move SOURCE onto DEST. Handles cross-filesystem moves.
161 If SOURCE is a directory, DEST must not exist.
162 Return 0 if successful, non-zero if an error occurred. */
165 do_move (const char *source, const char *dest, const struct cp_options *x)
167 static int first = 1;
169 int rename_succeeded;
176 /* Allocate space for remembering copied and created files. */
177 hash_init (INITIAL_HASH_MODULE, INITIAL_ENTRY_TAB_SIZE);
180 fail = copy (source, dest, 0, x, ©_into_self, &rename_succeeded);
184 const char *dir_to_remove;
187 /* In general, when copy returns with copy_into_self set, SOURCE is
188 the same as, or a parent of DEST. In this case we know it's a
189 parent. It doesn't make sense to move a directory into itself, and
190 besides in some situations doing so would give highly nonintuitive
191 results. Run this `mkdir b; touch a c; mv * b' in an empty
192 directory. Here's the result of running echo `find b -print`:
193 b b/a b/b b/b/a b/c. Notice that only file `a' was copied
194 into b/b. Handle this by giving a diagnostic, removing the
195 copied-into-self directory, DEST (`b/b' in the example),
198 dir_to_remove = NULL;
200 _("cannot move %s to a subdirectory of itself, %s"),
201 quote_n (0, source), quote_n (1, dest));
203 else if (rename_succeeded)
205 /* No need to remove anything. SOURCE was successfully
207 dir_to_remove = NULL;
211 /* This may mean SOURCE and DEST referred to different devices.
212 It may also conceivably mean that even though they referred
213 to the same device, rename wasn't implemented for that device.
215 E.g., (from Joel N. Weber),
216 [...] there might someday be cases where you can't rename
217 but you can copy where the device name is the same, especially
218 on Hurd. Consider an ftpfs with a primitive ftp server that
219 supports uploading, downloading and deleting, but not renaming.
221 Also, note that comparing device numbers is not a reliable
222 check for `can-rename'. Some systems can be set up so that
223 files from many different physical devices all have the same
224 st_dev field. This is a feature of some NFS mounting
227 We reach this point if SOURCE has been successfully copied
228 to DEST. Now we have to remove SOURCE.
230 This function used to resort to copying only when rename
231 failed and set errno to EXDEV. */
233 dir_to_remove = source;
236 if (dir_to_remove != NULL)
238 struct rm_options rm_options;
240 enum RM_status status;
242 rm_option_init (&rm_options);
243 rm_options.verbose = x->verbose;
247 fspec_init_file (&fs, dir_to_remove);
249 /* Remove any trailing slashes. This is necessary if we
250 took the else branch of movefile. */
251 strip_trailing_slashes_2 (fs.filename);
253 status = rm (&fs, 1, &rm_options);
254 assert (VALID_STATUS (status));
255 if (status == RM_ERROR)
261 error (0, errno, _("cannot remove %s"), quote (dir_to_remove));
271 /* Move file SOURCE onto DEST. Handles the case when DEST is a directory.
272 DEST_IS_DIR must be nonzero when DEST is a directory or a symlink to a
273 directory and zero otherwise.
274 Return 0 if successful, non-zero if an error occurred. */
277 movefile (char *source, char *dest, int dest_is_dir,
278 const struct cp_options *x)
280 int dest_had_trailing_slash = strip_trailing_slashes_2 (dest);
283 /* This code was introduced to handle the ambiguity in the semantics
284 of mv that is induced by the varying semantics of the rename function.
285 Some systems (e.g., Linux) have a rename function that honors a
286 trailing slash, while others (like Solaris 5,6,7) have a rename
287 function that ignores a trailing slash. I believe the Linux
288 rename semantics are POSIX and susv2 compliant. */
290 if (remove_trailing_slashes)
291 strip_trailing_slashes_2 (source);
293 /* In addition to when DEST is a directory, if DEST has a trailing
294 slash and neither SOURCE nor DEST is a directory, presume the target
295 is DEST/`basename source`. This converts `mv x y/' to `mv x y/x'.
296 This change means that the command `mv any file/' will now fail
297 rather than performing the move. The case when SOURCE is a
298 directory and DEST is not is properly diagnosed by do_move. */
300 if (dest_is_dir || (dest_had_trailing_slash && !is_real_dir (source)))
302 /* DEST is a directory; build full target filename. */
306 /* Remove trailing slashes before taking base_name.
307 Otherwise, base_name ("a/") returns "". */
308 strip_trailing_slashes_2 (source);
310 src_basename = base_name (source);
311 new_dest = path_concat (dest, src_basename, NULL);
312 if (new_dest == NULL)
314 fail = do_move (source, new_dest, x);
316 /* Do not free new_dest. It may have been squirreled away by
317 the remember_copied function. */
321 fail = do_move (source, dest, x);
331 fprintf (stderr, _("Try `%s --help' for more information.\n"),
336 Usage: %s [OPTION]... SOURCE DEST\n\
337 or: %s [OPTION]... SOURCE... DIRECTORY\n\
338 or: %s [OPTION]... --target-directory=DIRECTORY SOURCE...\n\
340 program_name, program_name, program_name);
342 Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n\
344 --backup[=CONTROL] make a backup of each existing destination file\n\
345 -b like --backup but does not accept an argument\n\
346 -f, --force never prompt before overwriting\n\
347 -i, --interactive prompt before overwrite\n\
348 --strip-trailing-slashes remove any trailing slashes from each SOURCE\n\
350 -S, --suffix=SUFFIX override the usual backup suffix\n\
351 --target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY\n\
352 -u, --update move only older or brand new non-directories\n\
353 -v, --verbose explain what is being done\n\
354 --help display this help and exit\n\
355 --version output version information and exit\n\
359 The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
360 The version control method may be selected via the --backup option or through\n\
361 the VERSION_CONTROL environment variable. Here are the values:\n\
363 none, off never make backups (even if --backup is given)\n\
364 numbered, t make numbered backups\n\
365 existing, nil numbered if numbered backups exist, simple otherwise\n\
366 simple, never always make simple backups\n\
368 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
374 main (int argc, char **argv)
378 int make_backups = 0;
380 char *backup_suffix_string;
381 char *version_control_string = NULL;
383 char *target_directory = NULL;
384 int target_directory_specified;
385 unsigned int n_files;
388 program_name = argv[0];
389 setlocale (LC_ALL, "");
390 bindtextdomain (PACKAGE, LOCALEDIR);
391 textdomain (PACKAGE);
393 atexit (close_stdout);
397 /* FIXME: consider not calling getenv for SIMPLE_BACKUP_SUFFIX unless
398 we'll actually use backup_suffix_string. */
399 backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
403 while ((c = getopt_long (argc, argv, "bfiuvS:V:", long_options, NULL)) != -1)
410 case 'V': /* FIXME: this is deprecated. Remove it in 2001. */
412 _("warning: --version-control (-V) is obsolete; support for\
413 it\nwill be removed in some future release. Use --backup=%s instead."
420 version_control_string = optarg;
428 case STRIP_TRAILING_SLASHES_OPTION:
429 remove_trailing_slashes = 1;
431 case TARGET_DIRECTORY_OPTION:
432 target_directory = optarg;
442 backup_suffix_string = optarg;
444 case_GETOPT_HELP_CHAR;
445 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
451 n_files = argc - optind;
452 file = argv + optind;
454 target_directory_specified = (target_directory != NULL);
455 if (target_directory == NULL && n_files != 0)
456 target_directory = file[n_files - 1];
458 dest_is_dir = (n_files > 0 && isdir (target_directory));
460 if (n_files == 0 || (n_files == 1 && !target_directory_specified))
462 error (0, 0, _("missing file argument"));
466 if (target_directory_specified)
470 error (0, 0, _("specified target, %s is not a directory"),
471 quote (target_directory));
475 else if (n_files > 2 && !dest_is_dir)
478 _("when moving multiple files, last argument must be a directory"));
482 if (backup_suffix_string)
483 simple_backup_suffix = xstrdup (backup_suffix_string);
485 x.backup_type = (make_backups
486 ? xget_version (_("backup type"),
487 version_control_string)
490 /* Move each arg but the last into the target_directory. */
492 unsigned int last_file_idx = (target_directory_specified
496 for (i = 0; i <= last_file_idx; ++i)
497 errors |= movefile (file[i], target_directory, dest_is_dir, &x);