util: reduce pointer hell in _alpm_makepath_mode
Simplify the implementation: - allocate and manipulate a copy of the passed in path rather than building out a path as the while loop progresses - use simple pointer arithmetic to skip uninteresting cases - use mkdir(3)'s return value and errno to detect failure Signed-off-by: Dave Reisner <dreisner@archlinux.org> Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
parent
31d95b8679
commit
f8892b9d08
1 changed files with 30 additions and 25 deletions
|
@ -101,36 +101,41 @@ int _alpm_makepath(const char *path)
|
||||||
*/
|
*/
|
||||||
int _alpm_makepath_mode(const char *path, mode_t mode)
|
int _alpm_makepath_mode(const char *path, mode_t mode)
|
||||||
{
|
{
|
||||||
/* A bit of pointer hell here. Descriptions:
|
char *ptr, *str;
|
||||||
* 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
|
|
||||||
* incr - incrementally generated path for use in stat/mkdir call
|
|
||||||
*/
|
|
||||||
char *orig, *str, *ptr, *incr;
|
|
||||||
mode_t oldmask = umask(0000);
|
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
|
||||||
orig = strdup(path);
|
STRDUP(str, path, return 1);
|
||||||
incr = calloc(strlen(orig) + 1, sizeof(char));
|
|
||||||
str = orig;
|
oldmask = umask(0000);
|
||||||
while((ptr = strsep(&str, "/"))) {
|
|
||||||
if(strlen(ptr)) {
|
for(ptr = str; *ptr; ptr++) {
|
||||||
/* we have another path component- append the newest component to
|
/* detect mid-path condition and zero length paths */
|
||||||
* existing string and create one more level of dir structure */
|
if(*ptr != '/' || ptr == str || ptr[-1] == '/') {
|
||||||
strcat(incr, "/");
|
continue;
|
||||||
strcat(incr, ptr);
|
|
||||||
if(access(incr, F_OK)) {
|
|
||||||
if(mkdir(incr, mode)) {
|
|
||||||
ret = 1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* temporarily mask the end of the path */
|
||||||
|
*ptr = '\0';
|
||||||
|
|
||||||
|
if(mkdir(str, 0755) < 0 && errno != EEXIST) {
|
||||||
|
ret = 1;
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* restore path separator */
|
||||||
|
*ptr = '/';
|
||||||
}
|
}
|
||||||
free(orig);
|
|
||||||
free(incr);
|
/* end of the string. add the full path. It will already exist when the path
|
||||||
|
* passed in has a trailing slash. */
|
||||||
|
if(mkdir(str, 0755) < 0 && errno != EEXIST) {
|
||||||
|
ret = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
done:
|
||||||
umask(oldmask);
|
umask(oldmask);
|
||||||
|
free(str);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue