提交 1b046a68 编写于 作者: J John Ferlan

virfile: Rename virFileUnlink to virFileRemove

Similar to commit id '35847860', it's possible to attempt to create
a 'netfs' directory in an NFS root-squash environment which will cause
the 'vol-delete' command to fail.  It's also possible error paths from
the 'vol-create' would result in an error to remove a created directory
if the permissions were incorrect (and disallowed root access).

Thus rename the virFileUnlink to be virFileRemove to match the C API
functionality, adjust the code to following using rmdir or unlink
depending on the path type, and then use/call it for the VIR_STORAGE_VOL_DIR
上级 91c8e75f
...@@ -1454,6 +1454,7 @@ virFileReadAllQuiet; ...@@ -1454,6 +1454,7 @@ virFileReadAllQuiet;
virFileReadHeaderFD; virFileReadHeaderFD;
virFileReadLimFD; virFileReadLimFD;
virFileRelLinkPointsTo; virFileRelLinkPointsTo;
virFileRemove;
virFileResolveAllLinks; virFileResolveAllLinks;
virFileResolveLink; virFileResolveLink;
virFileRewrite; virFileRewrite;
...@@ -1461,7 +1462,6 @@ virFileSanitizePath; ...@@ -1461,7 +1462,6 @@ virFileSanitizePath;
virFileSkipRoot; virFileSkipRoot;
virFileStripSuffix; virFileStripSuffix;
virFileTouch; virFileTouch;
virFileUnlink;
virFileUnlock; virFileUnlock;
virFileUpdatePerm; virFileUpdatePerm;
virFileWaitForDevices; virFileWaitForDevices;
......
...@@ -1203,25 +1203,23 @@ virStorageBackendFileSystemVolDelete(virConnectPtr conn ATTRIBUTE_UNUSED, ...@@ -1203,25 +1203,23 @@ virStorageBackendFileSystemVolDelete(virConnectPtr conn ATTRIBUTE_UNUSED,
switch ((virStorageVolType) vol->type) { switch ((virStorageVolType) vol->type) {
case VIR_STORAGE_VOL_FILE: 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) { vol->target.perms->gid) < 0) {
/* Silently ignore failures where the vol has already gone away */ /* Silently ignore failures where the vol has already gone away */
if (errno != ENOENT) { if (errno != ENOENT) {
virReportSystemError(errno, if (vol->type == VIR_STORAGE_VOL_FILE)
_("cannot unlink file '%s'"), virReportSystemError(errno,
vol->target.path); _("cannot unlink file '%s'"),
vol->target.path);
else
virReportSystemError(errno,
_("cannot remove directory '%s'"),
vol->target.path);
return -1; return -1;
} }
} }
break; 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_BLOCK:
case VIR_STORAGE_VOL_NETWORK: case VIR_STORAGE_VOL_NETWORK:
case VIR_STORAGE_VOL_NETDIR: case VIR_STORAGE_VOL_NETDIR:
......
...@@ -2315,8 +2315,8 @@ virFileOpenAs(const char *path, int openflags, mode_t mode, ...@@ -2315,8 +2315,8 @@ virFileOpenAs(const char *path, int openflags, mode_t mode,
} }
/* virFileUnlink: /* virFileRemove:
* @path: file to unlink * @path: file to unlink or directory to remove
* @uid: uid that was used to create the file (not required) * @uid: uid that was used to create the file (not required)
* @gid: gid 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, ...@@ -2327,7 +2327,7 @@ virFileOpenAs(const char *path, int openflags, mode_t mode,
* from the child. * from the child.
*/ */
int int
virFileUnlink(const char *path, virFileRemove(const char *path,
uid_t uid, uid_t uid,
gid_t gid) gid_t gid)
{ {
...@@ -2341,8 +2341,12 @@ virFileUnlink(const char *path, ...@@ -2341,8 +2341,12 @@ virFileUnlink(const char *path,
* the file/volume, then use unlink directly * the file/volume, then use unlink directly
*/ */
if ((geteuid() != 0) || if ((geteuid() != 0) ||
((uid == (uid_t) -1) && (gid == (gid_t) -1))) ((uid == (uid_t) -1) && (gid == (gid_t) -1))) {
return unlink(path); if (virFileIsDir(path))
return rmdir(path);
else
return unlink(path);
}
/* Otherwise, we have to deal with the NFS root-squash craziness /* Otherwise, we have to deal with the NFS root-squash craziness
* to run under the uid/gid that created the volume in order to * to run under the uid/gid that created the volume in order to
...@@ -2406,9 +2410,16 @@ virFileUnlink(const char *path, ...@@ -2406,9 +2410,16 @@ virFileUnlink(const char *path,
goto childerror; goto childerror;
} }
if (unlink(path) < 0) { if (virFileIsDir(path)) {
ret = errno; if (rmdir(path) < 0) {
goto childerror; ret = errno;
goto childerror;
}
} else {
if (unlink(path) < 0) {
ret = errno;
goto childerror;
}
} }
childerror: childerror:
...@@ -2643,7 +2654,7 @@ virDirCreate(const char *path ATTRIBUTE_UNUSED, ...@@ -2643,7 +2654,7 @@ virDirCreate(const char *path ATTRIBUTE_UNUSED,
} }
int int
virFileUnlink(const char *path, virFileRemove(const char *path,
uid_t uid ATTRIBUTE_UNUSED, uid_t uid ATTRIBUTE_UNUSED,
gid_t gid ATTRIBUTE_UNUSED) gid_t gid ATTRIBUTE_UNUSED)
{ {
......
...@@ -219,7 +219,7 @@ int virFileOpenAs(const char *path, int openflags, mode_t mode, ...@@ -219,7 +219,7 @@ int virFileOpenAs(const char *path, int openflags, mode_t mode,
uid_t uid, gid_t gid, uid_t uid, gid_t gid,
unsigned int flags) unsigned int flags)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK; 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 { enum {
VIR_DIR_CREATE_NONE = 0, VIR_DIR_CREATE_NONE = 0,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册