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

[libvirt] [PATCH] using one function to locate OpenVZ config files



There were 3 places with the same code.
I moved it to separate function.

other:
use O_RDONLY to read config file
Index: src/openvz_conf.c
===================================================================
RCS file: /data/cvs/libvirt/src/openvz_conf.c,v
retrieving revision 1.28
diff -u -a -r1.28 openvz_conf.c
--- src/openvz_conf.c	10 Jul 2008 12:21:09 -0000	1.28
+++ src/openvz_conf.c	10 Jul 2008 16:43:06 -0000
@@ -60,6 +60,7 @@
 static struct openvz_vm_def *openvzParseXML(virConnectPtr conn, xmlDocPtr xml);
 static int openvzGetVPSUUID(int vpsid, char *uuidstr);
 static int openvzSetUUID(int vpsid);
+static int openvzLocateConfFile(int vpsid, char *conffile, int maxlen);
 
 void
 openvzError (virConnectPtr conn, virErrorNumber code, const char *fmt, ...)
@@ -595,20 +596,13 @@
     char conf_file[PATH_MAX] ;
     char line[PATH_MAX] ;
     int ret, found = 0;
-    char * conf_dir;
     int fd ;
     char * sf, * token;
     char *saveptr = NULL;
 
-    conf_dir = openvzLocateConfDir();
-    if (conf_dir == NULL)
-        return -1;
-
-    if (snprintf(conf_file, PATH_MAX, "%s/%d.conf", conf_dir,vpsid) >= PATH_MAX)
+    if (openvzLocateConfFile(vpsid, conf_file, PATH_MAX)<0)
         return -1;
 
-    VIR_FREE(conf_dir);
-
     value[0] = 0;
 
     fd = open(conf_file, O_RDONLY);
@@ -638,6 +632,27 @@
     return ret ;
 }
 
+/* Locate config file of container
+* return -1 - error
+*         0 - OK
+*/
+static int
+openvzLocateConfFile(int vpsid, char *conffile, int maxlen)
+{
+    char * confdir;
+    int ret = 0;
+
+    confdir = openvzLocateConfDir();
+    if (confdir == NULL)
+        return -1;
+
+    if (snprintf(conffile, maxlen, "%s/%d.conf", confdir, vpsid) >= maxlen)
+        ret = -1;
+
+    VIR_FREE(confdir);
+    return ret;
+}
+
 static char
 *openvzLocateConfDir(void)
 {
@@ -686,16 +701,12 @@
     char line[1024];
     char uuidbuf[1024];
     char iden[1024];
-    char *conf_dir;
     int fd, ret;
 
-    conf_dir = openvzLocateConfDir();
-    if (conf_dir == NULL)
-        return -1;
-    sprintf(conf_file, "%s/%d.conf", conf_dir, vpsid);
-    VIR_FREE(conf_dir);
+   if (openvzLocateConfFile(vpsid, conf_file, PATH_MAX)<0)
+       return -1;
 
-    fd = open(conf_file, O_RDWR);
+    fd = open(conf_file, O_RDONLY);
     if(fd == -1)
         return -1;
 
@@ -730,13 +741,9 @@
     char conf_file[PATH_MAX];
     char uuidstr[VIR_UUID_STRING_BUFLEN];
     unsigned char uuid[VIR_UUID_BUFLEN];
-    char *conf_dir;
 
-    conf_dir = openvzLocateConfDir();
-    if (conf_dir == NULL)
-        return -1;
-    sprintf(conf_file, "%s/%d.conf", conf_dir, vpsid);
-    VIR_FREE(conf_dir);
+   if (openvzLocateConfFile(vpsid, conf_file, PATH_MAX)<0)
+       return -1;
 
     if (openvzGetVPSUUID(vpsid, uuidstr))
         return -1;

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