16
16
#include " Platform.h"
17
17
#include " utils/StringUtils.h"
18
18
#include " transforms/FileTransform.h"
19
+ #include " Logging.h"
19
20
20
21
#include " OCIOZArchive.h"
21
22
@@ -183,6 +184,9 @@ void addSupportedFiles(void * archiver, const char * path, const char * configWo
183
184
if (!possibleFormats.empty ())
184
185
{
185
186
// The extension is supported. Add the current file to the OCIOZ archive.
187
+ std::ostringstream logMessage;
188
+ logMessage << " Adding file: " << absPath;
189
+ LogInfo (logMessage.str ());
186
190
if (mz_zip_writer_add_path (
187
191
archiver, absPath.c_str (),
188
192
configWorkingDirectory, 0 , 1 ) != MZ_OK)
@@ -201,9 +205,9 @@ void addSupportedFiles(void * archiver, const char * path, const char * configWo
201
205
}
202
206
}
203
207
204
- void addReferencedFiles (void * archiver, const Config & config)
208
+ ContextRcPtr addReferencedFiles (void * archiver, const ConfigRcPtr & config)
205
209
{
206
- ConstContextRcPtr context = config. getCurrentContext ();
210
+ ConstContextRcPtr context = config-> getCurrentContext ();
207
211
ContextRcPtr ctxFilepath = Context::Create ();
208
212
ctxFilepath->setSearchPath (context->getSearchPath ());
209
213
ctxFilepath->setWorkingDir (context->getWorkingDir ());
@@ -212,20 +216,42 @@ void addReferencedFiles(void * archiver, const Config & config)
212
216
auto prefixLength = std::string (context->getWorkingDir ()).length () + 1 ; // +1 add trailing '/' TODO: improve this
213
217
214
218
std::set<std::string> files;
215
- config.GetAllFileReferences (files);
219
+ config->GetAllFileReferences (files);
220
+ std::set<std::string> added_files;
216
221
for (const auto &file : files)
217
222
{
218
223
const std::string resolvedPath = context->resolveFileLocation (file.c_str (), ctxFilepath);
219
224
const std::string relativePath = resolvedPath.substr (prefixLength);
225
+ std::ostringstream logMessage;
220
226
221
- auto returnCode = mz_zip_writer_add_file (archiver, resolvedPath.c_str (), relativePath.c_str ());
222
- if (returnCode != MZ_OK)
227
+ if (added_files.find (relativePath) == added_files.end ())
223
228
{
224
- std::ostringstream os;
225
- os << " Could not write file " << resolvedPath << " to in-memory archive.()" << returnCode << " )" ;
226
- throw Exception (os.str ().c_str ());
229
+ logMessage << " Adding file: " << file << " -> " << relativePath;
230
+ LogInfo (logMessage.str ());
231
+ auto returnCode = mz_zip_writer_add_file (archiver, resolvedPath.c_str (), relativePath.c_str ());
232
+ if (returnCode != MZ_OK)
233
+ {
234
+ std::ostringstream os;
235
+ os << " Could not write file " << resolvedPath << " to in-memory archive.()" << returnCode << " )" ;
236
+ throw Exception (os.str ().c_str ());
237
+ }
227
238
}
239
+ else
240
+ {
241
+ logMessage << " Skipping already added file: " << file << " -> " << relativePath;
242
+ LogInfo (logMessage.str ());
243
+ }
244
+ added_files.insert (relativePath);
228
245
}
246
+
247
+ const auto variableCount = ctxFilepath->getNumStringVars ();
248
+ for (auto i = 0 ; i != variableCount; ++i)
249
+ {
250
+ std::ostringstream logMessage;
251
+ logMessage << " Used Variable: " << ctxFilepath->getStringVarNameByIndex (i) << " -> " << ctxFilepath->getStringVarByIndex (i);
252
+ LogInfo (logMessage.str ());
253
+ }
254
+ return ctxFilepath;
229
255
}
230
256
231
257
// ////////////////////////////////////////////////////////////////////////////////////
@@ -250,8 +276,9 @@ void archiveConfig(std::ostream & ostream, const Config & config, const char * c
250
276
mz_zip_file file_info;
251
277
252
278
flags = EnvironmentOverride (flags);
279
+ const bool minimal = HasFlag (flags, ARCHIVE_FLAGS_MINIMAL);
253
280
254
- if (!config.isArchivable ()) // TODO: pass in flags?
281
+ if (!config.isArchivable (minimal )) // TODO: pass in flags?
255
282
{
256
283
std::ostringstream os;
257
284
os << " Config is not archivable." ;
@@ -261,11 +288,6 @@ void archiveConfig(std::ostream & ostream, const Config & config, const char * c
261
288
// Initialize.
262
289
memset (&file_info, 0 , sizeof (file_info));
263
290
264
- // Retrieve and store the config as string.
265
- std::stringstream ss;
266
- config.serialize (ss);
267
- std::string configStr = ss.str ();
268
-
269
291
// Write zip to memory stream.
270
292
#if MZ_VERSION_BUILD >= 040000
271
293
write_mem_stream = mz_stream_mem_create ();
@@ -303,10 +325,38 @@ void archiveConfig(std::ostream & ostream, const Config & config, const char * c
303
325
// Open the in-memory zip.
304
326
if (mz_zip_writer_open (archiver, write_mem_stream, 0 ) == MZ_OK)
305
327
{
328
+ ConfigRcPtr editiableConfig = config.createEditableCopy (); // TODO: is this a little heavy handed just so we can modify the envronment?
329
+ // /////////////////////
330
+ // Adding LUT files //
331
+ // /////////////////////
332
+ if (minimal)
333
+ {
334
+ ContextRcPtr archivedContext = addReferencedFiles (archiver, editiableConfig);
335
+
336
+ // Need to make sure the used evironment context is in the config
337
+ const auto variableCount = archivedContext->getNumStringVars ();
338
+ for (auto i = 0 ; i != variableCount; ++i)
339
+ {
340
+ editiableConfig->addEnvironmentVar (archivedContext->getStringVarNameByIndex (i), archivedContext->getStringVarByIndex (i));
341
+ }
342
+ }
343
+ else
344
+ {
345
+ // Add all supported files to in-memory zip from any directories under working directory.
346
+ // (recursive)
347
+ addSupportedFiles (archiver, configWorkingDirectory, configWorkingDirectory);
348
+ }
349
+
350
+ // ////////////////////////////
351
+ // Adding config to archive //
352
+ // ////////////////////////////
306
353
// Use a hardcoded name for the config's filename inside the archive.
307
354
std::string configFullname = std::string (OCIO_CONFIG_DEFAULT_NAME) +
308
355
std::string (OCIO_CONFIG_DEFAULT_FILE_EXT);
309
356
357
+ std::stringstream ss;
358
+ editiableConfig->serialize (ss);
359
+ std::string configStr = ss.str ();
310
360
// Get config string size.
311
361
int32_t configSize = (int32_t ) configStr.size ();
312
362
@@ -316,11 +366,13 @@ void archiveConfig(std::ostream & ostream, const Config & config, const char * c
316
366
file_info.version_madeby = MZ_VERSION_MADEBY;
317
367
file_info.compression_method = MZ_COMPRESS_METHOD_DEFLATE;
318
368
file_info.flag = MZ_ZIP_FLAG_UTF8;
319
- file_info.uncompressed_size = configSize;
369
+ file_info.uncompressed_size = configSize; // Retrieve and store the config as string.
370
+
371
+ constexpr uint32_t posix_attrib = 0100644 ; // File with -rw-r--r-- permissions
372
+ constexpr uint32_t msdos_attrib = 0200 ; // MSDOS equivalent
373
+ file_info.external_fa = msdos_attrib;
374
+ file_info.external_fa |= (posix_attrib << 16 );
320
375
321
- // ////////////////////////////
322
- // Adding config to archive //
323
- // ////////////////////////////
324
376
int32_t written = 0 ;
325
377
// Opens an entry in the in-memory zip file for writing.
326
378
if (mz_zip_writer_entry_open (archiver, &file_info) == MZ_OK)
@@ -343,16 +395,6 @@ void archiveConfig(std::ostream & ostream, const Config & config, const char * c
343
395
throw Exception (os.str ().c_str ());
344
396
}
345
397
346
- // /////////////////////
347
- // Adding LUT files //
348
- // /////////////////////
349
- // Add all supported files to in-memory zip from any directories under working directory.
350
- // (recursive)
351
- if (HasFlag (flags, ARCHIVE_FLAGS_MINIMAL))
352
- addReferencedFiles (archiver, config);
353
- else
354
- addSupportedFiles (archiver, configWorkingDirectory, configWorkingDirectory);
355
-
356
398
std::ostringstream comment;
357
399
comment << " Configuration written by archiveConfig() OCIO: " << GetVersion ();
358
400
mz_zip_writer_set_comment (archiver, comment.str ().c_str ());
0 commit comments