Fix opening of ZIP files of certificates on Windows (#1124).
authorCarl Hetherington <cth@carlh.net>
Tue, 2 Jan 2018 15:07:29 +0000 (15:07 +0000)
committerCarl Hetherington <cth@carlh.net>
Tue, 2 Jan 2018 17:37:36 +0000 (17:37 +0000)
src/lib/internet.cc
src/lib/scoped_temporary.cc

index aafdf3a839a342b99944c2c20bdd14a9c9c56b82..1e8c5d8e9e19dcdb2c4af155ca29ecdfec3c781c 100644 (file)
@@ -21,6 +21,7 @@
 #include "scoped_temporary.h"
 #include "compose.hpp"
 #include "exceptions.h"
+#include "cross.h"
 #include <curl/curl.h>
 #include <zip.h>
 #include <boost/function.hpp>
@@ -78,13 +79,23 @@ get_from_zip_url (string url, string file, bool pasv, function<void (boost::file
 
        /* Open the ZIP file and read `file' out of it */
 
-       struct zip* zip = zip_open (temp_zip.c_str(), 0, 0);
+       FILE* zip_file = fopen_boost (temp_zip.file (), "rb");
+       if (!zip_file) {
+               return optional<string> (_("Could not open downloaded ZIP file"));
+       }
+
+       zip_source_t* zip_source = zip_source_filep_create (zip_file, 0, -1, 0);
+       if (!zip_source) {
+               return optional<string> (_("Could not open downloaded ZIP file"));
+       }
+
+       zip_t* zip = zip_open_from_source (zip_source, 0, 0);
        if (!zip) {
                return optional<string> (_("Could not open downloaded ZIP file"));
        }
 
-       struct zip_file* zip_file = zip_fopen (zip, file.c_str(), 0);
-       if (!zip_file) {
+       struct zip_file* file_in_zip = zip_fopen (zip, file.c_str(), 0);
+       if (!file_in_zip) {
                return optional<string> (_("Unexpected ZIP file contents"));
        }
 
@@ -92,12 +103,14 @@ get_from_zip_url (string url, string file, bool pasv, function<void (boost::file
        f = temp_cert.open ("wb");
        char buffer[4096];
        while (true) {
-               int const N = zip_fread (zip_file, buffer, sizeof (buffer));
+               int const N = zip_fread (file_in_zip, buffer, sizeof (buffer));
                fwrite (buffer, 1, N, f);
                if (N < int (sizeof (buffer))) {
                        break;
                }
        }
+       zip_fclose (file_in_zip);
+       zip_close (zip);
        temp_cert.close ();
 
        load (temp_cert.file ());
index 4043caf3d28ee91c6224651fa9ca6e1103dc3191..ad4f882a2649ecfadb840e88c66c351dc7b60964 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2012-2018 Carl Hetherington <cth@carlh.net>
 
     This file is part of DCP-o-matic.
 
@@ -19,6 +19,8 @@
 */
 
 #include "scoped_temporary.h"
+#include "exceptions.h"
+#include "cross.h"
 
 /** Construct a ScopedTemporary.  A temporary filename is decided but the file is not opened
  *  until open() is called.
@@ -51,7 +53,10 @@ FILE*
 ScopedTemporary::open (char const * params)
 {
        close ();
-       _open = fopen (c_str(), params);
+       _open = fopen_boost (_file, params);
+       if (!_open) {
+               throw FileError ("Could not open scoped temporary", _file);
+       }
        return _open;
 }