From: Kristian Høgsberg Date: Wed, 12 Mar 2008 21:00:10 +0000 (-0400) Subject: install: Handle the case where a directory already exists. X-Git-Tag: 0.1~182^2~1 X-Git-Url: http://project.juiblex.co.uk/git/?a=commitdiff_plain;h=b870969fd62d148f7923c3acb44f980c691e380d;p=razor.git install: Handle the case where a directory already exists. --- diff --git a/rpm.c b/rpm.c index 8274ff5..d28372e 100644 --- a/rpm.c +++ b/rpm.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -265,16 +266,20 @@ create_path(struct installer *installer, const char *path, const char *name, unsigned int mode) { char buffer[PATH_MAX]; - int fd; + struct stat buf; + int fd, ret; if (razor_create_dir(installer->root, path) < 0) return -1; - snprintf(buffer, sizeof buffer, "%s%s/%s", + /* assertion: root doesn't end in a slash, path begins and end + * with a slash, name does not begin with a slash. */ + snprintf(buffer, sizeof buffer, "%s%s%s", installer->root, path, name); switch (mode >> 12) { case REG: + /* FIXME: handle the case where a file is already there. */ fd = open(buffer, O_WRONLY | O_CREAT | O_TRUNC, mode & 0x1ff); if (fd < 0){ fprintf(stderr, "failed to create file %s\n", buffer); @@ -297,7 +302,16 @@ create_path(struct installer *installer, } return 0; case XDIR: - return mkdir(buffer, mode & 0x1ff); + ret = mkdir(buffer, mode & 0x1ff); + if (ret == 0 || errno != EEXIST) + return ret; + if (stat(buffer, &buf) || !S_ISDIR(buf.st_mode)) { + /* FIXME: also check that mode match. */ + fprintf(stderr, + "%s exists but is not a directory\n", buffer); + return -1; + } + return 0; case PIPE: case CDEV: case BDEV: