1 /* install - copy files and set attributes
2 Copyright (C) 89, 90, 91, 95, 96, 1997 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.
56 David MacKenzie <djm@gnu.ai.mit.edu> */
65 #include <sys/types.h>
70 #include "backupfile.h"
71 #include "modechange.h"
77 # include <sys/wait.h>
85 # define BITSPERBYTE 8
88 struct passwd *getpwnam ();
89 struct group *getgrnam ();
91 #ifndef _POSIX_VERSION
97 # define endgrent() ((void) 0)
100 #ifndef HAVE_ENDPWENT
101 # define endpwent() ((void) 0)
104 /* True if C is an ASCII octal digit. */
105 #define isodigit(c) ((c) >= '0' && c <= '7')
107 /* Number of bytes of a file to copy at a time. */
108 #define READ_SIZE (32 * 1024)
111 # define UID_T_MAX ((uid_t)(~((unsigned long)1 << ((sizeof (uid_t) \
112 * BITSPERBYTE - 1)))))
116 # define GID_T_MAX ((gid_t)(~((unsigned long)1 << ((sizeof (gid_t) \
117 * BITSPERBYTE - 1)))))
124 enum backup_type get_version ();
126 static int change_timestamps __P ((const char *from, const char *to));
127 static int change_attributes __P ((const char *path, int no_need_to_chown));
128 static int copy_file __P ((const char *from, const char *to, int *to_created));
129 static int install_file_in_dir __P ((const char *from, const char *to_dir));
130 static int install_file_in_file __P ((const char *from, const char *to));
131 static void get_ids __P ((void));
132 static void strip __P ((const char *path));
133 static void usage __P ((int status));
135 /* The name this program was run with, for error messages. */
138 /* The user name that will own the files, or NULL to make the owner
139 the current user ID. */
140 static char *owner_name;
142 /* The user ID corresponding to `owner_name'. */
143 static uid_t owner_id;
145 /* The group name that will own the files, or NULL to make the group
146 the current group ID. */
147 static char *group_name;
149 /* The group ID corresponding to `group_name'. */
150 static gid_t group_id;
152 /* The permissions to which the files will be set. The umask has
156 /* If nonzero, strip executable files after copying them. */
157 static int strip_files;
159 /* If nonzero, preserve timestamps when installing files. */
160 static int preserve_timestamps;
162 /* If nonzero, install a directory instead of a regular file. */
165 /* If nonzero, show what we are doing. */
168 /* If nonzero, display usage information and exit. */
169 static int show_help;
171 /* If nonzero, print the version on standard output and exit. */
172 static int show_version;
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 {"version-control", required_argument, NULL, 'V'},
184 {"verbose", no_argument, NULL, 'v'},
185 {"help", no_argument, &show_help, 1},
186 {"version", no_argument, &show_version, 1},
191 main (int argc, char **argv)
195 char *symbolic_mode = NULL;
196 int make_backups = 0;
199 program_name = argv[0];
200 setlocale (LC_ALL, "");
201 bindtextdomain (PACKAGE, LOCALEDIR);
202 textdomain (PACKAGE);
208 preserve_timestamps = 0;
213 version = getenv ("SIMPLE_BACKUP_SUFFIX");
215 simple_backup_suffix = version;
216 version = getenv ("VERSION_CONTROL");
218 while ((optc = getopt_long (argc, argv, "bcsdg:m:o:pvV:S:", long_options,
243 symbolic_mode = optarg;
249 preserve_timestamps = 1;
252 simple_backup_suffix = optarg;
264 printf ("install (%s) %s\n", GNU_PACKAGE, VERSION);
271 /* Check for invalid combinations of arguments. */
272 if (dir_arg && strip_files)
274 _("the strip option may not be used when installing a directory"));
277 backup_type = get_version (version);
279 if (optind == argc || (optind == argc - 1 && !dir_arg))
281 error (0, 0, _("too few arguments"));
287 struct mode_change *change = mode_compile (symbolic_mode, 0);
288 if (change == MODE_INVALID)
289 error (1, 0, _("invalid mode `%s'"), symbolic_mode);
290 else if (change == MODE_MEMORY_EXHAUSTED)
291 error (1, 0, _("virtual memory exhausted"));
292 mode = mode_adjust (0, change);
299 for (; optind < argc; ++optind)
302 make_path (argv[optind], mode, mode, owner_id, group_id, 0,
303 (verbose ? "creating directory `%s'" : NULL));
308 if (optind == argc - 2)
310 if (!isdir (argv[argc - 1]))
311 errors = install_file_in_file (argv[argc - 2], argv[argc - 1]);
313 errors = install_file_in_dir (argv[argc - 2], argv[argc - 1]);
317 if (!isdir (argv[argc - 1]))
319 for (; optind < argc - 1; ++optind)
321 errors |= install_file_in_dir (argv[optind], argv[argc - 1]);
329 /* Copy file FROM onto file TO and give TO the appropriate
331 Return 0 if successful, 1 if an error occurs. */
334 install_file_in_file (const char *from, const char *to)
337 int no_need_to_chown;
339 if (copy_file (from, to, &to_created))
343 no_need_to_chown = (to_created
344 && owner_name == NULL
345 && group_name == NULL);
346 if (change_attributes (to, no_need_to_chown))
348 if (preserve_timestamps)
349 return change_timestamps (from, to);
353 /* Copy file FROM into directory TO_DIR, keeping its same name,
354 and give the copy the appropriate attributes.
355 Return 0 if successful, 1 if not. */
358 install_file_in_dir (const char *from, const char *to_dir)
364 from_base = base_name (from);
365 to = xmalloc ((unsigned) (strlen (to_dir) + strlen (from_base) + 2));
366 stpcpy (stpcpy (stpcpy (to, to_dir), "/"), from_base);
367 ret = install_file_in_file (from, to);
372 /* A chunk of a file being copied. */
373 static char buffer[READ_SIZE];
375 /* Copy file FROM onto file TO, creating TO if necessary.
376 Return 0 if the copy is successful, 1 if not. If the copy is
377 successful, set *TO_CREATED to nonzero if TO was created (if it did
378 not exist or did, but was unlinked) and to zero otherwise. If the
379 copy fails, don't modify *TO_CREATED. */
382 copy_file (const char *from, const char *to, int *to_created)
387 struct stat from_stats, to_stats;
388 int target_created = 1;
390 if (stat (from, &from_stats))
392 error (0, errno, "%s", from);
396 /* Allow installing from non-regular files like /dev/null.
397 Charles Karney reported that some Sun version of install allows that
398 and that sendmail's installation process relies on the behavior. */
399 if (S_ISDIR (from_stats.st_mode))
401 error (0, 0, _("`%s' is a directory"), from);
404 if (stat (to, &to_stats) == 0)
406 if (!S_ISREG (to_stats.st_mode))
408 error (0, 0, _("`%s' is not a regular file"), to);
411 if (from_stats.st_dev == to_stats.st_dev
412 && from_stats.st_ino == to_stats.st_ino)
414 error (0, 0, _("`%s' and `%s' are the same file"), from, to);
418 /* The destination file exists. Try to back it up if required. */
419 if (backup_type != none)
421 char *tmp_backup = find_backup_file_name (to);
424 if (tmp_backup == NULL)
425 error (1, 0, "virtual memory exhausted");
426 dst_backup = (char *) alloca (strlen (tmp_backup) + 1);
427 strcpy (dst_backup, tmp_backup);
429 if (rename (to, dst_backup))
433 error (0, errno, "cannot backup `%s'", to);
439 /* If unlink fails, try to proceed anyway. */
444 /* Now it's the time to give some feedback if requested. */
446 printf ("copying `%s' to `%s'\n", from, to);
448 fromfd = open (from, O_RDONLY, 0);
451 error (0, errno, "%s", from);
455 /* Make sure to open the file in a mode that allows writing. */
456 tofd = open (to, O_WRONLY | O_CREAT | O_TRUNC, 0600);
459 error (0, errno, "%s", to);
464 while ((bytes = safe_read (fromfd, buffer, READ_SIZE)) > 0)
465 if (full_write (tofd, buffer, bytes) < 0)
467 error (0, errno, "%s", to);
473 error (0, errno, "%s", from);
477 if (close (fromfd) < 0)
479 error (0, errno, "%s", from);
482 if (close (tofd) < 0)
484 error (0, errno, "%s", to);
488 *to_created = target_created;
497 /* Set the attributes of file or directory PATH.
498 If NO_NEED_TO_CHOWN is nonzero, don't call chown.
499 Return 0 if successful, 1 if not. */
502 change_attributes (const char *path, int no_need_to_chown)
506 /* chown must precede chmod because on some systems,
507 chown clears the set[ug]id bits for non-superusers,
508 resulting in incorrect permissions.
509 On System V, users can give away files with chown and then not
510 be able to chmod them. So don't give files away.
512 We don't pass -1 to chown to mean "don't change the value"
513 because SVR3 and earlier non-BSD systems don't support that.
515 We don't normally ignore errors from chown because the idea of
516 the install command is that the file is supposed to end up with
517 precisely the attributes that the user specified (or defaulted).
518 If the file doesn't end up with the group they asked for, they'll
519 want to know. But AFS returns EPERM when you try to change a
520 file's group; thus the kludge. */
522 if (!no_need_to_chown && chown (path, owner_id, group_id)
528 if (chmod (path, mode))
532 error (0, err, "%s", path);
538 /* Set the timestamps of file TO to match those of file FROM.
539 Return 0 if successful, 1 if not. */
542 change_timestamps (const char *from, const char *to)
547 if (stat (from, &stb))
549 error (0, errno, "%s", from);
552 utb.actime = stb.st_atime;
553 utb.modtime = stb.st_mtime;
554 if (utime (to, &utb))
556 error (0, errno, "%s", 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 (1, errno, _("fork system call failed"));
580 execlp ("strip", "strip", path, NULL);
581 error (1, errno, _("cannot run strip"));
583 default: /* Parent. */
584 /* Parent process. */
585 while (pid != wait (&status)) /* Wait for kid to finish. */
591 /* Initialize the user and group ownership of the files to install. */
601 pw = getpwnam (owner_name);
605 if (xstrtol (owner_name, NULL, 0, &tmp_long, NULL) != LONGINT_OK
606 || tmp_long < 0 || tmp_long > UID_T_MAX)
607 error (1, 0, _("invalid user `%s'"), owner_name);
608 owner_id = (uid_t) tmp_long;
611 owner_id = pw->pw_uid;
615 owner_id = getuid ();
619 gr = getgrnam (group_name);
623 if (xstrtol (group_name, NULL, 0, &tmp_long, NULL) != LONGINT_OK
624 || tmp_long < 0 || tmp_long > (long) GID_T_MAX)
625 error (1, 0, _("invalid group `%s'"), group_name);
626 group_id = (gid_t) tmp_long;
629 group_id = gr->gr_gid;
633 group_id = getgid ();
640 fprintf (stderr, _("Try `%s --help' for more information.\n"),
645 Usage: %s [OPTION]... SOURCE DEST (1st format)\n\
646 or: %s [OPTION]... SOURCE... DIRECTORY (2nd format)\n\
647 or: %s -d [OPTION]... DIRECTORY... (3rd format)\n\
649 program_name, program_name, program_name);
651 In first two formats, copy SOURCE to DEST or multiple SOURCE(s) to\n\
652 DIRECTORY, while setting permission modes and owner/group. In third\n\
653 format, make all components of the given DIRECTORY(ies).\n\
655 -b, --backup make backup before removal\n\
657 -d, --directory create [leading] directories, mandatory for 3rd format\n\
658 -g, --group=GROUP set group ownership, instead of process' current group\n\
659 -m, --mode=MODE set permission mode (as in chmod), instead of rwxr-xr-x\n\
660 -o, --owner=OWNER set ownership (super-user only)\n\
661 -p, --preserve-timestamps apply access/modification times of SOURCE files\n\
662 to corresponding destination files\n\
663 -s, --strip strip symbol tables, only for 1st and 2nd formats\n\
664 -S, --suffix=SUFFIX override the usual backup suffix\n\
665 --verbose print the name of each directory as it is created\n\
666 -V, --version-control=WORD override the usual version control\n\
667 --help display this help and exit\n\
668 --version output version information and exit\n\
672 The backup suffix is ~, unless set with SIMPLE_BACKUP_SUFFIX. The\n\
673 version control may be set with VERSION_CONTROL, values are:\n\
675 t, numbered make numbered backups\n\
676 nil, existing numbered if numbered backups exist, simple otherwise\n\
677 never, simple always make simple backups\n\
679 puts (_("\nReport bugs to <fileutils-bugs@gnu.org>."));