[libvirt] [PATCH] virsh: Ensure the parents of the readline history path exists

Instead of changing the existed virFileMakePath to accept mode argument and modifying a pile of its uses, this patch introduces virFileMakePathWithMode, and use it instead of mkdir() to create the readline history dir. --- src/libvirt_private.syms | 1 + src/util/util.c | 15 +++++++++++---- src/util/util.h | 2 ++ tools/virsh.c | 3 ++- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 6625fc6..b173590 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1146,6 +1146,7 @@ virFileIsDir; virFileLinkPointsTo; virFileLock; virFileMakePath; +virFileMakePathWithMode; virFileMatchesNameSuffix; virFileOpenAs; virFileOpenTty; diff --git a/src/util/util.c b/src/util/util.c index f886ea7..47b1366 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -1248,7 +1248,7 @@ int virDirCreate(const char *path ATTRIBUTE_UNUSED, } #endif /* WIN32 */ -static int virFileMakePathHelper(char *path) +static int virFileMakePathHelper(char *path, mode_t mode) { struct stat st; char *p; @@ -1272,13 +1272,13 @@ static int virFileMakePathHelper(char *path) if (p != path) { *p = '\0'; - if (virFileMakePathHelper(path) < 0) + if (virFileMakePathHelper(path, mode) < 0) return -1; *p = '/'; } - if (mkdir(path, 0777) < 0 && errno != EEXIST) + if (mkdir(path, mode) < 0 && errno != EEXIST) return -1; return 0; @@ -1292,13 +1292,20 @@ static int virFileMakePathHelper(char *path) */ int virFileMakePath(const char *path) { + return virFileMakePathWithMode(path, 0777); +} + +int +virFileMakePathWithMode(const char *path, + mode_t mode) +{ int ret = -1; char *tmp; if ((tmp = strdup(path)) == NULL) goto cleanup; - ret = virFileMakePathHelper(tmp); + ret = virFileMakePathHelper(tmp, mode); cleanup: VIR_FREE(tmp); diff --git a/src/util/util.h b/src/util/util.h index 0af7e6d..33226b2 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -115,6 +115,8 @@ enum { int virDirCreate(const char *path, mode_t mode, uid_t uid, gid_t gid, unsigned int flags) ATTRIBUTE_RETURN_CHECK; int virFileMakePath(const char *path) ATTRIBUTE_RETURN_CHECK; +int virFileMakePathWithMode(const char *path, + mode_t mode) ATTRIBUTE_RETURN_CHECK; char *virFileBuildPath(const char *dir, const char *name, diff --git a/tools/virsh.c b/tools/virsh.c index 9008837..01e7ce0 100644 --- a/tools/virsh.c +++ b/tools/virsh.c @@ -20627,7 +20627,8 @@ static void vshReadlineDeinit (vshControl *ctl) { if (ctl->historyfile != NULL) { - if (mkdir(ctl->historydir, 0755) < 0 && errno != EEXIST) { + if (virFileMakePathWithMode(ctl->historydir, 0755) < 0 && + errno != EEXIST) { char ebuf[1024]; vshError(ctl, _("Failed to create '%s': %s"), ctl->historydir, virStrerror(errno, ebuf, sizeof(ebuf))); -- 1.7.7.3

On 07/10/2012 05:33 AM, Osier Yang wrote:
Instead of changing the existed virFileMakePath to accept mode argument and modifying a pile of its uses, this patch introduces virFileMakePathWithMode, and use it instead of mkdir() to create the readline history dir. --- src/libvirt_private.syms | 1 + src/util/util.c | 15 +++++++++++---- src/util/util.h | 2 ++ tools/virsh.c | 3 ++- 4 files changed, 16 insertions(+), 5 deletions(-)
Does this always do the right thing? Remember, 'mkdir -p a/b/c' has the ability to create parent directories 'a' and 'a/b' with a different mode than the final directory 'a/b/c'; it is often the case that you want the parent directories to have more permissions than the final child.
+++ b/tools/virsh.c @@ -20627,7 +20627,8 @@ static void vshReadlineDeinit (vshControl *ctl) { if (ctl->historyfile != NULL) { - if (mkdir(ctl->historydir, 0755) < 0 && errno != EEXIST) { + if (virFileMakePathWithMode(ctl->historydir, 0755) < 0 &&
But if we are happy with all intermediate directories being created mode 0755 instead of the default of 0777 from the original virFileMakePath, then this patch makes sense. ACK. -- Eric Blake eblake@redhat.com +1-919-301-3266 Libvirt virtualization library http://libvirt.org

On 2012年07月10日 21:04, Eric Blake wrote:
On 07/10/2012 05:33 AM, Osier Yang wrote:
Instead of changing the existed virFileMakePath to accept mode argument and modifying a pile of its uses, this patch introduces virFileMakePathWithMode, and use it instead of mkdir() to create the readline history dir. --- src/libvirt_private.syms | 1 + src/util/util.c | 15 +++++++++++---- src/util/util.h | 2 ++ tools/virsh.c | 3 ++- 4 files changed, 16 insertions(+), 5 deletions(-)
Does this always do the right thing? Remember, 'mkdir -p a/b/c' has the ability to create parent directories 'a' and 'a/b' with a different mode than the final directory 'a/b/c'; it is often the case that you want the parent directories to have more permissions than the final child.
Oh, right. But looks like we don't have the requirement to set different mode for each parents yet. I pushed the patch, thanks for the reviewing! Regards, Osier
participants (2)
-
Eric Blake
-
Osier Yang