[patch 5/7] Cleanup configDir members

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



server->autostartNetworkConfigDir etc. is starting to
get a little unwieldly so put the config directory
paths in an array.

Signed-off-by: Mark McLoughlin <markmc@xxxxxxxxxx>

Index: libvirt/qemud/internal.h
===================================================================
--- libvirt.orig/qemud/internal.h
+++ libvirt/qemud/internal.h
@@ -54,6 +54,15 @@ typedef enum {
 #endif
 } qemudLogPriority;
 
+typedef enum {
+    QEMUD_DIR_DOMAINS = 0,
+    QEMUD_DIR_AUTO_DOMAINS,
+    QEMUD_DIR_NETWORKS,
+    QEMUD_DIR_AUTO_NETWORKS,
+
+    QEMUD_N_CONFIG_DIRS
+} qemudConfigDirType;
+
 /* Different types of QEMU acceleration possible */
 enum qemud_vm_virt_type {
     QEMUD_VIRT_QEMU,
@@ -292,14 +301,15 @@ struct qemud_server {
     struct qemud_network *networks;
     brControl *brctl;
     iptablesContext *iptables;
-    char configDir[PATH_MAX];
-    char networkConfigDir[PATH_MAX];
-    char autostartConfigDir[PATH_MAX];
-    char autostartNetworkConfigDir[PATH_MAX];
+    char configDirs[QEMUD_N_CONFIG_DIRS][PATH_MAX];
     char errorMessage[QEMUD_MAX_ERROR_LEN];
     int errorCode;
     unsigned int shutdown : 1;
 };
+#define domainsDir      configDirs[QEMUD_DIR_DOMAINS]
+#define autoDomainsDir  configDirs[QEMUD_DIR_AUTO_DOMAINS]
+#define networksDir     configDirs[QEMUD_DIR_NETWORKS]
+#define autoNetworksDir configDirs[QEMUD_DIR_AUTO_NETWORKS]
 
 int qemudStartVMDaemon(struct qemud_server *server,
                        struct qemud_vm *vm);
Index: libvirt/qemud/qemud.c
===================================================================
--- libvirt.orig/qemud/qemud.c
+++ libvirt/qemud/qemud.c
@@ -354,15 +354,22 @@ static int qemudListenUnix(struct qemud_
     return 0;
 }
 
-static int qemudInitPaths(int sys,
-                          char *configDir,
-                          char *networkConfigDir,
-                          char *autostartConfigDir,
-                          char *autostartNetworkConfigDir,
+static int qemudInitPaths(struct qemud_server *server,
+                          int sys,
                           char *sockname,
                           char *roSockname,
                           int maxlen) {
+    const char *paths[] = {
+        "libvirt/qemu",                    /* QEMUD_DIR_DOMAINS */
+        "libvirt/qemu/autostart",          /* QEMUD_DIR_AUTO_DOMAINS */
+        "libvirt/qemu/networks",           /* QEMUD_DIR_NETWORKS */
+        "libvirt/qemu/networks/autostart", /* QEMUD_DIR_AUTO_NETWORKS */
+    };
+
     uid_t uid;
+    struct passwd *pw;
+    const char *base = NULL;
+    int i;
 
     uid = geteuid();
 
@@ -372,18 +379,6 @@ static int qemudInitPaths(int sys,
             return -1;
         }
 
-        if (snprintf(configDir, maxlen, "%s/libvirt/qemu", SYSCONF_DIR) >= maxlen)
-            goto snprintf_error;
-
-        if (snprintf(networkConfigDir, maxlen, "%s/libvirt/qemu/networks", SYSCONF_DIR) >= maxlen)
-            goto snprintf_error;
-
-        if (snprintf(autostartConfigDir, maxlen, "%s/libvirt/qemu/autostart", SYSCONF_DIR) >= maxlen)
-            goto snprintf_error;
-
-        if (snprintf(autostartNetworkConfigDir, maxlen, "%s/libvirt/qemu/networks/autostart", SYSCONF_DIR) >= maxlen)
-            goto snprintf_error;
-
         if (snprintf(sockname, maxlen, "%s/run/libvirt/qemud-sock", LOCAL_STATE_DIR) >= maxlen)
             goto snprintf_error;
 
@@ -393,30 +388,24 @@ static int qemudInitPaths(int sys,
             goto snprintf_error;
 
         unlink(sockname);
-    } else {
-        struct passwd *pw;
 
+        base = SYSCONF_DIR;
+    } else {
         if (!(pw = getpwuid(uid))) {
             qemudLog(QEMUD_ERR, "Failed to find user record for uid '%d': %s",
                      uid, strerror(errno));
             return -1;
         }
 
-        if (snprintf(configDir, maxlen, "%s/.libvirt/qemu", pw->pw_dir) >= maxlen)
-            goto snprintf_error;
-
-        if (snprintf(networkConfigDir, maxlen, "%s/.libvirt/qemu/networks", pw->pw_dir) >= maxlen)
-            goto snprintf_error;
-
-        if (snprintf(autostartConfigDir, maxlen, "%s/.libvirt/qemu/autostart", pw->pw_dir) >= maxlen)
+        if (snprintf(sockname, maxlen, "@%s/.libvirt/qemud-sock", pw->pw_dir) >= maxlen)
             goto snprintf_error;
 
-        if (snprintf(autostartNetworkConfigDir, maxlen, "%s/.libvirt/qemu/networks/autostart", pw->pw_dir) >= maxlen)
-            goto snprintf_error;
+        base = pw->pw_dir;
+    }
 
-        if (snprintf(sockname, maxlen, "@%s/.libvirt/qemud-sock", pw->pw_dir) >= maxlen)
+    for (i = 0; i < QEMUD_N_CONFIG_DIRS; i++)
+        if (snprintf(server->configDirs[i], PATH_MAX, "%s/%s", base, paths[i]) >= PATH_MAX)
             goto snprintf_error;
-    }
 
     return 0;
 
@@ -443,9 +432,7 @@ static struct qemud_server *qemudInitial
 
     roSockname[0] = '\0';
 
-    if (qemudInitPaths(sys, server->configDir, server->networkConfigDir,
-                       server->autostartConfigDir, server->autostartNetworkConfigDir,
-                       sockname, roSockname, PATH_MAX) < 0)
+    if (qemudInitPaths(server, sys, sockname, roSockname, PATH_MAX) < 0)
         goto cleanup;
 
     if (qemudListenUnix(server, sockname, 0) < 0)
Index: libvirt/qemud/conf.c
===================================================================
--- libvirt.orig/qemud/conf.c
+++ libvirt/qemud/conf.c
@@ -1137,7 +1137,8 @@ int qemudBuildCommandLine(struct qemud_s
 
 /* Save a guest's config data into a persistent file */
 static int qemudSaveConfig(struct qemud_server *server,
-                           struct qemud_vm *vm) {
+                           struct qemud_vm *vm,
+                           const char *configDir) {
     char *xml;
     int fd = -1, ret = -1;
     int towrite;
@@ -1147,10 +1148,10 @@ static int qemudSaveConfig(struct qemud_
         return -1;
     }
 
-    if ((err = qemudEnsureDir(!vm->autostart ? server->configDir : server->autostartConfigDir))) {
+    if ((err = qemudEnsureDir(configDir))) {
         qemudReportError(server, VIR_ERR_INTERNAL_ERROR,
                          "cannot create config directory %s: %s",
-                         server->configDir, strerror(err));
+                         configDir, strerror(err));
         goto cleanup;
     }
 
@@ -1247,7 +1248,7 @@ struct qemud_vm *qemudLoadConfigXML(stru
         vm->configFile[PATH_MAX-1] = '\0';
     } else {
         if (save) {
-            if (qemudMakeConfigPath(!vm->autostart ? server->configDir : server->autostartConfigDir,
+            if (qemudMakeConfigPath(!vm->autostart ? server->domainsDir : server->autoDomainsDir,
                                     vm->def->name, ".xml", vm->configFile, PATH_MAX) < 0) {
                 qemudReportError(server, VIR_ERR_INTERNAL_ERROR,
                                  "cannot construct config file path");
@@ -1256,7 +1257,8 @@ struct qemud_vm *qemudLoadConfigXML(stru
                 return NULL;
             }
 
-            if (qemudSaveConfig(server, vm) < 0) {
+            if (qemudSaveConfig(server, vm,
+                                !vm->autostart ? server->domainsDir : server->autoDomainsDir) < 0) {
                 if (newVM)
                     qemudFreeVM(vm);
                 return NULL;
@@ -1277,7 +1279,8 @@ struct qemud_vm *qemudLoadConfigXML(stru
 
 
 static int qemudSaveNetworkConfig(struct qemud_server *server,
-                                  struct qemud_network *network) {
+                                  struct qemud_network *network,
+                                  const char *configDir) {
     char *xml;
     int fd, ret = -1;
     int towrite;
@@ -1287,10 +1290,10 @@ static int qemudSaveNetworkConfig(struct
         return -1;
     }
 
-    if ((err = qemudEnsureDir(!network->autostart ? server->networkConfigDir : server->autostartNetworkConfigDir))) {
+    if ((err = qemudEnsureDir(configDir))) {
         qemudReportError(server, VIR_ERR_INTERNAL_ERROR,
                          "cannot create config directory %s: %s",
-                         server->networkConfigDir, strerror(err));
+                         configDir, strerror(err));
         goto cleanup;
     }
 
@@ -1600,7 +1603,7 @@ struct qemud_network *qemudLoadNetworkCo
         network->configFile[PATH_MAX-1] = '\0';
     } else {
         if (save) {
-            if (qemudMakeConfigPath(!network->autostart ? server->networkConfigDir : server->autostartNetworkConfigDir,
+            if (qemudMakeConfigPath(!network->autostart ? server->networksDir : server->autoNetworksDir,
                                     network->def->name, ".xml", network->configFile, PATH_MAX) < 0) {
                 qemudReportError(server, VIR_ERR_INTERNAL_ERROR, "cannot construct config file path");
                 if (newNetwork)
@@ -1608,7 +1611,8 @@ struct qemud_network *qemudLoadNetworkCo
                 return NULL;
             }
 
-            if (qemudSaveNetworkConfig(server, network) < 0) {
+            if (qemudSaveNetworkConfig(server, network,
+                                       !network->autostart ? server->networksDir : server->autoNetworksDir) < 0) {
                 if (newNetwork)
                     qemudFreeNetwork(network);
                 return NULL;
@@ -1703,12 +1707,20 @@ int qemudScanConfigDir(struct qemud_serv
 
 /* Scan for all guest and network config files */
 int qemudScanConfigs(struct qemud_server *server) {
-    if (qemudScanConfigDir(server, server->configDir, 1, 0) < 0 ||
-        qemudScanConfigDir(server, server->autostartConfigDir, 1, 1) < 0)
-        return -1;
-    if (qemudScanConfigDir(server, server->networkConfigDir, 0, 0) < 0 ||
-        qemudScanConfigDir(server, server->autostartNetworkConfigDir, 0, 1) < 0)
-        return -1;
+    int i;
+
+    for (i = 0; i < QEMUD_N_CONFIG_DIRS; i++) {
+        int isGuest = 0, isAutostart = 0;
+
+        if (i == QEMUD_DIR_DOMAINS || i == QEMUD_DIR_AUTO_DOMAINS)
+            isGuest = 1;
+        if (i == QEMUD_DIR_AUTO_DOMAINS || i == QEMUD_DIR_AUTO_NETWORKS)
+            isAutostart = 1;
+
+        if (qemudScanConfigDir(server, server->configDirs[i], isGuest, isAutostart) < 0)
+            return -1;
+    }
+
     return 0;
 }
 

-- 


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]