Matt Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Mini make_directory implementation for busybox |
| 4 | * |
| 5 | * Copyright (C) 2001 Matt Kraai. |
Glenn L McGrath | fbef225 | 2002-08-23 17:19:26 +0000 | [diff] [blame] | 6 | * |
| 7 | * Rewriten in 2002 |
| 8 | * Copyright (C) 2002 Glenn McGrath |
| 9 | * Copyright (C) 2002 Vladimir N. Oleynik |
Matt Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame] | 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 | * General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 24 | * |
| 25 | */ |
| 26 | |
| 27 | #include <errno.h> |
| 28 | #include <fcntl.h> |
Glenn L McGrath | 210aa14 | 2002-08-24 20:00:52 +0000 | [diff] [blame] | 29 | #include <string.h> |
Matt Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame] | 30 | #include <sys/stat.h> |
| 31 | #include <sys/types.h> |
| 32 | #include <unistd.h> |
Eric Andersen | 764303f | 2001-06-22 03:07:19 +0000 | [diff] [blame] | 33 | #include <stdlib.h> |
Matt Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame] | 34 | |
| 35 | #include "libbb.h" |
| 36 | |
| 37 | /* Create the directory PATH with mode MODE, or the default if MODE is -1. |
| 38 | * Also create parent directories as necessary if flags contains |
| 39 | * FILEUTILS_RECUR. */ |
| 40 | |
Eric Andersen | 879d6c8 | 2001-08-02 09:58:19 +0000 | [diff] [blame] | 41 | int make_directory (char *path, long mode, int flags) |
Matt Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame] | 42 | { |
Glenn L McGrath | fbef225 | 2002-08-23 17:19:26 +0000 | [diff] [blame] | 43 | int ret; |
Glenn L McGrath | 4298e87 | 2002-10-19 23:56:41 +0000 | [diff] [blame] | 44 | |
Glenn L McGrath | fbef225 | 2002-08-23 17:19:26 +0000 | [diff] [blame] | 45 | /* Calling apps probably should use 0777 instead of -1 |
| 46 | * then we dont need this condition |
| 47 | */ |
| 48 | if (mode == -1) { |
| 49 | mode = 0777; |
| 50 | } |
| 51 | if (flags == FILEUTILS_RECUR) { |
| 52 | char *pp = strrchr(path, '/'); |
Glenn L McGrath | 4298e87 | 2002-10-19 23:56:41 +0000 | [diff] [blame] | 53 | if ((pp) && (pp != path)) { |
Glenn L McGrath | fbef225 | 2002-08-23 17:19:26 +0000 | [diff] [blame] | 54 | *pp = '\0'; |
| 55 | make_directory(path, mode, flags); |
| 56 | *pp = '/'; |
Matt Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame] | 57 | } |
| 58 | } |
Glenn L McGrath | fbef225 | 2002-08-23 17:19:26 +0000 | [diff] [blame] | 59 | ret = mkdir(path, mode); |
Glenn L McGrath | 193697d | 2002-08-24 20:11:38 +0000 | [diff] [blame] | 60 | if (ret == -1) { |
Glenn L McGrath | b3f7261 | 2002-09-22 02:12:30 +0000 | [diff] [blame] | 61 | if ((flags == FILEUTILS_RECUR) && (errno == EEXIST)) { |
Glenn L McGrath | 193697d | 2002-08-24 20:11:38 +0000 | [diff] [blame] | 62 | ret = 0; |
| 63 | } else { |
Glenn L McGrath | 891ee64 | 2002-09-28 08:30:47 +0000 | [diff] [blame] | 64 | perror_msg_and_die("Cannot create directory '%s'", path); |
Glenn L McGrath | 193697d | 2002-08-24 20:11:38 +0000 | [diff] [blame] | 65 | } |
Glenn L McGrath | fbef225 | 2002-08-23 17:19:26 +0000 | [diff] [blame] | 66 | } |
Glenn L McGrath | 193697d | 2002-08-24 20:11:38 +0000 | [diff] [blame] | 67 | return(ret); |
Matt Kraai | ceeff73 | 2001-06-21 19:41:37 +0000 | [diff] [blame] | 68 | } |