| 1 |
/* |
|---|
| 2 |
* Copyright (c) 2007, OmniTI Computer Consulting, Inc. |
|---|
| 3 |
* All rights reserved. |
|---|
| 4 |
* |
|---|
| 5 |
* Redistribution and use in source and binary forms, with or without |
|---|
| 6 |
* modification, are permitted provided that the following conditions are |
|---|
| 7 |
* met: |
|---|
| 8 |
* |
|---|
| 9 |
* * Redistributions of source code must retain the above copyright |
|---|
| 10 |
* notice, this list of conditions and the following disclaimer. |
|---|
| 11 |
* * Redistributions in binary form must reproduce the above |
|---|
| 12 |
* copyright notice, this list of conditions and the following |
|---|
| 13 |
* disclaimer in the documentation and/or other materials provided |
|---|
| 14 |
* with the distribution. |
|---|
| 15 |
* * Neither the name OmniTI Computer Consulting, Inc. nor the names |
|---|
| 16 |
* of its contributors may be used to endorse or promote products |
|---|
| 17 |
* derived from this software without specific prior written |
|---|
| 18 |
* permission. |
|---|
| 19 |
* |
|---|
| 20 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 21 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 22 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 23 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 24 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 25 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 26 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 27 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 28 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 29 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 30 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 31 |
*/ |
|---|
| 32 |
|
|---|
| 33 |
#include "noit_defines.h" |
|---|
| 34 |
#include "noit_xml.h" |
|---|
| 35 |
#include "utils/noit_log.h" |
|---|
| 36 |
#include "utils/noit_mkdir.h" |
|---|
| 37 |
#include <fcntl.h> |
|---|
| 38 |
#include <unistd.h> |
|---|
| 39 |
#include <errno.h> |
|---|
| 40 |
#include <assert.h> |
|---|
| 41 |
|
|---|
| 42 |
struct noit_xml_buffer_ptr { |
|---|
| 43 |
char *buff; |
|---|
| 44 |
int raw_len; |
|---|
| 45 |
int len; |
|---|
| 46 |
int allocd; |
|---|
| 47 |
}; |
|---|
| 48 |
static int |
|---|
| 49 |
noit_xml_save_writer(void *vstr, const char *buffer, int len) { |
|---|
| 50 |
struct noit_xml_buffer_ptr *clv = vstr; |
|---|
| 51 |
if(!clv->buff) { |
|---|
| 52 |
clv->allocd = 8192; |
|---|
| 53 |
clv->buff = malloc(clv->allocd); |
|---|
| 54 |
} |
|---|
| 55 |
while(len + clv->len > clv->allocd) { |
|---|
| 56 |
char *newbuff; |
|---|
| 57 |
int newsize = clv->allocd; |
|---|
| 58 |
newsize <<= 1; |
|---|
| 59 |
newbuff = realloc(clv->buff, newsize); |
|---|
| 60 |
if(!newbuff) { |
|---|
| 61 |
return -1; |
|---|
| 62 |
} |
|---|
| 63 |
clv->allocd = newsize; |
|---|
| 64 |
clv->buff = newbuff; |
|---|
| 65 |
} |
|---|
| 66 |
memcpy(clv->buff + clv->len, buffer, len); |
|---|
| 67 |
clv->len += len; |
|---|
| 68 |
return len; |
|---|
| 69 |
} |
|---|
| 70 |
static int |
|---|
| 71 |
noit_xml_save_closer(void *vstr) { |
|---|
| 72 |
struct noit_xml_buffer_ptr *clv = vstr; |
|---|
| 73 |
if(clv->buff == NULL) return 0; |
|---|
| 74 |
clv->buff[clv->len] = '\0'; |
|---|
| 75 |
return 0; |
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
char * |
|---|
| 79 |
noit_xmlSaveToBuffer(xmlDocPtr doc) { |
|---|
| 80 |
xmlOutputBufferPtr out; |
|---|
| 81 |
xmlCharEncodingHandlerPtr enc; |
|---|
| 82 |
struct noit_xml_buffer_ptr buf = { NULL,0,0,0 }; |
|---|
| 83 |
|
|---|
| 84 |
enc = xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF8); |
|---|
| 85 |
out = xmlOutputBufferCreateIO(noit_xml_save_writer, |
|---|
| 86 |
noit_xml_save_closer, |
|---|
| 87 |
&buf, enc); |
|---|
| 88 |
assert(out); |
|---|
| 89 |
xmlSaveFormatFileTo(out, doc, "utf8", 1); |
|---|
| 90 |
return buf.buff; |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
int |
|---|
| 94 |
noit_xmlSaveToFile(xmlDocPtr doc, const char *filename) { |
|---|
| 95 |
int rv = -1, fd, have_backup = 0, once; |
|---|
| 96 |
char tmpfile[PATH_MAX], bakfile[PATH_MAX]; |
|---|
| 97 |
xmlOutputBufferPtr out; |
|---|
| 98 |
xmlCharEncodingHandlerPtr enc; |
|---|
| 99 |
|
|---|
| 100 |
snprintf(tmpfile, sizeof(tmpfile), "%s.tmp", filename); |
|---|
| 101 |
snprintf(bakfile, sizeof(bakfile), "%s.bak", filename); |
|---|
| 102 |
once = 1; |
|---|
| 103 |
mkbkup: |
|---|
| 104 |
if(link(filename, bakfile) == 0) { |
|---|
| 105 |
unlink(filename); |
|---|
| 106 |
have_backup = 1; |
|---|
| 107 |
} |
|---|
| 108 |
else if (errno != ENOENT) { |
|---|
| 109 |
if(!once) return -1; |
|---|
| 110 |
once = 0; |
|---|
| 111 |
if(errno == EEXIST) { |
|---|
| 112 |
unlink(bakfile); |
|---|
| 113 |
goto mkbkup; |
|---|
| 114 |
} |
|---|
| 115 |
noitL(noit_error, "Cannot create backup for %s: %s\n", |
|---|
| 116 |
filename, strerror(errno)); |
|---|
| 117 |
return -1; |
|---|
| 118 |
} |
|---|
| 119 |
else { |
|---|
| 120 |
noitL(noit_debug, "link(%s,%s) -> %s\n", filename, bakfile, strerror(errno)); |
|---|
| 121 |
} |
|---|
| 122 |
|
|---|
| 123 |
retry: |
|---|
| 124 |
fd = open(tmpfile, O_WRONLY | O_CREAT | O_EXCL, 0640); |
|---|
| 125 |
if(fd < 0) { |
|---|
| 126 |
if(!once) return -1; |
|---|
| 127 |
once = 0; |
|---|
| 128 |
if(errno == ENOENT) { |
|---|
| 129 |
if(mkdir_for_file(tmpfile, 0750) == 0) goto retry; |
|---|
| 130 |
} |
|---|
| 131 |
if(errno == EEXIST) { |
|---|
| 132 |
unlink(tmpfile); |
|---|
| 133 |
goto retry; |
|---|
| 134 |
} |
|---|
| 135 |
return -1; |
|---|
| 136 |
} |
|---|
| 137 |
enc = xmlGetCharEncodingHandler(XML_CHAR_ENCODING_UTF8); |
|---|
| 138 |
out = xmlOutputBufferCreateFd(fd, enc); |
|---|
| 139 |
assert(out); |
|---|
| 140 |
xmlSaveFormatFileTo(out, doc, "utf8", 1); |
|---|
| 141 |
close(fd); |
|---|
| 142 |
rv = 0; |
|---|
| 143 |
if(link(tmpfile, filename)) { |
|---|
| 144 |
rv = -1; |
|---|
| 145 |
if(link(bakfile, filename) != 0) have_backup = 0; |
|---|
| 146 |
} |
|---|
| 147 |
unlink(tmpfile); |
|---|
| 148 |
if(have_backup) |
|---|
| 149 |
unlink(bakfile); |
|---|
| 150 |
return rv; |
|---|
| 151 |
} |
|---|