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> */
23 #include <sys/types.h>
28 #include "backupfile.h"
34 #include "modechange.h"
35 #include "path-concat.h"
40 /* The official name of this program (e.g., no `g' prefix). */
41 #define PROGRAM_NAME "install"
43 #define WRITTEN_BY _("Written by David MacKenzie.")
46 # include <sys/wait.h>
49 struct passwd *getpwnam ();
50 struct group *getgrnam ();
52 #ifndef _POSIX_VERSION
58 # define endgrent() ((void) 0)
62 # define endpwent() ((void) 0)
65 /* Initial number of entries in each hash table entry's table of inodes. */
66 #define INITIAL_HASH_MODULE 100
68 /* Initial number of entries in the inode hash table. */
69 #define INITIAL_ENTRY_TAB_SIZE 70
71 /* Number of bytes of a file to copy at a time. */
72 #define READ_SIZE (32 * 1024)
78 static int change_timestamps (const char *from, const char *to);
79 static int change_attributes (const char *path);
80 static int copy_file (const char *from, const char *to,
81 const struct cp_options *x);
82 static int install_file_to_path (const char *from, const char *to,
83 const struct cp_options *x);
84 static int install_file_in_dir (const char *from, const char *to_dir,
85 const struct cp_options *x);
86 static int install_file_in_file (const char *from, const char *to,
87 const struct cp_options *x);
88 static void get_ids (void);
89 static void strip (const char *path);
90 void usage (int status);
92 /* The name this program was run with, for error messages. */
95 /* The user name that will own the files, or NULL to make the owner
96 the current user ID. */
97 static char *owner_name;
99 /* The user ID corresponding to `owner_name'. */
100 static uid_t owner_id;
102 /* The group name that will own the files, or NULL to make the group
103 the current group ID. */
104 static char *group_name;
106 /* The group ID corresponding to `group_name'. */
107 static gid_t group_id;
109 /* The permissions to which the files will be set. The umask has
111 static mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
113 /* If nonzero, strip executable files after copying them. */
114 static int strip_files;
116 /* If nonzero, install a directory instead of a regular file. */
119 static struct option const long_options[] =
121 {"backup", optional_argument, NULL, 'b'},
122 {"directory", no_argument, NULL, 'd'},
123 {"group", required_argument, NULL, 'g'},
124 {"mode", required_argument, NULL, 'm'},
125 {"owner", required_argument, NULL, 'o'},
126 {"preserve-timestamps", no_argument, NULL, 'p'},
127 {"strip", no_argument, NULL, 's'},
128 {"suffix", required_argument, NULL, 'S'},
129 {"version-control", required_argument, NULL, 'V'}, /* Deprecated. FIXME. */
130 {"verbose", no_argument, NULL, 'v'},
131 {GETOPT_HELP_OPTION_DECL},
132 {GETOPT_VERSION_OPTION_DECL},
137 cp_option_init (struct cp_options *x)
139 x->copy_as_regular = 1;
140 x->dereference = DEREF_ALWAYS;
141 x->unlink_dest_before_opening = 1;
142 x->unlink_dest_after_failed_open = 0;
144 x->interactive = I_UNSPECIFIED;
146 x->myeuid = geteuid ();
147 x->one_file_system = 0;
148 x->preserve_ownership = 0;
149 x->preserve_links = 0;
150 x->preserve_mode = 0;
151 x->preserve_timestamps = 0;
152 x->require_preserve = 0;
154 x->sparse_mode = SPARSE_AUTO;
155 x->symbolic_link = 0;
156 x->backup_type = none;
158 /* Create destination files initially writable so we can run strip on them.
159 Although GNU strip works fine on read-only files, some others
162 x->mode = S_IRUSR | S_IWUSR;
174 main (int argc, char **argv)
178 const char *specified_mode = NULL;
179 int make_backups = 0;
180 char *backup_suffix_string;
181 char *version_control_string = NULL;
182 int mkdir_and_install = 0;
187 initialize_main (&argc, &argv);
188 program_name = argv[0];
189 setlocale (LC_ALL, "");
190 bindtextdomain (PACKAGE, LOCALEDIR);
191 textdomain (PACKAGE);
193 atexit (close_stdout);
203 /* FIXME: consider not calling getenv for SIMPLE_BACKUP_SUFFIX unless
204 we'll actually use backup_suffix_string. */
205 backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
207 while ((optc = getopt_long (argc, argv, "bcsDdg:m:o:pvV:S:", long_options,
215 case 'V': /* FIXME: this is deprecated. Remove it in 2001. */
217 _("warning: --version-control (-V) is obsolete; support for\
218 it\nwill be removed in some future release. Use --backup=%s instead."
225 version_control_string = optarg;
236 mkdir_and_install = 1;
245 specified_mode = optarg;
251 x.preserve_timestamps = 1;
255 backup_suffix_string = optarg;
257 case_GETOPT_HELP_CHAR;
258 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, WRITTEN_BY);
260 usage (EXIT_FAILURE);
264 /* Check for invalid combinations of arguments. */
265 if (dir_arg && strip_files)
266 error (EXIT_FAILURE, 0,
267 _("the strip option may not be used when installing a directory"));
269 if (backup_suffix_string)
270 simple_backup_suffix = xstrdup (backup_suffix_string);
272 x.backup_type = (make_backups
273 ? xget_version (_("backup type"),
274 version_control_string)
277 n_files = argc - optind;
278 file = argv + optind;
280 if (argc <= optind || (n_files == 1 && !dir_arg))
282 error (0, 0, _("too few arguments"));
283 usage (EXIT_FAILURE);
288 struct mode_change *change = mode_compile (specified_mode, 0);
289 if (change == MODE_INVALID)
290 error (EXIT_FAILURE, 0, _("invalid mode %s"), quote (specified_mode));
291 else if (change == MODE_MEMORY_EXHAUSTED)
293 mode = mode_adjust (0, change);
301 for (i = 0; i < n_files; i++)
304 make_path (file[i], mode, mode, owner_id, group_id, 0,
305 (x.verbose ? _("creating directory %s") : NULL));
310 /* FIXME: it's a little gross that this initialization is
311 required by copy.c::copy. */
316 if (mkdir_and_install)
317 errors = install_file_to_path (file[0], file[1], &x);
318 else if (!isdir (file[1]))
319 errors = install_file_in_file (file[0], file[1], &x);
321 errors = install_file_in_dir (file[0], file[1], &x);
326 const char *dest = file[n_files - 1];
330 _("installing multiple files, but last argument, %s \
331 is not a directory"),
333 usage (EXIT_FAILURE);
337 for (i = 0; i < n_files - 1; i++)
339 errors |= install_file_in_dir (file[i], dest, &x);
347 /* Copy file FROM onto file TO, creating any missing parent directories of TO.
348 Return 0 if successful, 1 if an error occurs */
351 install_file_to_path (const char *from, const char *to,
352 const struct cp_options *x)
357 dest_dir = dir_name (to);
359 /* check to make sure this is a path (not install a b ) */
360 if (!STREQ (dest_dir, ".")
361 && !isdir (dest_dir))
363 /* Someone will probably ask for a new option or three to specify
364 owner, group, and permissions for parent directories. Remember
365 that this option is intended mainly to help installers when the
366 distribution doesn't provide proper install rules. */
367 #define DIR_MODE (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
368 fail = make_path (dest_dir, DIR_MODE, DIR_MODE, owner_id, group_id, 0,
369 (x->verbose ? _("creating directory %s") : NULL));
373 fail = install_file_in_file (from, to, x);
380 /* Copy file FROM onto file TO and give TO the appropriate
382 Return 0 if successful, 1 if an error occurs. */
385 install_file_in_file (const char *from, const char *to,
386 const struct cp_options *x)
388 if (copy_file (from, to, x))
392 if (change_attributes (to))
394 if (x->preserve_timestamps)
395 return change_timestamps (from, to);
399 /* Copy file FROM into directory TO_DIR, keeping its same name,
400 and give the copy the appropriate attributes.
401 Return 0 if successful, 1 if not. */
404 install_file_in_dir (const char *from, const char *to_dir,
405 const struct cp_options *x)
407 const char *from_base;
411 from_base = base_name (from);
412 to = path_concat (to_dir, from_base, NULL);
413 ret = install_file_in_file (from, to, x);
418 /* Copy file FROM onto file TO, creating TO if necessary.
419 Return 0 if the copy is successful, 1 if not. */
422 copy_file (const char *from, const char *to, const struct cp_options *x)
425 int nonexistent_dst = 0;
428 /* Allow installing from non-regular files like /dev/null.
429 Charles Karney reported that some Sun version of install allows that
430 and that sendmail's installation process relies on the behavior. */
433 error (0, 0, _("%s is a directory"), quote (from));
437 fail = copy (from, to, nonexistent_dst, x, ©_into_self, NULL);
442 /* Set the attributes of file or directory PATH.
443 Return 0 if successful, 1 if not. */
446 change_attributes (const char *path)
450 /* chown must precede chmod because on some systems,
451 chown clears the set[ug]id bits for non-superusers,
452 resulting in incorrect permissions.
453 On System V, users can give away files with chown and then not
454 be able to chmod them. So don't give files away.
456 We don't normally ignore errors from chown because the idea of
457 the install command is that the file is supposed to end up with
458 precisely the attributes that the user specified (or defaulted).
459 If the file doesn't end up with the group they asked for, they'll
460 want to know. But AFS returns EPERM when you try to change a
461 file's group; thus the kludge. */
463 if (chown (path, owner_id, group_id)
469 error (0, errno, _("cannot change ownership of %s"), quote (path));
473 if (!err && chmod (path, mode))
475 error (0, errno, _("cannot change permissions of %s"), quote (path));
482 /* Set the timestamps of file TO to match those of file FROM.
483 Return 0 if successful, 1 if not. */
486 change_timestamps (const char *from, const char *to)
489 struct timespec timespec[2];
491 if (stat (from, &stb))
493 error (0, errno, _("cannot obtain time stamps for %s"), quote (from));
497 timespec[0].tv_sec = stb.st_atime;
498 timespec[0].tv_nsec = TIMESPEC_NS (stb.st_atim);
499 timespec[1].tv_sec = stb.st_mtime;
500 timespec[1].tv_nsec = TIMESPEC_NS (stb.st_mtim);
501 if (utimens (to, timespec))
503 error (0, errno, _("cannot set time stamps for %s"), quote (to));
509 /* Strip the symbol table from the file PATH.
510 We could dig the magic number out of the file first to
511 determine whether to strip it, but the header files and
512 magic numbers vary so much from system to system that making
513 it portable would be very difficult. Not worth the effort. */
516 strip (const char *path)
524 error (EXIT_FAILURE, errno, _("fork system call failed"));
527 execlp ("strip", "strip", path, NULL);
528 error (EXIT_FAILURE, errno, _("cannot run strip"));
530 default: /* Parent. */
531 /* Parent process. */
532 while (pid != wait (&status)) /* Wait for kid to finish. */
535 error (EXIT_FAILURE, 0, _("strip failed"));
540 /* Initialize the user and group ownership of the files to install. */
550 pw = getpwnam (owner_name);
553 unsigned long int tmp;
554 if (xstrtoul (owner_name, NULL, 0, &tmp, NULL) != LONGINT_OK
556 error (EXIT_FAILURE, 0, _("invalid user %s"), quote (owner_name));
560 owner_id = pw->pw_uid;
564 owner_id = (uid_t) -1;
568 gr = getgrnam (group_name);
571 unsigned long int tmp;
572 if (xstrtoul (group_name, NULL, 0, &tmp, NULL) != LONGINT_OK
574 error (EXIT_FAILURE, 0, _("invalid group %s"), quote (group_name));
578 group_id = gr->gr_gid;
582 group_id = (gid_t) -1;
589 fprintf (stderr, _("Try `%s --help' for more information.\n"),
594 Usage: %s [OPTION]... SOURCE DEST (1st format)\n\
595 or: %s [OPTION]... SOURCE... DIRECTORY (2nd format)\n\
596 or: %s -d [OPTION]... DIRECTORY... (3rd format)\n\
598 program_name, program_name, program_name);
600 In the first two formats, copy SOURCE to DEST or multiple SOURCE(s) to\n\
601 the existing DIRECTORY, while setting permission modes and owner/group.\n\
602 In the third format, create all components of the given DIRECTORY(ies).\n\
606 Mandatory arguments to long options are mandatory for short options too.\n\
609 --backup[=CONTROL] make a backup of each existing destination file\n\
610 -b like --backup but does not accept an argument\n\
612 -d, --directory treat all arguments as directory names; create all\n\
613 components of the specified directories\n\
616 -D create all leading components of DEST except the last,\n\
617 then copy SOURCE to DEST; useful in the 1st format\n\
618 -g, --group=GROUP set group ownership, instead of process' current group\n\
619 -m, --mode=MODE set permission mode (as in chmod), instead of rwxr-xr-x\n\
620 -o, --owner=OWNER set ownership (super-user only)\n\
623 -p, --preserve-timestamps apply access/modification times of SOURCE files\n\
624 to corresponding destination files\n\
625 -s, --strip strip symbol tables, only for 1st and 2nd formats\n\
626 -S, --suffix=SUFFIX override the usual backup suffix\n\
627 -v, --verbose print the name of each directory as it is created\n\
629 fputs (HELP_OPTION_DESCRIPTION, stdout);
630 fputs (VERSION_OPTION_DESCRIPTION, stdout);
633 The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
634 The version control method may be selected via the --backup option or through\n\
635 the VERSION_CONTROL environment variable. Here are the values:\n\
639 none, off never make backups (even if --backup is given)\n\
640 numbered, t make numbered backups\n\
641 existing, nil numbered if numbered backups exist, simple otherwise\n\
642 simple, never always make simple backups\n\
644 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);