*** empty log message ***
[platform/upstream/coreutils.git] / src / install.c
1 /* install - copy files and set attributes
2    Copyright (C) 89, 90, 91, 1995-2006 Free Software Foundation, Inc.
3
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)
7    any later version.
8
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.
13
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Written by David MacKenzie <djm@gnu.ai.mit.edu> */
19
20 #include <config.h>
21 #include <stdio.h>
22 #include <getopt.h>
23 #include <sys/types.h>
24 #include <signal.h>
25 #include <pwd.h>
26 #include <grp.h>
27
28 #include "system.h"
29 #include "backupfile.h"
30 #include "error.h"
31 #include "cp-hash.h"
32 #include "copy.h"
33 #include "dirname.h"
34 #include "filenamecat.h"
35 #include "mkdir-p.h"
36 #include "modechange.h"
37 #include "quote.h"
38 #include "stat-time.h"
39 #include "utimens.h"
40 #include "xstrtol.h"
41
42 /* The official name of this program (e.g., no `g' prefix).  */
43 #define PROGRAM_NAME "install"
44
45 #define AUTHORS "David MacKenzie"
46
47 #if HAVE_SYS_WAIT_H
48 # include <sys/wait.h>
49 #endif
50
51 #if ! HAVE_ENDGRENT
52 # define endgrent() ((void) 0)
53 #endif
54
55 #if ! HAVE_ENDPWENT
56 # define endpwent() ((void) 0)
57 #endif
58
59 /* Initial number of entries in each hash table entry's table of inodes.  */
60 #define INITIAL_HASH_MODULE 100
61
62 /* Initial number of entries in the inode hash table.  */
63 #define INITIAL_ENTRY_TAB_SIZE 70
64
65 /* Number of bytes of a file to copy at a time. */
66 #define READ_SIZE (32 * 1024)
67
68 static bool change_timestamps (struct stat const *from_sb, char const *to);
69 static bool change_attributes (char const *name);
70 static bool copy_file (const char *from, const char *to,
71                        const struct cp_options *x);
72 static bool install_file_in_file_parents (char const *from, char const *to,
73                                           struct cp_options const *x);
74 static bool install_file_in_dir (const char *from, const char *to_dir,
75                                  const struct cp_options *x);
76 static bool install_file_in_file (const char *from, const char *to,
77                                   const struct cp_options *x);
78 static void get_ids (void);
79 static void strip (char const *name);
80 void usage (int status);
81
82 /* The name this program was run with, for error messages. */
83 char *program_name;
84
85 /* The user name that will own the files, or NULL to make the owner
86    the current user ID. */
87 static char *owner_name;
88
89 /* The user ID corresponding to `owner_name'. */
90 static uid_t owner_id;
91
92 /* The group name that will own the files, or NULL to make the group
93    the current group ID. */
94 static char *group_name;
95
96 /* The group ID corresponding to `group_name'. */
97 static gid_t group_id;
98
99 /* The permissions to which the files will be set.  The umask has
100    no effect. */
101 static mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
102
103 /* If true, strip executable files after copying them. */
104 static bool strip_files;
105
106 /* If true, install a directory instead of a regular file. */
107 static bool dir_arg;
108
109 static struct option const long_options[] =
110 {
111   {"backup", optional_argument, NULL, 'b'},
112   {"directory", no_argument, NULL, 'd'},
113   {"group", required_argument, NULL, 'g'},
114   {"mode", required_argument, NULL, 'm'},
115   {"no-target-directory", no_argument, NULL, 'T'},
116   {"owner", required_argument, NULL, 'o'},
117   {"preserve-timestamps", no_argument, NULL, 'p'},
118   {"strip", no_argument, NULL, 's'},
119   {"suffix", required_argument, NULL, 'S'},
120   {"target-directory", required_argument, NULL, 't'},
121   {"verbose", no_argument, NULL, 'v'},
122   {GETOPT_HELP_OPTION_DECL},
123   {GETOPT_VERSION_OPTION_DECL},
124   {NULL, 0, NULL, 0}
125 };
126
127 static void
128 cp_option_init (struct cp_options *x)
129 {
130   x->copy_as_regular = true;
131   x->dereference = DEREF_ALWAYS;
132   x->unlink_dest_before_opening = true;
133   x->unlink_dest_after_failed_open = false;
134   x->hard_link = false;
135   x->interactive = I_UNSPECIFIED;
136   x->move_mode = false;
137   x->chown_privileges = chown_privileges ();
138   x->one_file_system = false;
139   x->preserve_ownership = false;
140   x->preserve_links = false;
141   x->preserve_mode = false;
142   x->preserve_timestamps = false;
143   x->require_preserve = false;
144   x->recursive = false;
145   x->sparse_mode = SPARSE_AUTO;
146   x->symbolic_link = false;
147   x->backup_type = no_backups;
148
149   /* Create destination files initially writable so we can run strip on them.
150      Although GNU strip works fine on read-only files, some others
151      would fail.  */
152   x->set_mode = true;
153   x->mode = S_IRUSR | S_IWUSR;
154   x->stdin_tty = false;
155
156   x->update = false;
157   x->verbose = false;
158   x->dest_info = NULL;
159   x->src_info = NULL;
160 }
161
162 /* FILE is the last operand of this command.  Return true if FILE is a
163    directory.  But report an error there is a problem accessing FILE,
164    or if FILE does not exist but would have to refer to an existing
165    directory if it referred to anything at all.  */
166
167 static bool
168 target_directory_operand (char const *file)
169 {
170   char const *b = last_component (file);
171   size_t blen = strlen (b);
172   bool looks_like_a_dir = (blen == 0 || ISSLASH (b[blen - 1]));
173   struct stat st;
174   int err = (stat (file, &st) == 0 ? 0 : errno);
175   bool is_a_dir = !err && S_ISDIR (st.st_mode);
176   if (err && err != ENOENT)
177     error (EXIT_FAILURE, err, _("accessing %s"), quote (file));
178   if (is_a_dir < looks_like_a_dir)
179     error (EXIT_FAILURE, err, _("target %s is not a directory"), quote (file));
180   return is_a_dir;
181 }
182
183 int
184 main (int argc, char **argv)
185 {
186   int optc;
187   bool ok = true;
188   const char *specified_mode = NULL;
189   bool make_backups = false;
190   char *backup_suffix_string;
191   char *version_control_string = NULL;
192   bool mkdir_and_install = false;
193   struct cp_options x;
194   char const *target_directory = NULL;
195   bool no_target_directory = false;
196   int n_files;
197   char **file;
198
199   initialize_main (&argc, &argv);
200   program_name = argv[0];
201   setlocale (LC_ALL, "");
202   bindtextdomain (PACKAGE, LOCALEDIR);
203   textdomain (PACKAGE);
204
205   atexit (close_stdout);
206
207   cp_option_init (&x);
208
209   owner_name = NULL;
210   group_name = NULL;
211   strip_files = false;
212   dir_arg = false;
213   umask (0);
214
215   /* FIXME: consider not calling getenv for SIMPLE_BACKUP_SUFFIX unless
216      we'll actually use backup_suffix_string.  */
217   backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
218
219   while ((optc = getopt_long (argc, argv, "bcsDdg:m:o:pt:TvS:", long_options,
220                               NULL)) != -1)
221     {
222       switch (optc)
223         {
224         case 'b':
225           make_backups = true;
226           if (optarg)
227             version_control_string = optarg;
228           break;
229         case 'c':
230           break;
231         case 's':
232           strip_files = true;
233 #ifdef SIGCHLD
234           /* System V fork+wait does not work if SIGCHLD is ignored.  */
235           signal (SIGCHLD, SIG_DFL);
236 #endif
237           break;
238         case 'd':
239           dir_arg = true;
240           break;
241         case 'D':
242           mkdir_and_install = true;
243           break;
244         case 'v':
245           x.verbose = true;
246           break;
247         case 'g':
248           group_name = optarg;
249           break;
250         case 'm':
251           specified_mode = optarg;
252           break;
253         case 'o':
254           owner_name = optarg;
255           break;
256         case 'p':
257           x.preserve_timestamps = true;
258           break;
259         case 'S':
260           make_backups = true;
261           backup_suffix_string = optarg;
262           break;
263         case 't':
264           if (target_directory)
265             error (EXIT_FAILURE, 0,
266                    _("multiple target directories specified"));
267           else
268             {
269               struct stat st;
270               if (stat (optarg, &st) != 0)
271                 error (EXIT_FAILURE, errno, _("accessing %s"), quote (optarg));
272               if (! S_ISDIR (st.st_mode))
273                 error (EXIT_FAILURE, 0, _("target %s is not a directory"),
274                        quote (optarg));
275             }
276           target_directory = optarg;
277           break;
278         case 'T':
279           no_target_directory = true;
280           break;
281         case_GETOPT_HELP_CHAR;
282         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
283         default:
284           usage (EXIT_FAILURE);
285         }
286     }
287
288   /* Check for invalid combinations of arguments. */
289   if (dir_arg & strip_files)
290     error (EXIT_FAILURE, 0,
291            _("the strip option may not be used when installing a directory"));
292   if (dir_arg && target_directory)
293     error (EXIT_FAILURE, 0,
294            _("target directory not allowed when installing a directory"));
295
296   if (backup_suffix_string)
297     simple_backup_suffix = xstrdup (backup_suffix_string);
298
299   x.backup_type = (make_backups
300                    ? xget_version (_("backup type"),
301                                    version_control_string)
302                    : no_backups);
303
304   n_files = argc - optind;
305   file = argv + optind;
306
307   if (n_files <= ! (dir_arg || target_directory))
308     {
309       if (n_files <= 0)
310         error (0, 0, _("missing file operand"));
311       else
312         error (0, 0, _("missing destination file operand after %s"),
313                quote (file[0]));
314       usage (EXIT_FAILURE);
315     }
316
317   if (no_target_directory)
318     {
319       if (target_directory)
320         error (EXIT_FAILURE, 0,
321                _("Cannot combine --target-directory (-t) "
322                  "and --no-target-directory (-T)"));
323       if (2 < n_files)
324         {
325           error (0, 0, _("extra operand %s"), quote (file[2]));
326           usage (EXIT_FAILURE);
327         }
328     }
329   else if (! (dir_arg || target_directory))
330     {
331       if (2 <= n_files && target_directory_operand (file[n_files - 1]))
332         target_directory = file[--n_files];
333       else if (2 < n_files)
334         error (EXIT_FAILURE, 0, _("target %s is not a directory"),
335                quote (file[n_files - 1]));
336     }
337
338   if (specified_mode)
339     {
340       struct mode_change *change = mode_compile (specified_mode);
341       if (!change)
342         error (EXIT_FAILURE, 0, _("invalid mode %s"), quote (specified_mode));
343       mode = mode_adjust (0, change, 0);
344       free (change);
345     }
346
347   get_ids ();
348
349   if (dir_arg)
350     {
351       int i;
352       int cwd_errno = 0;
353       for (i = 0; i < n_files; i++)
354         {
355           if (cwd_errno != 0 && IS_RELATIVE_FILE_NAME (file[i]))
356             {
357               error (0, cwd_errno, _("cannot return to working directory"));
358               ok = false;
359             }
360           else
361             ok &=
362               make_dir_parents (file[i], mode, mode, owner_id, group_id, false,
363                                 (x.verbose ? _("creating directory %s") : NULL),
364                                 &cwd_errno);
365         }
366     }
367   else
368     {
369       /* FIXME: it's a little gross that this initialization is
370          required by copy.c::copy. */
371       hash_init ();
372
373       if (!target_directory)
374         {
375           if (mkdir_and_install)
376             ok = install_file_in_file_parents (file[0], file[1], &x);
377           else
378             ok = install_file_in_file (file[0], file[1], &x);
379         }
380       else
381         {
382           int i;
383           dest_info_init (&x);
384           for (i = 0; i < n_files; i++)
385             {
386               ok &= install_file_in_dir (file[i], target_directory, &x);
387             }
388         }
389     }
390
391   exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
392 }
393
394 /* Copy file FROM onto file TO, creating any missing parent directories of TO.
395    Return true if successful.  */
396
397 static bool
398 install_file_in_file_parents (char const *from, char const *to,
399                               struct cp_options const *x)
400 {
401   char *dest_dir = dir_name (to);
402   bool ok = true;
403
404   /* Make sure that the parent of the destination is a directory.  */
405   if (! STREQ (dest_dir, "."))
406     {
407       /* Someone will probably ask for a new option or three to specify
408          owner, group, and permissions for parent directories.  Remember
409          that this option is intended mainly to help installers when the
410          distribution doesn't provide proper install rules.  */
411       mode_t dir_mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
412       int cwd_errno = 0;
413       ok = make_dir_parents (dest_dir, dir_mode, dir_mode,
414                              owner_id, group_id, true,
415                              (x->verbose ? _("creating directory %s") : NULL),
416                              &cwd_errno);
417       if (ok && cwd_errno != 0
418           && (IS_RELATIVE_FILE_NAME (from) || IS_RELATIVE_FILE_NAME (to)))
419         {
420           error (0, cwd_errno, _("cannot return to current directory"));
421           ok = false;
422         }
423     }
424
425   free (dest_dir);
426
427   if (ok)
428     ok = install_file_in_file (from, to, x);
429
430   return ok;
431 }
432
433 /* Copy file FROM onto file TO and give TO the appropriate
434    attributes.
435    Return true if successful.  */
436
437 static bool
438 install_file_in_file (const char *from, const char *to,
439                       const struct cp_options *x)
440 {
441   struct stat from_sb;
442   if (x->preserve_timestamps && stat (from, &from_sb) != 0)
443     {
444       error (0, errno, _("cannot stat %s"), quote (from));
445       return false;
446     }
447   if (! copy_file (from, to, x))
448     return false;
449   if (strip_files)
450     strip (to);
451   if (! change_attributes (to))
452     return false;
453   if (x->preserve_timestamps && (strip_files || ! S_ISREG (from_sb.st_mode)))
454     return change_timestamps (&from_sb, to);
455   return true;
456 }
457
458 /* Copy file FROM into directory TO_DIR, keeping its same name,
459    and give the copy the appropriate attributes.
460    Return true if successful.  */
461
462 static bool
463 install_file_in_dir (const char *from, const char *to_dir,
464                      const struct cp_options *x)
465 {
466   const char *from_base = last_component (from);
467   char *to = file_name_concat (to_dir, from_base, NULL);
468   bool ret = install_file_in_file (from, to, x);
469   free (to);
470   return ret;
471 }
472
473 /* Copy file FROM onto file TO, creating TO if necessary.
474    Return true if successful.  */
475
476 static bool
477 copy_file (const char *from, const char *to, const struct cp_options *x)
478 {
479   bool copy_into_self;
480
481   /* Allow installing from non-regular files like /dev/null.
482      Charles Karney reported that some Sun version of install allows that
483      and that sendmail's installation process relies on the behavior.
484      However, since !x->recursive, the call to "copy" will fail if FROM
485      is a directory.  */
486
487   return copy (from, to, false, x, &copy_into_self, NULL);
488 }
489
490 /* Set the attributes of file or directory NAME.
491    Return true if successful.  */
492
493 static bool
494 change_attributes (char const *name)
495 {
496   bool ok = true;
497
498   /* chown must precede chmod because on some systems,
499      chown clears the set[ug]id bits for non-superusers,
500      resulting in incorrect permissions.
501      On System V, users can give away files with chown and then not
502      be able to chmod them.  So don't give files away.
503
504      We don't normally ignore errors from chown because the idea of
505      the install command is that the file is supposed to end up with
506      precisely the attributes that the user specified (or defaulted).
507      If the file doesn't end up with the group they asked for, they'll
508      want to know.  But AFS returns EPERM when you try to change a
509      file's group; thus the kludge.  */
510
511   if (chown (name, owner_id, group_id) != 0
512 #ifdef AFS
513       && errno != EPERM
514 #endif
515       )
516     {
517       error (0, errno, _("cannot change ownership of %s"), quote (name));
518       ok = false;
519     }
520
521   if (ok && chmod (name, mode) != 0)
522     {
523       error (0, errno, _("cannot change permissions of %s"), quote (name));
524       ok = false;
525     }
526
527   return ok;
528 }
529
530 /* Set the timestamps of file TO to match those of file FROM.
531    Return true if successful.  */
532
533 static bool
534 change_timestamps (struct stat const *from_sb, char const *to)
535 {
536   struct timespec timespec[2];
537   timespec[0] = get_stat_atime (from_sb);
538   timespec[1] = get_stat_mtime (from_sb);
539
540   if (utimens (to, timespec))
541     {
542       error (0, errno, _("cannot set time stamps for %s"), quote (to));
543       return false;
544     }
545   return true;
546 }
547
548 /* Strip the symbol table from the file NAME.
549    We could dig the magic number out of the file first to
550    determine whether to strip it, but the header files and
551    magic numbers vary so much from system to system that making
552    it portable would be very difficult.  Not worth the effort. */
553
554 static void
555 strip (char const *name)
556 {
557   int status;
558   pid_t pid = fork ();
559
560   switch (pid)
561     {
562     case -1:
563       error (EXIT_FAILURE, errno, _("fork system call failed"));
564       break;
565     case 0:                     /* Child. */
566       execlp ("strip", "strip", name, NULL);
567       error (EXIT_FAILURE, errno, _("cannot run strip"));
568       break;
569     default:                    /* Parent. */
570       /* Parent process. */
571       while (pid != wait (&status))     /* Wait for kid to finish. */
572         /* Do nothing. */ ;
573       if (status)
574         error (EXIT_FAILURE, 0, _("strip failed"));
575       break;
576     }
577 }
578
579 /* Initialize the user and group ownership of the files to install. */
580
581 static void
582 get_ids (void)
583 {
584   struct passwd *pw;
585   struct group *gr;
586
587   if (owner_name)
588     {
589       pw = getpwnam (owner_name);
590       if (pw == NULL)
591         {
592           unsigned long int tmp;
593           if (xstrtoul (owner_name, NULL, 0, &tmp, NULL) != LONGINT_OK
594               || UID_T_MAX < tmp)
595             error (EXIT_FAILURE, 0, _("invalid user %s"), quote (owner_name));
596           owner_id = tmp;
597         }
598       else
599         owner_id = pw->pw_uid;
600       endpwent ();
601     }
602   else
603     owner_id = (uid_t) -1;
604
605   if (group_name)
606     {
607       gr = getgrnam (group_name);
608       if (gr == NULL)
609         {
610           unsigned long int tmp;
611           if (xstrtoul (group_name, NULL, 0, &tmp, NULL) != LONGINT_OK
612               || GID_T_MAX < tmp)
613             error (EXIT_FAILURE, 0, _("invalid group %s"), quote (group_name));
614           group_id = tmp;
615         }
616       else
617         group_id = gr->gr_gid;
618       endgrent ();
619     }
620   else
621     group_id = (gid_t) -1;
622 }
623
624 void
625 usage (int status)
626 {
627   if (status != EXIT_SUCCESS)
628     fprintf (stderr, _("Try `%s --help' for more information.\n"),
629              program_name);
630   else
631     {
632       printf (_("\
633 Usage: %s [OPTION]... [-T] SOURCE DEST\n\
634   or:  %s [OPTION]... SOURCE... DIRECTORY\n\
635   or:  %s [OPTION]... -t DIRECTORY SOURCE...\n\
636   or:  %s [OPTION]... -d DIRECTORY...\n\
637 "),
638               program_name, program_name, program_name, program_name);
639       fputs (_("\
640 In the first three forms, copy SOURCE to DEST or multiple SOURCE(s) to\n\
641 the existing DIRECTORY, while setting permission modes and owner/group.\n\
642 In the 4th form, create all components of the given DIRECTORY(ies).\n\
643 \n\
644 "), stdout);
645       fputs (_("\
646 Mandatory arguments to long options are mandatory for short options too.\n\
647 "), stdout);
648       fputs (_("\
649       --backup[=CONTROL]  make a backup of each existing destination file\n\
650   -b                  like --backup but does not accept an argument\n\
651   -c                  (ignored)\n\
652   -d, --directory     treat all arguments as directory names; create all\n\
653                         components of the specified directories\n\
654 "), stdout);
655       fputs (_("\
656   -D                  create all leading components of DEST except the last,\n\
657                         then copy SOURCE to DEST\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 "), stdout);
662       fputs (_("\
663   -p, --preserve-timestamps   apply access/modification times of SOURCE files\n\
664                         to corresponding destination files\n\
665   -s, --strip         strip symbol tables\n\
666   -S, --suffix=SUFFIX override the usual backup suffix\n\
667   -t, --target-directory=DIRECTORY  copy all SOURCE arguments into DIRECTORY\n\
668   -T, --no-target-directory  treat DEST as a normal file\n\
669   -v, --verbose       print the name of each directory as it is created\n\
670 "), stdout);
671       fputs (HELP_OPTION_DESCRIPTION, stdout);
672       fputs (VERSION_OPTION_DESCRIPTION, stdout);
673       fputs (_("\
674 \n\
675 The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
676 The version control method may be selected via the --backup option or through\n\
677 the VERSION_CONTROL environment variable.  Here are the values:\n\
678 \n\
679 "), stdout);
680       fputs (_("\
681   none, off       never make backups (even if --backup is given)\n\
682   numbered, t     make numbered backups\n\
683   existing, nil   numbered if numbered backups exist, simple otherwise\n\
684   simple, never   always make simple backups\n\
685 "), stdout);
686       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
687     }
688   exit (status);
689 }