1
0
mirror of https://github.com/moparisthebest/pacman synced 2024-12-22 15:58:50 -05:00

Update makepath to remove PATH_MAX usage

The start of a few commits to remove some PATH_MAX usage from our code. Use
a dynamically allocated string instead.

Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
Dan McGee 2008-05-04 18:39:22 -05:00
parent 2edd01a973
commit b49fc504ac
2 changed files with 41 additions and 32 deletions

View File

@ -185,34 +185,38 @@ int _alpm_makepath(const char *path)
/* does the same thing as 'mkdir -p' */
int _alpm_makepath_mode(const char *path, mode_t mode)
{
char *orig, *str, *ptr;
char full[PATH_MAX] = "";
mode_t oldmask;
oldmask = umask(0000);
/* A bit of pointer hell here. Descriptions:
* orig - a copy of path so we can safely butcher it with strsep
* str - the current position in the path string (after the delimiter)
* ptr - the original position of str after calling strsep
* incr - incrementally generated path for use in stat/mkdir call
*/
char *orig, *str, *ptr, *incr;
mode_t oldmask = umask(0000);
int ret = 0;
orig = strdup(path);
incr = calloc(strlen(orig) + 1, sizeof(char));
str = orig;
while((ptr = strsep(&str, "/"))) {
if(strlen(ptr)) {
struct stat buf;
strcat(full, "/");
strcat(full, ptr);
if(stat(full, &buf)) {
if(mkdir(full, mode)) {
FREE(orig);
/* we have another path component- append the newest component to
* existing string and create one more level of dir structure */
strcat(incr, "/");
strcat(incr, ptr);
if(stat(incr, &buf)) {
if(mkdir(incr, mode)) {
ret = 1;
break;
}
}
}
}
free(orig);
free(incr);
umask(oldmask);
_alpm_log(PM_LOG_ERROR, _("failed to make path '%s' : %s\n"),
path, strerror(errno));
return(1);
}
}
}
}
FREE(orig);
umask(oldmask);
return(0);
return(ret);
}
#define CPBUFSIZE 8 * 1024

View File

@ -121,33 +121,38 @@ int getcols()
/* does the same thing as 'mkdir -p' */
int makepath(const char *path)
{
char *orig, *str, *ptr;
char full[PATH_MAX+1] = "";
mode_t oldmask;
oldmask = umask(0000);
/* A bit of pointer hell here. Descriptions:
* orig - a copy of path so we can safely butcher it with strsep
* str - the current position in the path string (after the delimiter)
* ptr - the original position of str after calling strsep
* incr - incrementally generated path for use in stat/mkdir call
*/
char *orig, *str, *ptr, *incr;
mode_t oldmask = umask(0000);
int ret = 0;
orig = strdup(path);
incr = calloc(strlen(orig) + 1, sizeof(char));
str = orig;
while((ptr = strsep(&str, "/"))) {
if(strlen(ptr)) {
struct stat buf;
/* TODO we should use strncat */
strcat(full, "/");
strcat(full, ptr);
if(stat(full, &buf)) {
if(mkdir(full, 0755)) {
free(orig);
umask(oldmask);
return(1);
/* we have another path component- append the newest component to
* existing string and create one more level of dir structure */
strcat(incr, "/");
strcat(incr, ptr);
if(stat(incr, &buf)) {
if(mkdir(incr, 0755)) {
ret = 1;
break;
}
}
}
}
free(orig);
free(incr);
umask(oldmask);
return(0);
return(ret);
}
/* does the same thing as 'rm -rf' */