1 /* mv -- move or rename files
2 Copyright (C) 86, 89, 90, 91, 1995-2009 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 3 of the License, or
7 (at your option) any later version.
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, see <http://www.gnu.org/licenses/>. */
17 /* Written by Mike Parker, David MacKenzie, and Jim Meyering */
22 #include <sys/types.h>
24 #include <selinux/selinux.h>
27 #include "backupfile.h"
31 #include "filenamecat.h"
34 #include "root-dev-ino.h"
37 /* The official name of this program (e.g., no `g' prefix). */
38 #define PROGRAM_NAME "mv"
41 proper_name ("Mike Parker"), \
42 proper_name ("David MacKenzie"), \
43 proper_name ("Jim Meyering")
45 /* For long options that have no equivalent short option, use a
46 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
49 STRIP_TRAILING_SLASHES_OPTION
= CHAR_MAX
+ 1
52 /* Remove any trailing slashes from each SOURCE argument. */
53 static bool remove_trailing_slashes
;
55 static struct option
const long_options
[] =
57 {"backup", optional_argument
, NULL
, 'b'},
58 {"force", no_argument
, NULL
, 'f'},
59 {"interactive", no_argument
, NULL
, 'i'},
60 {"no-clobber", no_argument
, NULL
, 'n'},
61 {"no-target-directory", no_argument
, NULL
, 'T'},
62 {"strip-trailing-slashes", no_argument
, NULL
, STRIP_TRAILING_SLASHES_OPTION
},
63 {"suffix", required_argument
, NULL
, 'S'},
64 {"target-directory", required_argument
, NULL
, 't'},
65 {"update", no_argument
, NULL
, 'u'},
66 {"verbose", no_argument
, NULL
, 'v'},
67 {GETOPT_HELP_OPTION_DECL
},
68 {GETOPT_VERSION_OPTION_DECL
},
73 rm_option_init (struct rm_options
*x
)
75 x
->ignore_missing_files
= false;
77 x
->one_file_system
= false;
79 /* Should we prompt for removal, too? No. Prompting for the `move'
80 part is enough. It implies removal. */
81 x
->interactive
= RMI_NEVER
;
86 /* Since this program may well have to process additional command
87 line arguments after any call to `rm', that function must preserve
88 the initial working directory, in case one of those is a
90 x
->require_restore_cwd
= true;
93 static struct dev_ino dev_ino_buf
;
94 x
->root_dev_ino
= get_root_dev_ino (&dev_ino_buf
);
95 if (x
->root_dev_ino
== NULL
)
96 error (EXIT_FAILURE
, errno
, _("failed to get attributes of %s"),
102 cp_option_init (struct cp_options
*x
)
104 bool selinux_enabled
= (0 < is_selinux_enabled ());
106 cp_options_default (x
);
107 x
->copy_as_regular
= false; /* FIXME: maybe make this an option */
108 x
->dereference
= DEREF_NEVER
;
109 x
->unlink_dest_before_opening
= false;
110 x
->unlink_dest_after_failed_open
= false;
111 x
->hard_link
= false;
112 x
->interactive
= I_UNSPECIFIED
;
114 x
->one_file_system
= false;
115 x
->preserve_ownership
= true;
116 x
->preserve_links
= true;
117 x
->preserve_mode
= true;
118 x
->preserve_timestamps
= true;
119 x
->preserve_security_context
= selinux_enabled
;
120 x
->reduce_diagnostics
= false;
121 x
->require_preserve
= false; /* FIXME: maybe make this an option */
122 x
->require_preserve_context
= false;
123 x
->preserve_xattr
= true;
124 x
->require_preserve_xattr
= false;
126 x
->sparse_mode
= SPARSE_AUTO
; /* FIXME: maybe make this an option */
127 x
->symbolic_link
= false;
130 x
->stdin_tty
= isatty (STDIN_FILENO
);
132 x
->open_dangling_dest_symlink
= false;
139 /* FILE is the last operand of this command. Return true if FILE is a
140 directory. But report an error if there is a problem accessing FILE, other
141 than nonexistence (errno == ENOENT). */
144 target_directory_operand (char const *file
)
147 int err
= (stat (file
, &st
) == 0 ? 0 : errno
);
148 bool is_a_dir
= !err
&& S_ISDIR (st
.st_mode
);
149 if (err
&& err
!= ENOENT
)
150 error (EXIT_FAILURE
, err
, _("accessing %s"), quote (file
));
154 /* Move SOURCE onto DEST. Handles cross-file-system moves.
155 If SOURCE is a directory, DEST must not exist.
156 Return true if successful. */
159 do_move (const char *source
, const char *dest
, const struct cp_options
*x
)
162 bool rename_succeeded
;
163 bool ok
= copy (source
, dest
, false, x
, ©_into_self
, &rename_succeeded
);
167 char const *dir_to_remove
;
170 /* In general, when copy returns with copy_into_self set, SOURCE is
171 the same as, or a parent of DEST. In this case we know it's a
172 parent. It doesn't make sense to move a directory into itself, and
173 besides in some situations doing so would give highly nonintuitive
174 results. Run this `mkdir b; touch a c; mv * b' in an empty
175 directory. Here's the result of running echo `find b -print`:
176 b b/a b/b b/b/a b/c. Notice that only file `a' was copied
177 into b/b. Handle this by giving a diagnostic, removing the
178 copied-into-self directory, DEST (`b/b' in the example),
181 dir_to_remove
= NULL
;
184 else if (rename_succeeded
)
186 /* No need to remove anything. SOURCE was successfully
187 renamed to DEST. Or the user declined to rename a file. */
188 dir_to_remove
= NULL
;
192 /* This may mean SOURCE and DEST referred to different devices.
193 It may also conceivably mean that even though they referred
194 to the same device, rename wasn't implemented for that device.
196 E.g., (from Joel N. Weber),
197 [...] there might someday be cases where you can't rename
198 but you can copy where the device name is the same, especially
199 on Hurd. Consider an ftpfs with a primitive ftp server that
200 supports uploading, downloading and deleting, but not renaming.
202 Also, note that comparing device numbers is not a reliable
203 check for `can-rename'. Some systems can be set up so that
204 files from many different physical devices all have the same
205 st_dev field. This is a feature of some NFS mounting
208 We reach this point if SOURCE has been successfully copied
209 to DEST. Now we have to remove SOURCE.
211 This function used to resort to copying only when rename
212 failed and set errno to EXDEV. */
214 dir_to_remove
= source
;
217 if (dir_to_remove
!= NULL
)
219 struct rm_options rm_options
;
220 enum RM_status status
;
222 rm_option_init (&rm_options
);
223 rm_options
.verbose
= x
->verbose
;
225 status
= rm (1, &dir_to_remove
, &rm_options
);
226 assert (VALID_STATUS (status
));
227 if (status
== RM_ERROR
)
235 /* Move file SOURCE onto DEST. Handles the case when DEST is a directory.
236 Treat DEST as a directory if DEST_IS_DIR.
237 Return true if successful. */
240 movefile (char *source
, char *dest
, bool dest_is_dir
,
241 const struct cp_options
*x
)
245 /* This code was introduced to handle the ambiguity in the semantics
246 of mv that is induced by the varying semantics of the rename function.
247 Some systems (e.g., GNU/Linux) have a rename function that honors a
248 trailing slash, while others (like Solaris 5,6,7) have a rename
249 function that ignores a trailing slash. I believe the GNU/Linux
250 rename semantics are POSIX and susv2 compliant. */
252 if (remove_trailing_slashes
)
253 strip_trailing_slashes (source
);
257 /* Treat DEST as a directory; build the full filename. */
258 char const *src_basename
= last_component (source
);
259 char *new_dest
= file_name_concat (dest
, src_basename
, NULL
);
260 strip_trailing_slashes (new_dest
);
261 ok
= do_move (source
, new_dest
, x
);
266 ok
= do_move (source
, dest
, x
);
275 if (status
!= EXIT_SUCCESS
)
276 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
281 Usage: %s [OPTION]... [-T] SOURCE DEST\n\
282 or: %s [OPTION]... SOURCE... DIRECTORY\n\
283 or: %s [OPTION]... -t DIRECTORY SOURCE...\n\
285 program_name
, program_name
, program_name
);
287 Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n\
291 Mandatory arguments to long options are mandatory for short options too.\n\
294 --backup[=CONTROL] make a backup of each existing destination file\n\
295 -b like --backup but does not accept an argument\n\
296 -f, --force do not prompt before overwriting\n\
297 -i, --interactive prompt before overwrite\n\
298 -n, --no-clobber do not overwrite an existing file\n\
299 If you specify more than one of -i, -f, -n, only the final one takes effect.\n\
302 --strip-trailing-slashes remove any trailing slashes from each SOURCE\n\
304 -S, --suffix=SUFFIX override the usual backup suffix\n\
307 -t, --target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY\n\
308 -T, --no-target-directory treat DEST as a normal file\n\
309 -u, --update move only when the SOURCE file is newer\n\
310 than the destination file or when the\n\
311 destination file is missing\n\
312 -v, --verbose explain what is being done\n\
314 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
315 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
318 The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
319 The version control method may be selected via the --backup option or through\n\
320 the VERSION_CONTROL environment variable. Here are the values:\n\
324 none, off never make backups (even if --backup is given)\n\
325 numbered, t make numbered backups\n\
326 existing, nil numbered if numbered backups exist, simple otherwise\n\
327 simple, never always make simple backups\n\
329 emit_bug_reporting_address ();
335 main (int argc
, char **argv
)
339 bool make_backups
= false;
340 char *backup_suffix_string
;
341 char *version_control_string
= NULL
;
343 char *target_directory
= NULL
;
344 bool no_target_directory
= false;
348 initialize_main (&argc
, &argv
);
349 set_program_name (argv
[0]);
350 setlocale (LC_ALL
, "");
351 bindtextdomain (PACKAGE
, LOCALEDIR
);
352 textdomain (PACKAGE
);
354 atexit (close_stdin
);
358 /* Try to disable the ability to unlink a directory. */
359 priv_set_remove_linkdir ();
361 /* FIXME: consider not calling getenv for SIMPLE_BACKUP_SUFFIX unless
362 we'll actually use backup_suffix_string. */
363 backup_suffix_string
= getenv ("SIMPLE_BACKUP_SUFFIX");
365 while ((c
= getopt_long (argc
, argv
, "bfint:uvS:T", long_options
, NULL
))
373 version_control_string
= optarg
;
376 x
.interactive
= I_ALWAYS_YES
;
379 x
.interactive
= I_ASK_USER
;
382 x
.interactive
= I_ALWAYS_NO
;
384 case STRIP_TRAILING_SLASHES_OPTION
:
385 remove_trailing_slashes
= true;
388 if (target_directory
)
389 error (EXIT_FAILURE
, 0, _("multiple target directories specified"));
393 if (stat (optarg
, &st
) != 0)
394 error (EXIT_FAILURE
, errno
, _("accessing %s"), quote (optarg
));
395 if (! S_ISDIR (st
.st_mode
))
396 error (EXIT_FAILURE
, 0, _("target %s is not a directory"),
399 target_directory
= optarg
;
402 no_target_directory
= true;
412 backup_suffix_string
= optarg
;
414 case_GETOPT_HELP_CHAR
;
415 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
417 usage (EXIT_FAILURE
);
421 n_files
= argc
- optind
;
422 file
= argv
+ optind
;
424 if (n_files
<= !target_directory
)
427 error (0, 0, _("missing file operand"));
429 error (0, 0, _("missing destination file operand after %s"),
431 usage (EXIT_FAILURE
);
434 if (no_target_directory
)
436 if (target_directory
)
437 error (EXIT_FAILURE
, 0,
438 _("cannot combine --target-directory (-t) "
439 "and --no-target-directory (-T)"));
442 error (0, 0, _("extra operand %s"), quote (file
[2]));
443 usage (EXIT_FAILURE
);
446 else if (!target_directory
)
448 assert (2 <= n_files
);
449 if (target_directory_operand (file
[n_files
- 1]))
450 target_directory
= file
[--n_files
];
451 else if (2 < n_files
)
452 error (EXIT_FAILURE
, 0, _("target %s is not a directory"),
453 quote (file
[n_files
- 1]));
456 if (make_backups
&& x
.interactive
== I_ALWAYS_NO
)
459 _("options --backup and --no-clobber are mutually exclusive"));
460 usage (EXIT_FAILURE
);
463 if (backup_suffix_string
)
464 simple_backup_suffix
= xstrdup (backup_suffix_string
);
466 x
.backup_type
= (make_backups
467 ? xget_version (_("backup type"),
468 version_control_string
)
473 if (target_directory
)
477 /* Initialize the hash table only if we'll need it.
478 The problem it is used to detect can arise only if there are
479 two or more files to move. */
484 for (i
= 0; i
< n_files
; ++i
)
485 ok
&= movefile (file
[i
], target_directory
, true, &x
);
488 ok
= movefile (file
[0], file
[1], false, &x
);
490 exit (ok
? EXIT_SUCCESS
: EXIT_FAILURE
);