[Libvir] [PATCH] Rewrite openvzSetUUID.

Jim Meyering jim at meyering.net
Wed Feb 20 22:20:48 UTC 2008


I wrote:
...
> diff --git a/src/openvz_conf.c b/src/openvz_conf.c
...
> +	FILE *fp = fopen(conf_file, "a");
> +	if (fp == NULL)
> +	  return -1;
...
> +	/* Record failure if fseek, fprintf or fclose fails,
> +	   and be careful always to close the stream.  */
> +	if (fseek(fp, 0, SEEK_END) < 0) {
> +	    fclose(fp);
> +	    return -1;
> +	}

My thinking cap was not screwed on well enough :-)
The whole point of my using fopen's "a" (append) flag
was so that I wouldn't have to seek at all.
So here's that same patch without the useless fseek:

diff --git a/src/openvz_conf.c b/src/openvz_conf.c
index a274223..2e7b153 100644
--- a/src/openvz_conf.c
+++ b/src/openvz_conf.c
@@ -680,7 +680,6 @@ openvzSetUUID(int vpsid)
     char uuidstr[VIR_UUID_STRING_BUFLEN];
     unsigned char uuid[VIR_UUID_BUFLEN];
     char *conf_dir;
-    int fd, ret;

     conf_dir = openvzLocateConfDir();
     if (conf_dir == NULL)
@@ -688,23 +687,22 @@ openvzSetUUID(int vpsid)
     sprintf(conf_file, "%s/%d.conf", conf_dir, vpsid);
     free(conf_dir);

-    fd = open(conf_file, O_RDWR);
-    if(fd == -1)
+    if (openvzGetVPSUUID(vpsid, uuidstr))
         return -1;

-    ret = openvzGetVPSUUID(vpsid, uuidstr);
-    if(ret == -1)
-        return -1;
+    if (uuidstr[0] == 0) {
+	FILE *fp = fopen(conf_file, "a"); /* append */
+	if (fp == NULL)
+	  return -1;

-    if(uuidstr[0] == 0) {
         virUUIDGenerate(uuid);
         virUUIDFormat(uuid, uuidstr);

-        lseek(fd, 0, SEEK_END);
-        write(fd, "\n#UUID: ", 8);
-        write(fd, uuidstr, strlen(uuidstr));
-        write(fd, "\n", 1);
-        close(fd);
+	/* Record failure if fprintf or fclose fails,
+	   and be careful always to close the stream.  */
+	if ((fprintf(fp, "\n#UUID: %s\n", uuidstr) < 0)
+	    + (fclose(fp) == EOF))
+	    return -1;
     }

     return 0;
--
1.5.4.2.157.g733a68




More information about the libvir-list mailing list