(main) Avoid a minor race condition when `-m MODE' is specified, by using
[platform/upstream/coreutils.git] / src / mkdir.c
1 /* mkdir -- make directories
2    Copyright (C) 90, 1995-2002, 2004, 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 /* David MacKenzie <djm@ai.mit.edu>  */
19
20 #include <config.h>
21 #include <stdio.h>
22 #include <getopt.h>
23 #include <sys/types.h>
24
25 #include "system.h"
26 #include "chmod-safer.h"
27 #include "dirname.h"
28 #include "error.h"
29 #include "mkdir-p.h"
30 #include "modechange.h"
31 #include "quote.h"
32
33 /* The official name of this program (e.g., no `g' prefix).  */
34 #define PROGRAM_NAME "mkdir"
35
36 #define AUTHORS "David MacKenzie"
37
38 /* The name this program was run with. */
39 char *program_name;
40
41 static struct option const longopts[] =
42 {
43   {"mode", required_argument, NULL, 'm'},
44   {"parents", no_argument, NULL, 'p'},
45   {"verbose", no_argument, NULL, 'v'},
46   {GETOPT_HELP_OPTION_DECL},
47   {GETOPT_VERSION_OPTION_DECL},
48   {NULL, 0, NULL, 0}
49 };
50
51 void
52 usage (int status)
53 {
54   if (status != EXIT_SUCCESS)
55     fprintf (stderr, _("Try `%s --help' for more information.\n"),
56              program_name);
57   else
58     {
59       printf (_("Usage: %s [OPTION] DIRECTORY...\n"), program_name);
60       fputs (_("\
61 Create the DIRECTORY(ies), if they do not already exist.\n\
62 \n\
63 "), stdout);
64       fputs (_("\
65 Mandatory arguments to long options are mandatory for short options too.\n\
66 "), stdout);
67       fputs (_("\
68   -m, --mode=MODE   set permission mode (as in chmod), not rwxrwxrwx - umask\n\
69   -p, --parents     no error if existing, make parent directories as needed\n\
70   -v, --verbose     print a message for each created directory\n\
71 "), stdout);
72       fputs (HELP_OPTION_DESCRIPTION, stdout);
73       fputs (VERSION_OPTION_DESCRIPTION, stdout);
74       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
75     }
76   exit (status);
77 }
78
79 int
80 main (int argc, char **argv)
81 {
82   mode_t newmode;
83   mode_t tmp_mode;
84   mode_t parent_mode IF_LINT (= 0);
85   const char *specified_mode = NULL;
86   const char *verbose_fmt_string = NULL;
87   bool create_parents = false;
88   int exit_status = EXIT_SUCCESS;
89   int optc;
90   int cwd_errno = 0;
91
92   initialize_main (&argc, &argv);
93   program_name = argv[0];
94   setlocale (LC_ALL, "");
95   bindtextdomain (PACKAGE, LOCALEDIR);
96   textdomain (PACKAGE);
97
98   atexit (close_stdout);
99
100   while ((optc = getopt_long (argc, argv, "pm:v", longopts, NULL)) != -1)
101     {
102       switch (optc)
103         {
104         case 'p':
105           create_parents = true;
106           break;
107         case 'm':
108           specified_mode = optarg;
109           break;
110         case 'v': /* --verbose  */
111           verbose_fmt_string = _("created directory %s");
112           break;
113         case_GETOPT_HELP_CHAR;
114         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
115         default:
116           usage (EXIT_FAILURE);
117         }
118     }
119
120   if (optind == argc)
121     {
122       error (0, 0, _("missing operand"));
123       usage (EXIT_FAILURE);
124     }
125
126   newmode = S_IRWXUGO;
127
128   if (specified_mode || create_parents)
129     {
130       mode_t umask_value = umask (0);
131
132       parent_mode = (S_IRWXUGO & ~umask_value) | (S_IWUSR | S_IXUSR);
133
134       if (specified_mode)
135         {
136           struct mode_change *change = mode_compile (specified_mode);
137           if (!change)
138             error (EXIT_FAILURE, 0, _("invalid mode %s"),
139                    quote (specified_mode));
140           newmode = mode_adjust (S_IRWXUGO, change, umask_value);
141           free (change);
142         }
143       else
144         umask (umask_value);
145     }
146
147   /* This is the mode we'll use in the mknod or mkfifo call.
148      If it doesn't include S_IRUSR, use S_IRUSR so the final
149      open-for-fchmod will succeed.  */
150   tmp_mode = (newmode & S_IRUSR) ? newmode : S_IRUSR;
151
152   for (; optind < argc; ++optind)
153     {
154       char *dir = argv[optind];
155       bool ok;
156
157       if (create_parents)
158         {
159           if (cwd_errno != 0 && IS_RELATIVE_FILE_NAME (dir))
160             {
161               error (0, cwd_errno, _("cannot return to working directory"));
162               ok = false;
163             }
164           else
165             ok = make_dir_parents (dir, newmode, parent_mode,
166                                    -1, -1, true, verbose_fmt_string,
167                                    &cwd_errno);
168         }
169       else
170         {
171           ok = (mkdir (dir, tmp_mode) == 0);
172
173           if (! ok)
174             error (0, errno, _("cannot create directory %s"), quote (dir));
175           else if (verbose_fmt_string)
176             error (0, 0, verbose_fmt_string, quote (dir));
177
178           /* mkdir(2) is required to honor only the file permission bits.
179              In particular, it needn't do anything about `special' bits,
180              so if any were set in newmode, apply them with chmod.
181              This extra step is necessary in some cases when the containing
182              directory has a default ACL.  */
183
184           /* Set the permissions only if this directory has just
185              been created.  */
186
187           if (ok && specified_mode
188               && chmod_safer (dir, newmode, 0, S_IFDIR) != 0)
189             {
190               error (0, errno, _("cannot set permissions of directory %s"),
191                      quote (dir));
192               ok = false;
193             }
194         }
195
196       if (! ok)
197         exit_status = EXIT_FAILURE;
198     }
199
200   exit (exit_status);
201 }