1 /* install - copy files and set attributes
2 Copyright (C) 89, 90, 91, 1995-2005 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Written by David MacKenzie <djm@gnu.ai.mit.edu> */
23 #include <sys/types.h>
29 #include "backupfile.h"
34 #include "filenamecat.h"
36 #include "modechange.h"
41 /* The official name of this program (e.g., no `g' prefix). */
42 #define PROGRAM_NAME "install"
44 #define AUTHORS "David MacKenzie"
47 # include <sys/wait.h>
51 # define endgrent() ((void) 0)
55 # define endpwent() ((void) 0)
58 /* Initial number of entries in each hash table entry's table of inodes. */
59 #define INITIAL_HASH_MODULE 100
61 /* Initial number of entries in the inode hash table. */
62 #define INITIAL_ENTRY_TAB_SIZE 70
64 /* Number of bytes of a file to copy at a time. */
65 #define READ_SIZE (32 * 1024)
67 static bool change_timestamps (const char *from, const char *to);
68 static bool change_attributes (char const *name);
69 static bool copy_file (const char *from, const char *to,
70 const struct cp_options *x);
71 static bool install_file_in_file_parents (char const *from, char const *to,
72 struct cp_options const *x);
73 static bool install_file_in_dir (const char *from, const char *to_dir,
74 const struct cp_options *x);
75 static bool install_file_in_file (const char *from, const char *to,
76 const struct cp_options *x);
77 static void get_ids (void);
78 static void strip (char const *name);
79 void usage (int status);
81 /* The name this program was run with, for error messages. */
84 /* The user name that will own the files, or NULL to make the owner
85 the current user ID. */
86 static char *owner_name;
88 /* The user ID corresponding to `owner_name'. */
89 static uid_t owner_id;
91 /* The group name that will own the files, or NULL to make the group
92 the current group ID. */
93 static char *group_name;
95 /* The group ID corresponding to `group_name'. */
96 static gid_t group_id;
98 /* The permissions to which the files will be set. The umask has
100 static mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
102 /* If true, strip executable files after copying them. */
103 static bool strip_files;
105 /* If true, install a directory instead of a regular file. */
108 static struct option const long_options[] =
110 {"backup", optional_argument, NULL, 'b'},
111 {"directory", no_argument, NULL, 'd'},
112 {"group", required_argument, NULL, 'g'},
113 {"mode", required_argument, NULL, 'm'},
114 {"no-target-directory", no_argument, NULL, 'T'},
115 {"owner", required_argument, NULL, 'o'},
116 {"preserve-timestamps", no_argument, NULL, 'p'},
117 {"strip", no_argument, NULL, 's'},
118 {"suffix", required_argument, NULL, 'S'},
119 {"target-directory", required_argument, NULL, 't'},
120 {"verbose", no_argument, NULL, 'v'},
121 {GETOPT_HELP_OPTION_DECL},
122 {GETOPT_VERSION_OPTION_DECL},
127 cp_option_init (struct cp_options *x)
129 x->copy_as_regular = true;
130 x->dereference = DEREF_ALWAYS;
131 x->unlink_dest_before_opening = true;
132 x->unlink_dest_after_failed_open = false;
133 x->hard_link = false;
134 x->interactive = I_UNSPECIFIED;
135 x->move_mode = false;
136 x->chown_privileges = chown_privileges ();
137 x->one_file_system = false;
138 x->preserve_ownership = false;
139 x->preserve_links = false;
140 x->preserve_mode = false;
141 x->preserve_timestamps = false;
142 x->require_preserve = false;
143 x->recursive = false;
144 x->sparse_mode = SPARSE_AUTO;
145 x->symbolic_link = false;
146 x->backup_type = no_backups;
148 /* Create destination files initially writable so we can run strip on them.
149 Although GNU strip works fine on read-only files, some others
152 x->mode = S_IRUSR | S_IWUSR;
153 x->stdin_tty = false;
162 /* FILE is the last operand of this command. Return true if FILE is a
163 directory. But report an error there is a problem accessing FILE,
164 or if FILE does not exist but would have to refer to an existing
165 directory if it referred to anything at all. */
168 target_directory_operand (char const *file)
170 char const *b = base_name (file);
171 size_t blen = strlen (b);
172 bool looks_like_a_dir = (blen == 0 || ISSLASH (b[blen - 1]));
174 int err = (stat (file, &st) == 0 ? 0 : errno);
175 bool is_a_dir = !err && S_ISDIR (st.st_mode);
176 if (err && err != ENOENT)
177 error (EXIT_FAILURE, err, _("accessing %s"), quote (file));
178 if (is_a_dir < looks_like_a_dir)
179 error (EXIT_FAILURE, err, _("target %s is not a directory"), quote (file));
184 main (int argc, char **argv)
188 const char *specified_mode = NULL;
189 bool make_backups = false;
190 char *backup_suffix_string;
191 char *version_control_string = NULL;
192 bool mkdir_and_install = false;
194 char const *target_directory = NULL;
195 bool no_target_directory = false;
199 initialize_main (&argc, &argv);
200 program_name = argv[0];
201 setlocale (LC_ALL, "");
202 bindtextdomain (PACKAGE, LOCALEDIR);
203 textdomain (PACKAGE);
205 atexit (close_stdout);
215 /* FIXME: consider not calling getenv for SIMPLE_BACKUP_SUFFIX unless
216 we'll actually use backup_suffix_string. */
217 backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
219 while ((optc = getopt_long (argc, argv, "bcsDdg:m:o:pt:TvS:", long_options,
227 version_control_string = optarg;
234 /* System V fork+wait does not work if SIGCHLD is ignored. */
235 signal (SIGCHLD, SIG_DFL);
242 mkdir_and_install = true;
251 specified_mode = optarg;
257 x.preserve_timestamps = true;
261 backup_suffix_string = optarg;
264 if (target_directory)
265 error (EXIT_FAILURE, 0,
266 _("multiple target directories specified"));
270 if (stat (optarg, &st) != 0)
271 error (EXIT_FAILURE, errno, _("accessing %s"), quote (optarg));
272 if (! S_ISDIR (st.st_mode))
273 error (EXIT_FAILURE, 0, _("target %s is not a directory"),
276 target_directory = optarg;
279 no_target_directory = true;
281 case_GETOPT_HELP_CHAR;
282 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
284 usage (EXIT_FAILURE);
288 /* Check for invalid combinations of arguments. */
289 if (dir_arg & strip_files)
290 error (EXIT_FAILURE, 0,
291 _("the strip option may not be used when installing a directory"));
292 if (dir_arg && target_directory)
293 error (EXIT_FAILURE, 0,
294 _("target directory not allowed when installing a directory"));
296 if (backup_suffix_string)
297 simple_backup_suffix = xstrdup (backup_suffix_string);
299 x.backup_type = (make_backups
300 ? xget_version (_("backup type"),
301 version_control_string)
304 n_files = argc - optind;
305 file = argv + optind;
307 if (n_files <= ! (dir_arg || target_directory))
310 error (0, 0, _("missing file operand"));
312 error (0, 0, _("missing destination file operand after %s"),
314 usage (EXIT_FAILURE);
317 if (no_target_directory)
319 if (target_directory)
320 error (EXIT_FAILURE, 0,
321 _("Cannot combine --target-directory (-t) "
322 "and --no-target-directory (-T)"));
325 error (0, 0, _("extra operand %s"), quote (file[2]));
326 usage (EXIT_FAILURE);
329 else if (! (dir_arg || target_directory))
331 if (2 <= n_files && target_directory_operand (file[n_files - 1]))
332 target_directory = file[--n_files];
333 else if (2 < n_files)
334 error (EXIT_FAILURE, 0, _("target %s is not a directory"),
335 quote (file[n_files - 1]));
340 struct mode_change *change = mode_compile (specified_mode);
342 error (EXIT_FAILURE, 0, _("invalid mode %s"), quote (specified_mode));
343 mode = mode_adjust (0, change, 0);
353 for (i = 0; i < n_files; i++)
355 if (cwd_errno != 0 && IS_RELATIVE_FILE_NAME (file[i]))
357 error (0, cwd_errno, _("cannot return to working directory"));
362 make_dir_parents (file[i], mode, mode, owner_id, group_id, false,
363 (x.verbose ? _("creating directory %s") : NULL),
369 /* FIXME: it's a little gross that this initialization is
370 required by copy.c::copy. */
373 if (!target_directory)
375 if (mkdir_and_install)
376 ok = install_file_in_file_parents (file[0], file[1], &x);
378 ok = install_file_in_file (file[0], file[1], &x);
384 for (i = 0; i < n_files; i++)
386 ok &= install_file_in_dir (file[i], target_directory, &x);
391 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
394 /* Copy file FROM onto file TO, creating any missing parent directories of TO.
395 Return true if successful. */
398 install_file_in_file_parents (char const *from, char const *to,
399 struct cp_options const *x)
401 char *dest_dir = dir_name (to);
404 /* Make sure that the parent of the destination is a directory. */
405 if (! STREQ (dest_dir, "."))
407 /* Someone will probably ask for a new option or three to specify
408 owner, group, and permissions for parent directories. Remember
409 that this option is intended mainly to help installers when the
410 distribution doesn't provide proper install rules. */
411 mode_t dir_mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
413 ok = make_dir_parents (dest_dir, dir_mode, dir_mode,
414 owner_id, group_id, true,
415 (x->verbose ? _("creating directory %s") : NULL),
417 if (ok && cwd_errno != 0
418 && (IS_RELATIVE_FILE_NAME (from) || IS_RELATIVE_FILE_NAME (to)))
420 error (0, cwd_errno, _("cannot return to current directory"));
428 ok = install_file_in_file (from, to, x);
433 /* Copy file FROM onto file TO and give TO the appropriate
435 Return true if successful. */
438 install_file_in_file (const char *from, const char *to,
439 const struct cp_options *x)
441 if (! copy_file (from, to, x))
445 if (! change_attributes (to))
447 if (x->preserve_timestamps)
448 return change_timestamps (from, to);
452 /* Copy file FROM into directory TO_DIR, keeping its same name,
453 and give the copy the appropriate attributes.
454 Return true if successful. */
457 install_file_in_dir (const char *from, const char *to_dir,
458 const struct cp_options *x)
460 const char *from_base = base_name (from);
461 char *to = file_name_concat (to_dir, from_base, NULL);
462 bool ret = install_file_in_file (from, to, x);
467 /* Copy file FROM onto file TO, creating TO if necessary.
468 Return true if successful. */
471 copy_file (const char *from, const char *to, const struct cp_options *x)
475 /* Allow installing from non-regular files like /dev/null.
476 Charles Karney reported that some Sun version of install allows that
477 and that sendmail's installation process relies on the behavior.
478 However, since !x->recursive, the call to "copy" will fail if FROM
481 return copy (from, to, false, x, ©_into_self, NULL);
484 /* Set the attributes of file or directory NAME.
485 Return true if successful. */
488 change_attributes (char const *name)
492 /* chown must precede chmod because on some systems,
493 chown clears the set[ug]id bits for non-superusers,
494 resulting in incorrect permissions.
495 On System V, users can give away files with chown and then not
496 be able to chmod them. So don't give files away.
498 We don't normally ignore errors from chown because the idea of
499 the install command is that the file is supposed to end up with
500 precisely the attributes that the user specified (or defaulted).
501 If the file doesn't end up with the group they asked for, they'll
502 want to know. But AFS returns EPERM when you try to change a
503 file's group; thus the kludge. */
505 if (chown (name, owner_id, group_id) != 0
511 error (0, errno, _("cannot change ownership of %s"), quote (name));
515 if (ok && chmod (name, mode) != 0)
517 error (0, errno, _("cannot change permissions of %s"), quote (name));
524 /* Set the timestamps of file TO to match those of file FROM.
525 Return true if successful. */
528 change_timestamps (const char *from, const char *to)
531 struct timespec timespec[2];
533 if (stat (from, &stb))
535 error (0, errno, _("cannot obtain time stamps for %s"), quote (from));
539 timespec[0].tv_sec = stb.st_atime;
540 timespec[0].tv_nsec = TIMESPEC_NS (stb.st_atim);
541 timespec[1].tv_sec = stb.st_mtime;
542 timespec[1].tv_nsec = TIMESPEC_NS (stb.st_mtim);
543 if (utimens (to, timespec))
545 error (0, errno, _("cannot set time stamps for %s"), quote (to));
551 /* Strip the symbol table from the file NAME.
552 We could dig the magic number out of the file first to
553 determine whether to strip it, but the header files and
554 magic numbers vary so much from system to system that making
555 it portable would be very difficult. Not worth the effort. */
558 strip (char const *name)
566 error (EXIT_FAILURE, errno, _("fork system call failed"));
569 execlp ("strip", "strip", name, NULL);
570 error (EXIT_FAILURE, errno, _("cannot run strip"));
572 default: /* Parent. */
573 /* Parent process. */
574 while (pid != wait (&status)) /* Wait for kid to finish. */
577 error (EXIT_FAILURE, 0, _("strip failed"));
582 /* Initialize the user and group ownership of the files to install. */
592 pw = getpwnam (owner_name);
595 unsigned long int tmp;
596 if (xstrtoul (owner_name, NULL, 0, &tmp, NULL) != LONGINT_OK
598 error (EXIT_FAILURE, 0, _("invalid user %s"), quote (owner_name));
602 owner_id = pw->pw_uid;
606 owner_id = (uid_t) -1;
610 gr = getgrnam (group_name);
613 unsigned long int tmp;
614 if (xstrtoul (group_name, NULL, 0, &tmp, NULL) != LONGINT_OK
616 error (EXIT_FAILURE, 0, _("invalid group %s"), quote (group_name));
620 group_id = gr->gr_gid;
624 group_id = (gid_t) -1;
630 if (status != EXIT_SUCCESS)
631 fprintf (stderr, _("Try `%s --help' for more information.\n"),
636 Usage: %s [OPTION]... [-T] SOURCE DEST\n\
637 or: %s [OPTION]... SOURCE... DIRECTORY\n\
638 or: %s [OPTION]... -t DIRECTORY SOURCE...\n\
639 or: %s [OPTION]... -d DIRECTORY...\n\
641 program_name, program_name, program_name, program_name);
643 In the first three forms, copy SOURCE to DEST or multiple SOURCE(s) to\n\
644 the existing DIRECTORY, while setting permission modes and owner/group.\n\
645 In the 4th form, create all components of the given DIRECTORY(ies).\n\
649 Mandatory arguments to long options are mandatory for short options too.\n\
652 --backup[=CONTROL] make a backup of each existing destination file\n\
653 -b like --backup but does not accept an argument\n\
655 -d, --directory treat all arguments as directory names; create all\n\
656 components of the specified directories\n\
659 -D create all leading components of DEST except the last,\n\
660 then copy SOURCE to DEST\n\
661 -g, --group=GROUP set group ownership, instead of process' current group\n\
662 -m, --mode=MODE set permission mode (as in chmod), instead of rwxr-xr-x\n\
663 -o, --owner=OWNER set ownership (super-user only)\n\
666 -p, --preserve-timestamps apply access/modification times of SOURCE files\n\
667 to corresponding destination files\n\
668 -s, --strip strip symbol tables\n\
669 -S, --suffix=SUFFIX override the usual backup suffix\n\
670 -t, --target-directory=DIRECTORY copy all SOURCE arguments into DIRECTORY\n\
671 -T, --no-target-directory treat DEST as a normal file\n\
672 -v, --verbose print the name of each directory as it is created\n\
674 fputs (HELP_OPTION_DESCRIPTION, stdout);
675 fputs (VERSION_OPTION_DESCRIPTION, stdout);
678 The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
679 The version control method may be selected via the --backup option or through\n\
680 the VERSION_CONTROL environment variable. Here are the values:\n\
684 none, off never make backups (even if --backup is given)\n\
685 numbered, t make numbered backups\n\
686 existing, nil numbered if numbered backups exist, simple otherwise\n\
687 simple, never always make simple backups\n\
689 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);