1 /* install - copy files and set attributes
2 Copyright (C) 89, 90, 91, 1995-1999 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 /* Copy files and set their permission modes and, if possible,
19 their owner and group. Used similarly to `cp'; typically
20 used in Makefiles to copy programs into their destination
21 directories. It can also be used to create the destination
22 directories and any leading directories, and to set the final
23 directory's modes. It refuses to copy files onto themselves.
27 Set the group ownership of the installed file or directory
28 to the group ID of GROUP (default is process's current
29 group). GROUP may also be a numeric group ID.
32 Set the permission mode for the installed file or directory
33 to MODE, which is an octal number (default is 0755).
36 If run as root, set the ownership of the installed file to
37 the user ID of OWNER (default is root). OWNER may also be
40 -c No effect. For compatibility with old Unix versions of install.
43 Strip the symbol tables from installed files.
45 -p, --preserve-timestamps
46 Retain creation and modification timestamps when installing files.
49 Create a directory and its leading directories, if they
50 do not already exist. Set the owner, group and mode
51 as given on the command line. Any leading directories
52 that are created are also given those attributes.
53 This is different from the SunOS 4.0 install, which gives
54 directories that it creates the default attributes.
57 Like the -d option, but a file is installed, along with the directory.
58 Useful when installing into a new directory, and the install
59 process doesn't properly comprehend making directories.
61 David MacKenzie <djm@gnu.ai.mit.edu> */
70 #include <sys/types.h>
75 #include "backupfile.h"
82 #include "modechange.h"
83 #include "path-concat.h"
84 #include "version-etc.h"
87 /* The official name of this program (e.g., no `g' prefix). */
88 #define PROGRAM_NAME "install"
90 #define AUTHORS "David MacKenzie"
93 # include <sys/wait.h>
100 struct passwd *getpwnam ();
101 struct group *getgrnam ();
103 #ifndef _POSIX_VERSION
109 # define endgrent() ((void) 0)
113 # define endpwent() ((void) 0)
116 /* Initial number of entries in each hash table entry's table of inodes. */
117 #define INITIAL_HASH_MODULE 100
119 /* Initial number of entries in the inode hash table. */
120 #define INITIAL_ENTRY_TAB_SIZE 70
122 /* True if C is an ASCII octal digit. */
123 #define isodigit(c) ((c) >= '0' && c <= '7')
125 /* Number of bytes of a file to copy at a time. */
126 #define READ_SIZE (32 * 1024)
133 static int change_timestamps PARAMS ((const char *from, const char *to));
134 static int change_attributes PARAMS ((const char *path));
135 static int copy_file PARAMS ((const char *from, const char *to,
136 const struct cp_options *x));
137 static int install_file_to_path PARAMS ((const char *from, const char *to,
138 const struct cp_options *x));
139 static int install_file_in_dir PARAMS ((const char *from, const char *to_dir,
140 const struct cp_options *x));
141 static int install_file_in_file PARAMS ((const char *from, const char *to,
142 const struct cp_options *x));
143 static void get_ids PARAMS ((void));
144 static void strip PARAMS ((const char *path));
145 void usage PARAMS ((int status));
147 /* The name this program was run with, for error messages. */
150 /* The user name that will own the files, or NULL to make the owner
151 the current user ID. */
152 static char *owner_name;
154 /* The user ID corresponding to `owner_name'. */
155 static uid_t owner_id;
157 /* The group name that will own the files, or NULL to make the group
158 the current group ID. */
159 static char *group_name;
161 /* The group ID corresponding to `group_name'. */
162 static gid_t group_id;
164 /* The permissions to which the files will be set. The umask has
166 static mode_t mode = 0755;
168 /* If nonzero, strip executable files after copying them. */
169 static int strip_files;
171 /* If nonzero, install a directory instead of a regular file. */
174 static struct option const long_options[] =
176 {"strip", no_argument, NULL, 's'},
177 {"directory", no_argument, NULL, 'd'},
178 {"group", required_argument, NULL, 'g'},
179 {"mode", required_argument, NULL, 'm'},
180 {"owner", required_argument, NULL, 'o'},
181 {"preserve-timestamps", no_argument, NULL, 'p'},
182 {"backup", no_argument, NULL, 'b'},
183 {"suffix", required_argument, NULL, 'S'},
184 {"version-control", required_argument, NULL, 'V'},
185 {"verbose", no_argument, NULL, 'v'},
186 {GETOPT_HELP_OPTION_DECL},
187 {GETOPT_VERSION_OPTION_DECL},
192 cp_option_init (struct cp_options *x)
194 x->copy_as_regular = 1;
198 /* If unlink fails, try to proceed anyway. */
199 x->failed_unlink_is_fatal = 0;
204 x->myeuid = geteuid ();
205 x->one_file_system = 0;
206 x->preserve_owner_and_group = 0;
207 x->preserve_chmod_bits = 0;
208 x->preserve_timestamps = 0;
209 x->require_preserve = 0;
211 x->sparse_mode = SPARSE_AUTO;
212 x->symbolic_link = 0;
213 x->backup_type = none;
215 /* Create destination files initially writable so we can run strip on them.
216 Although GNU strip works fine on read-only files, some others
228 main (int argc, char **argv)
232 char *symbolic_mode = NULL;
233 int make_backups = 0;
235 int mkdir_and_install = 0;
240 program_name = argv[0];
241 setlocale (LC_ALL, "");
242 bindtextdomain (PACKAGE, LOCALEDIR);
243 textdomain (PACKAGE);
253 /* FIXME: consider not calling getenv for SIMPLE_BACKUP_SUFFIX unless
254 we'll actually use simple_backup_suffix. */
255 version = getenv ("SIMPLE_BACKUP_SUFFIX");
257 simple_backup_suffix = version;
260 while ((optc = getopt_long (argc, argv, "bcsDdg:m:o:pvV:S:", long_options,
279 mkdir_and_install = 1;
288 symbolic_mode = optarg;
294 x.preserve_timestamps = 1;
297 simple_backup_suffix = optarg;
302 case_GETOPT_HELP_CHAR;
303 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
309 /* Check for invalid combinations of arguments. */
310 if (dir_arg && strip_files)
312 _("the strip option may not be used when installing a directory"));
315 x.backup_type = xget_version ("--version-control", version);
317 n_files = argc - optind;
318 file = argv + optind;
320 if (n_files == 0 || (n_files == 1 && !dir_arg))
322 error (0, 0, _("too few arguments"));
328 struct mode_change *change = mode_compile (symbolic_mode, 0);
329 if (change == MODE_INVALID)
330 error (1, 0, _("invalid mode `%s'"), symbolic_mode);
331 else if (change == MODE_MEMORY_EXHAUSTED)
332 error (1, 0, _("virtual memory exhausted"));
333 mode = mode_adjust (0, change);
341 for (i = 0; i < n_files; i++)
344 make_path (file[i], mode, mode, owner_id, group_id, 0,
345 (x.verbose ? "creating directory `%s'" : NULL));
350 /* FIXME: it's a little gross that this initialization is
351 required by copy.c::copy. */
352 hash_init (INITIAL_HASH_MODULE, INITIAL_ENTRY_TAB_SIZE);
356 if (mkdir_and_install)
357 errors = install_file_to_path (file[0], file[1], &x);
358 else if (!isdir (file[1]))
359 errors = install_file_in_file (file[0], file[1], &x);
361 errors = install_file_in_dir (file[0], file[1], &x);
366 const char *dest = file[n_files - 1];
370 _("installing multiple files, but last argument (%s) \
371 is not a directory"),
375 for (i = 0; i < n_files - 1; i++)
377 errors |= install_file_in_dir (file[i], dest, &x);
387 /* Copy file FROM onto file TO, creating any missing parent directories of TO.
388 Return 0 if successful, 1 if an error occurs */
391 install_file_to_path (const char *from, const char *to,
392 const struct cp_options *x)
397 dest_dir = dir_name (to);
399 /* check to make sure this is a path (not install a b ) */
400 if (!STREQ (dest_dir, ".")
401 && !isdir (dest_dir))
403 /* Someone will probably ask for a new option or three to specify
404 owner, group, and permissions for parent directories. Remember
405 that this option is intended mainly to help installers when the
406 distribution doesn't provide proper install rules. */
407 #define DIR_MODE 0755
408 fail = make_path (dest_dir, DIR_MODE, DIR_MODE, owner_id, group_id, 0,
409 (x->verbose ? _("creating directory `%s'") : NULL));
412 fail = install_file_in_dir (from, dest_dir, x);
416 fail = install_file_in_file (from, to, x);
424 /* Copy file FROM onto file TO and give TO the appropriate
426 Return 0 if successful, 1 if an error occurs. */
429 install_file_in_file (const char *from, const char *to,
430 const struct cp_options *x)
432 if (copy_file (from, to, x))
436 if (change_attributes (to))
438 if (x->preserve_timestamps)
439 return change_timestamps (from, to);
443 /* Copy file FROM into directory TO_DIR, keeping its same name,
444 and give the copy the appropriate attributes.
445 Return 0 if successful, 1 if not. */
448 install_file_in_dir (const char *from, const char *to_dir,
449 const struct cp_options *x)
455 from_base = base_name (from);
456 to = path_concat (to_dir, from_base, NULL);
457 ret = install_file_in_file (from, to, x);
462 /* Copy file FROM onto file TO, creating TO if necessary.
463 Return 0 if the copy is successful, 1 if not. */
466 copy_file (const char *from, const char *to, const struct cp_options *x)
469 int nonexistent_dst = 0;
472 /* Allow installing from non-regular files like /dev/null.
473 Charles Karney reported that some Sun version of install allows that
474 and that sendmail's installation process relies on the behavior. */
477 error (0, 0, _("`%s' is a directory"), from);
481 fail = copy (from, to, nonexistent_dst, x, ©_into_self, NULL);
486 /* Set the attributes of file or directory PATH.
487 Return 0 if successful, 1 if not. */
490 change_attributes (const char *path)
494 /* chown must precede chmod because on some systems,
495 chown clears the set[ug]id bits for non-superusers,
496 resulting in incorrect permissions.
497 On System V, users can give away files with chown and then not
498 be able to chmod them. So don't give files away.
500 We don't normally ignore errors from chown because the idea of
501 the install command is that the file is supposed to end up with
502 precisely the attributes that the user specified (or defaulted).
503 If the file doesn't end up with the group they asked for, they'll
504 want to know. But AFS returns EPERM when you try to change a
505 file's group; thus the kludge. */
507 if (chown (path, owner_id, group_id)
513 if (chmod (path, mode))
517 error (0, err, "%s", path);
523 /* Set the timestamps of file TO to match those of file FROM.
524 Return 0 if successful, 1 if not. */
527 change_timestamps (const char *from, const char *to)
532 if (stat (from, &stb))
534 error (0, errno, "%s", from);
538 /* There's currently no interface to set file timestamps with
539 better than 1-second resolution, so discard any fractional
540 part of the source timestamp. */
542 utb.actime = stb.st_atime;
543 utb.modtime = stb.st_mtime;
544 if (utime (to, &utb))
546 error (0, errno, "%s", to);
552 /* Strip the symbol table from the file PATH.
553 We could dig the magic number out of the file first to
554 determine whether to strip it, but the header files and
555 magic numbers vary so much from system to system that making
556 it portable would be very difficult. Not worth the effort. */
559 strip (const char *path)
568 error (1, errno, _("fork system call failed"));
571 execlp ("strip", "strip", path, NULL);
572 error (1, errno, _("cannot run strip"));
574 default: /* Parent. */
575 /* Parent process. */
576 while (pid != wait (&status)) /* Wait for kid to finish. */
582 /* Initialize the user and group ownership of the files to install. */
592 pw = getpwnam (owner_name);
596 if (xstrtol (owner_name, NULL, 0, &tmp_long, NULL) != LONGINT_OK
597 || tmp_long < 0 || tmp_long > UID_T_MAX)
598 error (1, 0, _("invalid user `%s'"), owner_name);
599 owner_id = (uid_t) tmp_long;
602 owner_id = pw->pw_uid;
606 owner_id = (uid_t) -1;
610 gr = getgrnam (group_name);
614 if (xstrtol (group_name, NULL, 0, &tmp_long, NULL) != LONGINT_OK
615 || tmp_long < 0 || tmp_long > GID_T_MAX)
616 error (1, 0, _("invalid group `%s'"), group_name);
617 group_id = (gid_t) tmp_long;
620 group_id = gr->gr_gid;
624 group_id = (gid_t) -1;
631 fprintf (stderr, _("Try `%s --help' for more information.\n"),
636 Usage: %s [OPTION]... SOURCE DEST (1st format)\n\
637 or: %s [OPTION]... SOURCE... DIRECTORY (2nd format)\n\
638 or: %s -d [OPTION]... DIRECTORY... (3rd format)\n\
640 program_name, program_name, program_name);
642 In the first two formats, copy SOURCE to DEST or multiple SOURCE(s) to\n\
643 the existing DIRECTORY, while setting permission modes and owner/group.\n\
644 In the third format, create all components of the given DIRECTORY(ies).\n\
646 -b, --backup make backup before removal\n\
648 -d, --directory treat all arguments as directory names; create all\n\
649 components of the specified directories\n\
650 -D create all leading components of DEST except the last,\n\
651 then copy SOURCE to DEST; useful in the 1st format\n\
652 -g, --group=GROUP set group ownership, instead of process' current group\n\
653 -m, --mode=MODE set permission mode (as in chmod), instead of rwxr-xr-x\n\
654 -o, --owner=OWNER set ownership (super-user only)\n\
655 -p, --preserve-timestamps apply access/modification times of SOURCE files\n\
656 to corresponding destination files\n\
657 -s, --strip strip symbol tables, only for 1st and 2nd formats\n\
658 -S, --suffix=SUFFIX override the usual backup suffix\n\
659 --verbose print the name of each directory as it is created\n\
660 -V, --version-control=WORD override the usual version control\n\
661 --help display this help and exit\n\
662 --version output version information and exit\n\
666 The backup suffix is ~, unless set with SIMPLE_BACKUP_SUFFIX. The\n\
667 version control may be set with VERSION_CONTROL, values are:\n\
669 none, off never make backups (even if --backup is given)\n\
670 numbered, t make numbered backups\n\
671 existing, nil numbered if numbered backups exist, simple otherwise\n\
672 simple, never always make simple backups\n\
674 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));