1 /* install - copy files and set attributes
2 Copyright (C) 89, 90, 91, 1995-2004 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 David MacKenzie <djm@gnu.ai.mit.edu> */
23 #include <sys/types.h>
29 #include "backupfile.h"
35 #include "modechange.h"
36 #include "path-concat.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>
50 struct passwd *getpwnam ();
51 struct group *getgrnam ();
53 #ifndef _POSIX_VERSION
59 # define endgrent() ((void) 0)
63 # define endpwent() ((void) 0)
66 /* Initial number of entries in each hash table entry's table of inodes. */
67 #define INITIAL_HASH_MODULE 100
69 /* Initial number of entries in the inode hash table. */
70 #define INITIAL_ENTRY_TAB_SIZE 70
72 /* Number of bytes of a file to copy at a time. */
73 #define READ_SIZE (32 * 1024)
79 static int change_timestamps (const char *from, const char *to);
80 static int change_attributes (const char *path);
81 static int copy_file (const char *from, const char *to,
82 const struct cp_options *x);
83 static int install_file_to_path (const char *from, const char *to,
84 const struct cp_options *x);
85 static int install_file_in_dir (const char *from, const char *to_dir,
86 const struct cp_options *x);
87 static int install_file_in_file (const char *from, const char *to,
88 const struct cp_options *x);
89 static void get_ids (void);
90 static void strip (const char *path);
91 void usage (int status);
93 /* For long options that have no equivalent short option, use a
94 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
97 TARGET_DIRECTORY_OPTION = CHAR_MAX + 1
100 /* The name this program was run with, for error messages. */
103 /* The user name that will own the files, or NULL to make the owner
104 the current user ID. */
105 static char *owner_name;
107 /* The user ID corresponding to `owner_name'. */
108 static uid_t owner_id;
110 /* The group name that will own the files, or NULL to make the group
111 the current group ID. */
112 static char *group_name;
114 /* The group ID corresponding to `group_name'. */
115 static gid_t group_id;
117 /* The permissions to which the files will be set. The umask has
119 static mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
121 /* If nonzero, strip executable files after copying them. */
122 static int strip_files;
124 /* If nonzero, install a directory instead of a regular file. */
127 static struct option const long_options[] =
129 {"backup", optional_argument, NULL, 'b'},
130 {"directory", no_argument, NULL, 'd'},
131 {"group", required_argument, NULL, 'g'},
132 {"mode", required_argument, NULL, 'm'},
133 {"owner", required_argument, NULL, 'o'},
134 {"preserve-timestamps", no_argument, NULL, 'p'},
135 {"strip", no_argument, NULL, 's'},
136 {"suffix", required_argument, NULL, 'S'},
137 {"target-directory", required_argument, NULL, TARGET_DIRECTORY_OPTION},
138 {"version-control", required_argument, NULL, 'V'}, /* Deprecated. FIXME. */
139 {"verbose", no_argument, NULL, 'v'},
140 {GETOPT_HELP_OPTION_DECL},
141 {GETOPT_VERSION_OPTION_DECL},
146 cp_option_init (struct cp_options *x)
148 x->copy_as_regular = 1;
149 x->dereference = DEREF_ALWAYS;
150 x->unlink_dest_before_opening = 1;
151 x->unlink_dest_after_failed_open = 0;
153 x->interactive = I_UNSPECIFIED;
155 x->myeuid = geteuid ();
156 x->one_file_system = 0;
157 x->preserve_ownership = 0;
158 x->preserve_links = 0;
159 x->preserve_mode = 0;
160 x->preserve_timestamps = 0;
161 x->require_preserve = 0;
163 x->sparse_mode = SPARSE_AUTO;
164 x->symbolic_link = 0;
165 x->backup_type = none;
167 /* Create destination files initially writable so we can run strip on them.
168 Although GNU strip works fine on read-only files, some others
171 x->mode = S_IRUSR | S_IWUSR;
181 /* FILE is the last operand of this command. Return true if FILE is a
182 directory. But report an error there is a problem accessing FILE,
183 or if FILE does not exist but would have to refer to an existing
184 directory if it referred to anything at all. */
187 target_directory_operand (char const *file)
189 char const *b = base_name (file);
190 size_t blen = strlen (b);
191 bool looks_like_a_dir = (blen == 0 || ISSLASH (b[blen - 1]));
193 int err = (stat (file, &st) == 0 ? 0 : errno);
194 bool is_a_dir = !err && S_ISDIR (st.st_mode);
195 if (err && err != ENOENT)
196 error (EXIT_FAILURE, err, _("accessing %s"), quote (file));
197 if (is_a_dir < looks_like_a_dir)
198 error (EXIT_FAILURE, err, _("target %s is not a directory"), quote (file));
203 main (int argc, char **argv)
207 const char *specified_mode = NULL;
208 int make_backups = 0;
209 char *backup_suffix_string;
210 char *version_control_string = NULL;
211 int mkdir_and_install = 0;
213 char const *target_directory = NULL;
217 initialize_main (&argc, &argv);
218 program_name = argv[0];
219 setlocale (LC_ALL, "");
220 bindtextdomain (PACKAGE, LOCALEDIR);
221 textdomain (PACKAGE);
223 atexit (close_stdout);
233 /* FIXME: consider not calling getenv for SIMPLE_BACKUP_SUFFIX unless
234 we'll actually use backup_suffix_string. */
235 backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
237 while ((optc = getopt_long (argc, argv, "bcsDdg:m:o:pvV:S:", long_options,
245 case 'V': /* FIXME: this is deprecated. Remove it in 2001. */
247 _("warning: --version-control (-V) is obsolete; support for\
248 it\nwill be removed in some future release. Use --backup=%s instead."
255 version_control_string = optarg;
262 /* System V fork+wait does not work if SIGCHLD is ignored. */
263 signal (SIGCHLD, SIG_DFL);
270 mkdir_and_install = 1;
279 specified_mode = optarg;
285 x.preserve_timestamps = 1;
289 backup_suffix_string = optarg;
291 case TARGET_DIRECTORY_OPTION:
292 if (target_directory)
293 error (EXIT_FAILURE, 0,
294 _("multiple target directories specified"));
298 if (stat (optarg, &st) != 0)
299 error (EXIT_FAILURE, errno, _("accessing %s"), quote (optarg));
300 if (! S_ISDIR (st.st_mode))
301 error (EXIT_FAILURE, 0, _("target %s is not a directory"),
304 target_directory = optarg;
306 case_GETOPT_HELP_CHAR;
307 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
309 usage (EXIT_FAILURE);
313 /* Check for invalid combinations of arguments. */
314 if (dir_arg && strip_files)
315 error (EXIT_FAILURE, 0,
316 _("the strip option may not be used when installing a directory"));
317 if (dir_arg && target_directory)
318 error (EXIT_FAILURE, 0,
319 _("target directory not allowed when installing a directory"));
321 if (backup_suffix_string)
322 simple_backup_suffix = xstrdup (backup_suffix_string);
324 x.backup_type = (make_backups
325 ? xget_version (_("backup type"),
326 version_control_string)
329 n_files = argc - optind;
330 file = argv + optind;
332 if (n_files <= !target_directory)
335 error (0, 0, _("missing file operand"));
337 error (0, 0, _("missing destination file operand after %s"),
339 usage (EXIT_FAILURE);
342 if (!target_directory)
344 if (2 <= n_files && target_directory_operand (file[n_files - 1]))
345 target_directory = file[--n_files];
346 else if (2 < n_files)
347 error (EXIT_FAILURE, 0, _("target %s is not a directory"),
348 quote (file[n_files - 1]));
353 struct mode_change *change = mode_compile (specified_mode, 0);
354 if (change == MODE_INVALID)
355 error (EXIT_FAILURE, 0, _("invalid mode %s"), quote (specified_mode));
356 else if (change == MODE_MEMORY_EXHAUSTED)
358 mode = mode_adjust (0, change);
366 for (i = 0; i < n_files; i++)
369 make_path (file[i], mode, mode, owner_id, group_id, 0,
370 (x.verbose ? _("creating directory %s") : NULL));
375 /* FIXME: it's a little gross that this initialization is
376 required by copy.c::copy. */
379 if (!target_directory)
381 if (mkdir_and_install)
382 errors = install_file_to_path (file[0], file[1], &x);
384 errors = install_file_in_file (file[0], file[1], &x);
390 for (i = 0; i < n_files; i++)
392 errors |= install_file_in_dir (file[i], target_directory, &x);
397 exit (errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
400 /* Copy file FROM onto file TO, creating any missing parent directories of TO.
401 Return 0 if successful, 1 if an error occurs */
404 install_file_to_path (const char *from, const char *to,
405 const struct cp_options *x)
410 dest_dir = dir_name (to);
412 /* check to make sure this is a path (not install a b ) */
413 if (!STREQ (dest_dir, ".")
414 && !isdir (dest_dir))
416 /* Someone will probably ask for a new option or three to specify
417 owner, group, and permissions for parent directories. Remember
418 that this option is intended mainly to help installers when the
419 distribution doesn't provide proper install rules. */
420 #define DIR_MODE (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
421 fail = make_path (dest_dir, DIR_MODE, DIR_MODE, owner_id, group_id, 0,
422 (x->verbose ? _("creating directory %s") : NULL));
426 fail = install_file_in_file (from, to, x);
433 /* Copy file FROM onto file TO and give TO the appropriate
435 Return 0 if successful, 1 if an error occurs. */
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 0 if successful, 1 if not. */
457 install_file_in_dir (const char *from, const char *to_dir,
458 const struct cp_options *x)
460 const char *from_base;
464 from_base = base_name (from);
465 to = path_concat (to_dir, from_base, NULL);
466 ret = install_file_in_file (from, to, x);
471 /* Copy file FROM onto file TO, creating TO if necessary.
472 Return 0 if the copy is successful, 1 if not. */
475 copy_file (const char *from, const char *to, const struct cp_options *x)
478 int nonexistent_dst = 0;
481 /* Allow installing from non-regular files like /dev/null.
482 Charles Karney reported that some Sun version of install allows that
483 and that sendmail's installation process relies on the behavior. */
486 error (0, 0, _("%s is a directory"), quote (from));
490 fail = copy (from, to, nonexistent_dst, x, ©_into_self, NULL);
495 /* Set the attributes of file or directory PATH.
496 Return 0 if successful, 1 if not. */
499 change_attributes (const char *path)
503 /* chown must precede chmod because on some systems,
504 chown clears the set[ug]id bits for non-superusers,
505 resulting in incorrect permissions.
506 On System V, users can give away files with chown and then not
507 be able to chmod them. So don't give files away.
509 We don't normally ignore errors from chown because the idea of
510 the install command is that the file is supposed to end up with
511 precisely the attributes that the user specified (or defaulted).
512 If the file doesn't end up with the group they asked for, they'll
513 want to know. But AFS returns EPERM when you try to change a
514 file's group; thus the kludge. */
516 if (chown (path, owner_id, group_id)
522 error (0, errno, _("cannot change ownership of %s"), quote (path));
526 if (!err && chmod (path, mode))
528 error (0, errno, _("cannot change permissions of %s"), quote (path));
535 /* Set the timestamps of file TO to match those of file FROM.
536 Return 0 if successful, 1 if not. */
539 change_timestamps (const char *from, const char *to)
542 struct timespec timespec[2];
544 if (stat (from, &stb))
546 error (0, errno, _("cannot obtain time stamps for %s"), quote (from));
550 timespec[0].tv_sec = stb.st_atime;
551 timespec[0].tv_nsec = TIMESPEC_NS (stb.st_atim);
552 timespec[1].tv_sec = stb.st_mtime;
553 timespec[1].tv_nsec = TIMESPEC_NS (stb.st_mtim);
554 if (utimens (to, timespec))
556 error (0, errno, _("cannot set time stamps for %s"), quote (to));
562 /* Strip the symbol table from the file PATH.
563 We could dig the magic number out of the file first to
564 determine whether to strip it, but the header files and
565 magic numbers vary so much from system to system that making
566 it portable would be very difficult. Not worth the effort. */
569 strip (const char *path)
577 error (EXIT_FAILURE, errno, _("fork system call failed"));
580 execlp ("strip", "strip", path, NULL);
581 error (EXIT_FAILURE, errno, _("cannot run strip"));
583 default: /* Parent. */
584 /* Parent process. */
585 while (pid != wait (&status)) /* Wait for kid to finish. */
588 error (EXIT_FAILURE, 0, _("strip failed"));
593 /* Initialize the user and group ownership of the files to install. */
603 pw = getpwnam (owner_name);
606 unsigned long int tmp;
607 if (xstrtoul (owner_name, NULL, 0, &tmp, NULL) != LONGINT_OK
609 error (EXIT_FAILURE, 0, _("invalid user %s"), quote (owner_name));
613 owner_id = pw->pw_uid;
617 owner_id = (uid_t) -1;
621 gr = getgrnam (group_name);
624 unsigned long int tmp;
625 if (xstrtoul (group_name, NULL, 0, &tmp, NULL) != LONGINT_OK
627 error (EXIT_FAILURE, 0, _("invalid group %s"), quote (group_name));
631 group_id = gr->gr_gid;
635 group_id = (gid_t) -1;
641 if (status != EXIT_SUCCESS)
642 fprintf (stderr, _("Try `%s --help' for more information.\n"),
647 Usage: %s [OPTION]... SOURCE DEST (1st format)\n\
648 or: %s [OPTION]... SOURCE... DIRECTORY (2nd format)\n\
649 or: %s [OPTION]... --target-directory=DIRECTORY SOURCE... (3rd format)\n\
650 or: %s -d [OPTION]... DIRECTORY... (4th format)\n\
652 program_name, program_name, program_name, program_name);
654 In the first three formats, copy SOURCE to DEST or multiple SOURCE(s) to\n\
655 the existing DIRECTORY, while setting permission modes and owner/group.\n\
656 In the fourth format, create all components of the given DIRECTORY(ies).\n\
660 Mandatory arguments to long options are mandatory for short options too.\n\
663 --backup[=CONTROL] make a backup of each existing destination file\n\
664 -b like --backup but does not accept an argument\n\
666 -d, --directory treat all arguments as directory names; create all\n\
667 components of the specified directories\n\
670 -D create all leading components of DEST except the last,\n\
671 then copy SOURCE to DEST; useful in the 1st format\n\
672 -g, --group=GROUP set group ownership, instead of process' current group\n\
673 -m, --mode=MODE set permission mode (as in chmod), instead of rwxr-xr-x\n\
674 -o, --owner=OWNER set ownership (super-user only)\n\
677 -p, --preserve-timestamps apply access/modification times of SOURCE files\n\
678 to corresponding destination files\n\
679 -s, --strip strip symbol tables, only for 1st and 2nd formats\n\
680 -S, --suffix=SUFFIX override the usual backup suffix\n\
681 --target-directory=DIRECTORY copy all SOURCE arguments into DIRECTORY\n\
682 -v, --verbose print the name of each directory as it is created\n\
684 fputs (HELP_OPTION_DESCRIPTION, stdout);
685 fputs (VERSION_OPTION_DESCRIPTION, stdout);
688 The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
689 The version control method may be selected via the --backup option or through\n\
690 the VERSION_CONTROL environment variable. Here are the values:\n\
694 none, off never make backups (even if --backup is given)\n\
695 numbered, t make numbered backups\n\
696 existing, nil numbered if numbered backups exist, simple otherwise\n\
697 simple, never always make simple backups\n\
699 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);