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

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

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