maint: update all copyright year number ranges
[platform/upstream/coreutils.git] / src / mkdir.c
1 /* mkdir -- make directories
2    Copyright (C) 1990-2013 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 /* David MacKenzie <djm@ai.mit.edu>  */
18
19 #include <config.h>
20 #include <stdio.h>
21 #include <getopt.h>
22 #include <sys/types.h>
23 #include <selinux/selinux.h>
24
25 #include "system.h"
26 #include "error.h"
27 #include "mkdir-p.h"
28 #include "modechange.h"
29 #include "prog-fprintf.h"
30 #include "quote.h"
31 #include "savewd.h"
32
33 /* The official name of this program (e.g., no 'g' prefix).  */
34 #define PROGRAM_NAME "mkdir"
35
36 #define AUTHORS proper_name ("David MacKenzie")
37
38 static struct option const longopts[] =
39 {
40   {GETOPT_SELINUX_CONTEXT_OPTION_DECL},
41   {"mode", required_argument, NULL, 'm'},
42   {"parents", no_argument, NULL, 'p'},
43   {"verbose", no_argument, NULL, 'v'},
44   {GETOPT_HELP_OPTION_DECL},
45   {GETOPT_VERSION_OPTION_DECL},
46   {NULL, 0, NULL, 0}
47 };
48
49 void
50 usage (int status)
51 {
52   if (status != EXIT_SUCCESS)
53     emit_try_help ();
54   else
55     {
56       printf (_("Usage: %s [OPTION]... DIRECTORY...\n"), program_name);
57       fputs (_("\
58 Create the DIRECTORY(ies), if they do not already exist.\n\
59 \n\
60 "), stdout);
61       fputs (_("\
62 Mandatory arguments to long options are mandatory for short options too.\n\
63 "), stdout);
64       fputs (_("\
65   -m, --mode=MODE   set file mode (as in chmod), not a=rwx - umask\n\
66   -p, --parents     no error if existing, make parent directories as needed\n\
67   -v, --verbose     print a message for each created directory\n\
68   -Z, --context=CTX  set the SELinux security context of each created\n\
69                       directory to CTX\n\
70 "), stdout);
71       fputs (HELP_OPTION_DESCRIPTION, stdout);
72       fputs (VERSION_OPTION_DESCRIPTION, stdout);
73       emit_ancillary_info ();
74     }
75   exit (status);
76 }
77
78 /* Options passed to subsidiary functions.  */
79 struct mkdir_options
80 {
81   /* Function to make an ancestor, or NULL if ancestors should not be
82      made.  */
83   int (*make_ancestor_function) (char const *, char const *, void *);
84
85   /* Mode for ancestor directory.  */
86   mode_t ancestor_mode;
87
88   /* Mode for directory itself.  */
89   mode_t mode;
90
91   /* File mode bits affected by MODE.  */
92   mode_t mode_bits;
93
94   /* If not null, format to use when reporting newly made directories.  */
95   char const *created_directory_format;
96 };
97
98 /* Report that directory DIR was made, if OPTIONS requests this.  */
99 static void
100 announce_mkdir (char const *dir, void *options)
101 {
102   struct mkdir_options const *o = options;
103   if (o->created_directory_format)
104     prog_fprintf (stdout, o->created_directory_format, quote (dir));
105 }
106
107 /* Make ancestor directory DIR, whose last component is COMPONENT,
108    with options OPTIONS.  Assume the working directory is COMPONENT's
109    parent.  Return 0 if successful and the resulting directory is
110    readable, 1 if successful but the resulting directory is not
111    readable, -1 (setting errno) otherwise.  */
112 static int
113 make_ancestor (char const *dir, char const *component, void *options)
114 {
115   struct mkdir_options const *o = options;
116   int r = mkdir (component, o->ancestor_mode);
117   if (r == 0)
118     {
119       r = ! (o->ancestor_mode & S_IRUSR);
120       announce_mkdir (dir, options);
121     }
122   return r;
123 }
124
125 /* Process a command-line file name.  */
126 static int
127 process_dir (char *dir, struct savewd *wd, void *options)
128 {
129   struct mkdir_options const *o = options;
130   return (make_dir_parents (dir, wd, o->make_ancestor_function, options,
131                             o->mode, announce_mkdir,
132                             o->mode_bits, (uid_t) -1, (gid_t) -1, true)
133           ? EXIT_SUCCESS
134           : EXIT_FAILURE);
135 }
136
137 int
138 main (int argc, char **argv)
139 {
140   const char *specified_mode = NULL;
141   int optc;
142   security_context_t scontext = NULL;
143   struct mkdir_options options;
144
145   options.make_ancestor_function = NULL;
146   options.mode = S_IRWXUGO;
147   options.mode_bits = 0;
148   options.created_directory_format = NULL;
149
150   initialize_main (&argc, &argv);
151   set_program_name (argv[0]);
152   setlocale (LC_ALL, "");
153   bindtextdomain (PACKAGE, LOCALEDIR);
154   textdomain (PACKAGE);
155
156   atexit (close_stdout);
157
158   while ((optc = getopt_long (argc, argv, "pm:vZ:", longopts, NULL)) != -1)
159     {
160       switch (optc)
161         {
162         case 'p':
163           options.make_ancestor_function = make_ancestor;
164           break;
165         case 'm':
166           specified_mode = optarg;
167           break;
168         case 'v': /* --verbose  */
169           options.created_directory_format = _("created directory %s");
170           break;
171         case 'Z':
172           scontext = optarg;
173           break;
174         case_GETOPT_HELP_CHAR;
175         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
176         default:
177           usage (EXIT_FAILURE);
178         }
179     }
180
181   if (optind == argc)
182     {
183       error (0, 0, _("missing operand"));
184       usage (EXIT_FAILURE);
185     }
186
187   if (scontext && setfscreatecon (scontext) < 0)
188     error (EXIT_FAILURE, errno,
189            _("failed to set default file creation context to %s"),
190            quote (scontext));
191
192   if (options.make_ancestor_function || specified_mode)
193     {
194       mode_t umask_value = umask (0);
195
196       options.ancestor_mode = (S_IRWXUGO & ~umask_value) | (S_IWUSR | S_IXUSR);
197
198       if (specified_mode)
199         {
200           struct mode_change *change = mode_compile (specified_mode);
201           if (!change)
202             error (EXIT_FAILURE, 0, _("invalid mode %s"),
203                    quote (specified_mode));
204           options.mode = mode_adjust (S_IRWXUGO, true, umask_value, change,
205                                       &options.mode_bits);
206           free (change);
207         }
208       else
209         options.mode = S_IRWXUGO & ~umask_value;
210     }
211
212   exit (savewd_process_files (argc - optind, argv + optind,
213                               process_dir, &options));
214 }