25
25
#include " llvm/ADT/StringRef.h"
26
26
#include " llvm/Support/Error.h"
27
27
28
+ #include < fstream>
28
29
#include < string_view>
29
30
#include < zip.h>
30
31
@@ -98,12 +99,14 @@ llvm::Error PatchableZipPayload::ensureOpen() {
98
99
retVal = extractLibZipError (
99
100
" Failure while opening in memory circuit module (zip) " , zipError);
100
101
}
102
+ inMemoryZipSource = zs;
101
103
} else {
102
104
if ((zip = zip_open (path.c_str (), 0 , &errorCode)) == nullptr ) {
103
105
zip_error_set (&zipError, errorCode, errno);
104
106
retVal = extractLibZipError (
105
107
" Failure while opening circuit module (zip) file " , zipError);
106
108
}
109
+ inMemoryZipSource = nullptr ;
107
110
}
108
111
109
112
zip_error_fini (&zipError);
@@ -156,6 +159,9 @@ llvm::Error PatchableZipPayload::writeBack() {
156
159
157
160
zip_error_fini (&err);
158
161
162
+ if (inMemoryZipSource)
163
+ zip_source_keep (inMemoryZipSource);
164
+
159
165
if (zip_close (zip)) {
160
166
auto *err = zip_get_error (zip);
161
167
return extractLibZipError (" writing payload file" , *err);
@@ -174,68 +180,25 @@ llvm::Error PatchableZipPayload::writeString(std::string *outputString) {
174
180
outStringStream.emplace (*outputString);
175
181
llvm::raw_ostream *ostream = outStringStream.getPointer ();
176
182
177
- // load all files in zip if required
178
-
179
- auto numEntries = zip_get_num_entries (zip, 0 );
180
- zip_stat_t zs;
181
- assert (numEntries >= 0 );
182
- zip_stat_init (&zs);
183
- for (ssize_t i = 0 ; i < numEntries; i++) {
184
- zip_stat_index (zip, i, 0 , &zs);
185
- llvm::StringRef name (zs.name );
186
- auto binaryDataOrErr = readMember (name);
187
-
188
- if (!binaryDataOrErr)
189
- return binaryDataOrErr.takeError ();
190
- }
191
-
192
- zip_source_t *new_zip_src;
193
- zip_t *new_zip;
194
-
195
- zip_error_t err;
196
-
197
- zip_error_init (&err);
198
-
199
- // open a zip source, buffer is allocated internally to libzip
200
- if ((new_zip_src = zip_source_buffer_create (nullptr , 0 , 0 , &err)) == nullptr )
201
- return extractLibZipError (" Can't create zip source for new archive" , err);
202
-
203
- zip_source_keep (new_zip_src);
204
-
205
- if ((new_zip = zip_open_from_source (new_zip_src, ZIP_TRUNCATE, &err)) ==
206
- nullptr ) {
207
- zip_source_free (new_zip_src);
208
- return extractLibZipError (
209
- " Can't create/open an archive from the new archive source:" , err);
210
- }
211
-
212
- for (auto &item : files) {
213
- auto &path = item.first ;
214
- auto &buf = item.second .buf ;
215
-
216
- auto error = addFileToZip (new_zip, path, buf, err);
217
- if (error) {
218
- zip_source_free (new_zip_src);
219
- return error;
183
+ if (inMemoryZipSource) {
184
+ // read from in memory source
185
+ zip_int64_t sz;
186
+ char *outbuffer =
187
+ qssc::payload::read_zip_src_to_buffer (inMemoryZipSource, sz);
188
+ if (outbuffer) {
189
+ ostream->write (outbuffer, sz);
190
+ free (outbuffer);
220
191
}
192
+ zip_source_free (inMemoryZipSource);
193
+ inMemoryZipSource = nullptr ;
194
+ } else {
195
+ // re-read file from disk
196
+ std::ostringstream buf;
197
+ std::ifstream input (path.c_str ());
198
+ buf << input.rdbuf ();
199
+ ostream->write (buf.str ().c_str (), buf.str ().length ());
221
200
}
222
-
223
- zip_error_fini (&err);
224
-
225
- if (zip_close (new_zip)) {
226
- auto *err = zip_get_error (new_zip);
227
- return extractLibZipError (" Closing in memory zip" , *err);
228
- }
229
-
230
- // ===---- Reopen for copying ----===//
231
- zip_int64_t sz;
232
- char *outbuffer = qssc::payload::read_zip_src_to_buffer (new_zip_src, sz);
233
- if (outbuffer) {
234
- // output the new archive to the stream
235
- ostream->write (outbuffer, sz);
236
- ostream->flush ();
237
- free (outbuffer);
238
- }
201
+ ostream->flush ();
239
202
return llvm::Error::success ();
240
203
}
241
204
0 commit comments