1 /* install - copy files and set attributes
2 Copyright (C) 89, 90, 91, 1995-2001 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>
56 struct passwd *getpwnam ();
57 struct group *getgrnam ();
59 #ifndef _POSIX_VERSION
65 # define endgrent() ((void) 0)
69 # define endpwent() ((void) 0)
72 /* Initial number of entries in each hash table entry's table of inodes. */
73 #define INITIAL_HASH_MODULE 100
75 /* Initial number of entries in the inode hash table. */
76 #define INITIAL_ENTRY_TAB_SIZE 70
78 /* Number of bytes of a file to copy at a time. */
79 #define READ_SIZE (32 * 1024)
86 static int change_timestamps PARAMS ((const char *from, const char *to));
87 static int change_attributes PARAMS ((const char *path));
88 static int copy_file PARAMS ((const char *from, const char *to,
89 const struct cp_options *x));
90 static int install_file_to_path PARAMS ((const char *from, const char *to,
91 const struct cp_options *x));
92 static int install_file_in_dir PARAMS ((const char *from, const char *to_dir,
93 const struct cp_options *x));
94 static int install_file_in_file PARAMS ((const char *from, const char *to,
95 const struct cp_options *x));
96 static void get_ids PARAMS ((void));
97 static void strip PARAMS ((const char *path));
98 void usage PARAMS ((int status));
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 {"version-control", required_argument, NULL, 'V'}, /* Deprecated. FIXME. */
138 {"verbose", no_argument, NULL, 'v'},
139 {GETOPT_HELP_OPTION_DECL},
140 {GETOPT_VERSION_OPTION_DECL},
145 cp_option_init (struct cp_options *x)
147 x->copy_as_regular = 1;
148 x->dereference = DEREF_ALWAYS;
149 x->unlink_dest_before_opening = 1;
150 x->unlink_dest_after_failed_open = 0;
152 /* If unlink fails, try to proceed anyway. */
153 x->failed_unlink_is_fatal = 0;
156 x->interactive = I_UNSPECIFIED;
158 x->myeuid = geteuid ();
159 x->one_file_system = 0;
160 x->preserve_owner_and_group = 0;
161 x->preserve_chmod_bits = 0;
162 x->preserve_timestamps = 0;
163 x->require_preserve = 0;
165 x->sparse_mode = SPARSE_AUTO;
166 x->symbolic_link = 0;
167 x->backup_type = none;
169 /* Create destination files initially writable so we can run strip on them.
170 Although GNU strip works fine on read-only files, some others
173 x->mode = S_IRUSR | S_IWUSR;
183 main (int argc, char **argv)
187 const char *specified_mode = NULL;
188 int make_backups = 0;
189 char *backup_suffix_string;
190 char *version_control_string = NULL;
191 int mkdir_and_install = 0;
196 program_name = argv[0];
197 setlocale (LC_ALL, "");
198 bindtextdomain (PACKAGE, LOCALEDIR);
199 textdomain (PACKAGE);
201 atexit (close_stdout);
211 /* FIXME: consider not calling getenv for SIMPLE_BACKUP_SUFFIX unless
212 we'll actually use backup_suffix_string. */
213 backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
215 while ((optc = getopt_long (argc, argv, "bcsDdg:m:o:pvV:S:", long_options,
223 case 'V': /* FIXME: this is deprecated. Remove it in 2001. */
225 _("warning: --version-control (-V) is obsolete; support for\
226 it\nwill be removed in some future release. Use --backup=%s instead."
233 version_control_string = optarg;
244 mkdir_and_install = 1;
253 specified_mode = optarg;
259 x.preserve_timestamps = 1;
263 backup_suffix_string = optarg;
265 case_GETOPT_HELP_CHAR;
266 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
272 /* Check for invalid combinations of arguments. */
273 if (dir_arg && strip_files)
275 _("the strip option may not be used when installing a directory"));
277 if (backup_suffix_string)
278 simple_backup_suffix = xstrdup (backup_suffix_string);
280 x.backup_type = (make_backups
281 ? xget_version (_("backup type"),
282 version_control_string)
285 n_files = argc - optind;
286 file = argv + optind;
288 if (n_files == 0 || (n_files == 1 && !dir_arg))
290 error (0, 0, _("too few arguments"));
296 struct mode_change *change = mode_compile (specified_mode, 0);
297 if (change == MODE_INVALID)
298 error (1, 0, _("invalid mode %s"), quote (specified_mode));
299 else if (change == MODE_MEMORY_EXHAUSTED)
301 mode = mode_adjust (0, change);
309 for (i = 0; i < n_files; i++)
312 make_path (file[i], mode, mode, owner_id, group_id, 0,
313 (x.verbose ? _("creating directory %s") : NULL));
318 /* FIXME: it's a little gross that this initialization is
319 required by copy.c::copy. */
320 hash_init (INITIAL_HASH_MODULE, INITIAL_ENTRY_TAB_SIZE);
324 if (mkdir_and_install)
325 errors = install_file_to_path (file[0], file[1], &x);
326 else if (!isdir (file[1]))
327 errors = install_file_in_file (file[0], file[1], &x);
329 errors = install_file_in_dir (file[0], file[1], &x);
334 const char *dest = file[n_files - 1];
338 _("installing multiple files, but last argument, %s \
339 is not a directory"),
343 for (i = 0; i < n_files - 1; i++)
345 errors |= install_file_in_dir (file[i], dest, &x);
353 /* Copy file FROM onto file TO, creating any missing parent directories of TO.
354 Return 0 if successful, 1 if an error occurs */
357 install_file_to_path (const char *from, const char *to,
358 const struct cp_options *x)
363 dest_dir = dir_name (to);
365 /* check to make sure this is a path (not install a b ) */
366 if (!STREQ (dest_dir, ".")
367 && !isdir (dest_dir))
369 /* Someone will probably ask for a new option or three to specify
370 owner, group, and permissions for parent directories. Remember
371 that this option is intended mainly to help installers when the
372 distribution doesn't provide proper install rules. */
373 #define DIR_MODE (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
374 fail = make_path (dest_dir, DIR_MODE, DIR_MODE, owner_id, group_id, 0,
375 (x->verbose ? _("creating directory %s") : NULL));
379 fail = install_file_in_file (from, to, x);
386 /* Copy file FROM onto file TO and give TO the appropriate
388 Return 0 if successful, 1 if an error occurs. */
391 install_file_in_file (const char *from, const char *to,
392 const struct cp_options *x)
394 if (copy_file (from, to, x))
398 if (change_attributes (to))
400 if (x->preserve_timestamps)
401 return change_timestamps (from, to);
405 /* Copy file FROM into directory TO_DIR, keeping its same name,
406 and give the copy the appropriate attributes.
407 Return 0 if successful, 1 if not. */
410 install_file_in_dir (const char *from, const char *to_dir,
411 const struct cp_options *x)
413 const char *from_base;
417 from_base = base_name (from);
418 to = path_concat (to_dir, from_base, NULL);
419 ret = install_file_in_file (from, to, x);
424 /* Copy file FROM onto file TO, creating TO if necessary.
425 Return 0 if the copy is successful, 1 if not. */
428 copy_file (const char *from, const char *to, const struct cp_options *x)
431 int nonexistent_dst = 0;
434 /* Allow installing from non-regular files like /dev/null.
435 Charles Karney reported that some Sun version of install allows that
436 and that sendmail's installation process relies on the behavior. */
439 error (0, 0, _("%s is a directory"), quote (from));
443 fail = copy (from, to, nonexistent_dst, x, ©_into_self, NULL);
448 /* Set the attributes of file or directory PATH.
449 Return 0 if successful, 1 if not. */
452 change_attributes (const char *path)
456 /* chown must precede chmod because on some systems,
457 chown clears the set[ug]id bits for non-superusers,
458 resulting in incorrect permissions.
459 On System V, users can give away files with chown and then not
460 be able to chmod them. So don't give files away.
462 We don't normally ignore errors from chown because the idea of
463 the install command is that the file is supposed to end up with
464 precisely the attributes that the user specified (or defaulted).
465 If the file doesn't end up with the group they asked for, they'll
466 want to know. But AFS returns EPERM when you try to change a
467 file's group; thus the kludge. */
469 if (chown (path, owner_id, group_id)
475 error (0, errno, "cannot change ownership of %s", quote (path));
479 if (!err && chmod (path, mode))
481 error (0, errno, "cannot change permissions of %s", quote (path));
488 /* Set the timestamps of file TO to match those of file FROM.
489 Return 0 if successful, 1 if not. */
492 change_timestamps (const char *from, const char *to)
497 if (stat (from, &stb))
499 error (0, errno, _("cannot obtain time stamps for %s"), quote (from));
503 /* There's currently no interface to set file timestamps with
504 better than 1-second resolution, so discard any fractional
505 part of the source timestamp. */
507 utb.actime = stb.st_atime;
508 utb.modtime = stb.st_mtime;
509 if (utime (to, &utb))
511 error (0, errno, _("cannot set time stamps for %s"), quote (to));
517 /* Strip the symbol table from the file PATH.
518 We could dig the magic number out of the file first to
519 determine whether to strip it, but the header files and
520 magic numbers vary so much from system to system that making
521 it portable would be very difficult. Not worth the effort. */
524 strip (const char *path)
532 error (1, errno, _("cannot fork"));
535 execlp ("strip", "strip", path, NULL);
536 error (1, errno, _("cannot run strip"));
538 default: /* Parent. */
539 /* Parent process. */
540 while (pid != wait (&status)) /* Wait for kid to finish. */
543 error (1, 0, _("strip failed"));
548 /* Initialize the user and group ownership of the files to install. */
558 pw = getpwnam (owner_name);
562 if (xstrtol (owner_name, NULL, 0, &tmp_long, NULL) != LONGINT_OK
563 || tmp_long < 0 || tmp_long > UID_T_MAX)
564 error (1, 0, _("invalid user %s"), quote (owner_name));
565 owner_id = (uid_t) tmp_long;
568 owner_id = pw->pw_uid;
572 owner_id = (uid_t) -1;
576 gr = getgrnam (group_name);
580 if (xstrtol (group_name, NULL, 0, &tmp_long, NULL) != LONGINT_OK
581 || tmp_long < 0 || tmp_long > GID_T_MAX)
582 error (1, 0, _("invalid group %s"), quote (group_name));
583 group_id = (gid_t) tmp_long;
586 group_id = gr->gr_gid;
590 group_id = (gid_t) -1;
597 fprintf (stderr, _("Try `%s --help' for more information.\n"),
602 Usage: %s [OPTION]... SOURCE DEST (1st format)\n\
603 or: %s [OPTION]... SOURCE... DIRECTORY (2nd format)\n\
604 or: %s -d [OPTION]... DIRECTORY... (3rd format)\n\
606 program_name, program_name, program_name);
608 In the first two formats, copy SOURCE to DEST or multiple SOURCE(s) to\n\
609 the existing DIRECTORY, while setting permission modes and owner/group.\n\
610 In the third format, create all components of the given DIRECTORY(ies).\n\
612 --backup[=CONTROL] make a backup of each existing destination file\n\
613 -b like --backup but does not accept an argument\n\
615 -d, --directory treat all arguments as directory names; create all\n\
616 components of the specified directories\n\
617 -D create all leading components of DEST except the last,\n\
618 then copy SOURCE to DEST; useful in the 1st format\n\
619 -g, --group=GROUP set group ownership, instead of process' current group\n\
620 -m, --mode=MODE set permission mode (as in chmod), instead of rwxr-xr-x\n\
621 -o, --owner=OWNER set ownership (super-user only)\n\
622 -p, --preserve-timestamps apply access/modification times of SOURCE files\n\
623 to corresponding destination files\n\
624 -s, --strip strip symbol tables, only for 1st and 2nd formats\n\
625 -S, --suffix=SUFFIX override the usual backup suffix\n\
626 -v, --verbose print the name of each directory as it is created\n\
627 --help display this help and exit\n\
628 --version output version information and exit\n\
632 The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
633 The version control method may be selected via the --backup option or through\n\
634 the VERSION_CONTROL environment variable. Here are the values:\n\
636 none, off never make backups (even if --backup is given)\n\
637 numbered, t make numbered backups\n\
638 existing, nil numbered if numbered backups exist, simple otherwise\n\
639 simple, never always make simple backups\n\
641 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));