1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* Copyright (C) 2005 Novell, Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU Lesser General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <config.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#ifndef O_BINARY
#define O_BINARY 0
#endif
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <glib.h>
#include <glib/gstdio.h>
#include "e-xml-utils.h"
#ifdef G_OS_WIN32
#define fsync(fd) 0
#endif
xmlDocPtr
e_xml_parse_file (const char *filename)
{
xmlDocPtr result = NULL;
#if GLIB_CHECK_VERSION (2, 8, 0)
GMappedFile *mapped_file;
mapped_file = g_mapped_file_new (filename, FALSE, NULL);
if (mapped_file) {
result = xmlParseMemory (g_mapped_file_get_contents (mapped_file),
g_mapped_file_get_length (mapped_file));
g_mapped_file_free (mapped_file);
}
#else
result = xmlParseFile (filename);
#endif
return result;
}
int
e_xml_save_file (const char *filename, xmlDocPtr doc)
{
char *filesave, *xmlbuf;
size_t n, written = 0;
int ret, fd, size;
int errnosave;
ssize_t w;
gchar *dirname = g_path_get_dirname (filename);
gchar *basename = g_path_get_basename (filename);
gchar *savebasename = g_strconcat (".#", basename, NULL);
g_free (basename);
filesave = g_build_filename (dirname, savebasename, NULL);
g_free (savebasename);
g_free (dirname);
fd = g_open (filesave, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0600);
if (fd == -1) {
g_free (filesave);
return -1;
}
xmlDocDumpFormatMemory (doc, (xmlChar **) &xmlbuf, &size, TRUE);
if (size <= 0) {
close (fd);
g_unlink (filesave);
g_free (filesave);
errno = ENOMEM;
return -1;
}
n = (size_t) size;
do {
do {
w = write (fd, xmlbuf + written, n - written);
} while (w == -1 && errno == EINTR);
if (w > 0)
written += w;
} while (w != -1 && written < n);
xmlFree (xmlbuf);
if (written < n || fsync (fd) == -1) {
errnosave = errno;
close (fd);
g_unlink (filesave);
g_free (filesave);
errno = errnosave;
return -1;
}
while ((ret = close (fd)) == -1 && errno == EINTR)
;
if (ret == -1) {
g_free (filesave);
return -1;
}
if (g_rename (filesave, filename) == -1) {
errnosave = errno;
g_unlink (filesave);
g_free (filesave);
errno = errnosave;
return -1;
}
g_free (filesave);
return 0;
}
xmlNode *
e_xml_get_child_by_name (const xmlNode *parent, const xmlChar *child_name)
{
xmlNode *child;
g_return_val_if_fail (parent != NULL, NULL);
g_return_val_if_fail (child_name != NULL, NULL);
for (child = parent->xmlChildrenNode; child != NULL; child = child->next) {
if (xmlStrcmp (child->name, child_name) == 0) {
return child;
}
}
return NULL;
}
|