-
-
Notifications
You must be signed in to change notification settings - Fork 279
/
Copy pathRtt_Archive.cpp
1235 lines (1018 loc) · 26.4 KB
/
Rtt_Archive.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//////////////////////////////////////////////////////////////////////////////
//
// This file is part of the Solar2D game engine.
// With contributions from Dianchu Technology
// For overview and more information on licensing please refer to README.md
// Home page: https://github.com/coronalabs/corona
// Contact: support@coronalabs.com
//
//////////////////////////////////////////////////////////////////////////////
#include "Core/Rtt_Build.h"
#include "Rtt_Archive.h"
#include "Rtt_LuaFile.h"
#include "Core/Rtt_String.h"
#include "Core/Rtt_FileSystem.h"
#if !defined( Rtt_NO_ARCHIVE )
#include "Rtt_LuaContext.h"
#include "Rtt_Runtime.h"
#endif
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#if defined( Rtt_WIN_ENV ) || defined( Rtt_POWERVR_ENV ) // || defined( Rtt_NXS_ENV )
#include <io.h>
#include <sys/stat.h>
static const unsigned S_IRUSR = _S_IREAD; ///< read by user
static const unsigned S_IWUSR = _S_IWRITE; ///< write by user
#elif defined( Rtt_ANDROID_ENV )
#include "NativeToJavaBridge.h"
#include <sys/mman.h>
#else
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#endif
#include <errno.h>
#include <sys/stat.h>
// #define Rtt_DEBUG_ARCHIVE 1
// ----------------------------------------------------------------------------
namespace Rtt
{
// ----------------------------------------------------------------------------
static bool
CopyFile( FILE *src, FILE *dst, long *numBytes )
{
bool result = true;
Rtt_ASSERT( src && dst );
long startPos = ftell( dst );
int c;
while( (c = getc( src )) != EOF )
{
if ( ! Rtt_VERIFY( EOF != putc( c, dst ) ) )
{
Rtt_TRACE( ( "ERROR: Copy could not be copied!\n" ) );
result = false;
goto exit_gracefully;
}
}
/*
// Only works for plain text files
#if 1
char buf[256];
const size_t kNumElem = sizeof(buf) / sizeof(buf[0]);
const size_t kElemSize = sizeof( buf[0] ); // obj size
size_t objectsRead = 0;
while ( ( objectsRead = fread( buf, kElemSize, kNumElem, src ) ) )
{
if ( fwrite( buf, kElemSize, objectsRead, dst ) < objectsRead )
{
result = false;
goto exit_gracefully;
}
}
#else
char buf[256];
const size_t kBufSize = sizeof(buf) / sizeof(buf[0]);
while ( fgets( buf, kBufSize, src ) )
{
if ( ! Rtt_VERIFY( EOF != fputs( buf, dst ) ) )
{
Rtt_TRACE( ( "ERROR: Copy could not be copied!\n" ) );
result = false;
goto exit_gracefully;
}
}
#endif
*/
exit_gracefully:
if ( numBytes )
{
*numBytes = ftell( dst ) - startPos;
}
return result;
}
template < size_t N >
static size_t
GetByteAlignedValue( size_t x )
{
Rtt_STATIC_ASSERT( 0 == (N & (N-1)) ); // Ensure that N is a power of 2
return ( x + (N-1) ) & (~(N-1));
}
static size_t
GetFileSize( const char *filepath )
{
Rtt_ASSERT( filepath );
struct stat statbuf;
int result = stat( filepath, & statbuf );
if (result != 0)
{
fprintf(stderr, "car: cannot stat file '%s'\n", filepath);
}
Rtt_UNUSED( result ); Rtt_ASSERT( 0 == result );
return statbuf.st_size;
}
/*
#if defined( Rtt_DEBUG ) && !defined( Rtt_ANDROID_ENV )
static bool
Diff( const char *path1, const char *path2 )
{
size_t len1 = GetFileSize( path1 );
size_t len2 = GetFileSize( path2 );
bool result = ( len1 == len2 );
if ( result )
{
#if defined( Rtt_WIN_DESKTOP_ENV ) || defined( Rtt_POWERVR_ENV )
WinFile file1;
WinFile file2;
file1.Open( path1 );
file1.Open( path2 );
if ( !file1.IsOpen() || !file2.IsOpen() )
return false;
if ( file1.GetFileSize() != file2.GetFileSize() )
return false;
result = ( 0 == memcmp( file1.GetContents(), file2.GetContents(), file1.GetFileSize() ) );
#elif defined( Rtt_WIN_PHONE_ENV )
//TODO: To be implemented later...
Rtt_ASSERT_NOT_IMPLEMENTED();
#else
int fd1 = open( path1, O_RDONLY );
void *p1 = mmap( NULL, len1, PROT_READ, MAP_SHARED, fd1, 0 );
int fd2 = open( path2, O_RDONLY );
void *p2 = mmap( NULL, len2, PROT_READ, MAP_SHARED, fd2, 0 );
result = ( 0 == memcmp( p1, p2, len1 ) );
munmap( p2, len2 );
close( fd2 );
munmap( p1, len1 );
close( fd1 );
#endif
}
return result;
}
#endif
*/
static char
DirSeparator()
{
#if defined( Rtt_WIN_ENV ) || defined( Rtt_POWERVR_ENV )
return '\\';
#else
return '/';
#endif
}
static const char*
GetBasename( const char* path )
{
const char kDirSeparator = DirSeparator();
const char* result = path;
for ( path = strchr( path, kDirSeparator );
path && '\0' != *path;
path = strchr( path, kDirSeparator ) )
{
++path;
result = path;
}
return result;
}
// ----------------------------------------------------------------------------
struct ArchiveWriterEntry
{
U32 type;
U32 offset;
const char* name;
size_t nameLen;
const char* srcPath;
size_t srcLen;
};
class ArchiveWriter
{
public:
enum
{
kTagSize = sizeof(U32)*2,
kVersion = 0x1
};
public:
ArchiveWriter();
~ArchiveWriter();
public:
int Initialize( const char *dstPath );
public:
int Serialize( Archive::Tag tag, U32 len ) const;
int Serialize( U32 value ) const;
int Serialize( const char *value, size_t len ) const;
int Serialize( const char *filepath ) const;
public:
// int Serialize( ArchiveWriterEntry& entry );
public:
S32 GetPosition() const;
private:
FILE *fDst;
};
// ----------------------------------------------------------------------------
ArchiveWriter::ArchiveWriter()
: fDst( NULL )
{
}
ArchiveWriter::~ArchiveWriter()
{
if ( Rtt_VERIFY( fDst ) )
{
Rtt_FileClose( fDst );
}
}
int
ArchiveWriter::Initialize( const char *dstPath )
{
int result = 0;
fDst = Rtt_FileOpen(dstPath, "wb");
if (fDst == NULL)
{
fprintf(stderr, "car: cannot open archive '%s' for writing\n", dstPath);
}
else
{
FILE *dst = fDst;
result += fprintf( dst, "%c", 'r');
result += fprintf( dst, "%c", 'a');
result += fprintf( dst, "%c", 'c');
result += fprintf( dst, "%c", kVersion );
}
return result;
}
int
ArchiveWriter::Serialize( Archive::Tag tag, U32 len ) const
{
Rtt_ASSERT( fDst );
int result = Serialize( tag );
result += Serialize( len );
return result;
}
int
ArchiveWriter::Serialize( U32 value ) const
{
Rtt_ASSERT( fDst );
return fprintf( fDst, "%c%c%c%c",
(unsigned char)(value & 0xFF),
(unsigned char)(value >> 8 & 0xFF),
(unsigned char)(value >> 16 & 0xFF),
(unsigned char)(value >> 24 & 0xFF) );
}
int
ArchiveWriter::Serialize( const char *value, size_t len ) const
{
Rtt_ASSERT( fDst );
FILE *dst = fDst;
int result = Serialize( (U32) len );
// size_t len4 = (len + 3) & (~0x3); // next multiple of 4 if len is not a multiple of 4
++len; // increment to include space for '\0'-termination
size_t len4 = GetByteAlignedValue< 4 >( len );
for ( size_t i = 0; i < len; i++ )
{
result += fprintf( dst, "%c", value[i] );
}
// Pad 0's to 4-byte align
for ( size_t i = len; i < len4; i++ )
{
result += fprintf( dst, "%c", 0 );
}
return result;
}
int
ArchiveWriter::Serialize( const char *filepath ) const
{
Rtt_ASSERT( fDst );
Rtt_ASSERT( filepath );
int result = 0;
FILE *src = Rtt_FileOpen( filepath, "rb" );
if (src == NULL)
{
fprintf(stderr, "car: cannot serialize file '%s' (%s)\n", filepath, strerror(errno));
return 0;
}
else
{
size_t len = GetFileSize( filepath );
size_t len4 = GetByteAlignedValue< 4 >( len );
FILE *dst = fDst;
long numBytes = 0;
if ( Rtt_VERIFY( CopyFile( src, dst, & numBytes ) ) )
{
Rtt_ASSERT( numBytes >= 0 && (size_t)numBytes == len );
size_t numChars = len4 - len;
Rtt_ASSERT( numChars < 4 );
switch( numChars )
{
case 3:
fprintf( dst, "%c", '\0' );
case 2:
fprintf( dst, "%c", '\0' );
case 1:
fprintf( dst, "%c", '\0' );
default:
break;
}
result += len4;
}
else
{
result += numBytes;
}
Rtt_FileClose(src);
}
return result;
}
/*
int
ArchiveWriter::Serialize( ArchiveWriterEntry& entry )
{
}
*/
S32
ArchiveWriter::GetPosition() const
{
Rtt_ASSERT( fDst );
return (S32) ftell( fDst );
}
// ----------------------------------------------------------------------------
class ArchiveReader
{
public:
ArchiveReader();
// ~ArchiveReader();
bool Initialize( const void* data, size_t numBytes );
public:
U32 ParseTag( U32& rLength );
U32 ParseU32();
const char* ParseString();
void* ParseData( U32& rLength );
public:
bool Seek( S32 offset, bool fromOrigin );
protected:
void VerifyBounds() const;
private:
const void *fPos;
const void *fData;
size_t fDataLen;
U8 fVersion;
};
ArchiveReader::ArchiveReader()
: fPos( NULL ),
fData( NULL ),
fDataLen( 0 ),
fVersion( 0 )
{
}
bool
ArchiveReader::Initialize( const void* data, size_t numBytes )
{
const U8 kHeader[] = { 'r', 'a', 'c', ArchiveWriter::kVersion };
const size_t kHeaderSize = sizeof( kHeader );
bool result = ( data && numBytes > kHeaderSize && 0 == memcmp( data, kHeader, kHeaderSize ) );
if ( result )
{
fPos = ((U8*)data) + kHeaderSize;
fData = data;
fDataLen = numBytes;
fVersion = ArchiveWriter::kVersion;
#if Rtt_DEBUG_ARCHIVE
Rtt_TRACE( ( "[ArchiveReader::Initialize] inData(%p) fPos(%p) fData(%p) headerSize(%ld) fDataLen(%ld)\n",
data, fPos, fData, kHeaderSize, fDataLen ) );
#endif
}
#if Rtt_DEBUG_ARCHIVE
else
{
Rtt_TRACE( ( "[ArchiveReader::Initialize] header check failed: numBytes %ld, kHeaderSize %ld\n",
numBytes, kHeaderSize ) );
}
#endif
return result;
}
static U32
ReadU32( U32 *p )
{
#ifdef Rtt_LITTLE_ENDIAN
return *p;
#else
U8 *pp = (U8*)p;
return ((U32)pp[0])
| (((U32)pp[1]) << 8)
| (((U32)pp[2]) << 16)
| (((U32)pp[3]) << 24);
#endif
}
U32
ArchiveReader::ParseTag( U32& rLength )
{
VerifyBounds();
U32 *p = (U32*)fPos;
U32 tag = ReadU32( p ); p++; // *p++;
rLength = ReadU32( p ); p++; // *p++;
fPos = p;
VerifyBounds();
return tag;
}
U32
ArchiveReader::ParseU32()
{
VerifyBounds();
U32 *p = (U32*)fPos;
U32 result = ReadU32( p ); p++; // *p++;
fPos = p;
VerifyBounds();
return result;
}
const char*
ArchiveReader::ParseString()
{
VerifyBounds();
U32 *p = (U32*)fPos;
U32 len = ReadU32( p ); p++; // *p++;
const char* result = reinterpret_cast< char* >( p );
Rtt_ASSERT( strlen( result ) == len );
// 4-byte alignment was calculated using string size (which includes '\0' termination byte)
fPos = p + GetByteAlignedValue< 4 >( len + 1 ) / sizeof( *p );
VerifyBounds();
return result;
}
void*
ArchiveReader::ParseData( U32& rLength )
{
VerifyBounds();
U32 *p = (U32*)fPos;
U32 len = ReadU32( p ); p++; // *p++;
rLength = len;
void* result = p;
fPos = p + GetByteAlignedValue< 4 >( len ) / sizeof( *p );
VerifyBounds();
return result;
}
bool
ArchiveReader::Seek( S32 offset, bool fromOrigin )
{
VerifyBounds();
bool result = false;
if ( fromOrigin )
{
result = ( offset >= 0 );
if ( result )
{
fPos = ((U8*)fData) + offset;
}
}
else
{
void *p = ((U8*)fPos) + offset;
void *pEnd = ((U8*)fData) + fDataLen;
result = ( p >= fData && p < pEnd );
if ( result )
{
fPos = p;
}
}
VerifyBounds();
return result;
}
void
ArchiveReader::VerifyBounds() const
{
// fprintf(stderr, "fPos %p, fData %p, fDataLen 0x%lx (%d)\n", fPos, fData, fDataLen, fDataLen);
Rtt_ASSERT( fPos >= fData && fPos < (((U8*)fData) + fDataLen ) );
}
// ----------------------------------------------------------------------------
void
Archive::Serialize( const char *dstPath, int numSrcPaths, const char *srcPaths[] )
{
std::vector<std::string> fileList;
size_t fileCount = 0;
#ifdef WIN32
char tmpDirTemplate[_MAX_PATH];
const char* tmp = getenv("TMP");
if (tmp == NULL)
{
tmp = getenv("TEMP");
}
if (tmp)
{
_snprintf(tmpDirTemplate, sizeof(tmpDirTemplate), "%s\\CBXXXXXX", tmp);
}
else
{
strcpy(tmpDirTemplate, "\\tmp\\CBXXXXXX");
}
#else
char tmpDirTemplate[] = "/tmp/CBXXXXXX";
#endif
const char *tmpDirName = Rtt_MakeTempDirectory(tmpDirTemplate);
if (Rtt_FileExists(dstPath))
{
// Archive already exists, extract the current contents so we can overwrite
// old files with fresh copies and add any new ones (this is done to avoid
// implementing actual archive editing)
// First extract any files already in the archive to preserve them
Deserialize(tmpDirName, dstPath);
// Copy all the new files to the temporary directory (overwriting any with the same
// name that might have been extracted to preserve the semantics of car files)
for ( int i = 0; i < numSrcPaths; i++ )
{
String tmpFileCopy(tmpDirName);
tmpFileCopy.AppendPathComponent(GetBasename(srcPaths[i]));
if ( ! Rtt_CopyFile(srcPaths[i], tmpFileCopy.GetString()))
{
fprintf(stderr, "car: cannot open '%s' for reading\n", srcPaths[i]);
return;
}
}
// Enumerate the files now in the temporary directory and make the new archive with them
fileList = Rtt_ListFiles(tmpDirName);
fileCount = fileList.size();
}
else
{
// Archive doesn't already exist so we can't be updating it,
// just copy the file paths to our file list
for ( int i = 0; i < numSrcPaths; i++ )
{
fileList.push_back(srcPaths[i]);
}
fileCount = numSrcPaths;
}
ArchiveWriter writer;
int startPos = writer.Initialize( dstPath );
if ( Rtt_VERIFY( startPos > 0 ) )
{
ArchiveWriterEntry* entries = new ArchiveWriterEntry[fileCount];
U32 contentsLen = sizeof(U32); // numElements
size_t entryIdx = 0;
for (std::vector<std::string>::iterator it = fileList.begin(); it != fileList.end(); ++it)
{
ArchiveWriterEntry& entry = entries[entryIdx++];
entry.type = kLuaObjectResource;
entry.offset = 0;
const char* path = it->c_str();
entry.name = GetBasename( path );
entry.nameLen = strlen( entry.name );
entry.srcPath = path;
entry.srcLen = GetFileSize( path );
// type, offset, numChars, string data
contentsLen += 3*sizeof(U32) + GetByteAlignedValue< 4 >( entry.nameLen + 1 );
}
U32 offsetBase = startPos + contentsLen;
offsetBase += writer.Serialize( Archive::kContentsTag, contentsLen );
// Contents
// --------------------------
// U32 numElements
// Record[] {
// U32 type
// U32 offset
// String name
// }
//
// String
// --------------------------
// U32 length
// U8[] bytes (4 byte-aligned padding)
writer.Serialize( (int)fileCount );
for ( size_t i = 0; i < fileCount; i++ )
{
ArchiveWriterEntry& entry = entries[i];
writer.Serialize( entry.type );
writer.Serialize( offsetBase );
writer.Serialize( entry.name, entry.nameLen );
// store offset for this entry
entry.offset = offsetBase;
// For next offset, add srcLen *and* bytes for tag, length
offsetBase +=
GetByteAlignedValue< 4 >( entry.srcLen )
+ ArchiveWriter::kTagSize
+ sizeof(U32);
}
// Data
// --------------------------
// String data
for ( size_t i = 0; i < fileCount; i++ )
{
ArchiveWriterEntry& entry = entries[i];
Rtt_ASSERT(
writer.GetPosition() >= 0
&& (size_t)writer.GetPosition() == entry.offset );
// data tag length = sizeof( length ) + byte-aligned len of bytes buffer
writer.Serialize( kDataTag, sizeof( U32 ) + (U32) GetByteAlignedValue< 4 >( entry.srcLen ) );
writer.Serialize( (U32) entry.srcLen );
writer.Serialize( entry.srcPath );
}
// EOF
writer.Serialize( kEOFTag, 0 );
delete [] entries;
}
Rtt_DeleteDirectory(tmpDirName);
}
static void
WriteFile( const char *dstDir, const char *filename, const void *src, size_t srcNumBytes )
{
Rtt_ASSERT( dstDir );
Rtt_ASSERT( filename );
Rtt_ASSERT( src );
String path(dstDir);
path.AppendPathComponent(filename);
int fd = Rtt_FileDescriptorOpen( path, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR );
if (fd == -1)
{
fprintf(stderr, "car: cannot open '%s' for writing\n", path.GetString());
return;
}
else
{
// Set size of file
_lseek( fd, srcNumBytes - 1, SEEK_SET );
_write( fd, "", 1 );
int canWrite = 1;
void *dst = Rtt_FileMemoryMap( fd, 0, srcNumBytes, canWrite );
if (dst == NULL)
{
fprintf(stderr, "car: cannot map '%s' for writing\n", path.GetString());
return;
}
else
{
*(U32*)dst = 0xdeadbeef;
memcpy( dst, src, srcNumBytes );
Rtt_FileMemoryUnmap( dst, srcNumBytes );
}
Rtt_FileDescriptorClose( fd );
}
}
size_t
Archive::Deserialize( const char *dstDir, const char *srcCarFile )
{
int fd = Rtt_FileDescriptorOpen(srcCarFile, O_RDONLY, S_IRUSR);
struct stat statbuf;
size_t count = 0;
if (fd == -1)
{
fprintf(stderr, "car: cannot open archive '%s'\n", srcCarFile);
return 0;
}
if (fstat( fd, & statbuf ) == -1)
{
fprintf(stderr, "car: cannot stat archive '%s'\n", srcCarFile);
return 0;
}
size_t dataLen = statbuf.st_size;
void *data = Rtt_FileMemoryMap(fd, 0, dataLen, false);
Rtt_FileDescriptorClose(fd);
ArchiveReader reader;
if (reader.Initialize(data, dataLen) == 0)
{
fprintf(stderr, "car: file '%s' is not a car archive\n", srcCarFile);
}
else
{
U32 tag = kUnknownTag;
//while( kEOFTag != tag )
{
U32 tagLen;
tag = reader.ParseTag( tagLen );
switch( tag )
{
case kContentsTag:
{
U32 numElements = reader.ParseU32();
ArchiveEntry *entries = (ArchiveEntry*)Rtt_MALLOC( & allocator, sizeof( ArchiveEntry )*numElements );
for ( U32 i = 0; i < numElements; i++ )
{
ArchiveEntry& entry = entries[i];
entry.type = reader.ParseU32();
entry.offset = reader.ParseU32();
entry.name = reader.ParseString();
}
for ( U32 i = 0; i < numElements; i++ )
{
ArchiveEntry& entry = entries[i];
reader.Seek( entry.offset, true );
U32 tagLen;
U32 tag = reader.ParseTag( tagLen );
if ( Rtt_VERIFY( Archive::kDataTag == tag ) )
{
U32 resourceLen = 0;
void* resource = reader.ParseData( resourceLen );
WriteFile( dstDir, entry.name, resource, resourceLen );
++count;
}
}
Rtt_FREE( entries );
}
break;
default:
Rtt_ASSERT_NOT_REACHED();
break;
}
}
}
if (data != NULL)
{
Rtt_FileMemoryUnmap(data, dataLen);
}
return count;
}
void
Archive::List(const char *srcCarFile)
{
int fd = Rtt_FileDescriptorOpen(srcCarFile, O_RDONLY, S_IRUSR);
struct stat statbuf;
if (fd == -1)
{
fprintf(stderr, "car: cannot open archive '%s'\n", srcCarFile);
return;
}
if (fstat( fd, & statbuf ) == -1)
{
fprintf(stderr, "car: cannot stat archive '%s'\n", srcCarFile);
return;
}
size_t dataLen = statbuf.st_size;
void *data = Rtt_FileMemoryMap(fd, 0, dataLen, false);
Rtt_FileDescriptorClose(fd);
ArchiveReader reader;
if (reader.Initialize(data, dataLen) == 0)
{
fprintf(stderr, "car: file '%s' is not a car archive\n", srcCarFile);
}
else
{
U32 tag = kUnknownTag;
//while( kEOFTag != tag )
{
U32 tagLen;
tag = reader.ParseTag( tagLen );
switch( tag )
{
case kContentsTag:
{
U32 numElements = reader.ParseU32();
ArchiveEntry *entries = (ArchiveEntry*)Rtt_MALLOC( & allocator, sizeof( ArchiveEntry )*numElements );
for ( U32 i = 0; i < numElements; i++ )
{
ArchiveEntry& entry = entries[i];
entry.type = reader.ParseU32();
entry.offset = reader.ParseU32();
entry.name = reader.ParseString();
}
for ( U32 i = 0; i < numElements; i++ )
{
ArchiveEntry& entry = entries[i];
reader.Seek( entry.offset, true );
U32 tagLen;
U32 tag = reader.ParseTag( tagLen );
if ( Rtt_VERIFY( Archive::kDataTag == tag ) )
{
U32 resourceLen = 0;
reader.ParseData( resourceLen );
printf("%7d %s\n", resourceLen, entry.name);
}
}
Rtt_FREE( entries );
}
break;
default:
Rtt_ASSERT_NOT_REACHED();
break;
}
}
}
if (data != NULL)
{
Rtt_FileMemoryUnmap(data, dataLen);
}
}
// ----------------------------------------------------------------------------
#if !defined( Rtt_NO_ARCHIVE )
// ----------------------------------------------------------------------------
Archive::Archive( Rtt_Allocator& allocator, const char *srcPath )
: fAllocator( allocator ),
fEntries( NULL ),
fNumEntries( 0 ),
#if defined( Rtt_ARCHIVE_COPY_DATA )
fBits( &allocator ),
#endif
fData( NULL )
{
#if defined( Rtt_WIN_PHONE_ENV ) || defined(Rtt_NXS_ENV)
FILE* filePointer = Rtt_FileOpen(srcPath, "rb");
if (filePointer)
{
fseek(filePointer, 0, SEEK_END);
fDataLen = ftell(filePointer);
if (fDataLen > 0)
{
const size_t MAX_BYTES_PER_READ = 1024;
rewind(filePointer);
fData = Rtt_MALLOC(&allocator, fDataLen);
for (long totalBytesRead = 0; totalBytesRead < fDataLen;)
{
size_t bytesRead = fread(((U8*)fData + totalBytesRead), 1, MAX_BYTES_PER_READ, filePointer);
if (bytesRead < MAX_BYTES_PER_READ)
{
int errorNumber = errno;
if (ferror(filePointer) && errorNumber)
{
Rtt_FREE((void*)fData);
fData = NULL;
fDataLen = 0;
Rtt_LogException(strerror(errorNumber));