]> wimlib.net Git - wimlib/blobdiff - src/xml.c
filedes_t => int
[wimlib] / src / xml.c
index ff37757f64972a052e7634eb8a9b07488d2ba746..da47b28ea3815841e386f6545dd10578d01154ca 100644 (file)
--- a/src/xml.c
+++ b/src/xml.c
@@ -1236,8 +1236,9 @@ libxml_global_cleanup()
  * Reads the XML data from a WIM file.
  */
 int
-read_xml_data(FILE *fp, const struct resource_entry *res_entry,
-             utf16lechar **xml_data_ret, struct wim_info **info_ret)
+read_xml_data(int in_fd,
+             const struct resource_entry *res_entry,
+             struct wim_info **info_ret)
 {
        utf16lechar *xml_data;
        xmlDoc *doc;
@@ -1265,10 +1266,13 @@ read_xml_data(FILE *fp, const struct resource_entry *res_entry,
                goto out;
        }
 
-       ret = read_uncompressed_resource(fp, res_entry->offset,
-                                        res_entry->size, xml_data);
-       if (ret != 0)
+       if (full_pread(in_fd, xml_data,
+                      res_entry->size, res_entry->offset) != res_entry->size)
+       {
+               ERROR_WITH_ERRNO("Error reading XML data");
+               ret = WIMLIB_ERR_READ;
                goto out_free_xml_data;
+       }
 
        /* Null-terminate just in case */
        ((u8*)xml_data)[res_entry->size] = 0;
@@ -1300,13 +1304,7 @@ read_xml_data(FILE *fp, const struct resource_entry *res_entry,
                ret = WIMLIB_ERR_XML;
                goto out_free_doc;
        }
-
        ret = xml_read_wim_info(root, info_ret);
-       if (ret != 0)
-               goto out_free_doc;
-
-       *xml_data_ret = xml_data;
-       xml_data = NULL;
 out_free_doc:
        DEBUG("Freeing XML tree.");
        xmlFreeDoc(doc);
@@ -1330,7 +1328,7 @@ out:
  * the offset of the XML data.
  */
 int
-write_xml_data(const struct wim_info *wim_info, int image, FILE *out,
+write_xml_data(const struct wim_info *wim_info, int image, int out_fd,
               u64 total_bytes, struct resource_entry *out_res_entry)
 {
        xmlCharEncodingHandler *encoding_handler;
@@ -1344,7 +1342,7 @@ write_xml_data(const struct wim_info *wim_info, int image, FILE *out,
                        (wim_info != NULL && image >= 1 &&
                         image <= wim_info->num_images));
 
-       start_offset = ftello(out);
+       start_offset = filedes_offset(out_fd);
        if (start_offset == -1)
                return WIMLIB_ERR_WRITE;
 
@@ -1353,7 +1351,8 @@ write_xml_data(const struct wim_info *wim_info, int image, FILE *out,
 
        /* 2 bytes endianness marker for UTF-16LE.  This is _required_ for WIM
         * XML data. */
-       if ((putc(0xff, out)) == EOF || (putc(0xfe, out) == EOF)) {
+       static u8 bom[2] = {0xff, 0xfe};
+       if (full_write(out_fd, bom, 2) != 2) {
                ERROR_WITH_ERRNO("Error writing XML data");
                return WIMLIB_ERR_WRITE;
        }
@@ -1379,7 +1378,7 @@ write_xml_data(const struct wim_info *wim_info, int image, FILE *out,
                goto out;
        }
 
-       out_buffer = xmlOutputBufferCreateFile(out, encoding_handler);
+       out_buffer = xmlOutputBufferCreateFd(out_fd, encoding_handler);
        if (!out_buffer) {
                ERROR("Failed to allocate xmlOutputBuffer");
                ret = WIMLIB_ERR_NOMEM;
@@ -1429,12 +1428,7 @@ write_xml_data(const struct wim_info *wim_info, int image, FILE *out,
 
        DEBUG("Ended XML document");
 
-       /* Call xmlFreeTextWriter() before ftello() because the former will
-        * flush the file stream. */
-       xmlFreeTextWriter(writer);
-       writer = NULL;
-
-       end_offset = ftello(out);
+       end_offset = filedes_offset(out_fd);
        if (end_offset == -1) {
                ret = WIMLIB_ERR_WRITE;
        } else {
@@ -1492,16 +1486,38 @@ wimlib_image_name_in_use(const WIMStruct *w, const tchar *name)
 WIMLIBAPI int
 wimlib_extract_xml_data(WIMStruct *w, FILE *fp)
 {
-       size_t bytes_written;
+       size_t size;
+       void *buf;
+       int ret;
 
-       if (!w->xml_data)
-               return WIMLIB_ERR_INVALID_PARAM;
-       bytes_written = fwrite(w->xml_data, 1, w->hdr.xml_res_entry.size, fp);
-       if (bytes_written != w->hdr.xml_res_entry.size) {
+       size = w->hdr.xml_res_entry.size;
+       if (sizeof(size_t) < sizeof(u64))
+               if (size != w->hdr.xml_res_entry.size)
+                       return WIMLIB_ERR_INVALID_PARAM;
+
+       buf = MALLOC(size);
+       if (!buf)
+               return WIMLIB_ERR_NOMEM;
+
+       if (full_pread(w->in_fd,
+                      buf,
+                      w->hdr.xml_res_entry.size,
+                      w->hdr.xml_res_entry.offset) != w->hdr.xml_res_entry.size)
+       {
+               ERROR_WITH_ERRNO("Error reading XML data");
+               ret = WIMLIB_ERR_READ;
+               goto out_free_buf;
+       }
+
+       if (fwrite(buf, 1, size, fp) != size) {
                ERROR_WITH_ERRNO("Failed to extract XML data");
-               return WIMLIB_ERR_WRITE;
+               ret = WIMLIB_ERR_WRITE;
+       } else {
+               ret = 0;
        }
-       return 0;
+out_free_buf:
+       FREE(buf);
+       return ret;
 }
 
 /* Sets the name of an image in the WIM. */