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