diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 1bb41f78115e589b84dea54ef6575b10a2ab8375..8c1f388146531ead708578efb54dd825a72a42ca 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1454,6 +1454,7 @@ virFileReadAllQuiet; virFileReadHeaderFD; virFileReadLimFD; virFileRelLinkPointsTo; +virFileRemove; virFileResolveAllLinks; virFileResolveLink; virFileRewrite; @@ -1461,7 +1462,6 @@ virFileSanitizePath; virFileSkipRoot; virFileStripSuffix; virFileTouch; -virFileUnlink; virFileUnlock; virFileUpdatePerm; virFileWaitForDevices; diff --git a/src/storage/storage_backend_fs.c b/src/storage/storage_backend_fs.c index f41a41e816a11bbcb48ea0eff500dbaa3d5debd0..99ea394029c9bef6b14364968b1d2fe51d703862 100644 --- a/src/storage/storage_backend_fs.c +++ b/src/storage/storage_backend_fs.c @@ -1203,25 +1203,23 @@ virStorageBackendFileSystemVolDelete(virConnectPtr conn ATTRIBUTE_UNUSED, switch ((virStorageVolType) vol->type) { case VIR_STORAGE_VOL_FILE: - if (virFileUnlink(vol->target.path, vol->target.perms->uid, + case VIR_STORAGE_VOL_DIR: + if (virFileRemove(vol->target.path, vol->target.perms->uid, vol->target.perms->gid) < 0) { /* Silently ignore failures where the vol has already gone away */ if (errno != ENOENT) { - virReportSystemError(errno, - _("cannot unlink file '%s'"), - vol->target.path); + if (vol->type == VIR_STORAGE_VOL_FILE) + virReportSystemError(errno, + _("cannot unlink file '%s'"), + vol->target.path); + else + virReportSystemError(errno, + _("cannot remove directory '%s'"), + vol->target.path); return -1; } } break; - case VIR_STORAGE_VOL_DIR: - if (rmdir(vol->target.path) < 0) { - virReportSystemError(errno, - _("cannot remove directory '%s'"), - vol->target.path); - return -1; - } - break; case VIR_STORAGE_VOL_BLOCK: case VIR_STORAGE_VOL_NETWORK: case VIR_STORAGE_VOL_NETDIR: diff --git a/src/util/virfile.c b/src/util/virfile.c index fe9f8d4ce969cab4836adaf08e8089be57bf38b0..668daf8ddd832b24987e0bb64e91aea3865684d6 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -2315,8 +2315,8 @@ virFileOpenAs(const char *path, int openflags, mode_t mode, } -/* virFileUnlink: - * @path: file to unlink +/* virFileRemove: + * @path: file to unlink or directory to remove * @uid: uid that was used to create the file (not required) * @gid: gid that was used to create the file (not required) * @@ -2327,7 +2327,7 @@ virFileOpenAs(const char *path, int openflags, mode_t mode, * from the child. */ int -virFileUnlink(const char *path, +virFileRemove(const char *path, uid_t uid, gid_t gid) { @@ -2341,8 +2341,12 @@ virFileUnlink(const char *path, * the file/volume, then use unlink directly */ if ((geteuid() != 0) || - ((uid == (uid_t) -1) && (gid == (gid_t) -1))) - return unlink(path); + ((uid == (uid_t) -1) && (gid == (gid_t) -1))) { + if (virFileIsDir(path)) + return rmdir(path); + else + return unlink(path); + } /* Otherwise, we have to deal with the NFS root-squash craziness * to run under the uid/gid that created the volume in order to @@ -2406,9 +2410,16 @@ virFileUnlink(const char *path, goto childerror; } - if (unlink(path) < 0) { - ret = errno; - goto childerror; + if (virFileIsDir(path)) { + if (rmdir(path) < 0) { + ret = errno; + goto childerror; + } + } else { + if (unlink(path) < 0) { + ret = errno; + goto childerror; + } } childerror: @@ -2643,7 +2654,7 @@ virDirCreate(const char *path ATTRIBUTE_UNUSED, } int -virFileUnlink(const char *path, +virFileRemove(const char *path, uid_t uid ATTRIBUTE_UNUSED, gid_t gid ATTRIBUTE_UNUSED) { diff --git a/src/util/virfile.h b/src/util/virfile.h index 797ca658883a67ddd6cb25de2ac5dd15a4b98db6..312f22674cf45426d7bc4d54295d6bc56b7b0042 100644 --- a/src/util/virfile.h +++ b/src/util/virfile.h @@ -219,7 +219,7 @@ int virFileOpenAs(const char *path, int openflags, mode_t mode, uid_t uid, gid_t gid, unsigned int flags) ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK; -int virFileUnlink(const char *path, uid_t uid, gid_t gid); +int virFileRemove(const char *path, uid_t uid, gid_t gid); enum { VIR_DIR_CREATE_NONE = 0,