1 /* install - copy files and set attributes
2 Copyright (C) 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 David MacKenzie <djm@gnu.ai.mit.edu> */
27 #include <sys/types.h>
32 #include "backupfile.h"
38 #include "modechange.h"
39 #include "path-concat.h"
43 /* The official name of this program (e.g., no `g' prefix). */
44 #define PROGRAM_NAME "install"
46 #define AUTHORS "David MacKenzie"
49 # include <sys/wait.h>
52 struct passwd *getpwnam ();
53 struct group *getgrnam ();
55 #ifndef _POSIX_VERSION
61 # define endgrent() ((void) 0)
65 # define endpwent() ((void) 0)
68 /* Initial number of entries in each hash table entry's table of inodes. */
69 #define INITIAL_HASH_MODULE 100
71 /* Initial number of entries in the inode hash table. */
72 #define INITIAL_ENTRY_TAB_SIZE 70
74 /* Number of bytes of a file to copy at a time. */
75 #define READ_SIZE (32 * 1024)
81 static int change_timestamps (const char *from, const char *to);
82 static int change_attributes (const char *path);
83 static int copy_file (const char *from, const char *to,
84 const struct cp_options *x);
85 static int install_file_to_path (const char *from, const char *to,
86 const struct cp_options *x);
87 static int install_file_in_dir (const char *from, const char *to_dir,
88 const struct cp_options *x);
89 static int install_file_in_file (const char *from, const char *to,
90 const struct cp_options *x);
91 static void get_ids (void);
92 static void strip (const char *path);
93 void usage (int status);
95 /* The name this program was run with, for error messages. */
98 /* The user name that will own the files, or NULL to make the owner
99 the current user ID. */
100 static char *owner_name;
102 /* The user ID corresponding to `owner_name'. */
103 static uid_t owner_id;
105 /* The group name that will own the files, or NULL to make the group
106 the current group ID. */
107 static char *group_name;
109 /* The group ID corresponding to `group_name'. */
110 static gid_t group_id;
112 /* The permissions to which the files will be set. The umask has
114 static mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
116 /* If nonzero, strip executable files after copying them. */
117 static int strip_files;
119 /* If nonzero, install a directory instead of a regular file. */
122 static struct option const long_options[] =
124 {"backup", optional_argument, NULL, 'b'},
125 {"directory", no_argument, NULL, 'd'},
126 {"group", required_argument, NULL, 'g'},
127 {"mode", required_argument, NULL, 'm'},
128 {"owner", required_argument, NULL, 'o'},
129 {"preserve-timestamps", no_argument, NULL, 'p'},
130 {"strip", no_argument, NULL, 's'},
131 {"suffix", required_argument, NULL, 'S'},
132 {"version-control", required_argument, NULL, 'V'}, /* Deprecated. FIXME. */
133 {"verbose", no_argument, NULL, 'v'},
134 {GETOPT_HELP_OPTION_DECL},
135 {GETOPT_VERSION_OPTION_DECL},
140 cp_option_init (struct cp_options *x)
142 x->copy_as_regular = 1;
143 x->dereference = DEREF_ALWAYS;
144 x->unlink_dest_before_opening = 1;
145 x->unlink_dest_after_failed_open = 0;
147 x->interactive = I_UNSPECIFIED;
149 x->myeuid = geteuid ();
150 x->one_file_system = 0;
151 x->preserve_ownership = 0;
152 x->preserve_links = 0;
153 x->preserve_mode = 0;
154 x->preserve_timestamps = 0;
155 x->require_preserve = 0;
157 x->sparse_mode = SPARSE_AUTO;
158 x->symbolic_link = 0;
159 x->backup_type = none;
161 /* Create destination files initially writable so we can run strip on them.
162 Although GNU strip works fine on read-only files, some others
165 x->mode = S_IRUSR | S_IWUSR;
177 main (int argc, char **argv)
181 const char *specified_mode = NULL;
182 int make_backups = 0;
183 char *backup_suffix_string;
184 char *version_control_string = NULL;
185 int mkdir_and_install = 0;
190 initialize_main (&argc, &argv);
191 program_name = argv[0];
192 setlocale (LC_ALL, "");
193 bindtextdomain (PACKAGE, LOCALEDIR);
194 textdomain (PACKAGE);
196 atexit (close_stdout);
206 /* FIXME: consider not calling getenv for SIMPLE_BACKUP_SUFFIX unless
207 we'll actually use backup_suffix_string. */
208 backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
210 while ((optc = getopt_long (argc, argv, "bcsDdg:m:o:pvV:S:", long_options,
218 case 'V': /* FIXME: this is deprecated. Remove it in 2001. */
220 _("warning: --version-control (-V) is obsolete; support for\
221 it\nwill be removed in some future release. Use --backup=%s instead."
228 version_control_string = optarg;
239 mkdir_and_install = 1;
248 specified_mode = optarg;
254 x.preserve_timestamps = 1;
258 backup_suffix_string = optarg;
260 case_GETOPT_HELP_CHAR;
261 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
263 usage (EXIT_FAILURE);
267 /* Check for invalid combinations of arguments. */
268 if (dir_arg && strip_files)
269 error (EXIT_FAILURE, 0,
270 _("the strip option may not be used when installing a directory"));
272 if (backup_suffix_string)
273 simple_backup_suffix = xstrdup (backup_suffix_string);
275 x.backup_type = (make_backups
276 ? xget_version (_("backup type"),
277 version_control_string)
280 n_files = argc - optind;
281 file = argv + optind;
283 if (argc <= optind || (n_files == 1 && !dir_arg))
285 error (0, 0, _("too few arguments"));
286 usage (EXIT_FAILURE);
291 struct mode_change *change = mode_compile (specified_mode, 0);
292 if (change == MODE_INVALID)
293 error (EXIT_FAILURE, 0, _("invalid mode %s"), quote (specified_mode));
294 else if (change == MODE_MEMORY_EXHAUSTED)
296 mode = mode_adjust (0, change);
304 for (i = 0; i < n_files; i++)
307 make_path (file[i], mode, mode, owner_id, group_id, 0,
308 (x.verbose ? _("creating directory %s") : NULL));
313 /* FIXME: it's a little gross that this initialization is
314 required by copy.c::copy. */
319 if (mkdir_and_install)
320 errors = install_file_to_path (file[0], file[1], &x);
321 else if (!isdir (file[1]))
322 errors = install_file_in_file (file[0], file[1], &x);
324 errors = install_file_in_dir (file[0], file[1], &x);
329 const char *dest = file[n_files - 1];
333 _("installing multiple files, but last argument, %s \
334 is not a directory"),
336 usage (EXIT_FAILURE);
340 for (i = 0; i < n_files - 1; i++)
342 errors |= install_file_in_dir (file[i], dest, &x);
350 /* Copy file FROM onto file TO, creating any missing parent directories of TO.
351 Return 0 if successful, 1 if an error occurs */
354 install_file_to_path (const char *from, const char *to,
355 const struct cp_options *x)
360 dest_dir = dir_name (to);
362 /* check to make sure this is a path (not install a b ) */
363 if (!STREQ (dest_dir, ".")
364 && !isdir (dest_dir))
366 /* Someone will probably ask for a new option or three to specify
367 owner, group, and permissions for parent directories. Remember
368 that this option is intended mainly to help installers when the
369 distribution doesn't provide proper install rules. */
370 #define DIR_MODE (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
371 fail = make_path (dest_dir, DIR_MODE, DIR_MODE, owner_id, group_id, 0,
372 (x->verbose ? _("creating directory %s") : NULL));
376 fail = install_file_in_file (from, to, x);
383 /* Copy file FROM onto file TO and give TO the appropriate
385 Return 0 if successful, 1 if an error occurs. */
388 install_file_in_file (const char *from, const char *to,
389 const struct cp_options *x)
391 if (copy_file (from, to, x))
395 if (change_attributes (to))
397 if (x->preserve_timestamps)
398 return change_timestamps (from, to);
402 /* Copy file FROM into directory TO_DIR, keeping its same name,
403 and give the copy the appropriate attributes.
404 Return 0 if successful, 1 if not. */
407 install_file_in_dir (const char *from, const char *to_dir,
408 const struct cp_options *x)
410 const char *from_base;
414 from_base = base_name (from);
415 to = path_concat (to_dir, from_base, NULL);
416 ret = install_file_in_file (from, to, x);
421 /* Copy file FROM onto file TO, creating TO if necessary.
422 Return 0 if the copy is successful, 1 if not. */
425 copy_file (const char *from, const char *to, const struct cp_options *x)
428 int nonexistent_dst = 0;
431 /* Allow installing from non-regular files like /dev/null.
432 Charles Karney reported that some Sun version of install allows that
433 and that sendmail's installation process relies on the behavior. */
436 error (0, 0, _("%s is a directory"), quote (from));
440 fail = copy (from, to, nonexistent_dst, x, ©_into_self, NULL);
445 /* Set the attributes of file or directory PATH.
446 Return 0 if successful, 1 if not. */
449 change_attributes (const char *path)
453 /* chown must precede chmod because on some systems,
454 chown clears the set[ug]id bits for non-superusers,
455 resulting in incorrect permissions.
456 On System V, users can give away files with chown and then not
457 be able to chmod them. So don't give files away.
459 We don't normally ignore errors from chown because the idea of
460 the install command is that the file is supposed to end up with
461 precisely the attributes that the user specified (or defaulted).
462 If the file doesn't end up with the group they asked for, they'll
463 want to know. But AFS returns EPERM when you try to change a
464 file's group; thus the kludge. */
466 if (chown (path, owner_id, group_id)
472 error (0, errno, _("cannot change ownership of %s"), quote (path));
476 if (!err && chmod (path, mode))
478 error (0, errno, _("cannot change permissions of %s"), quote (path));
485 /* Set the timestamps of file TO to match those of file FROM.
486 Return 0 if successful, 1 if not. */
489 change_timestamps (const char *from, const char *to)
494 if (stat (from, &stb))
496 error (0, errno, _("cannot obtain time stamps for %s"), quote (from));
500 /* There's currently no interface to set file timestamps with
501 better than 1-second resolution, so discard any fractional
502 part of the source timestamp. */
504 utb.actime = stb.st_atime;
505 utb.modtime = stb.st_mtime;
506 if (utime (to, &utb))
508 error (0, errno, _("cannot set time stamps for %s"), quote (to));
514 /* Strip the symbol table from the file PATH.
515 We could dig the magic number out of the file first to
516 determine whether to strip it, but the header files and
517 magic numbers vary so much from system to system that making
518 it portable would be very difficult. Not worth the effort. */
521 strip (const char *path)
529 error (EXIT_FAILURE, errno, _("fork system call failed"));
532 execlp ("strip", "strip", path, NULL);
533 error (EXIT_FAILURE, errno, _("cannot run strip"));
535 default: /* Parent. */
536 /* Parent process. */
537 while (pid != wait (&status)) /* Wait for kid to finish. */
540 error (EXIT_FAILURE, 0, _("strip failed"));
545 /* Initialize the user and group ownership of the files to install. */
555 pw = getpwnam (owner_name);
558 unsigned long int tmp;
559 if (xstrtoul (owner_name, NULL, 0, &tmp, NULL) != LONGINT_OK
561 error (EXIT_FAILURE, 0, _("invalid user %s"), quote (owner_name));
565 owner_id = pw->pw_uid;
569 owner_id = (uid_t) -1;
573 gr = getgrnam (group_name);
576 unsigned long int tmp;
577 if (xstrtoul (group_name, NULL, 0, &tmp, NULL) != LONGINT_OK
579 error (EXIT_FAILURE, 0, _("invalid group %s"), quote (group_name));
583 group_id = gr->gr_gid;
587 group_id = (gid_t) -1;
594 fprintf (stderr, _("Try `%s --help' for more information.\n"),
599 Usage: %s [OPTION]... SOURCE DEST (1st format)\n\
600 or: %s [OPTION]... SOURCE... DIRECTORY (2nd format)\n\
601 or: %s -d [OPTION]... DIRECTORY... (3rd format)\n\
603 program_name, program_name, program_name);
605 In the first two formats, copy SOURCE to DEST or multiple SOURCE(s) to\n\
606 the existing DIRECTORY, while setting permission modes and owner/group.\n\
607 In the third format, create all components of the given DIRECTORY(ies).\n\
611 Mandatory arguments to long options are mandatory for short options too.\n\
614 --backup[=CONTROL] make a backup of each existing destination file\n\
615 -b like --backup but does not accept an argument\n\
617 -d, --directory treat all arguments as directory names; create all\n\
618 components of the specified directories\n\
621 -D create all leading components of DEST except the last,\n\
622 then copy SOURCE to DEST; useful in the 1st format\n\
623 -g, --group=GROUP set group ownership, instead of process' current group\n\
624 -m, --mode=MODE set permission mode (as in chmod), instead of rwxr-xr-x\n\
625 -o, --owner=OWNER set ownership (super-user only)\n\
628 -p, --preserve-timestamps apply access/modification times of SOURCE files\n\
629 to corresponding destination files\n\
630 -s, --strip strip symbol tables, only for 1st and 2nd formats\n\
631 -S, --suffix=SUFFIX override the usual backup suffix\n\
632 -v, --verbose print the name of each directory as it is created\n\
634 fputs (HELP_OPTION_DESCRIPTION, stdout);
635 fputs (VERSION_OPTION_DESCRIPTION, stdout);
638 The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
639 The version control method may be selected via the --backup option or through\n\
640 the VERSION_CONTROL environment variable. Here are the values:\n\
644 none, off never make backups (even if --backup is given)\n\
645 numbered, t make numbered backups\n\
646 existing, nil numbered if numbered backups exist, simple otherwise\n\
647 simple, never always make simple backups\n\
649 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);