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 | */ |
| 9 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 10 | /* BB_AUDIT SUSv3 compliant */ |
| 11 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/mkdir.html */ |
Matt Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame] | 12 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 13 | /* 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 Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 19 | /* Nov 28, 2006 Yoshinori Sato <ysato@users.sourceforge.jp>: Add SELinux Support. |
| 20 | */ |
| 21 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 22 | //usage:#define mkdir_trivial_usage |
| 23 | //usage: "[OPTIONS] DIRECTORY..." |
| 24 | //usage:#define mkdir_full_usage "\n\n" |
| 25 | //usage: "Create DIRECTORY\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 26 | //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 Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 40 | #include "libbb.h" |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 41 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 42 | /* This is a NOFORK applet. Be very careful! */ |
| 43 | |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 44 | #if ENABLE_FEATURE_MKDIR_LONG_OPTIONS |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 45 | static const char mkdir_longopts[] ALIGN1 = |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 46 | "mode\0" Required_argument "m" |
| 47 | "parents\0" No_argument "p" |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 48 | #if ENABLE_SELINUX |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 49 | "context\0" Required_argument "Z" |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 50 | #endif |
Simon B | 3698ed1 | 2012-05-06 15:03:32 +0200 | [diff] [blame] | 51 | "verbose\0" No_argument "v" |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 52 | ; |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 53 | #endif |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 54 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 55 | int mkdir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 56 | int mkdir_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 57 | { |
Timo Teras | af36ba2 | 2012-02-10 09:55:24 +0200 | [diff] [blame] | 58 | long mode = -1; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 59 | int status = EXIT_SUCCESS; |
Matt Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame] | 60 | int flags = 0; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 61 | unsigned opt; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 62 | char *smode; |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 63 | #if ENABLE_SELINUX |
| 64 | security_context_t scontext; |
| 65 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 66 | |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 67 | #if ENABLE_FEATURE_MKDIR_LONG_OPTIONS |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 68 | applet_long_options = mkdir_longopts; |
Bernhard Reutner-Fischer | 01d23ad | 2006-05-26 20:19:22 +0000 | [diff] [blame] | 69 | #endif |
Simon B | 3698ed1 | 2012-05-06 15:03:32 +0200 | [diff] [blame] | 70 | opt = getopt32(argv, "m:p" IF_SELINUX("Z:") "v", &smode IF_SELINUX(,&scontext)); |
Denis Vlasenko | 22dcd04 | 2006-11-19 13:53:50 +0000 | [diff] [blame] | 71 | if (opt & 1) { |
Timo Teras | af36ba2 | 2012-02-10 09:55:24 +0200 | [diff] [blame] | 72 | mode_t mmode = 0777; |
| 73 | if (!bb_parse_mode(smode, &mmode)) { |
Denis Vlasenko | 22dcd04 | 2006-11-19 13:53:50 +0000 | [diff] [blame] | 74 | bb_error_msg_and_die("invalid mode '%s'", smode); |
Eric Andersen | fa0540f | 1999-10-22 18:18:31 +0000 | [diff] [blame] | 75 | } |
Timo Teras | af36ba2 | 2012-02-10 09:55:24 +0200 | [diff] [blame] | 76 | mode = mmode; |
Eric Andersen | fa0540f | 1999-10-22 18:18:31 +0000 | [diff] [blame] | 77 | } |
Denis Vlasenko | 22dcd04 | 2006-11-19 13:53:50 +0000 | [diff] [blame] | 78 | if (opt & 2) |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 79 | flags |= FILEUTILS_RECUR; |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 80 | #if ENABLE_SELINUX |
| 81 | if (opt & 4) { |
| 82 | selinux_or_die(); |
Denis Vlasenko | 39c651e | 2007-03-12 18:22:55 +0000 | [diff] [blame] | 83 | setfscreatecon_or_die(scontext); |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 84 | } |
| 85 | #endif |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 86 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 87 | argv += optind; |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 88 | if (!argv[0]) |
| 89 | bb_show_usage(); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 90 | |
| 91 | do { |
| 92 | if (bb_make_directory(*argv, mode, flags)) { |
| 93 | status = EXIT_FAILURE; |
| 94 | } |
| 95 | } while (*++argv); |
| 96 | |
| 97 | return status; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 98 | } |