diff options
| author | Carl Hetherington <cth@carlh.net> | 2018-01-02 15:07:29 +0000 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2018-01-02 17:37:36 +0000 |
| commit | c9548bb8272ffe5804085f0a7a9f19305f0d513b (patch) | |
| tree | eb5ef3e103de07c357539d4aa2ee31c9700f21c4 /src/lib | |
| parent | 356e2d767c4a3dc65869dc1d75476a94e2e2ba07 (diff) | |
Fix opening of ZIP files of certificates on Windows (#1124).
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/internet.cc | 21 | ||||
| -rw-r--r-- | src/lib/scoped_temporary.cc | 9 |
2 files changed, 24 insertions, 6 deletions
diff --git a/src/lib/internet.cc b/src/lib/internet.cc index aafdf3a83..1e8c5d8e9 100644 --- a/src/lib/internet.cc +++ b/src/lib/internet.cc @@ -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 ()); diff --git a/src/lib/scoped_temporary.cc b/src/lib/scoped_temporary.cc index 4043caf3d..ad4f882a2 100644 --- a/src/lib/scoped_temporary.cc +++ b/src/lib/scoped_temporary.cc @@ -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; } |
