[Ocfs2-tools-commits] jlbec commits r1051 - trunk/o2cb_ctl
svn-commits at oss.oracle.com
svn-commits at oss.oracle.com
Wed Aug 17 21:22:35 CDT 2005
Author: jlbec
Date: 2005-08-17 21:22:33 -0500 (Wed, 17 Aug 2005)
New Revision: 1051
Modified:
trunk/o2cb_ctl/o2cb_config.c
Log:
o Write cluster.conf to a temporary and rename(2).
Modified: trunk/o2cb_ctl/o2cb_config.c
===================================================================
--- trunk/o2cb_ctl/o2cb_config.c 2005-08-18 00:35:09 UTC (rev 1050)
+++ trunk/o2cb_ctl/o2cb_config.c 2005-08-18 02:22:33 UTC (rev 1051)
@@ -23,6 +23,7 @@
#include <string.h>
#include <stdlib.h>
+#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
@@ -339,10 +340,86 @@
return rc;
}
+static gint write_file(const gchar *text, const gchar *filename)
+{
+ int rc, fd;
+ GString *template;
+ FILE *file;
+ size_t written, len;
+ rc = mkdir("/etc/ocfs2", 0755);
+ if (rc)
+ {
+ rc = -errno;
+ if (rc == -EEXIST)
+ rc = 0;
+ else
+ goto out;
+ }
+
+ rc = -ENOMEM;
+ template = g_string_new(filename);
+ if (!template)
+ goto out;
+
+ g_string_append(template, "XXXXXX");
+ fd = mkstemp(template->str);
+ rc = -errno;
+ if (fd < 0)
+ goto out_free;
+
+ file = fdopen(fd, "w");
+ if (!file)
+ {
+ rc = -errno;
+ close(fd);
+ goto out_unlink;
+ }
+
+ len = strlen(text);
+ written = fwrite(text, sizeof(char), len, file);
+ if (written != len)
+ {
+ if (feof(file))
+ rc = -EIO;
+ else if (ferror(file))
+ rc = -errno;
+ else
+ rc = -EIO;
+
+ fclose(file);
+ goto out_unlink;
+ }
+
+ fchmod(fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+
+ rc = fclose(file);
+ if (rc)
+ {
+ rc = -errno;
+ goto out_unlink;
+ }
+
+ rc = rename(template->str, filename);
+ if (rc)
+ rc = -errno;
+
+out_unlink:
+ if (rc)
+ unlink(template->str);
+
+out_free:
+ g_string_free(template, TRUE);
+
+out:
+ return rc;
+}
+
+
gint o2cb_config_store(O2CBConfig *config, const gchar *filename)
{
int rc;
+ char *text;
JConfig *cf;
O2CBCluster *cluster;
GList *list;
@@ -358,24 +435,20 @@
cluster = (O2CBCluster *)list->data;
rc = o2cb_cluster_store(cf, cluster);
if (rc)
- break;
+ goto out;
list = list->next;
}
- if (!rc)
- {
- rc = mkdir("/etc/ocfs2", 0755);
- if (rc)
- {
- rc = -errno;
- if (rc == -EEXIST)
- rc = 0;
- }
- if (!rc && !j_config_dump_file(cf, filename))
- rc = -EIO;
- }
+ rc = -ENOMEM;
+ text = j_config_dump_memory(cf);
+ if (!text)
+ goto out;
+ rc = write_file(text, filename);
+ g_free(text);
+
+out:
return rc;
} /* o2cb_config_store() */
More information about the Ocfs2-tools-commits
mailing list