mkdir.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini mkdir implementation for busybox
  4. *
  5. * Copyright (C) 2001 Matt Kraai <kraai@alumni.carnegiemellon.edu>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  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. */
  14. /* Nov 28, 2006 Yoshinori Sato <ysato@users.sourceforge.jp>: Add SELinux Support.
  15. */
  16. //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
  26. //applet:IF_MKDIR(APPLET_NOFORK(mkdir, mkdir, BB_DIR_BIN, BB_SUID_DROP, mkdir))
  27. //kbuild:lib-$(CONFIG_MKDIR) += mkdir.o
  28. /* BB_AUDIT SUSv3 compliant */
  29. /* http://www.opengroup.org/onlinepubs/007904975/utilities/mkdir.html */
  30. //usage:#define mkdir_trivial_usage
  31. //usage: "[OPTIONS] DIRECTORY..."
  32. //usage:#define mkdir_full_usage "\n\n"
  33. //usage: "Create DIRECTORY\n"
  34. //usage: "\n -m MODE Mode"
  35. //usage: "\n -p No error if exists; make parent directories as needed"
  36. //usage: IF_SELINUX(
  37. //usage: "\n -Z Set security context"
  38. //usage: )
  39. //usage:
  40. //usage:#define mkdir_example_usage
  41. //usage: "$ mkdir /tmp/foo\n"
  42. //usage: "$ mkdir /tmp/foo\n"
  43. //usage: "/tmp/foo: File exists\n"
  44. //usage: "$ mkdir /tmp/foo/bar/baz\n"
  45. //usage: "/tmp/foo/bar/baz: No such file or directory\n"
  46. //usage: "$ mkdir -p /tmp/foo/bar/baz\n"
  47. #include "libbb.h"
  48. /* This is a NOFORK applet. Be very careful! */
  49. #if ENABLE_FEATURE_MKDIR_LONG_OPTIONS
  50. static const char mkdir_longopts[] ALIGN1 =
  51. "mode\0" Required_argument "m"
  52. "parents\0" No_argument "p"
  53. #if ENABLE_SELINUX
  54. "context\0" Required_argument "Z"
  55. #endif
  56. #if ENABLE_FEATURE_VERBOSE
  57. "verbose\0" No_argument "v"
  58. #endif
  59. ;
  60. #endif
  61. int mkdir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  62. int mkdir_main(int argc UNUSED_PARAM, char **argv)
  63. {
  64. long mode = -1;
  65. int status = EXIT_SUCCESS;
  66. int flags = 0;
  67. unsigned opt;
  68. char *smode;
  69. #if ENABLE_SELINUX
  70. security_context_t scontext;
  71. #endif
  72. #if ENABLE_FEATURE_MKDIR_LONG_OPTIONS
  73. applet_long_options = mkdir_longopts;
  74. #endif
  75. opt = getopt32(argv, "m:pv" IF_SELINUX("Z:"), &smode IF_SELINUX(,&scontext));
  76. if (opt & 1) {
  77. mode_t mmode = bb_parse_mode(smode, 0777);
  78. if (mmode == (mode_t)-1) {
  79. bb_error_msg_and_die("invalid mode '%s'", smode);
  80. }
  81. mode = mmode;
  82. }
  83. if (opt & 2)
  84. flags |= FILEUTILS_RECUR;
  85. if ((opt & 4) && FILEUTILS_VERBOSE)
  86. flags |= FILEUTILS_VERBOSE;
  87. #if ENABLE_SELINUX
  88. if (opt & 8) {
  89. selinux_or_die();
  90. setfscreatecon_or_die(scontext);
  91. }
  92. #endif
  93. argv += optind;
  94. if (!argv[0])
  95. bb_show_usage();
  96. do {
  97. if (bb_make_directory(*argv, mode, flags)) {
  98. status = EXIT_FAILURE;
  99. }
  100. } while (*++argv);
  101. return status;
  102. }