Skip to content

Commit

Permalink
Merge branch 'bee-dep-fixes'
Browse files Browse the repository at this point in the history
* bee-dep-fixes:
  bee-dep: minor cleanup
  bee-dep: fix creation of BEE_CACHEDIR
  bee-dep: mkdirp() free(pdir) early and check for error later
  bee-dep: changed mkdirp() again - removed recursion
  • Loading branch information
mariux committed Dec 16, 2011
2 parents 2f2c48c + 712c1f8 commit ca5370a
Showing 1 changed file with 30 additions and 22 deletions.
52 changes: 30 additions & 22 deletions src/bee-dep.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#include <libgen.h>
#include <unistd.h>
#include <getopt.h>
#include <errno.h>
#include <assert.h>

#include "graph.h"

Expand Down Expand Up @@ -157,32 +159,45 @@ static FILE *open_and_lock(char *filename, char *mode)
return f;
}

/* create all directories in path with mode mode */
int mkdirp(char *path, mode_t mode)
{
char *dir, *pdir;
struct stat st;
char *dir, *pdir, *end;
int ret;

if(path == NULL) {
return -1;
}
assert(path);

dir = strdup(path);
pdir = dirname(dir);
if(stat(pdir, &st) == -1) {
mkdirp(pdir, mode);
}
dir = end = path;

while(*dir) {
/* skip "/" */
dir = end + strspn(end, "/");

/* skip non-"/" */
end = dir + strcspn(dir, "/");

/* grab everything in path till current end */
if(!(pdir = strndup(path, end - path)))
return -1;

free(dir);
/* create the directory ; ignore err if it already exists */
ret = mkdir(pdir, mode);

return mkdir(path, mode);
free(pdir);

if(ret == -1 && errno != EEXIST)
return -1;
}

return 0;
}

int main(int argc, char *argv[])
{
int c, help, rebuild, update, remove, print, options;
char found;
char cachefile[PATH_MAX + 1], path[PATH_MAX + 1], tmp[PATH_MAX + 1];
char *bee_metadir, *bee_cachedir, *dir, *pkgname;
char *bee_metadir, *bee_cachedir, *pkgname;
struct hash *graph;
struct stat st;
FILE *cache;
Expand Down Expand Up @@ -257,18 +272,11 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE);
}

dir = strdup(cachefile);
dir = dirname(dir);

if (stat(dir, &st) == -1 && mkdirp(dir, 0755) == -1) {
perror("bee-dep: mkdir");
if (mkdirp(bee_cachedir, 0755) == -1) {
perror("bee-dep: mkdirp");
exit(EXIT_FAILURE);
}

free(dir);

graph = NULL;

found = (stat(cachefile, &st) != -1 && S_ISREG(st.st_mode));

graph = hash_new();
Expand Down

0 comments on commit ca5370a

Please sign in to comment.