blob: fcc34f1addf81b8cf7311dea6c65b01a6a0132f5 [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 */
Manuel Novoa III cad53642003-03-19 09:13:01 +00009/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
10 *
11 * Fixed broken permission setting when -p was used; especially in
12 * conjunction with -m.
13 */
Denis Vlasenko49622d72007-03-10 16:58:49 +000014/* Nov 28, 2006 Yoshinori Sato <ysato@users.sourceforge.jp>: Add SELinux Support.
15 */
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010016//config:config MKDIR
17//config: bool "mkdir"
18//config: default y
19//config: help
20//config: mkdir is used to create directories with the specified names.
21//config:
22//config:config FEATURE_MKDIR_LONG_OPTIONS
23//config: bool "Enable long options"
24//config: default y
25//config: depends on MKDIR && LONG_OPTS
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010026
27//applet:IF_MKDIR(APPLET_NOFORK(mkdir, mkdir, BB_DIR_BIN, BB_SUID_DROP, mkdir))
28
29//kbuild:lib-$(CONFIG_MKDIR) += mkdir.o
30
31/* BB_AUDIT SUSv3 compliant */
32/* http://www.opengroup.org/onlinepubs/007904975/utilities/mkdir.html */
Denis Vlasenko49622d72007-03-10 16:58:49 +000033
Pere Orga34425382011-03-31 14:43:25 +020034//usage:#define mkdir_trivial_usage
35//usage: "[OPTIONS] DIRECTORY..."
36//usage:#define mkdir_full_usage "\n\n"
37//usage: "Create DIRECTORY\n"
Pere Orga34425382011-03-31 14:43:25 +020038//usage: "\n -m MODE Mode"
39//usage: "\n -p No error if exists; make parent directories as needed"
40//usage: IF_SELINUX(
41//usage: "\n -Z Set security context"
42//usage: )
43//usage:
44//usage:#define mkdir_example_usage
45//usage: "$ mkdir /tmp/foo\n"
46//usage: "$ mkdir /tmp/foo\n"
47//usage: "/tmp/foo: File exists\n"
48//usage: "$ mkdir /tmp/foo/bar/baz\n"
49//usage: "/tmp/foo/bar/baz: No such file or directory\n"
50//usage: "$ mkdir -p /tmp/foo/bar/baz\n"
51
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000052#include "libbb.h"
Erik Andersenfac10d72000-02-07 05:29:42 +000053
Denis Vlasenko99912ca2007-04-10 15:43:37 +000054/* This is a NOFORK applet. Be very careful! */
55
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000056#if ENABLE_FEATURE_MKDIR_LONG_OPTIONS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000057static const char mkdir_longopts[] ALIGN1 =
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +000058 "mode\0" Required_argument "m"
59 "parents\0" No_argument "p"
Denis Vlasenko49622d72007-03-10 16:58:49 +000060#if ENABLE_SELINUX
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +000061 "context\0" Required_argument "Z"
Denis Vlasenko49622d72007-03-10 16:58:49 +000062#endif
Denys Vlasenko17f84182014-05-19 16:23:50 +020063#if ENABLE_FEATURE_VERBOSE
Simon B3698ed12012-05-06 15:03:32 +020064 "verbose\0" No_argument "v"
Denys Vlasenko17f84182014-05-19 16:23:50 +020065#endif
Denis Vlasenko990d0f62007-07-24 15:54:42 +000066 ;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000067#endif
Eric Andersen8876fb22003-06-20 09:01:58 +000068
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000069int mkdir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +010070int mkdir_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000071{
Timo Terasaf36ba22012-02-10 09:55:24 +020072 long mode = -1;
Manuel Novoa III cad53642003-03-19 09:13:01 +000073 int status = EXIT_SUCCESS;
Matt Kraaiceeff732001-06-21 19:41:37 +000074 int flags = 0;
Denis Vlasenko67b23e62006-10-03 21:00:06 +000075 unsigned opt;
Eric Andersen8876fb22003-06-20 09:01:58 +000076 char *smode;
Denis Vlasenko49622d72007-03-10 16:58:49 +000077#if ENABLE_SELINUX
78 security_context_t scontext;
79#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000080
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000081#if ENABLE_FEATURE_MKDIR_LONG_OPTIONS
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +000082 applet_long_options = mkdir_longopts;
Bernhard Reutner-Fischer01d23ad2006-05-26 20:19:22 +000083#endif
Denys Vlasenko17f84182014-05-19 16:23:50 +020084 opt = getopt32(argv, "m:pv" IF_SELINUX("Z:"), &smode IF_SELINUX(,&scontext));
Denis Vlasenko22dcd042006-11-19 13:53:50 +000085 if (opt & 1) {
Denys Vlasenko5711a2a2015-10-07 17:55:33 +020086 mode_t mmode = bb_parse_mode(smode, 0777);
87 if (mmode == (mode_t)-1) {
Denis Vlasenko22dcd042006-11-19 13:53:50 +000088 bb_error_msg_and_die("invalid mode '%s'", smode);
Eric Andersenfa0540f1999-10-22 18:18:31 +000089 }
Timo Terasaf36ba22012-02-10 09:55:24 +020090 mode = mmode;
Eric Andersenfa0540f1999-10-22 18:18:31 +000091 }
Denis Vlasenko22dcd042006-11-19 13:53:50 +000092 if (opt & 2)
Eric Andersen8876fb22003-06-20 09:01:58 +000093 flags |= FILEUTILS_RECUR;
Denys Vlasenko17f84182014-05-19 16:23:50 +020094 if ((opt & 4) && FILEUTILS_VERBOSE)
95 flags |= FILEUTILS_VERBOSE;
Denis Vlasenko49622d72007-03-10 16:58:49 +000096#if ENABLE_SELINUX
Denys Vlasenko17f84182014-05-19 16:23:50 +020097 if (opt & 8) {
Denis Vlasenko49622d72007-03-10 16:58:49 +000098 selinux_or_die();
Denis Vlasenko39c651e2007-03-12 18:22:55 +000099 setfscreatecon_or_die(scontext);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000100 }
101#endif
Eric Andersenf6be9441999-10-13 21:12:06 +0000102
Manuel Novoa III cad53642003-03-19 09:13:01 +0000103 argv += optind;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100104 if (!argv[0])
105 bb_show_usage();
Manuel Novoa III cad53642003-03-19 09:13:01 +0000106
107 do {
108 if (bb_make_directory(*argv, mode, flags)) {
109 status = EXIT_FAILURE;
110 }
111 } while (*++argv);
112
113 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +0000114}