diff options
author | Matt Kraai | 2001-06-21 19:41:37 +0000 |
---|---|---|
committer | Matt Kraai | 2001-06-21 19:41:37 +0000 |
commit | ceeff7381929930fe8d7e33543e285d5fdcf1c68 (patch) | |
tree | 3cdbaddffecc92649215fdc71a43b4e8e86b7ea3 | |
parent | 091781e20eb055ac286b5a617d53a50c7d6c451e (diff) | |
download | busybox-ceeff7381929930fe8d7e33543e285d5fdcf1c68.zip busybox-ceeff7381929930fe8d7e33543e285d5fdcf1c68.tar.gz |
Rewrote mkdir (and touched lots of things in the process).
-rw-r--r-- | Makefile | 5 | ||||
-rw-r--r-- | archival/dpkg.c | 2 | ||||
-rw-r--r-- | archival/tar.c | 7 | ||||
-rw-r--r-- | coreutils/dirname.c | 14 | ||||
-rw-r--r-- | coreutils/mkdir.c | 97 | ||||
-rw-r--r-- | dirname.c | 14 | ||||
-rw-r--r-- | dpkg.c | 2 | ||||
-rw-r--r-- | editors/sed.c | 9 | ||||
-rw-r--r-- | include/libbb.h | 4 | ||||
-rw-r--r-- | libbb/create_path.c | 71 | ||||
-rw-r--r-- | libbb/dirname.c | 48 | ||||
-rw-r--r-- | libbb/libbb.h | 4 | ||||
-rw-r--r-- | libbb/make_directory.c | 66 | ||||
-rw-r--r-- | libbb/strdup_substr.c | 32 | ||||
-rw-r--r-- | mkdir.c | 97 | ||||
-rw-r--r-- | sed.c | 9 | ||||
-rw-r--r-- | tar.c | 7 |
17 files changed, 233 insertions, 255 deletions
@@ -237,7 +237,7 @@ endif LIBBB = libbb LIBBB_LIB = libbb.a LIBBB_CSRC= ask_confirmation.c chomp.c concat_path_file.c copy_file.c \ -copy_file_chunk.c create_path.c daemon.c device_open.c error_msg.c \ +copy_file_chunk.c daemon.c device_open.c error_msg.c \ error_msg_and_die.c fgets_str.c find_mount_point.c find_pid_by_name.c \ find_root_device.c full_read.c full_write.c get_console.c \ get_last_path_component.c get_line_from_file.c gz_open.c human_readable.c \ @@ -248,7 +248,8 @@ print_file.c process_escape_sequence.c read_package_field.c recursive_action.c \ safe_read.c safe_strncpy.c syscalls.c syslog_msg_with_name.c time_string.c \ trim.c unarchive.c unzip.c vdprintf.c verror_msg.c vperror_msg.c wfopen.c xfuncs.c \ xgetcwd.c xreadlink.c xregcomp.c interface.c remove_file.c last_char_is.c \ -copyfd.c vherror_msg.c herror_msg.c herror_msg_and_die.c xgethostbyname.c +copyfd.c vherror_msg.c herror_msg.c herror_msg_and_die.c xgethostbyname.c \ +dirname.c make_directory.c strdup_substr.c LIBBB_OBJS=$(patsubst %.c,$(LIBBB)/%.o, $(LIBBB_CSRC)) LIBBB_CFLAGS = -I$(LIBBB) ifneq ($(strip $(BB_SRC_DIR)),) diff --git a/archival/dpkg.c b/archival/dpkg.c index 4224672..7dd46be 100644 --- a/archival/dpkg.c +++ b/archival/dpkg.c @@ -837,7 +837,7 @@ extern int dpkg_main(int argc, char **argv) optind++; } - create_path(infodir, S_IRWXU); + make_directory(infodir, S_IRWXU, FILEUTILS_RECUR); status = status_read(); diff --git a/archival/tar.c b/archival/tar.c index 55fb12c..e68194f 100644 --- a/archival/tar.c +++ b/archival/tar.c @@ -338,7 +338,9 @@ tarExtractRegularFile(TarInfo *header, int extractFlag, int tostdoutFlag) if (extractFlag==TRUE && tostdoutFlag==FALSE) { /* Create the path to the file, just in case it isn't there... * This should not screw up path permissions or anything. */ - create_path(header->name, 0777); + char *dir = dirname (header->name); + make_directory (dir, -1, FILEUTILS_RECUR); + free (dir); if ((outFd=open(header->name, O_CREAT|O_TRUNC|O_WRONLY, header->mode & ~S_IFMT)) < 0) { error_msg(io_error, header->name, strerror(errno)); @@ -397,8 +399,7 @@ tarExtractDirectory(TarInfo *header, int extractFlag, int tostdoutFlag) if (extractFlag==FALSE || tostdoutFlag==TRUE) return( TRUE); - if (create_path(header->name, header->mode) != TRUE) { - perror_msg("%s: Cannot mkdir", header->name); + if (make_directory(header->name, header->mode, FILEUTILS_RECUR) < 0) { return( FALSE); } /* make the final component, just in case it was diff --git a/coreutils/dirname.c b/coreutils/dirname.c index 935a831..b534e69 100644 --- a/coreutils/dirname.c +++ b/coreutils/dirname.c @@ -30,21 +30,11 @@ extern int dirname_main(int argc, char **argv) { - char* s; - if ((argc < 2) || (**(argv + 1) == '-')) show_usage(); argv++; - s=*argv+strlen(*argv)-1; - while (s > *argv && *s == '/') { - *s-- = '\0'; - } - s = strrchr(*argv, '/'); - if (s != NULL && s == *argv) - s[1] = '\0'; - else if (s != NULL) - *s = '\0'; - puts(s ? *argv : "."); + puts (dirname (argv[0])); + return EXIT_SUCCESS; } diff --git a/coreutils/mkdir.c b/coreutils/mkdir.c index d78f57e..03c49f0 100644 --- a/coreutils/mkdir.c +++ b/coreutils/mkdir.c @@ -2,8 +2,7 @@ /* * Mini mkdir implementation for busybox * - * Copyright (C) 1999,2000,2001 by Lineo, inc. - * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org> + * Copyright (C) 2001 Matt Kraai <kraai@alumni.carnegiemellon.edu> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -21,79 +20,45 @@ * */ -#include <stdio.h> #include <errno.h> -#include <string.h> +#include <getopt.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <fcntl.h> +#include <unistd.h> #include <stdlib.h> -#include "busybox.h" - - -static int parentFlag = FALSE; -static mode_t mode = 0777; +#include <string.h> +#include "busybox.h" -extern int mkdir_main(int argc, char **argv) +extern int mkdir_main (int argc, char **argv) { - int i = FALSE; + mode_t mode = -1; + int flags = 0; + int status = 0; + int i, opt; - argc--; - argv++; - - /* Parse any options */ - while (argc > 0 && **argv == '-') { - while (i == FALSE && *++(*argv)) { - switch (**argv) { - case 'm': - if (--argc == 0) - show_usage(); - /* Find the specified modes */ - mode = 0; - if (parse_mode(*(++argv), &mode) == FALSE) { - error_msg_and_die("Unknown mode: %s", *argv); - } - /* Set the umask for this process so it doesn't - * screw up whatever the user just entered. */ - umask(0); - i = TRUE; - break; - case 'p': - parentFlag = TRUE; - break; - default: - show_usage(); - } + while ((opt = getopt (argc, argv, "m:p")) != -1) { + switch (opt) { + case 'm': + mode = 0777; + if (!parse_mode (optarg, &mode)) + error_msg_and_die ("invalid mode `%s'", optarg); + break; + case 'p': + flags |= FILEUTILS_RECUR; + break; + default: + show_usage (); } - argc--; - argv++; } - if (argc < 1) { - show_usage(); - } + if (optind == argc) + show_usage (); - while (argc > 0) { - int status; - struct stat statBuf; - char buf[BUFSIZ + 1]; + for (i = optind; i < argc; i++) + if (make_directory (argv[i], mode, flags) < 0) + status = 1; - if (strlen(*argv) > BUFSIZ - 1) { - error_msg_and_die(name_too_long); - } - strcpy(buf, *argv); - status = stat(buf, &statBuf); - if (parentFlag == FALSE && status != -1 && errno != ENOENT) { - error_msg_and_die("%s: File exists", buf); - } - if (parentFlag == TRUE) { - strcat(buf, "/"); - create_path(buf, mode); - } else { - if (mkdir(buf, mode) != 0 && parentFlag == FALSE) { - perror_msg_and_die(buf); - } - } - argc--; - argv++; - } - return EXIT_SUCCESS; + return status; } @@ -30,21 +30,11 @@ extern int dirname_main(int argc, char **argv) { - char* s; - if ((argc < 2) || (**(argv + 1) == '-')) show_usage(); argv++; - s=*argv+strlen(*argv)-1; - while (s > *argv && *s == '/') { - *s-- = '\0'; - } - s = strrchr(*argv, '/'); - if (s != NULL && s == *argv) - s[1] = '\0'; - else if (s != NULL) - *s = '\0'; - puts(s ? *argv : "."); + puts (dirname (argv[0])); + return EXIT_SUCCESS; } @@ -837,7 +837,7 @@ extern int dpkg_main(int argc, char **argv) optind++; } - create_path(infodir, S_IRWXU); + make_directory(infodir, S_IRWXU, FILEUTILS_RECUR); status = status_read(); diff --git a/editors/sed.c b/editors/sed.c index e7208b0..89b1668 100644 --- a/editors/sed.c +++ b/editors/sed.c @@ -195,15 +195,6 @@ static int get_address(struct sed_cmd *sed_cmd, const char *str, int *linenum, r return idx; } -static char *strdup_substr(const char *str, int start, int end) -{ - int size = end - start + 1; - char *newstr = xmalloc(size); - memcpy(newstr, str+start, size-1); - newstr[size-1] = '\0'; - return newstr; -} - static int parse_subst_cmd(struct sed_cmd *sed_cmd, const char *substr) { int oldidx, cflags = REG_NEWLINE; diff --git a/include/libbb.h b/include/libbb.h index e42ca9f..c83cb7e 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -243,6 +243,10 @@ extern FILE *gz_open(FILE *compressed_file, int *pid); extern struct hostent *xgethostbyname(const char *name); +char *dirname (const char *path); +char *strdup_substr (const char *s, int start, int end); +int make_directory (char *path, mode_t mode, int flags); + #define CT_AUTO 0 #define CT_UNIX2DOS 1 #define CT_DOS2UNIX 2 diff --git a/libbb/create_path.c b/libbb/create_path.c deleted file mode 100644 index 328afc3..0000000 --- a/libbb/create_path.c +++ /dev/null @@ -1,71 +0,0 @@ -/* vi: set sw=4 ts=4: */ -/* - * Utility routines. - * - * Copyright (C) tons of folks. Tracking down who wrote what - * isn't something I'm going to worry about... If you wrote something - * here, please feel free to acknowledge your work. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Based in part on code from sash, Copyright (c) 1999 by David I. Bell - * Permission has been granted to redistribute this code under the GPL. - * - */ - -#include <stdio.h> -#include <errno.h> -#include <string.h> -#include "libbb.h" - -/* - * Attempt to create the directories along the specified path, except for - * the final component. The mode is given for the final directory only, - * while all previous ones get default protections. Errors are not reported - * here, as failures to restore files can be reported later. - */ -extern int create_path(const char *name, int mode) -{ - char *cp; - char *cpOld; - char buf[BUFSIZ + 1]; - int retVal = 0; - - strcpy(buf, name); - for (cp = buf; *cp == '/'; cp++); - cp = strchr(cp, '/'); - while (cp) { - cpOld = cp; - cp = strchr(cp + 1, '/'); - *cpOld = '\0'; - retVal = mkdir(buf, cp ? 0777 : mode); - if (retVal != 0 && errno != EEXIST) { - perror_msg("%s", buf); - return FALSE; - } - *cpOld = '/'; - } - return TRUE; -} - - -/* END CODE */ -/* -Local Variables: -c-file-style: "linux" -c-basic-offset: 4 -tab-width: 4 -End: -*/ diff --git a/libbb/dirname.c b/libbb/dirname.c new file mode 100644 index 0000000..2e89fc1 --- /dev/null +++ b/libbb/dirname.c @@ -0,0 +1,48 @@ +/* vi: set sw=4 ts=4: */ +/* + * Mini dirname function. + * + * Copyright (C) 2001 Matt Kraai. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "libbb.h" + +/* Return a string on the heap containing the directory component of PATH. */ + +char *dirname(const char *path) +{ + const char *s; + + /* Go to the end of the string. */ + s = path + strlen(path) - 1; + + /* Strip off trailing /s (unless it is also the leading /). */ + while (path < s && s[0] == '/') + s--; + + /* Strip the last component. */ + while (path <= s && s[0] != '/') + s--; + + while (path < s && s[0] == '/') + s--; + + if (s < path) + return xstrdup ("."); + else + return strdup_substr (path, 0, s - path + 1); +} diff --git a/libbb/libbb.h b/libbb/libbb.h index e42ca9f..c83cb7e 100644 --- a/libbb/libbb.h +++ b/libbb/libbb.h @@ -243,6 +243,10 @@ extern FILE *gz_open(FILE *compressed_file, int *pid); extern struct hostent *xgethostbyname(const char *name); +char *dirname (const char *path); +char *strdup_substr (const char *s, int start, int end); +int make_directory (char *path, mode_t mode, int flags); + #define CT_AUTO 0 #define CT_UNIX2DOS 1 #define CT_DOS2UNIX 2 diff --git a/libbb/make_directory.c b/libbb/make_directory.c new file mode 100644 index 0000000..e2e28a8 --- /dev/null +++ b/libbb/make_directory.c @@ -0,0 +1,66 @@ +/* vi: set sw=4 ts=4: */ +/* + * Mini make_directory implementation for busybox + * + * Copyright (C) 2001 Matt Kraai. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include <errno.h> +#include <fcntl.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <unistd.h> + +#include "libbb.h" + +/* Create the directory PATH with mode MODE, or the default if MODE is -1. + * Also create parent directories as necessary if flags contains + * FILEUTILS_RECUR. */ + +int make_directory (char *path, mode_t mode, int flags) +{ + if (!(flags & FILEUTILS_RECUR)) { + if (mkdir (path, 0777) < 0) { + perror_msg ("Cannot create directory `%s'", path); + return -1; + } + + if (mode != -1 && chmod (path, mode) < 0) { + perror_msg ("Cannot set permissions of directory `%s'", path); + return -1; + } + } else { + struct stat st; + + if (stat (path, &st) < 0 && errno == ENOENT) { + char *parent = dirname (path); + mode_t mask = umask (0); + umask (mask); + + if (make_directory (parent, (0777 & ~mask) | 0300, + FILEUTILS_RECUR) < 0) + return -1; + free (parent); + + if (make_directory (path, mode, 0) < 0) + return -1; + } + } + + return 0; +} diff --git a/libbb/strdup_substr.c b/libbb/strdup_substr.c new file mode 100644 index 0000000..4542d5f --- /dev/null +++ b/libbb/strdup_substr.c @@ -0,0 +1,32 @@ +/* vi: set sw=4 ts=4: */ +/* + * Mini strdup_substr function. + * + * Copyright (C) 2001 Mark Whitley. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* Return a substring of STR, starting at index START and ending at END, + * allocated on the heap. */ + +char *strdup_substr(const char *str, int start, int end) +{ + int size = end - start + 1; + char *newstr = xmalloc(size); + memcpy(newstr, str+start, size-1); + newstr[size-1] = '\0'; + return newstr; +} @@ -2,8 +2,7 @@ /* * Mini mkdir implementation for busybox * - * Copyright (C) 1999,2000,2001 by Lineo, inc. - * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org> + * Copyright (C) 2001 Matt Kraai <kraai@alumni.carnegiemellon.edu> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -21,79 +20,45 @@ * */ -#include <stdio.h> #include <errno.h> -#include <string.h> +#include <getopt.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <fcntl.h> +#include <unistd.h> #include <stdlib.h> -#include "busybox.h" - - -static int parentFlag = FALSE; -static mode_t mode = 0777; +#include <string.h> +#include "busybox.h" -extern int mkdir_main(int argc, char **argv) +extern int mkdir_main (int argc, char **argv) { - int i = FALSE; + mode_t mode = -1; + int flags = 0; + int status = 0; + int i, opt; - argc--; - argv++; - - /* Parse any options */ - while (argc > 0 && **argv == '-') { - while (i == FALSE && *++(*argv)) { - switch (**argv) { - case 'm': - if (--argc == 0) - show_usage(); - /* Find the specified modes */ - mode = 0; - if (parse_mode(*(++argv), &mode) == FALSE) { - error_msg_and_die("Unknown mode: %s", *argv); - } - /* Set the umask for this process so it doesn't - * screw up whatever the user just entered. */ - umask(0); - i = TRUE; - break; - case 'p': - parentFlag = TRUE; - break; - default: - show_usage(); - } + while ((opt = getopt (argc, argv, "m:p")) != -1) { + switch (opt) { + case 'm': + mode = 0777; + if (!parse_mode (optarg, &mode)) + error_msg_and_die ("invalid mode `%s'", optarg); + break; + case 'p': + flags |= FILEUTILS_RECUR; + break; + default: + show_usage (); } - argc--; - argv++; } - if (argc < 1) { - show_usage(); - } + if (optind == argc) + show_usage (); - while (argc > 0) { - int status; - struct stat statBuf; - char buf[BUFSIZ + 1]; + for (i = optind; i < argc; i++) + if (make_directory (argv[i], mode, flags) < 0) + status = 1; - if (strlen(*argv) > BUFSIZ - 1) { - error_msg_and_die(name_too_long); - } - strcpy(buf, *argv); - status = stat(buf, &statBuf); - if (parentFlag == FALSE && status != -1 && errno != ENOENT) { - error_msg_and_die("%s: File exists", buf); - } - if (parentFlag == TRUE) { - strcat(buf, "/"); - create_path(buf, mode); - } else { - if (mkdir(buf, mode) != 0 && parentFlag == FALSE) { - perror_msg_and_die(buf); - } - } - argc--; - argv++; - } - return EXIT_SUCCESS; + return status; } @@ -195,15 +195,6 @@ static int get_address(struct sed_cmd *sed_cmd, const char *str, int *linenum, r return idx; } -static char *strdup_substr(const char *str, int start, int end) -{ - int size = end - start + 1; - char *newstr = xmalloc(size); - memcpy(newstr, str+start, size-1); - newstr[size-1] = '\0'; - return newstr; -} - static int parse_subst_cmd(struct sed_cmd *sed_cmd, const char *substr) { int oldidx, cflags = REG_NEWLINE; @@ -338,7 +338,9 @@ tarExtractRegularFile(TarInfo *header, int extractFlag, int tostdoutFlag) if (extractFlag==TRUE && tostdoutFlag==FALSE) { /* Create the path to the file, just in case it isn't there... * This should not screw up path permissions or anything. */ - create_path(header->name, 0777); + char *dir = dirname (header->name); + make_directory (dir, -1, FILEUTILS_RECUR); + free (dir); if ((outFd=open(header->name, O_CREAT|O_TRUNC|O_WRONLY, header->mode & ~S_IFMT)) < 0) { error_msg(io_error, header->name, strerror(errno)); @@ -397,8 +399,7 @@ tarExtractDirectory(TarInfo *header, int extractFlag, int tostdoutFlag) if (extractFlag==FALSE || tostdoutFlag==TRUE) return( TRUE); - if (create_path(header->name, header->mode) != TRUE) { - perror_msg("%s: Cannot mkdir", header->name); + if (make_directory(header->name, header->mode, FILEUTILS_RECUR) < 0) { return( FALSE); } /* make the final component, just in case it was |