Rewrote mkdir (and touched lots of things in the process).

This commit is contained in:
Matt Kraai 2001-06-21 19:41:37 +00:00
parent 091781e20e
commit ceeff73819
17 changed files with 233 additions and 255 deletions

View File

@ -237,7 +237,7 @@ endif
LIBBB = libbb LIBBB = libbb
LIBBB_LIB = libbb.a LIBBB_LIB = libbb.a
LIBBB_CSRC= ask_confirmation.c chomp.c concat_path_file.c copy_file.c \ 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 \ 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 \ 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 \ 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 \ 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 \ 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 \ 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_OBJS=$(patsubst %.c,$(LIBBB)/%.o, $(LIBBB_CSRC))
LIBBB_CFLAGS = -I$(LIBBB) LIBBB_CFLAGS = -I$(LIBBB)
ifneq ($(strip $(BB_SRC_DIR)),) ifneq ($(strip $(BB_SRC_DIR)),)

View File

@ -837,7 +837,7 @@ extern int dpkg_main(int argc, char **argv)
optind++; optind++;
} }
create_path(infodir, S_IRWXU); make_directory(infodir, S_IRWXU, FILEUTILS_RECUR);
status = status_read(); status = status_read();

View File

@ -338,7 +338,9 @@ tarExtractRegularFile(TarInfo *header, int extractFlag, int tostdoutFlag)
if (extractFlag==TRUE && tostdoutFlag==FALSE) { if (extractFlag==TRUE && tostdoutFlag==FALSE) {
/* Create the path to the file, just in case it isn't there... /* Create the path to the file, just in case it isn't there...
* This should not screw up path permissions or anything. */ * 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, if ((outFd=open(header->name, O_CREAT|O_TRUNC|O_WRONLY,
header->mode & ~S_IFMT)) < 0) { header->mode & ~S_IFMT)) < 0) {
error_msg(io_error, header->name, strerror(errno)); error_msg(io_error, header->name, strerror(errno));
@ -397,8 +399,7 @@ tarExtractDirectory(TarInfo *header, int extractFlag, int tostdoutFlag)
if (extractFlag==FALSE || tostdoutFlag==TRUE) if (extractFlag==FALSE || tostdoutFlag==TRUE)
return( TRUE); return( TRUE);
if (create_path(header->name, header->mode) != TRUE) { if (make_directory(header->name, header->mode, FILEUTILS_RECUR) < 0) {
perror_msg("%s: Cannot mkdir", header->name);
return( FALSE); return( FALSE);
} }
/* make the final component, just in case it was /* make the final component, just in case it was

View File

@ -30,21 +30,11 @@
extern int dirname_main(int argc, char **argv) extern int dirname_main(int argc, char **argv)
{ {
char* s;
if ((argc < 2) || (**(argv + 1) == '-')) if ((argc < 2) || (**(argv + 1) == '-'))
show_usage(); show_usage();
argv++; argv++;
s=*argv+strlen(*argv)-1; puts (dirname (argv[0]));
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 : ".");
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -2,8 +2,7 @@
/* /*
* Mini mkdir implementation for busybox * Mini mkdir implementation for busybox
* *
* Copyright (C) 1999,2000,2001 by Lineo, inc. * Copyright (C) 2001 Matt Kraai <kraai@alumni.carnegiemellon.edu>
* Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -21,79 +20,45 @@
* *
*/ */
#include <stdio.h>
#include <errno.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 <stdlib.h>
#include <string.h>
#include "busybox.h" #include "busybox.h"
extern int mkdir_main (int argc, char **argv)
static int parentFlag = FALSE;
static mode_t mode = 0777;
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--; while ((opt = getopt (argc, argv, "m:p")) != -1) {
argv++; switch (opt) {
case 'm':
/* Parse any options */ mode = 0777;
while (argc > 0 && **argv == '-') { if (!parse_mode (optarg, &mode))
while (i == FALSE && *++(*argv)) { error_msg_and_die ("invalid mode `%s'", optarg);
switch (**argv) { break;
case 'm': case 'p':
if (--argc == 0) flags |= FILEUTILS_RECUR;
show_usage(); break;
/* Find the specified modes */ default:
mode = 0; show_usage ();
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();
}
} }
argc--;
argv++;
} }
if (argc < 1) { if (optind == argc)
show_usage(); show_usage ();
}
while (argc > 0) { for (i = optind; i < argc; i++)
int status; if (make_directory (argv[i], mode, flags) < 0)
struct stat statBuf; status = 1;
char buf[BUFSIZ + 1];
if (strlen(*argv) > BUFSIZ - 1) { return status;
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;
} }

View File

@ -30,21 +30,11 @@
extern int dirname_main(int argc, char **argv) extern int dirname_main(int argc, char **argv)
{ {
char* s;
if ((argc < 2) || (**(argv + 1) == '-')) if ((argc < 2) || (**(argv + 1) == '-'))
show_usage(); show_usage();
argv++; argv++;
s=*argv+strlen(*argv)-1; puts (dirname (argv[0]));
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 : ".");
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

2
dpkg.c
View File

@ -837,7 +837,7 @@ extern int dpkg_main(int argc, char **argv)
optind++; optind++;
} }
create_path(infodir, S_IRWXU); make_directory(infodir, S_IRWXU, FILEUTILS_RECUR);
status = status_read(); status = status_read();

View File

@ -195,15 +195,6 @@ static int get_address(struct sed_cmd *sed_cmd, const char *str, int *linenum, r
return idx; 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) static int parse_subst_cmd(struct sed_cmd *sed_cmd, const char *substr)
{ {
int oldidx, cflags = REG_NEWLINE; int oldidx, cflags = REG_NEWLINE;

View File

@ -243,6 +243,10 @@ extern FILE *gz_open(FILE *compressed_file, int *pid);
extern struct hostent *xgethostbyname(const char *name); 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_AUTO 0
#define CT_UNIX2DOS 1 #define CT_UNIX2DOS 1
#define CT_DOS2UNIX 2 #define CT_DOS2UNIX 2

View File

@ -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:
*/

48
libbb/dirname.c Normal file
View File

@ -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);
}

View File

@ -243,6 +243,10 @@ extern FILE *gz_open(FILE *compressed_file, int *pid);
extern struct hostent *xgethostbyname(const char *name); 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_AUTO 0
#define CT_UNIX2DOS 1 #define CT_UNIX2DOS 1
#define CT_DOS2UNIX 2 #define CT_DOS2UNIX 2

66
libbb/make_directory.c Normal file
View File

@ -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;
}

32
libbb/strdup_substr.c Normal file
View File

@ -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;
}

97
mkdir.c
View File

@ -2,8 +2,7 @@
/* /*
* Mini mkdir implementation for busybox * Mini mkdir implementation for busybox
* *
* Copyright (C) 1999,2000,2001 by Lineo, inc. * Copyright (C) 2001 Matt Kraai <kraai@alumni.carnegiemellon.edu>
* Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -21,79 +20,45 @@
* *
*/ */
#include <stdio.h>
#include <errno.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 <stdlib.h>
#include <string.h>
#include "busybox.h" #include "busybox.h"
extern int mkdir_main (int argc, char **argv)
static int parentFlag = FALSE;
static mode_t mode = 0777;
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--; while ((opt = getopt (argc, argv, "m:p")) != -1) {
argv++; switch (opt) {
case 'm':
/* Parse any options */ mode = 0777;
while (argc > 0 && **argv == '-') { if (!parse_mode (optarg, &mode))
while (i == FALSE && *++(*argv)) { error_msg_and_die ("invalid mode `%s'", optarg);
switch (**argv) { break;
case 'm': case 'p':
if (--argc == 0) flags |= FILEUTILS_RECUR;
show_usage(); break;
/* Find the specified modes */ default:
mode = 0; show_usage ();
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();
}
} }
argc--;
argv++;
} }
if (argc < 1) { if (optind == argc)
show_usage(); show_usage ();
}
while (argc > 0) { for (i = optind; i < argc; i++)
int status; if (make_directory (argv[i], mode, flags) < 0)
struct stat statBuf; status = 1;
char buf[BUFSIZ + 1];
if (strlen(*argv) > BUFSIZ - 1) { return status;
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;
} }

9
sed.c
View File

@ -195,15 +195,6 @@ static int get_address(struct sed_cmd *sed_cmd, const char *str, int *linenum, r
return idx; 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) static int parse_subst_cmd(struct sed_cmd *sed_cmd, const char *substr)
{ {
int oldidx, cflags = REG_NEWLINE; int oldidx, cflags = REG_NEWLINE;

7
tar.c
View File

@ -338,7 +338,9 @@ tarExtractRegularFile(TarInfo *header, int extractFlag, int tostdoutFlag)
if (extractFlag==TRUE && tostdoutFlag==FALSE) { if (extractFlag==TRUE && tostdoutFlag==FALSE) {
/* Create the path to the file, just in case it isn't there... /* Create the path to the file, just in case it isn't there...
* This should not screw up path permissions or anything. */ * 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, if ((outFd=open(header->name, O_CREAT|O_TRUNC|O_WRONLY,
header->mode & ~S_IFMT)) < 0) { header->mode & ~S_IFMT)) < 0) {
error_msg(io_error, header->name, strerror(errno)); error_msg(io_error, header->name, strerror(errno));
@ -397,8 +399,7 @@ tarExtractDirectory(TarInfo *header, int extractFlag, int tostdoutFlag)
if (extractFlag==FALSE || tostdoutFlag==TRUE) if (extractFlag==FALSE || tostdoutFlag==TRUE)
return( TRUE); return( TRUE);
if (create_path(header->name, header->mode) != TRUE) { if (make_directory(header->name, header->mode, FILEUTILS_RECUR) < 0) {
perror_msg("%s: Cannot mkdir", header->name);
return( FALSE); return( FALSE);
} }
/* make the final component, just in case it was /* make the final component, just in case it was