Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini mkdir implementation for busybox |
| 4 | * |
Matt Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame] | 5 | * Copyright (C) 2001 Matt Kraai <kraai@alumni.carnegiemellon.edu> |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 8 | */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 9 | /* 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 Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 14 | /* Nov 28, 2006 Yoshinori Sato <ysato@users.sourceforge.jp>: Add SELinux Support. |
| 15 | */ |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 16 | //config:config MKDIR |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 17 | //config: bool "mkdir (4.5 kb)" |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 18 | //config: default y |
| 19 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 20 | //config: mkdir is used to create directories with the specified names. |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 21 | |
| 22 | //applet:IF_MKDIR(APPLET_NOFORK(mkdir, mkdir, BB_DIR_BIN, BB_SUID_DROP, mkdir)) |
| 23 | |
| 24 | //kbuild:lib-$(CONFIG_MKDIR) += mkdir.o |
| 25 | |
| 26 | /* BB_AUDIT SUSv3 compliant */ |
| 27 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/mkdir.html */ |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 28 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 29 | //usage:#define mkdir_trivial_usage |
Denys Vlasenko | 11f1a25 | 2020-12-13 19:04:19 +0100 | [diff] [blame] | 30 | //usage: "[-m MODE] [-p] DIRECTORY..." |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 31 | //usage:#define mkdir_full_usage "\n\n" |
| 32 | //usage: "Create DIRECTORY\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 33 | //usage: "\n -m MODE Mode" |
| 34 | //usage: "\n -p No error if exists; make parent directories as needed" |
| 35 | //usage: IF_SELINUX( |
| 36 | //usage: "\n -Z Set security context" |
| 37 | //usage: ) |
| 38 | //usage: |
| 39 | //usage:#define mkdir_example_usage |
| 40 | //usage: "$ mkdir /tmp/foo\n" |
| 41 | //usage: "$ mkdir /tmp/foo\n" |
| 42 | //usage: "/tmp/foo: File exists\n" |
| 43 | //usage: "$ mkdir /tmp/foo/bar/baz\n" |
| 44 | //usage: "/tmp/foo/bar/baz: No such file or directory\n" |
| 45 | //usage: "$ mkdir -p /tmp/foo/bar/baz\n" |
| 46 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 47 | #include "libbb.h" |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 48 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 49 | /* This is a NOFORK applet. Be very careful! */ |
| 50 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 51 | int mkdir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 52 | int mkdir_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 53 | { |
Timo Teras | af36ba2 | 2012-02-10 09:55:24 +0200 | [diff] [blame] | 54 | long mode = -1; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 55 | int status = EXIT_SUCCESS; |
Matt Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame] | 56 | int flags = 0; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 57 | unsigned opt; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 58 | char *smode; |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 59 | #if ENABLE_SELINUX |
| 60 | security_context_t scontext; |
| 61 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 62 | |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 63 | opt = getopt32long(argv, "m:pv" IF_SELINUX("Z:"), |
| 64 | "mode\0" Required_argument "m" |
| 65 | "parents\0" No_argument "p" |
| 66 | # if ENABLE_SELINUX |
| 67 | "context\0" Required_argument "Z" |
| 68 | # endif |
| 69 | # if ENABLE_FEATURE_VERBOSE |
| 70 | "verbose\0" No_argument "v" |
| 71 | # endif |
| 72 | , &smode IF_SELINUX(,&scontext) |
| 73 | ); |
Denis Vlasenko | 22dcd04 | 2006-11-19 13:53:50 +0000 | [diff] [blame] | 74 | if (opt & 1) { |
Denys Vlasenko | 5711a2a | 2015-10-07 17:55:33 +0200 | [diff] [blame] | 75 | mode_t mmode = bb_parse_mode(smode, 0777); |
| 76 | if (mmode == (mode_t)-1) { |
Denis Vlasenko | 22dcd04 | 2006-11-19 13:53:50 +0000 | [diff] [blame] | 77 | bb_error_msg_and_die("invalid mode '%s'", smode); |
Eric Andersen | fa0540f | 1999-10-22 18:18:31 +0000 | [diff] [blame] | 78 | } |
Timo Teras | af36ba2 | 2012-02-10 09:55:24 +0200 | [diff] [blame] | 79 | mode = mmode; |
Eric Andersen | fa0540f | 1999-10-22 18:18:31 +0000 | [diff] [blame] | 80 | } |
Denis Vlasenko | 22dcd04 | 2006-11-19 13:53:50 +0000 | [diff] [blame] | 81 | if (opt & 2) |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 82 | flags |= FILEUTILS_RECUR; |
Denys Vlasenko | 17f8418 | 2014-05-19 16:23:50 +0200 | [diff] [blame] | 83 | if ((opt & 4) && FILEUTILS_VERBOSE) |
| 84 | flags |= FILEUTILS_VERBOSE; |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 85 | #if ENABLE_SELINUX |
Denys Vlasenko | 17f8418 | 2014-05-19 16:23:50 +0200 | [diff] [blame] | 86 | if (opt & 8) { |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 87 | selinux_or_die(); |
Denis Vlasenko | 39c651e | 2007-03-12 18:22:55 +0000 | [diff] [blame] | 88 | setfscreatecon_or_die(scontext); |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 89 | } |
| 90 | #endif |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 91 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 92 | argv += optind; |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 93 | if (!argv[0]) |
| 94 | bb_show_usage(); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 95 | |
| 96 | do { |
| 97 | if (bb_make_directory(*argv, mode, flags)) { |
| 98 | status = EXIT_FAILURE; |
| 99 | } |
| 100 | } while (*++argv); |
| 101 | |
| 102 | return status; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 103 | } |