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