#ifdef-out matchpathcon-related code, for now.
[platform/upstream/coreutils.git] / src / install.c
1 /* install - copy files and set attributes
2    Copyright (C) 89, 90, 91, 1995-2007 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 3 of the License, or
7    (at your option) 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, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by David MacKenzie <djm@gnu.ai.mit.edu> */
18
19 #include <config.h>
20 #include <stdio.h>
21 #include <getopt.h>
22 #include <sys/types.h>
23 #include <signal.h>
24 #include <pwd.h>
25 #include <grp.h>
26 #include <selinux/selinux.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 "filenamecat.h"
34 #include "mkancesdirs.h"
35 #include "mkdir-p.h"
36 #include "modechange.h"
37 #include "quote.h"
38 #include "quotearg.h"
39 #include "savewd.h"
40 #include "stat-time.h"
41 #include "utimens.h"
42 #include "xstrtol.h"
43
44 /* The official name of this program (e.g., no `g' prefix).  */
45 #define PROGRAM_NAME "install"
46
47 #define AUTHORS "David MacKenzie"
48
49 #if HAVE_SYS_WAIT_H
50 # include <sys/wait.h>
51 #endif
52
53 static int selinux_enabled = 0;
54 static bool use_default_selinux_context = true;
55
56 #if ! HAVE_ENDGRENT
57 # define endgrent() ((void) 0)
58 #endif
59
60 #if ! HAVE_ENDPWENT
61 # define endpwent() ((void) 0)
62 #endif
63
64 #if ! HAVE_LCHOWN
65 # define lchown(name, uid, gid) chown (name, uid, gid)
66 #endif
67
68 /* Initial number of entries in each hash table entry's table of inodes.  */
69 #define INITIAL_HASH_MODULE 100
70
71 /* Initial number of entries in the inode hash table.  */
72 #define INITIAL_ENTRY_TAB_SIZE 70
73
74 /* Number of bytes of a file to copy at a time. */
75 #define READ_SIZE (32 * 1024)
76
77 static bool change_timestamps (struct stat const *from_sb, char const *to);
78 static bool change_attributes (char const *name);
79 static bool copy_file (const char *from, const char *to,
80                        const struct cp_options *x);
81 static bool install_file_in_file_parents (char const *from, char *to,
82                                           struct cp_options *x);
83 static bool install_file_in_dir (const char *from, const char *to_dir,
84                                  const struct cp_options *x);
85 static bool install_file_in_file (const char *from, const char *to,
86                                   const struct cp_options *x);
87 static void get_ids (void);
88 static void strip (char const *name);
89 static void announce_mkdir (char const *dir, void *options);
90 static int make_ancestor (char const *dir, char const *component,
91                           void *options);
92 void usage (int status);
93
94 /* The name this program was run with, for error messages. */
95 char *program_name;
96
97 /* The user name that will own the files, or NULL to make the owner
98    the current user ID. */
99 static char *owner_name;
100
101 /* The user ID corresponding to `owner_name'. */
102 static uid_t owner_id;
103
104 /* The group name that will own the files, or NULL to make the group
105    the current group ID. */
106 static char *group_name;
107
108 /* The group ID corresponding to `group_name'. */
109 static gid_t group_id;
110
111 #define DEFAULT_MODE (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
112
113 /* The file mode bits to which non-directory files will be set.  The umask has
114    no effect. */
115 static mode_t mode = DEFAULT_MODE;
116
117 /* Similar, but for directories.  */
118 static mode_t dir_mode = DEFAULT_MODE;
119
120 /* The file mode bits that the user cares about.  This should be a
121    superset of DIR_MODE and a subset of CHMOD_MODE_BITS.  This matters
122    for directories, since otherwise directories may keep their S_ISUID
123    or S_ISGID bits.  */
124 static mode_t dir_mode_bits = CHMOD_MODE_BITS;
125
126 /* If true, strip executable files after copying them. */
127 static bool strip_files;
128
129 /* If true, install a directory instead of a regular file. */
130 static bool dir_arg;
131
132 /* For long options that have no equivalent short option, use a
133    non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
134 enum
135 {
136   PRESERVE_CONTEXT_OPTION = CHAR_MAX + 1
137 };
138
139 static struct option const long_options[] =
140 {
141   {"backup", optional_argument, NULL, 'b'},
142   {GETOPT_SELINUX_CONTEXT_OPTION_DECL},
143   {"directory", no_argument, NULL, 'd'},
144   {"group", required_argument, NULL, 'g'},
145   {"mode", required_argument, NULL, 'm'},
146   {"no-target-directory", no_argument, NULL, 'T'},
147   {"owner", required_argument, NULL, 'o'},
148   {"preserve-timestamps", no_argument, NULL, 'p'},
149   {"preserve-context", no_argument, NULL, PRESERVE_CONTEXT_OPTION},
150   /* Continue silent support for --preserve_context until Jan 2008. FIXME-obs
151      After that, FIXME-obs: warn in, say, late 2008, and disable altogether
152      a year or two later.  */
153   {"preserve_context", no_argument, NULL, PRESERVE_CONTEXT_OPTION},
154   {"strip", no_argument, NULL, 's'},
155   {"suffix", required_argument, NULL, 'S'},
156   {"target-directory", required_argument, NULL, 't'},
157   {"verbose", no_argument, NULL, 'v'},
158   {GETOPT_HELP_OPTION_DECL},
159   {GETOPT_VERSION_OPTION_DECL},
160   {NULL, 0, NULL, 0}
161 };
162
163 static void
164 cp_option_init (struct cp_options *x)
165 {
166   x->copy_as_regular = true;
167   x->dereference = DEREF_ALWAYS;
168   x->unlink_dest_before_opening = true;
169   x->unlink_dest_after_failed_open = false;
170   x->hard_link = false;
171   x->interactive = I_UNSPECIFIED;
172   x->move_mode = false;
173   x->chown_privileges = chown_privileges ();
174   x->one_file_system = false;
175   x->preserve_ownership = false;
176   x->preserve_links = false;
177   x->preserve_mode = false;
178   x->preserve_timestamps = false;
179   x->require_preserve = false;
180   x->require_preserve_context = false;
181   x->recursive = false;
182   x->sparse_mode = SPARSE_AUTO;
183   x->symbolic_link = false;
184   x->backup_type = no_backups;
185
186   /* Create destination files initially writable so we can run strip on them.
187      Although GNU strip works fine on read-only files, some others
188      would fail.  */
189   x->set_mode = true;
190   x->mode = S_IRUSR | S_IWUSR;
191   x->stdin_tty = false;
192
193   x->update = false;
194   x->preserve_security_context = false;
195   x->verbose = false;
196   x->dest_info = NULL;
197   x->src_info = NULL;
198 }
199
200 /* Modify file context to match the specified policy.
201    If an error occurs the file will remain with the default directory
202    context.  */
203 static void
204 setdefaultfilecon (char const *file)
205 {
206   struct stat st;
207   security_context_t scontext = NULL;
208   if (selinux_enabled != 1)
209     {
210       /* Indicate no context found. */
211       return;
212     }
213   if (lstat (file, &st) != 0)
214     return;
215
216 #ifdef ENABLE_WHEN_MATCHPATHCON_IS_MORE_EFFICIENT
217   if (IS_ABSOLUTE_FILE_NAME (file))
218     {
219       /* Calling matchpathcon_init_prefix (NULL, "/first_component/")
220          is an optimization to minimize the expense of the following
221          matchpathcon call.  */
222       char const *p0;
223       char const *p = file + 1;
224       while (ISSLASH (*p))
225         ++p;
226
227       /* Record final leading slash, for when FILE starts with two or more.  */
228       p0 = p - 1;
229
230       if (*p)
231         {
232           char *prefix;
233           do
234             {
235               ++p;
236             }
237           while (*p && !ISSLASH (*p));
238
239           prefix = malloc (p - p0 + 2);
240           if (prefix)
241             {
242               stpcpy (stpncpy (prefix, p0, p - p0), "/");
243               matchpathcon_init_prefix (NULL, prefix);
244               free (prefix);
245             }
246         }
247     }
248
249   /* If there's an error determining the context, or it has none,
250      return to allow default context */
251   if ((matchpathcon (file, st.st_mode, &scontext) != 0) ||
252       STREQ (scontext, "<<none>>"))
253     {
254       if (scontext != NULL)
255         freecon (scontext);
256       return;
257     }
258 #endif
259
260   if (lsetfilecon (file, scontext) < 0 && errno != ENOTSUP)
261     error (0, errno,
262            _("warning: %s: failed to change context to %s"),
263            quotearg_colon (file), scontext);
264
265   freecon (scontext);
266   return;
267 }
268
269 /* FILE is the last operand of this command.  Return true if FILE is a
270    directory.  But report an error there is a problem accessing FILE,
271    or if FILE does not exist but would have to refer to an existing
272    directory if it referred to anything at all.  */
273
274 static bool
275 target_directory_operand (char const *file)
276 {
277   char const *b = last_component (file);
278   size_t blen = strlen (b);
279   bool looks_like_a_dir = (blen == 0 || ISSLASH (b[blen - 1]));
280   struct stat st;
281   int err = (stat (file, &st) == 0 ? 0 : errno);
282   bool is_a_dir = !err && S_ISDIR (st.st_mode);
283   if (err && err != ENOENT)
284     error (EXIT_FAILURE, err, _("accessing %s"), quote (file));
285   if (is_a_dir < looks_like_a_dir)
286     error (EXIT_FAILURE, err, _("target %s is not a directory"), quote (file));
287   return is_a_dir;
288 }
289
290 /* Process a command-line file name, for the -d option.  */
291 static int
292 process_dir (char *dir, struct savewd *wd, void *options)
293 {
294   return (make_dir_parents (dir, wd,
295                             make_ancestor, options,
296                             dir_mode, announce_mkdir,
297                             dir_mode_bits, owner_id, group_id, false)
298           ? EXIT_SUCCESS
299           : EXIT_FAILURE);
300 }
301
302 int
303 main (int argc, char **argv)
304 {
305   int optc;
306   int exit_status = EXIT_SUCCESS;
307   const char *specified_mode = NULL;
308   bool make_backups = false;
309   char *backup_suffix_string;
310   char *version_control_string = NULL;
311   bool mkdir_and_install = false;
312   struct cp_options x;
313   char const *target_directory = NULL;
314   bool no_target_directory = false;
315   int n_files;
316   char **file;
317   security_context_t scontext = NULL;
318   /* set iff kernel has extra selinux system calls */
319   selinux_enabled = (0 < is_selinux_enabled ());
320
321   initialize_main (&argc, &argv);
322   program_name = argv[0];
323   setlocale (LC_ALL, "");
324   bindtextdomain (PACKAGE, LOCALEDIR);
325   textdomain (PACKAGE);
326
327   atexit (close_stdin);
328
329   cp_option_init (&x);
330
331   owner_name = NULL;
332   group_name = NULL;
333   strip_files = false;
334   dir_arg = false;
335   umask (0);
336
337   /* FIXME: consider not calling getenv for SIMPLE_BACKUP_SUFFIX unless
338      we'll actually use backup_suffix_string.  */
339   backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
340
341   while ((optc = getopt_long (argc, argv, "bcsDdg:m:o:pt:TvS:Z:", long_options,
342                               NULL)) != -1)
343     {
344       switch (optc)
345         {
346         case 'b':
347           make_backups = true;
348           if (optarg)
349             version_control_string = optarg;
350           break;
351         case 'c':
352           break;
353         case 's':
354           strip_files = true;
355 #ifdef SIGCHLD
356           /* System V fork+wait does not work if SIGCHLD is ignored.  */
357           signal (SIGCHLD, SIG_DFL);
358 #endif
359           break;
360         case 'd':
361           dir_arg = true;
362           break;
363         case 'D':
364           mkdir_and_install = true;
365           break;
366         case 'v':
367           x.verbose = true;
368           break;
369         case 'g':
370           group_name = optarg;
371           break;
372         case 'm':
373           specified_mode = optarg;
374           break;
375         case 'o':
376           owner_name = optarg;
377           break;
378         case 'p':
379           x.preserve_timestamps = true;
380           break;
381         case 'S':
382           make_backups = true;
383           backup_suffix_string = optarg;
384           break;
385         case 't':
386           if (target_directory)
387             error (EXIT_FAILURE, 0,
388                    _("multiple target directories specified"));
389           else
390             {
391               struct stat st;
392               if (stat (optarg, &st) != 0)
393                 error (EXIT_FAILURE, errno, _("accessing %s"), quote (optarg));
394               if (! S_ISDIR (st.st_mode))
395                 error (EXIT_FAILURE, 0, _("target %s is not a directory"),
396                        quote (optarg));
397             }
398           target_directory = optarg;
399           break;
400         case 'T':
401           no_target_directory = true;
402           break;
403
404         case PRESERVE_CONTEXT_OPTION:
405           if ( ! selinux_enabled)
406             {
407               error (0, 0, _("Warning: ignoring --preserve-context; "
408                              "this kernel is not SELinux-enabled."));
409               break;
410             }
411           x.preserve_security_context = true;
412           use_default_selinux_context = false;
413           break;
414         case 'Z':
415           if ( ! selinux_enabled)
416             {
417               error (0, 0, _("Warning: ignoring --context (-Z); "
418                              "this kernel is not SELinux-enabled."));
419               break;
420             }
421           scontext = optarg;
422           use_default_selinux_context = false;
423           break;
424         case_GETOPT_HELP_CHAR;
425         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
426         default:
427           usage (EXIT_FAILURE);
428         }
429     }
430
431   /* Check for invalid combinations of arguments. */
432   if (dir_arg & strip_files)
433     error (EXIT_FAILURE, 0,
434            _("the strip option may not be used when installing a directory"));
435   if (dir_arg && target_directory)
436     error (EXIT_FAILURE, 0,
437            _("target directory not allowed when installing a directory"));
438
439   if (x.preserve_security_context && scontext != NULL)
440     error (EXIT_FAILURE, 0,
441            _("cannot force target context to %s and preserve it"),
442            quote (scontext));
443
444   if (backup_suffix_string)
445     simple_backup_suffix = xstrdup (backup_suffix_string);
446
447   x.backup_type = (make_backups
448                    ? xget_version (_("backup type"),
449                                    version_control_string)
450                    : no_backups);
451
452   if (scontext && setfscreatecon (scontext) < 0)
453     error (EXIT_FAILURE, errno,
454            _("failed to set default file creation context to %s"),
455            quote (scontext));
456
457   n_files = argc - optind;
458   file = argv + optind;
459
460   if (n_files <= ! (dir_arg || target_directory))
461     {
462       if (n_files <= 0)
463         error (0, 0, _("missing file operand"));
464       else
465         error (0, 0, _("missing destination file operand after %s"),
466                quote (file[0]));
467       usage (EXIT_FAILURE);
468     }
469
470   if (no_target_directory)
471     {
472       if (target_directory)
473         error (EXIT_FAILURE, 0,
474                _("Cannot combine --target-directory (-t) "
475                  "and --no-target-directory (-T)"));
476       if (2 < n_files)
477         {
478           error (0, 0, _("extra operand %s"), quote (file[2]));
479           usage (EXIT_FAILURE);
480         }
481     }
482   else if (! (dir_arg || target_directory))
483     {
484       if (2 <= n_files && target_directory_operand (file[n_files - 1]))
485         target_directory = file[--n_files];
486       else if (2 < n_files)
487         error (EXIT_FAILURE, 0, _("target %s is not a directory"),
488                quote (file[n_files - 1]));
489     }
490
491   if (specified_mode)
492     {
493       struct mode_change *change = mode_compile (specified_mode);
494       if (!change)
495         error (EXIT_FAILURE, 0, _("invalid mode %s"), quote (specified_mode));
496       mode = mode_adjust (0, false, 0, change, NULL);
497       dir_mode = mode_adjust (0, true, 0, change, &dir_mode_bits);
498       free (change);
499     }
500
501   get_ids ();
502
503   if (dir_arg)
504     exit_status = savewd_process_files (n_files, file, process_dir, &x);
505   else
506     {
507       /* FIXME: it's a little gross that this initialization is
508          required by copy.c::copy. */
509       hash_init ();
510
511       if (!target_directory)
512         {
513           if (! (mkdir_and_install
514                  ? install_file_in_file_parents (file[0], file[1], &x)
515                  : install_file_in_file (file[0], file[1], &x)))
516             exit_status = EXIT_FAILURE;
517         }
518       else
519         {
520           int i;
521           dest_info_init (&x);
522           for (i = 0; i < n_files; i++)
523             if (! install_file_in_dir (file[i], target_directory, &x))
524               exit_status = EXIT_FAILURE;
525         }
526     }
527
528   exit (exit_status);
529 }
530
531 /* Copy file FROM onto file TO, creating any missing parent directories of TO.
532    Return true if successful.  */
533
534 static bool
535 install_file_in_file_parents (char const *from, char *to,
536                               struct cp_options *x)
537 {
538   bool save_working_directory =
539     ! (IS_ABSOLUTE_FILE_NAME (from) && IS_ABSOLUTE_FILE_NAME (to));
540   int status = EXIT_SUCCESS;
541
542   struct savewd wd;
543   savewd_init (&wd);
544   if (! save_working_directory)
545     savewd_finish (&wd);
546
547   if (mkancesdirs (to, &wd, make_ancestor, x) == -1)
548     {
549       error (0, errno, _("cannot create directory %s"), to);
550       status = EXIT_FAILURE;
551     }
552
553   if (save_working_directory)
554     {
555       int restore_result = savewd_restore (&wd, status);
556       int restore_errno = errno;
557       savewd_finish (&wd);
558       if (EXIT_SUCCESS < restore_result)
559         return false;
560       if (restore_result < 0 && status == EXIT_SUCCESS)
561         {
562           error (0, restore_errno, _("cannot create directory %s"), to);
563           return false;
564         }
565     }
566
567   return (status == EXIT_SUCCESS && install_file_in_file (from, to, x));
568 }
569
570 /* Copy file FROM onto file TO and give TO the appropriate
571    attributes.
572    Return true if successful.  */
573
574 static bool
575 install_file_in_file (const char *from, const char *to,
576                       const struct cp_options *x)
577 {
578   struct stat from_sb;
579   if (x->preserve_timestamps && stat (from, &from_sb) != 0)
580     {
581       error (0, errno, _("cannot stat %s"), quote (from));
582       return false;
583     }
584   if (! copy_file (from, to, x))
585     return false;
586   if (strip_files)
587     strip (to);
588   if (x->preserve_timestamps && (strip_files || ! S_ISREG (from_sb.st_mode))
589       && ! change_timestamps (&from_sb, to))
590     return false;
591   return change_attributes (to);
592 }
593
594 /* Copy file FROM into directory TO_DIR, keeping its same name,
595    and give the copy the appropriate attributes.
596    Return true if successful.  */
597
598 static bool
599 install_file_in_dir (const char *from, const char *to_dir,
600                      const struct cp_options *x)
601 {
602   const char *from_base = last_component (from);
603   char *to = file_name_concat (to_dir, from_base, NULL);
604   bool ret = install_file_in_file (from, to, x);
605   free (to);
606   return ret;
607 }
608
609 /* Copy file FROM onto file TO, creating TO if necessary.
610    Return true if successful.  */
611
612 static bool
613 copy_file (const char *from, const char *to, const struct cp_options *x)
614 {
615   bool copy_into_self;
616
617   /* Allow installing from non-regular files like /dev/null.
618      Charles Karney reported that some Sun version of install allows that
619      and that sendmail's installation process relies on the behavior.
620      However, since !x->recursive, the call to "copy" will fail if FROM
621      is a directory.  */
622
623   return copy (from, to, false, x, &copy_into_self, NULL);
624 }
625
626 /* Set the attributes of file or directory NAME.
627    Return true if successful.  */
628
629 static bool
630 change_attributes (char const *name)
631 {
632   bool ok = false;
633   /* chown must precede chmod because on some systems,
634      chown clears the set[ug]id bits for non-superusers,
635      resulting in incorrect permissions.
636      On System V, users can give away files with chown and then not
637      be able to chmod them.  So don't give files away.
638
639      We don't normally ignore errors from chown because the idea of
640      the install command is that the file is supposed to end up with
641      precisely the attributes that the user specified (or defaulted).
642      If the file doesn't end up with the group they asked for, they'll
643      want to know.  */
644
645   if (! (owner_id == (uid_t) -1 && group_id == (gid_t) -1)
646       && lchown (name, owner_id, group_id) != 0)
647     error (0, errno, _("cannot change ownership of %s"), quote (name));
648   else if (chmod (name, mode) != 0)
649     error (0, errno, _("cannot change permissions of %s"), quote (name));
650   else
651     ok = true;
652
653   if (use_default_selinux_context)
654     setdefaultfilecon (name);
655
656   return ok;
657 }
658
659 /* Set the timestamps of file TO to match those of file FROM.
660    Return true if successful.  */
661
662 static bool
663 change_timestamps (struct stat const *from_sb, char const *to)
664 {
665   struct timespec timespec[2];
666   timespec[0] = get_stat_atime (from_sb);
667   timespec[1] = get_stat_mtime (from_sb);
668
669   if (utimens (to, timespec))
670     {
671       error (0, errno, _("cannot set time stamps for %s"), quote (to));
672       return false;
673     }
674   return true;
675 }
676
677 /* Strip the symbol table from the file NAME.
678    We could dig the magic number out of the file first to
679    determine whether to strip it, but the header files and
680    magic numbers vary so much from system to system that making
681    it portable would be very difficult.  Not worth the effort. */
682
683 static void
684 strip (char const *name)
685 {
686   int status;
687   pid_t pid = fork ();
688
689   switch (pid)
690     {
691     case -1:
692       error (EXIT_FAILURE, errno, _("fork system call failed"));
693       break;
694     case 0:                     /* Child. */
695       execlp ("strip", "strip", name, NULL);
696       error (EXIT_FAILURE, errno, _("cannot run strip"));
697       break;
698     default:                    /* Parent. */
699       if (waitpid (pid, &status, 0) < 0)
700         error (EXIT_FAILURE, errno, _("waiting for strip"));
701       else if (! WIFEXITED (status) || WEXITSTATUS (status))
702         error (EXIT_FAILURE, 0, _("strip process terminated abnormally"));
703       break;
704     }
705 }
706
707 /* Initialize the user and group ownership of the files to install. */
708
709 static void
710 get_ids (void)
711 {
712   struct passwd *pw;
713   struct group *gr;
714
715   if (owner_name)
716     {
717       pw = getpwnam (owner_name);
718       if (pw == NULL)
719         {
720           unsigned long int tmp;
721           if (xstrtoul (owner_name, NULL, 0, &tmp, NULL) != LONGINT_OK
722               || UID_T_MAX < tmp)
723             error (EXIT_FAILURE, 0, _("invalid user %s"), quote (owner_name));
724           owner_id = tmp;
725         }
726       else
727         owner_id = pw->pw_uid;
728       endpwent ();
729     }
730   else
731     owner_id = (uid_t) -1;
732
733   if (group_name)
734     {
735       gr = getgrnam (group_name);
736       if (gr == NULL)
737         {
738           unsigned long int tmp;
739           if (xstrtoul (group_name, NULL, 0, &tmp, NULL) != LONGINT_OK
740               || GID_T_MAX < tmp)
741             error (EXIT_FAILURE, 0, _("invalid group %s"), quote (group_name));
742           group_id = tmp;
743         }
744       else
745         group_id = gr->gr_gid;
746       endgrent ();
747     }
748   else
749     group_id = (gid_t) -1;
750 }
751
752 /* Report that directory DIR was made, if OPTIONS requests this.  */
753 static void
754 announce_mkdir (char const *dir, void *options)
755 {
756   struct cp_options const *x = options;
757   if (x->verbose)
758     error (0, 0, _("creating directory %s"), quote (dir));
759 }
760
761 /* Make ancestor directory DIR, whose last file name component is
762    COMPONENT, with options OPTIONS.  Assume the working directory is
763    COMPONENT's parent.  */
764 static int
765 make_ancestor (char const *dir, char const *component, void *options)
766 {
767   int r = mkdir (component, DEFAULT_MODE);
768   if (r == 0)
769     announce_mkdir (dir, options);
770   return r;
771 }
772
773 void
774 usage (int status)
775 {
776   if (status != EXIT_SUCCESS)
777     fprintf (stderr, _("Try `%s --help' for more information.\n"),
778              program_name);
779   else
780     {
781       printf (_("\
782 Usage: %s [OPTION]... [-T] SOURCE DEST\n\
783   or:  %s [OPTION]... SOURCE... DIRECTORY\n\
784   or:  %s [OPTION]... -t DIRECTORY SOURCE...\n\
785   or:  %s [OPTION]... -d DIRECTORY...\n\
786 "),
787               program_name, program_name, program_name, program_name);
788       fputs (_("\
789 In the first three forms, copy SOURCE to DEST or multiple SOURCE(s) to\n\
790 the existing DIRECTORY, while setting permission modes and owner/group.\n\
791 In the 4th form, create all components of the given DIRECTORY(ies).\n\
792 \n\
793 "), stdout);
794       fputs (_("\
795 Mandatory arguments to long options are mandatory for short options too.\n\
796 "), stdout);
797       fputs (_("\
798       --backup[=CONTROL]  make a backup of each existing destination file\n\
799   -b                  like --backup but does not accept an argument\n\
800   -c                  (ignored)\n\
801   -d, --directory     treat all arguments as directory names; create all\n\
802                         components of the specified directories\n\
803 "), stdout);
804       fputs (_("\
805   -D                  create all leading components of DEST except the last,\n\
806                         then copy SOURCE to DEST\n\
807   -g, --group=GROUP   set group ownership, instead of process' current group\n\
808   -m, --mode=MODE     set permission mode (as in chmod), instead of rwxr-xr-x\n\
809   -o, --owner=OWNER   set ownership (super-user only)\n\
810 "), stdout);
811       fputs (_("\
812   -p, --preserve-timestamps   apply access/modification times of SOURCE files\n\
813                         to corresponding destination files\n\
814   -s, --strip         strip symbol tables\n\
815   -S, --suffix=SUFFIX  override the usual backup suffix\n\
816   -t, --target-directory=DIRECTORY  copy all SOURCE arguments into DIRECTORY\n\
817   -T, --no-target-directory  treat DEST as a normal file\n\
818   -v, --verbose       print the name of each directory as it is created\n\
819 "), stdout);
820       fputs (_("\
821       --preserve-context  preserve SELinux security context\n\
822   -Z, --context=CONTEXT  set SELinux security context of files and directories\n\
823 "), stdout);
824
825       fputs (HELP_OPTION_DESCRIPTION, stdout);
826       fputs (VERSION_OPTION_DESCRIPTION, stdout);
827       fputs (_("\
828 \n\
829 The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
830 The version control method may be selected via the --backup option or through\n\
831 the VERSION_CONTROL environment variable.  Here are the values:\n\
832 \n\
833 "), stdout);
834       fputs (_("\
835   none, off       never make backups (even if --backup is given)\n\
836   numbered, t     make numbered backups\n\
837   existing, nil   numbered if numbered backups exist, simple otherwise\n\
838   simple, never   always make simple backups\n\
839 "), stdout);
840       emit_bug_reporting_address ();
841     }
842   exit (status);
843 }