Add a new boolean 'created' to virDirCreate to be set when a directory
is created either directly or in a fork'd child. This will allow
a caller to make "decisions" regarding whether or not to delete the
directory since virDirCreate has many other failure scenarios and there's
no guarantee that the mkdir was the cause for failure.
Signed-off-by: John Ferlan <jferlan(a)redhat.com>
---
src/storage/storage_backend_fs.c | 4 ++++
src/util/virfile.c | 9 +++++++--
src/util/virfile.h | 2 +-
3 files changed, 12 insertions(+), 3 deletions(-)
diff --git a/src/storage/storage_backend_fs.c b/src/storage/storage_backend_fs.c
index b39b5a5..fddec4b 100644
--- a/src/storage/storage_backend_fs.c
+++ b/src/storage/storage_backend_fs.c
@@ -786,6 +786,7 @@ virStorageBackendFileSystemBuild(virConnectPtr conn ATTRIBUTE_UNUSED,
mode_t mode;
bool needs_create_as_uid;
unsigned int dir_create_flags;
+ bool created = false;
virCheckFlags(VIR_STORAGE_POOL_BUILD_OVERWRITE |
VIR_STORAGE_POOL_BUILD_NO_OVERWRITE, ret);
@@ -831,6 +832,7 @@ virStorageBackendFileSystemBuild(virConnectPtr conn ATTRIBUTE_UNUSED,
mode,
pool->def->target.perms.uid,
pool->def->target.perms.gid,
+ &created,
dir_create_flags) < 0)
goto error;
@@ -1081,6 +1083,7 @@ static int createFileDir(virConnectPtr conn ATTRIBUTE_UNUSED,
unsigned int flags)
{
int err;
+ bool created = false;
virCheckFlags(0, -1);
@@ -1104,6 +1107,7 @@ static int createFileDir(virConnectPtr conn ATTRIBUTE_UNUSED,
vol->target.perms->mode),
vol->target.perms->uid,
vol->target.perms->gid,
+ &created,
(pool->def->type == VIR_STORAGE_POOL_NETFS
? VIR_DIR_CREATE_AS_UID : 0))) < 0) {
return -1;
diff --git a/src/util/virfile.c b/src/util/virfile.c
index a783406..0b70d41 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -2412,6 +2412,7 @@ virFileRemove(const char *path,
static int
virDirCreateNoFork(const char *path,
mode_t mode, uid_t uid, gid_t gid,
+ bool *created,
unsigned int flags)
{
int ret = 0;
@@ -2424,6 +2425,7 @@ virDirCreateNoFork(const char *path,
path);
goto error;
}
+ *created = true;
}
if (stat(path, &st) == -1) {
@@ -2454,6 +2456,7 @@ virDirCreateNoFork(const char *path,
int
virDirCreate(const char *path,
mode_t mode, uid_t uid, gid_t gid,
+ bool *created,
unsigned int flags)
{
struct stat st;
@@ -2480,7 +2483,7 @@ virDirCreate(const char *path,
|| (geteuid() != 0)
|| ((uid == (uid_t) -1) && (gid == (gid_t) -1))
|| ((flags & VIR_DIR_CREATE_ALLOW_EXIST) && virFileExists(path))) {
- return virDirCreateNoFork(path, mode, uid, gid, flags);
+ return virDirCreateNoFork(path, mode, uid, gid, created, flags);
}
if (uid == (uid_t) -1)
@@ -2522,7 +2525,7 @@ virDirCreate(const char *path,
*/
if (status == EACCES) {
virResetLastError();
- return virDirCreateNoFork(path, mode, uid, gid, flags);
+ return virDirCreateNoFork(path, mode, uid, gid, created, flags);
}
if (status)
@@ -2548,6 +2551,7 @@ virDirCreate(const char *path,
}
goto childerror;
}
+ *created = true;
/* check if group was set properly by creating after
* setgid. If not, try doing it with chown */
@@ -2616,6 +2620,7 @@ virDirCreate(const char *path ATTRIBUTE_UNUSED,
mode_t mode ATTRIBUTE_UNUSED,
uid_t uid ATTRIBUTE_UNUSED,
gid_t gid ATTRIBUTE_UNUSED,
+ bool *created ATTRIBUTE_UNUSED,
unsigned int flags_unused ATTRIBUTE_UNUSED)
{
virReportError(VIR_ERR_INTERNAL_ERROR,
diff --git a/src/util/virfile.h b/src/util/virfile.h
index f6e7566..855134f 100644
--- a/src/util/virfile.h
+++ b/src/util/virfile.h
@@ -227,7 +227,7 @@ enum {
VIR_DIR_CREATE_ALLOW_EXIST = (1 << 1),
};
int virDirCreate(const char *path, mode_t mode, uid_t uid, gid_t gid,
- unsigned int flags) ATTRIBUTE_RETURN_CHECK;
+ bool *created, unsigned int flags) ATTRIBUTE_RETURN_CHECK;
int virDirRead(DIR *dirp, struct dirent **ent, const char *dirname)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
--
2.1.0