~zorba-coders/zorba/xqdoc_fixes_archive

« back to all changes in this revision

Viewing changes to src/archive_module.xq.src/archive_module.cpp

  • Committer: Matthias Brantner
  • Date: 2012-07-19 22:38:17 UTC
  • Revision ID: matthias.brantner@28msec.com-20120719223817-djfqyx6d17d2lxnq
- reworked the way options are passed and named
- added a way to specify a compression algorithm for each entry in a zip archive (requires special libarchive version)

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
#include <sys/types.h>
33
33
#include "archive.h"
34
34
#include "archive_entry.h"
 
35
#include "config.h"
35
36
 
36
37
#include <sys/timeb.h>
37
38
#ifdef UNIX
231
232
            throwError("ARCH0004", lMsg.str().c_str());
232
233
          }
233
234
        }
234
 
        else if(lAttrName.getLocalName() == "options"){
235
 
          theOptions = lAttr.getStringValue();
 
235
        else if(lAttrName.getLocalName() == "compression"){
 
236
          theCompression = lAttr.getStringValue();
 
237
          std::transform(
 
238
              theCompression.begin(),
 
239
              theCompression.end(),
 
240
              theCompression.begin(), ::toupper);
236
241
        }
237
242
      }
238
243
    }
243
248
  ********************/
244
249
 
245
250
  ArchiveFunction::ArchiveOptions::ArchiveOptions()
246
 
    : theAlgorithm("NONE"),
247
 
      theFormat("ZIP"),
248
 
      theCompressionLevel(4)
 
251
    : theCompression("DEFLATE"),
 
252
      theFormat("ZIP")
249
253
  {}
250
254
 
251
255
  std::string
275
279
  void
276
280
  ArchiveFunction::ArchiveOptions::setValues(struct archive* aArchive)
277
281
  {
278
 
    theAlgorithm = ArchiveFunction::compressionName(archive_compression(aArchive));
 
282
    theCompression = ArchiveFunction::compressionName(
 
283
        archive_compression(aArchive));
279
284
    theFormat = ArchiveFunction::formatName(archive_format(aArchive));
280
 
    theCompressionLevel = archive_compression(aArchive);
281
285
  }
282
286
 
283
287
  void
293
297
      Item lOptionName;
294
298
      lOption.getNodeName(lOptionName);
295
299
 
296
 
      if (lOptionName.getLocalName() == "algorithm")
 
300
      if (lOptionName.getLocalName() == "compression")
297
301
      {
298
 
        theAlgorithm = getAttributeValue(lOption);
 
302
        theCompression = lOption.getStringValue().c_str();
 
303
        std::transform(
 
304
            theCompression.begin(),
 
305
            theCompression.end(),
 
306
            theCompression.begin(), ::toupper);
299
307
      }
300
308
      else if (lOptionName.getLocalName() == "format")
301
309
      {
302
 
        theFormat = getAttributeValue(lOption);
303
 
      }
304
 
      else if (lOptionName.getLocalName() == "compression")
305
 
      {
306
 
        std::string lTmp = getAttributeValue(lOption);
307
 
        theCompressionLevel = atoi(lTmp.c_str());
 
310
        theFormat = lOption.getStringValue().c_str();
 
311
        std::transform(
 
312
            theFormat.begin(),
 
313
            theFormat.end(),
 
314
            theFormat.begin(), ::toupper);
308
315
      }
309
316
    }
310
317
  }
329
336
  void
330
337
  ArchiveFunction::ArchiveCompressor::setOptions(const ArchiveOptions& aOptions)
331
338
  {
 
339
    theOptions = aOptions;
 
340
 
332
341
    int lFormatCode = formatCode(aOptions.getFormat().c_str());
333
342
    int lErr = archive_write_set_format(theArchive, lFormatCode);
334
343
    ArchiveFunction::checkForError(lErr, 0, theArchive);
335
344
 
336
 
    int lCompressionCode = compressionCode(aOptions.getAlgorithm().c_str());
 
345
    int lCompressionCode = compressionCode(aOptions.getCompression().c_str());
337
346
    setArchiveCompression(theArchive, lCompressionCode);
338
347
 
339
348
    lErr = archive_write_open(
551
560
      std::istream* lStream;
552
561
      bool lDeleteStream;
553
562
      uint64_t lFileSize;
554
 
      int lErr;
555
563
      
556
564
      lDeleteStream = getStream(
557
565
          aEntry, aFile, lStream, lFileSize);
564
572
      archive_entry_set_perm(theEntry, 0644);
565
573
      archive_entry_set_size(theEntry, lFileSize);
566
574
 
 
575
      if (theOptions.getFormat() == "ZIP")
 
576
      {
 
577
        int lNextComp;
 
578
        std::string lNextCompString;
 
579
        if (aEntry.getCompression().length() > 0)
 
580
        {
 
581
          lNextCompString = aEntry.getCompression().c_str();
 
582
          lNextComp = compressionCode(lNextCompString);
 
583
#ifndef ZORBA_LIBARCHIVE_HAVE_SET_COMPRESSION
 
584
          std::ostringstream lMsg;
 
585
          lMsg << lNextCompString << ": setting different compression algorithms for each entry is not supported by the used version of libarchive";
 
586
          throwError("ARCH0099", lMsg.str().c_str());
 
587
#endif
 
588
        }
 
589
        else
 
590
        {
 
591
          lNextCompString = theOptions.getCompression();
 
592
          lNextComp = compressionCode(lNextCompString);
 
593
        }
 
594
        if (lNextComp < ZORBA_ARCHIVE_COMPRESSION_DEFLATE)
 
595
        {
 
596
          std::ostringstream lMsg;
 
597
          lMsg << lNextCompString << ": compression algorithm not supported for ZIP format (required: deflate, store)";
 
598
          throwError("ARCH0002", lMsg.str().c_str());
 
599
        }
 
600
 
 
601
#ifdef ZORBA_LIBARCHIVE_HAVE_SET_COMPRESSION
 
602
        setArchiveCompression(theArchive, lNextComp);
 
603
#endif
 
604
      }
 
605
 
567
606
      archive_write_header(theArchive, theEntry);
568
607
 
569
 
      if(strlen(aEntry.getOptions().c_str())>0){
570
 
        lErr = archive_write_set_options(theArchive, aEntry.getOptions().c_str());
571
 
        checkForError(lErr, NULL, theArchive);
572
 
      }
573
 
 
574
608
      char lBuf[ZORBA_ARCHIVE_MAX_READ_BUF];
575
609
      while (lStream->good())
576
610
      {
676
710
    switch (c)
677
711
    {
678
712
      case ARCHIVE_COMPRESSION_NONE: return "NONE";
 
713
      case ZORBA_ARCHIVE_COMPRESSION_DEFLATE: return "DEFLATE";
 
714
      case ZORBA_ARCHIVE_COMPRESSION_STORE: return "STORE";
679
715
      case ARCHIVE_COMPRESSION_GZIP: return "GZIP";
680
716
      case ARCHIVE_COMPRESSION_BZIP2: return "BZIP2";
681
717
      case ARCHIVE_COMPRESSION_COMPRESS: return "COMPRESS";
746
782
    {
747
783
      return ARCHIVE_COMPRESSION_NONE;
748
784
    }
 
785
    if (c == "STORE")
 
786
    {
 
787
      return ZORBA_ARCHIVE_COMPRESSION_STORE;
 
788
    }
 
789
    if (c == "DEFLATE")
 
790
    {
 
791
      return ZORBA_ARCHIVE_COMPRESSION_DEFLATE;
 
792
    }
749
793
    else if (c == "GZIP")
750
794
    {
751
795
      return ARCHIVE_COMPRESSION_GZIP;
781
825
    int lErr = 0;
782
826
    switch (c)
783
827
    {
 
828
#ifdef ZORBA_LIBARCHIVE_HAVE_SET_COMPRESSION
 
829
      case ZORBA_ARCHIVE_COMPRESSION_STORE:
 
830
        lErr = archive_write_zip_set_compression_store(a); break;
 
831
      case ZORBA_ARCHIVE_COMPRESSION_DEFLATE:
 
832
      case ARCHIVE_COMPRESSION_NONE:
 
833
        lErr = archive_write_zip_set_compression_deflate(a); break;
 
834
#else
 
835
      case ZORBA_ARCHIVE_COMPRESSION_STORE:
 
836
        archive_write_set_option(a, "zip", "compression", "store");
 
837
        break;
 
838
      case ZORBA_ARCHIVE_COMPRESSION_DEFLATE:
 
839
        archive_write_set_option(a, "zip", "compression", "deflate");
 
840
        break;
784
841
      case ARCHIVE_COMPRESSION_NONE:
785
842
        lErr = archive_write_set_compression_none(a); break;
 
843
#endif
786
844
      case ARCHIVE_COMPRESSION_GZIP:
787
845
        lErr = archive_write_set_compression_gzip(a); break;
788
846
      case ARCHIVE_COMPRESSION_BZIP2:
1057
1115
 *  or are not in a list (ArchiveEntrySet)
1058
1116
 ******************************************************************************/
1059
1117
  struct archive_entry*
1060
 
    ExtractFunction::ExtractItemSequence::ExtractIterator::lookForHeader(bool aMatch, ArchiveOptions* aOptions)
 
1118
    ExtractFunction::ExtractItemSequence::ExtractIterator::lookForHeader(
 
1119
        bool aMatch,
 
1120
        ArchiveOptions* aOptions)
1061
1121
  {
1062
1122
    struct archive_entry *lEntry = 0;
1063
1123
 
1345
1405
 
1346
1406
    std::string lFormat =
1347
1407
      ArchiveFunction::formatName(archive_format(theArchive));
1348
 
    std::string lAlgorithm =
 
1408
    std::string lCompression =
1349
1409
      ArchiveFunction::compressionName(archive_compression(theArchive));
1350
1410
 
1351
1411
    if (lFormat == "ZIP")
1352
1412
    {
1353
 
      lAlgorithm = "DEFLATE";
 
1413
      lCompression = "DEFLATE";
1354
1414
    }
1355
1415
 
1356
1416
    zorba::Item lUntypedQName = theFactory->createQName(
1363
1423
    zorba::Item lFormatQName = theFactory->createQName(
1364
1424
        ArchiveModule::getModuleURI(), "format");
1365
1425
 
1366
 
    zorba::Item lAlgorithmQName = theFactory->createQName(
1367
 
        ArchiveModule::getModuleURI(), "algorithm");
1368
 
 
1369
 
    zorba::Item lValueQName = theFactory->createQName("", "value"); 
 
1426
    zorba::Item lCompressionQName = theFactory->createQName(
 
1427
        ArchiveModule::getModuleURI(), "compression");
1370
1428
 
1371
1429
    zorba::Item lNoParent;
1372
1430
    aRes = theFactory->createElementNode(
1377
1435
        aRes, lFormatQName, lTmpQName, true, false, NsBindings());
1378
1436
 
1379
1437
    lTmpQName = lUntypedQName;
1380
 
    zorba::Item lAlgorithmItem = theFactory->createElementNode(
1381
 
        aRes, lAlgorithmQName, lTmpQName, true, false, NsBindings());
1382
 
 
1383
 
    zorba::Item lTmpItem = theFactory->createString(lFormat);
1384
 
 
1385
 
    lTmpQName = lUntypedQName;
1386
 
    theFactory->createAttributeNode(
1387
 
        lFormatItem, lValueQName, lTmpQName, lTmpItem);
1388
 
 
1389
 
    lTmpItem = theFactory->createString(lAlgorithm);
1390
 
 
1391
 
    lTmpQName = lUntypedQName;
1392
 
    theFactory->createAttributeNode(
1393
 
        lAlgorithmItem, lValueQName, lTmpQName, lTmpItem);
 
1438
    zorba::Item lCompressionItem = theFactory->createElementNode(
 
1439
        aRes, lCompressionQName, lTmpQName, true, false, NsBindings());
 
1440
 
 
1441
    theFactory->createTextNode(lFormatItem, lFormat);
 
1442
    theFactory->createTextNode(lCompressionItem, lCompression);
1394
1443
 
1395
1444
    return true;
1396
1445
  }