blob: 4a8e43e4346fbc83f54473058fec81147e68edb6 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenf6be9441999-10-13 21:12:06 +00002/*
3 * Mini mkdir implementation for busybox
4 *
Matt Kraaiceeff732001-06-21 19:41:37 +00005 * Copyright (C) 2001 Matt Kraai <kraai@alumni.carnegiemellon.edu>
Eric Andersenf6be9441999-10-13 21:12:06 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersenf6be9441999-10-13 21:12:06 +00008 */
9
Manuel Novoa III cad53642003-03-19 09:13:01 +000010/* BB_AUDIT SUSv3 compliant */
11/* http://www.opengroup.org/onlinepubs/007904975/utilities/mkdir.html */
Matt Kraaiceeff732001-06-21 19:41:37 +000012
Manuel Novoa III cad53642003-03-19 09:13:01 +000013/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
14 *
15 * Fixed broken permission setting when -p was used; especially in
16 * conjunction with -m.
17 */
18
Denis Vlasenko49622d72007-03-10 16:58:49 +000019/* Nov 28, 2006 Yoshinori Sato <ysato@users.sourceforge.jp>: Add SELinux Support.
20 */
21
Pere Orga34425382011-03-31 14:43:25 +020022//usage:#define mkdir_trivial_usage
23//usage: "[OPTIONS] DIRECTORY..."
24//usage:#define mkdir_full_usage "\n\n"
25//usage: "Create DIRECTORY\n"
Pere Orga34425382011-03-31 14:43:25 +020026//usage: "\n -m MODE Mode"
27//usage: "\n -p No error if exists; make parent directories as needed"
28//usage: IF_SELINUX(
29//usage: "\n -Z Set security context"
30//usage: )
31//usage:
32//usage:#define mkdir_example_usage
33//usage: "$ mkdir /tmp/foo\n"
34//usage: "$ mkdir /tmp/foo\n"
35//usage: "/tmp/foo: File exists\n"
36//usage: "$ mkdir /tmp/foo/bar/baz\n"
37//usage: "/tmp/foo/bar/baz: No such file or directory\n"
38//usage: "$ mkdir -p /tmp/foo/bar/baz\n"
39
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000040#include "libbb.h"
Erik Andersenfac10d72000-02-07 05:29:42 +000041
Denis Vlasenko99912ca2007-04-10 15:43:37 +000042/* This is a NOFORK applet. Be very careful! */
43
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000044#if ENABLE_FEATURE_MKDIR_LONG_OPTIONS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000045static const char mkdir_longopts[] ALIGN1 =
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +000046 "mode\0" Required_argument "m"
47 "parents\0" No_argument "p"
Denis Vlasenko49622d72007-03-10 16:58:49 +000048#if ENABLE_SELINUX
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +000049 "context\0" Required_argument "Z"
Denis Vlasenko49622d72007-03-10 16:58:49 +000050#endif
Simon B3698ed12012-05-06 15:03:32 +020051 "verbose\0" No_argument "v"
Denis Vlasenko990d0f62007-07-24 15:54:42 +000052 ;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000053#endif
Eric Andersen8876fb22003-06-20 09:01:58 +000054
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000055int mkdir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +010056int mkdir_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000057{
Timo Terasaf36ba22012-02-10 09:55:24 +020058 long mode = -1;
Manuel Novoa III cad53642003-03-19 09:13:01 +000059 int status = EXIT_SUCCESS;
Matt Kraaiceeff732001-06-21 19:41:37 +000060 int flags = 0;
Denis Vlasenko67b23e62006-10-03 21:00:06 +000061 unsigned opt;
Eric Andersen8876fb22003-06-20 09:01:58 +000062 char *smode;
Denis Vlasenko49622d72007-03-10 16:58:49 +000063#if ENABLE_SELINUX
64 security_context_t scontext;
65#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000066
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000067#if ENABLE_FEATURE_MKDIR_LONG_OPTIONS
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +000068 applet_long_options = mkdir_longopts;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000069#endif
Simon B3698ed12012-05-06 15:03:32 +020070 opt = getopt32(argv, "m:p" IF_SELINUX("Z:") "v", &smode IF_SELINUX(,&scontext));
Denis Vlasenko22dcd042006-11-19 13:53:50 +000071 if (opt & 1) {
Timo Terasaf36ba22012-02-10 09:55:24 +020072 mode_t mmode = 0777;
73 if (!bb_parse_mode(smode, &mmode)) {
Denis Vlasenko22dcd042006-11-19 13:53:50 +000074 bb_error_msg_and_die("invalid mode '%s'", smode);
Eric Andersenfa0540f1999-10-22 18:18:31 +000075 }
Timo Terasaf36ba22012-02-10 09:55:24 +020076 mode = mmode;
Eric Andersenfa0540f1999-10-22 18:18:31 +000077 }
Denis Vlasenko22dcd042006-11-19 13:53:50 +000078 if (opt & 2)
Eric Andersen8876fb22003-06-20 09:01:58 +000079 flags |= FILEUTILS_RECUR;
Denis Vlasenko49622d72007-03-10 16:58:49 +000080#if ENABLE_SELINUX
81 if (opt & 4) {
82 selinux_or_die();
Denis Vlasenko39c651e2007-03-12 18:22:55 +000083 setfscreatecon_or_die(scontext);
Denis Vlasenko49622d72007-03-10 16:58:49 +000084 }
85#endif
Eric Andersenf6be9441999-10-13 21:12:06 +000086
Manuel Novoa III cad53642003-03-19 09:13:01 +000087 argv += optind;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +010088 if (!argv[0])
89 bb_show_usage();
Manuel Novoa III cad53642003-03-19 09:13:01 +000090
91 do {
92 if (bb_make_directory(*argv, mode, flags)) {
93 status = EXIT_FAILURE;
94 }
95 } while (*++argv);
96
97 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +000098}