mirror of
https://github.com/moparisthebest/pacman
synced 2024-12-23 00:08: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:
parent
2edd01a973
commit
b49fc504ac
@ -185,34 +185,38 @@ int _alpm_makepath(const char *path)
|
|||||||
/* does the same thing as 'mkdir -p' */
|
/* does the same thing as 'mkdir -p' */
|
||||||
int _alpm_makepath_mode(const char *path, mode_t mode)
|
int _alpm_makepath_mode(const char *path, mode_t mode)
|
||||||
{
|
{
|
||||||
char *orig, *str, *ptr;
|
/* A bit of pointer hell here. Descriptions:
|
||||||
char full[PATH_MAX] = "";
|
* orig - a copy of path so we can safely butcher it with strsep
|
||||||
mode_t oldmask;
|
* str - the current position in the path string (after the delimiter)
|
||||||
|
* ptr - the original position of str after calling strsep
|
||||||
oldmask = umask(0000);
|
* 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);
|
orig = strdup(path);
|
||||||
|
incr = calloc(strlen(orig) + 1, sizeof(char));
|
||||||
str = orig;
|
str = orig;
|
||||||
while((ptr = strsep(&str, "/"))) {
|
while((ptr = strsep(&str, "/"))) {
|
||||||
if(strlen(ptr)) {
|
if(strlen(ptr)) {
|
||||||
struct stat buf;
|
struct stat buf;
|
||||||
|
/* we have another path component- append the newest component to
|
||||||
strcat(full, "/");
|
* existing string and create one more level of dir structure */
|
||||||
strcat(full, ptr);
|
strcat(incr, "/");
|
||||||
if(stat(full, &buf)) {
|
strcat(incr, ptr);
|
||||||
if(mkdir(full, mode)) {
|
if(stat(incr, &buf)) {
|
||||||
FREE(orig);
|
if(mkdir(incr, mode)) {
|
||||||
|
ret = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
free(orig);
|
||||||
|
free(incr);
|
||||||
umask(oldmask);
|
umask(oldmask);
|
||||||
_alpm_log(PM_LOG_ERROR, _("failed to make path '%s' : %s\n"),
|
return(ret);
|
||||||
path, strerror(errno));
|
|
||||||
return(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
FREE(orig);
|
|
||||||
umask(oldmask);
|
|
||||||
return(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#define CPBUFSIZE 8 * 1024
|
#define CPBUFSIZE 8 * 1024
|
||||||
|
@ -121,33 +121,38 @@ int getcols()
|
|||||||
/* does the same thing as 'mkdir -p' */
|
/* does the same thing as 'mkdir -p' */
|
||||||
int makepath(const char *path)
|
int makepath(const char *path)
|
||||||
{
|
{
|
||||||
char *orig, *str, *ptr;
|
/* A bit of pointer hell here. Descriptions:
|
||||||
char full[PATH_MAX+1] = "";
|
* orig - a copy of path so we can safely butcher it with strsep
|
||||||
mode_t oldmask;
|
* str - the current position in the path string (after the delimiter)
|
||||||
|
* ptr - the original position of str after calling strsep
|
||||||
oldmask = umask(0000);
|
* 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);
|
orig = strdup(path);
|
||||||
|
incr = calloc(strlen(orig) + 1, sizeof(char));
|
||||||
str = orig;
|
str = orig;
|
||||||
while((ptr = strsep(&str, "/"))) {
|
while((ptr = strsep(&str, "/"))) {
|
||||||
if(strlen(ptr)) {
|
if(strlen(ptr)) {
|
||||||
struct stat buf;
|
struct stat buf;
|
||||||
|
/* we have another path component- append the newest component to
|
||||||
/* TODO we should use strncat */
|
* existing string and create one more level of dir structure */
|
||||||
strcat(full, "/");
|
strcat(incr, "/");
|
||||||
strcat(full, ptr);
|
strcat(incr, ptr);
|
||||||
if(stat(full, &buf)) {
|
if(stat(incr, &buf)) {
|
||||||
if(mkdir(full, 0755)) {
|
if(mkdir(incr, 0755)) {
|
||||||
free(orig);
|
ret = 1;
|
||||||
umask(oldmask);
|
break;
|
||||||
return(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
free(orig);
|
free(orig);
|
||||||
|
free(incr);
|
||||||
umask(oldmask);
|
umask(oldmask);
|
||||||
return(0);
|
return(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* does the same thing as 'rm -rf' */
|
/* does the same thing as 'rm -rf' */
|
||||||
|
Loading…
Reference in New Issue
Block a user