-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgmpackagemanager.cpp
981 lines (849 loc) · 30.4 KB
/
gmpackagemanager.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
#include "gmpackagemanager.h"
#include "gmpackagebuilder.h"
#include <QDir>
QDataStream::ByteOrder GmPackageManager::LoPackageByteOrder = QDataStream::LittleEndian;
QDataStream & operator>>(QDataStream &in, GmPackageFileInfoItem &item)
{
// set byte order to LoPackage default byte order
in.setByteOrder(GmPackageManager::getLoPackageByteOrder());
in >> item.filename;
in >> item.position;
in >> item.compressedDataLength;
in >> item.originalDataLength;
int permissions = 0;
in >> permissions;
item.permissions = (QFile::Permissions)permissions;
in >> item.sort;
in >> item.compressFlag;
in >> item.deleteFlag;
in >> item.isSymLink;
in >> item.symLinkTarget;
return in;
}
QDataStream & operator<<(QDataStream &out, const GmPackageFileInfoItem &item)
{
// set byte order to LoPackage default byte order
out.setByteOrder(GmPackageManager::getLoPackageByteOrder());
out << item.filename;
out << item.position;
out << item.compressedDataLength;
out << item.originalDataLength;
int permissions = (int) item.permissions;
out << permissions;
out << item.sort;
out << item.compressFlag;
out << item.deleteFlag;
out << item.isSymLink;
out << item.symLinkTarget;
return out;
}
GmPackageManager::GmPackageManager()
{
init();
}
GmPackageManager::GmPackageManager(const QString & packageFilename)
{
init();
setPackageFilename(packageFilename);
}
GmPackageManager::~GmPackageManager() { }
bool GmPackageManager::setPackageFilename(const QString & packageFilename)
{
m_packageFilename = packageFilename;
if (m_packageFilename.isEmpty()) {
m_errorMessage = QString("Package file name is empty.");
return false;
}
bool ok = load();
return ok;
}
const QString & GmPackageManager::getPackageFilename() const
{
return m_packageFilename;
}
bool GmPackageManager::load()
{
if (m_packageFilename.isEmpty()) {
m_errorMessage = QString("Package file name is empty.");
return false;
}
bool ok = false;
QFile packageFile(m_packageFilename);
ok = packageFile.open(QIODevice::ReadOnly);
if (!ok) {
m_errorMessage = QString("Opens package file %1 failure.").arg(m_packageFilename);
return false;
}
// load file information list
ok = loadFileInfo(packageFile);
if (!ok) return false;
// load version and compress flag
ok = readPackageFileHeader(packageFile);
if (!ok) return false;
return true;
}
bool GmPackageManager::load(const QString & packageFilename)
{
bool ok = setPackageFilename(packageFilename);
return ok;
}
bool GmPackageManager::isValid()
{
return (!m_fileInfoList.isEmpty());
}
const QString & GmPackageManager::getErrorMessage() const
{
return m_errorMessage;
}
int GmPackageManager::getVersion() const
{
return m_version;
}
void GmPackageManager::setCompressFlag(bool compressFlag)
{
m_compressFlag = compressFlag ? 0x01 : 0x0;
}
bool GmPackageManager::getCompressFlag() const
{
return (m_compressFlag == 0x01);
}
bool GmPackageManager::setCompressionLevel(int compressionLevel)
{
if ((compressionLevel < 0 && compressionLevel != -1) || compressionLevel > 9) {
m_errorMessage = QString("Compression level is out of range (-1, 0, ... 9).");
return false;
}
m_compressionLevel = compressionLevel;
return true;
}
QDataStream::ByteOrder GmPackageManager::getLoPackageByteOrder()
{
return LoPackageByteOrder;
}
QDataStream::ByteOrder GmPackageManager::getCurrentSystemByteOrder()
{
char tmp[4];
int *data;
tmp[0] = 1;
tmp[1] = 2;
tmp[2] = 3;
tmp[3] = 4;
data = (int*) tmp;
if (*data == 0x01020304)
return QDataStream::BigEndian;
else
return QDataStream::LittleEndian;
}
void* GmPackageManager::transEndian(void *data, size_t size, size_t nitems)
{
char *byte = (char*) data;
if (size <= 1) return data;
size_t i, j;
char tmp;
for (j = 0; j < nitems; j++) {
for (i = size - 1; i >= size / 2; i--) {
tmp = byte[i + j * size];
byte[i + j * size] = byte[size - i - 1 + j * size];
byte[size - i - 1 + j * size] = tmp;
}
}
return byte;
}
void GmPackageManager::init()
{
m_version = 2;
m_compressFlag = 0;
m_encryption = 1;
strncpy(m_fileIdentification, "GMTOOLKITPACKAGEFILE", sizeof(m_fileIdentification));
m_packageFileStartPosition = 0;
m_compressionLevel = 9;
}
char *GmPackageManager::readDataFile(QFile & packageFile, const GmPackageFileInfoItem & item)
{
if (item.originalDataLength == 0 || item.compressedDataLength == 0) return NULL;
if (!packageFile.isOpen()) return NULL;
qint64 fileDataStartPosition = getFileDataStartPosition(item);
bool ok = packageFile.seek(fileDataStartPosition);
if (!ok) {
m_errorMessage = QString("Seeks position to %1 of file %2 failure.").arg(fileDataStartPosition).arg(packageFile.fileName());
return NULL;
}
char *data = NULL;
char *compressedData = new char[item.compressedDataLength];
if (compressedData == NULL) return NULL;
ok = readDataBlock(compressedData, item.compressedDataLength, packageFile);
if (!ok) {
if (compressedData) delete []compressedData;
return NULL;
}
if (item.compressFlag) {
// uncompress data
QByteArray ucba = qUncompress((uchar*) compressedData, item.compressedDataLength);
if (ucba.size() != item.originalDataLength) {
if (compressedData) delete []compressedData;
return NULL;
}
if (compressedData) delete []compressedData;
data = new char[item.originalDataLength];
if (data == NULL) return NULL;
memcpy(data, ucba.constData(), item.originalDataLength);
} else {
data = compressedData;
}
return data;
}
char *GmPackageManager::readDataFile(QFile & packageFile, const QString & filename, qint64 & fileSize)
{
for (int i = 0; i < m_fileInfoList.size(); i++) {
const GmPackageFileInfoItem & item = m_fileInfoList.at(i);
if (item.deleteFlag) continue;
if (item.filename == filename) {
char *data = readDataFile(packageFile, item);
fileSize = item.originalDataLength;
return data;
}
}
return NULL;
}
bool GmPackageManager::readDataBlock(char *data, qint64 dataLength, QFile & packageFile)
{
if (data == NULL || dataLength == 0) return false;
if (!packageFile.isOpen()) return false;
qint64 oldPosition = packageFile.pos();
qint64 nb = packageFile.read(data, dataLength);
if (nb < 0 || nb != dataLength) {
packageFile.seek(oldPosition);
m_errorMessage = QString("Reads data from file %1 failure.").arg(packageFile.fileName());
return false;
}
if (m_encryption) {
for (qint64 i = 0; i < dataLength; i++) data[i] ^= 0x62;
}
return true;
}
qint64 GmPackageManager::getFileDataStartPosition(const GmPackageFileInfoItem & item) const
{
return item.position + m_packageFileStartPosition;
}
bool GmPackageManager::writeDataFile(const QByteArray & fba, QFile & packageFile, GmPackageFileInfoItem & item)
{
return writeDataFile(fba.constData(), (qint64) fba.length(), packageFile, item);
}
bool GmPackageManager::writeDataFile(const char *data, qint64 dataLength, QFile & packageFile, GmPackageFileInfoItem & item)
{
if (data == NULL || dataLength == 0) return false;
if (!packageFile.isOpen()) return false;
item.position = packageFile.pos();
item.originalDataLength = dataLength;
item.compressedDataLength = dataLength;
item.compressFlag = m_compressFlag;
char *data2 = (char*) data;
qint64 dataLength2 = dataLength;
QByteArray cba;
if (m_compressFlag) {
if (dataLength > 0x7FFFFFFF) {
item.setCompressFlag(false);
} else {
// compress data
cba = qCompress((const uchar *) data, (int) dataLength, m_compressionLevel);
if (cba.size() == 0) {
item.setCompressFlag(false);
} else {
data2 = (char*) cba.constData();
dataLength2 = (qint64) cba.size();
item.compressedDataLength = dataLength2;
}
}
}
// output compressed data to package and free compressed data buffer
bool ok = writeDataBlock(data2, dataLength2, packageFile);
if (!ok) return false;
return true;
}
bool GmPackageManager::writeDataBlock(const char *data, qint64 dataLength, QFile & packageFile)
{
if (data == NULL || dataLength == 0) return false;
if (!packageFile.isOpen()) return false;
qint64 oldPosition = packageFile.pos();
qint64 nb = 0;
if (m_encryption) {
QByteArray ba;
ba.setRawData(data, dataLength);
char *edata = ba.data();
for (qint64 i = 0; i < dataLength; i++) edata[i] ^= 0x62;
nb = packageFile.write(edata, dataLength);
} else {
nb = packageFile.write(data, dataLength);
}
if (nb < 0 || nb != dataLength) {
packageFile.seek(oldPosition);
m_errorMessage = QString("Writes data to file %1 failure.").arg(packageFile.fileName());
return false;
}
return true;
}
bool GmPackageManager::writePackageFileHeader(QFile & packageFile)
{
// output version and compress flag
QDataStream out(&packageFile);
out.setByteOrder(LoPackageByteOrder);
qint64 headerStartPosition = m_packageFileStartPosition;
bool ok = packageFile.seek(headerStartPosition);
if (!ok) {
m_errorMessage = QString("Seeks position to %1 of file %2 failure.").arg(headerStartPosition).arg(packageFile.fileName());
return false;
}
// output package file version and compress flag
out << m_version;
out << m_compressFlag;
out << m_encryption;
int err = out.writeRawData(m_fileIdentification, sizeof(m_fileIdentification));
if (err < 0) ok = false;
if (ok) ok = out.status() == QDataStream::Ok;
return ok;
}
bool GmPackageManager::readPackageFileHeader(QFile & packageFile)
{
QDataStream in(&packageFile);
in.setByteOrder(LoPackageByteOrder);
qint64 headerStartPosition = getPackageFileHeaderStartPosition();
if (headerStartPosition < 0) {
m_errorMessage = QString("Gets header position of package %1 failure.").arg(packageFile.fileName());
return false;
}
bool ok = packageFile.seek(headerStartPosition);
if (!ok) {
m_errorMessage = QString("Seeks position to %1 of file %2 failure.").arg(headerStartPosition).arg(packageFile.fileName());
return false;
}
// input package file version and compress flag
in >> m_version;
in >> m_compressFlag;
if (m_version == 2) {
in >> m_encryption;
in.readRawData(m_fileIdentification, sizeof(m_fileIdentification));
}
ok = in.status() == QDataStream::Ok;
return ok;
}
size_t GmPackageManager::getPackageFileHeaderSize() const
{
size_t headerSize = 0;
if (m_version == 1) headerSize = sizeof (int) + sizeof (quint8);
return headerSize;
}
qint64 GmPackageManager::getPackageFileHeaderStartPosition()
{
if (!isValid()) {
m_errorMessage = QString("The package is invalid.");
return -1;
}
size_t headerSize = getPackageFileHeaderSize();
const GmPackageFileInfoItem & item = m_fileInfoList.first();
qint64 pos = getFileDataStartPosition(item) - (qint64) headerSize;
return pos;
}
const QList<GmPackageFileInfoItem> & GmPackageManager::getFileInfoList() const
{
return m_fileInfoList;
}
bool GmPackageManager::getFileInfoList(int sort, QList<GmPackageFileInfoItem> & fileInfoList) const
{
QList<int> sortList;
sortList.append(sort);
return getFileInfoList(sortList, fileInfoList);
}
bool GmPackageManager::getFileInfoList(const QList<int> & sortList, QList<GmPackageFileInfoItem> & fileInfoList) const
{
return getFileInfoList(m_fileInfoList, sortList, fileInfoList);
}
bool GmPackageManager::getFileInfoList(const QList<GmPackageFileInfoItem> & sourceFileInfoList,
const QList<int> & sortList, QList<GmPackageFileInfoItem> & fileInfoList)
{
fileInfoList.clear();
for (int i = 0; i < sourceFileInfoList.size(); i++) {
const GmPackageFileInfoItem & item = sourceFileInfoList.at(i);
if (item.deleteFlag) continue;
if (sortList.contains(item.sort)) {
fileInfoList.append(item);
}
}
return (!fileInfoList.isEmpty());
}
bool GmPackageManager::getFileInfoList(const QString & startDirName, bool containsSubdir, QList<GmPackageFileInfoItem> & fileInfoList) const
{
return getFileInfoList(m_fileInfoList, startDirName, containsSubdir, fileInfoList);
}
bool GmPackageManager::getFileInfoList(const QList<GmPackageFileInfoItem> & sourceFileInfoList,
const QString & startDirName, bool containsSubdir, QList<GmPackageFileInfoItem> & fileInfoList)
{
fileInfoList.clear();
if (startDirName.isEmpty()) {
for (int i = 0; i < sourceFileInfoList.size(); i++) {
const GmPackageFileInfoItem & item = sourceFileInfoList.at(i);
if (item.deleteFlag) continue;
if (containsSubdir) {
fileInfoList.append(item);
} else if (!item.filename.contains(QDir::separator())) {
fileInfoList.append(item);
}
}
} else {
QString dirNameSep = startDirName;
if (!dirNameSep.endsWith(QDir::separator())) dirNameSep += QDir::separator();
for (int i = 0; i < sourceFileInfoList.size(); i++) {
const GmPackageFileInfoItem & item = sourceFileInfoList.at(i);
if (item.deleteFlag) continue;
if (item.filename.startsWith(dirNameSep)) {
if (containsSubdir) {
fileInfoList.append(item);
} else {
QString filename = item.filename.mid(dirNameSep.length());
if (!filename.contains(QDir::separator())) {
fileInfoList.append(item);
}
}
}
}
}
return (!fileInfoList.isEmpty());
}
bool GmPackageManager::getFilenames(const QString & startDirName, bool containsSubdir, QStringList & filenames) const
{
filenames.clear();
if (startDirName.isEmpty()) {
for (int i = 0; i < m_fileInfoList.size(); i++) {
const GmPackageFileInfoItem & item = m_fileInfoList.at(i);
if (item.deleteFlag) continue;
if (containsSubdir) {
filenames.append(item.filename);
} else if (!item.filename.contains(QDir::separator())) {
filenames.append(item.filename);
}
}
} else {
QString dirNameSep = startDirName;
if (!dirNameSep.endsWith(QDir::separator())) dirNameSep += QDir::separator();
for (int i = 0; i < m_fileInfoList.size(); i++) {
const GmPackageFileInfoItem & item = m_fileInfoList.at(i);
if (item.deleteFlag) continue;
if (item.filename.startsWith(dirNameSep) &&
item.filename.length() > dirNameSep.length()) {
QString filename = item.filename.mid(dirNameSep.length());
if (containsSubdir) {
filenames.append(filename);
} else if (!filename.contains(QDir::separator())) {
filenames.append(filename);
}
}
}
}
return (!filenames.isEmpty());
}
bool GmPackageManager::getFilenames(const QList<GmPackageFileInfoItem> & fileInfoList, QStringList & filenames)
{
filenames.clear();
for (int i = 0; i < fileInfoList.size(); i++) {
const GmPackageFileInfoItem & item = fileInfoList.at(i);
if (item.deleteFlag) continue;
filenames.append(item.filename);
}
return (!filenames.isEmpty());
}
bool GmPackageManager::getDirNames(const QString & startDirName, QStringList & dirNames) const
{
dirNames.clear();
if (startDirName.isEmpty()) {
for (int i = 0; i < m_fileInfoList.size(); i++) {
const GmPackageFileInfoItem & item = m_fileInfoList.at(i);
if (item.deleteFlag) continue;
int index = item.filename.indexOf(QDir::separator());
if (index > 0) {
QString dirName = item.filename.mid(0, index);
if (!dirNames.contains(dirName)) dirNames.append(dirName);
}
}
} else {
QString dirNameSep = startDirName;
if (!dirNameSep.endsWith(QDir::separator())) dirNameSep += QDir::separator();
for (int i = 0; i < m_fileInfoList.size(); i++) {
const GmPackageFileInfoItem & item = m_fileInfoList.at(i);
if (item.deleteFlag) continue;
if (item.filename.startsWith(dirNameSep) &&
item.filename.length() > dirNameSep.length()) {
QString filename = item.filename.mid(dirNameSep.length());
int index = filename.indexOf(QDir::separator());
if (index > 0) {
QString dirName = item.filename.mid(0, index);
if (!dirNames.contains(dirName)) dirNames.append(dirName);
}
}
}
}
return (!dirNames.isEmpty());
}
qint64 GmPackageManager::getFileDataSize(const QString & startDirName, bool containsSubdir) const
{
QList<GmPackageFileInfoItem> fileInfoList;
bool ok = getFileInfoList(startDirName, containsSubdir, fileInfoList);
if (!ok) return 0;
qint64 fsize = getFileDataSize(fileInfoList);
return fsize;
}
qint64 GmPackageManager::getFileDataSize(const QList<GmPackageFileInfoItem> & fileInfoList)
{
qint64 fsize = 0;
for (int i = 0; i < fileInfoList.size(); i++) {
const GmPackageFileInfoItem & item = fileInfoList.at(i);
if (item.deleteFlag) continue;
fsize += item.originalDataLength;
}
return fsize;
}
int GmPackageManager::getFileNumber() const
{
return m_fileInfoList.size();
}
int GmPackageManager::getFileNumber(int sort) const
{
int fileNumber = 0;
for (int i = 0; i < m_fileInfoList.size(); i++) {
const GmPackageFileInfoItem & item = m_fileInfoList.at(i);
if (item.deleteFlag) continue;
if (item.sort == sort) fileNumber++;
}
return fileNumber;
}
int GmPackageManager::getFileNumber(const QList<int> & sortList) const
{
int fileNumber = 0;
for (int i = 0; i < m_fileInfoList.size(); i++) {
const GmPackageFileInfoItem & item = m_fileInfoList.at(i);
if (item.deleteFlag) continue;
if (sortList.contains(item.sort)) fileNumber++;
}
return fileNumber;
}
bool GmPackageManager::fileExists(const QString & filename) const
{
for (int i = 0; i < m_fileInfoList.size(); i++) {
const GmPackageFileInfoItem & item = m_fileInfoList.at(i);
if (item.deleteFlag) continue;
if (item.filename == filename)
return true;
}
return false;
}
int GmPackageManager::indexOf(const QString & filename) const
{
for (int i = 0; i < m_fileInfoList.size(); i++) {
const GmPackageFileInfoItem & item = m_fileInfoList.at(i);
if (item.deleteFlag) continue;
if (item.filename == filename)
return i;
}
return -1;
}
bool GmPackageManager::getFileInfo(int index, GmPackageFileInfoItem & item)
{
if (index < 0 || index >= m_fileInfoList.size()) {
m_errorMessage = QString("Index is out of range (0...%1)").arg(m_fileInfoList.size() - 1);
return false;
}
item = m_fileInfoList.at(index);
return true;
}
bool GmPackageManager::getFileInfo(const QString & filename, GmPackageFileInfoItem & item)
{
int index = indexOf(filename);
return getFileInfo(index, item);
}
GmPackageFileInfoItem * GmPackageManager::getFileInfo(int index)
{
if (index < 0 || index >= m_fileInfoList.size()) {
m_errorMessage = QString("Index is out of range (0...%1)").arg(m_fileInfoList.size() - 1);
return NULL;
}
return &m_fileInfoList[index];
}
GmPackageFileInfoItem * GmPackageManager::getFileInfo(const QString & filename)
{
int index = indexOf(filename);
return getFileInfo(index);
}
qint64 GmPackageManager::getFileInfoStartPosition()
{
if (!isValid()) {
m_errorMessage = QString("The package is invalid.");
return -1;
}
const GmPackageFileInfoItem & item = m_fileInfoList.last();
qint64 pos = getFileDataStartPosition(item) + item.compressedDataLength;
return pos;
}
bool GmPackageManager::removeDataFile(const QString & filename)
{
for (int i = 0; i < m_fileInfoList.size(); i++) {
GmPackageFileInfoItem & item = m_fileInfoList[i];
if (item.deleteFlag) continue;
if (item.filename == filename) {
item.setDeleteFlag(true);
return true;
}
}
m_errorMessage = QString("File %1 not exists.").arg(filename);
return false;
}
bool GmPackageManager::removeDataFile(const QString & filename, QFile & packageFile)
{
bool ok = false;
if (!isValid()) ok = load();
if (!ok) return false;
ok = removeDataFile(filename);
if (!ok) return false;
ok = saveFileInfo(packageFile);
return ok;
}
bool GmPackageManager::removeDataFile(const QString & filename, const QString & packageFilename)
{
bool ok = false;
QFile packageFile(packageFilename);
ok = packageFile.open(QIODevice::ReadWrite);
if (!ok) return false;
GmPackageManager lopm(packageFilename);
ok = lopm.removeDataFile(filename, packageFile);
return ok;
}
void GmPackageManager::setPackageFileSort(int sort)
{
for (int i = 0; i < m_fileInfoList.size(); i++) {
GmPackageFileInfoItem & item = m_fileInfoList[i];
item.sort = sort;
}
}
bool GmPackageManager::setPackageFileSort(int sort, QFile & packageFile)
{
bool ok = false;
if (!isValid()) ok = load();
if (!ok) return false;
setPackageFileSort(sort);
ok = saveFileInfo(packageFile);
return ok;
}
bool GmPackageManager::setPackageFileSort(int sort, const QString & packageFilename)
{
bool ok = false;
QFile packageFile(packageFilename);
ok = packageFile.open(QIODevice::ReadWrite);
if (!ok) return false;
GmPackageManager lopm(packageFilename);
ok = lopm.setPackageFileSort(sort, packageFile);
return ok;
}
bool GmPackageManager::appendFileInfo(const GmPackageFileInfoItem & item)
{
if (fileExists(item.filename)) return false;
m_fileInfoList.append(item);
return true;
}
bool GmPackageManager::loadFileInfo(QFile & packageFile)
{
m_fileInfoList.clear();
bool ok = false;
QDataStream in(&packageFile);
in.setByteOrder(GmPackageManager::getLoPackageByteOrder());
qint64 fsize = packageFile.size();
qint64 infoTailSize = (qint64) (sizeof (int) + sizeof (qint64) * 2);
qint64 infoTailStartPosition = fsize - infoTailSize;
ok = packageFile.seek(infoTailStartPosition);
if (!ok) {
m_errorMessage = QString("Seeks position to %1 of file %2 failure.").arg(infoTailStartPosition).arg(packageFile.fileName());
return false;
}
int infoCount = 0;
qint64 packageInfoDataStartPos = 0;
qint64 packageFileSize = 0;
in >> infoCount;
in >> packageInfoDataStartPos;
in >> packageFileSize;
if (fsize < packageFileSize) return false;
qint64 fileInfoDataSize = packageFileSize - packageInfoDataStartPos - infoTailSize;
if (fsize > packageFileSize) {
m_packageFileStartPosition = fsize - packageFileSize;
packageInfoDataStartPos += m_packageFileStartPosition;
}
if (infoCount == 0) return false;
ok = packageFile.seek(packageInfoDataStartPos);
if (!ok) {
m_errorMessage = QString("Seeks position to %1 of file %2 failure.").arg(packageInfoDataStartPos).arg(packageFile.fileName());
return false;
}
// input compress flag
quint8 compressFlag;
in >> compressFlag;
if (compressFlag) {
char *data = new char[fileInfoDataSize];
if (data == NULL) {
m_errorMessage = QString("Allocs data buffer [%1] failure.").arg(fileInfoDataSize);
return false;
}
ok = readDataBlock(data, fileInfoDataSize, packageFile);
if (!ok) {
delete []data;
m_errorMessage = QString("Reads data block from file %1 failure.").arg(packageFile.fileName());
return false;
} else {
// uncompress data
QByteArray fileInfoListBuf = qUncompress((uchar*) data, fileInfoDataSize);
if (fileInfoListBuf.size() == 0) {
delete []data;
m_errorMessage = QString("Uncompress data block [%1] failure.").arg(fileInfoDataSize);
return false;
}
delete []data;
QDataStream inb(&fileInfoListBuf, QIODevice::ReadOnly);
for (int i = 0; i < infoCount; i++) {
GmPackageFileInfoItem item;
inb >> item;
m_fileInfoList.append(item);
}
}
} else {
// input LoPFileInfoItems
for (int i = 0; i < infoCount; i++) {
GmPackageFileInfoItem item;
in >> item;
m_fileInfoList.append(item);
}
}
return true;
}
bool GmPackageManager::saveFileInfo(QFile & packageFile)
{
qint64 packageInfoDataStartPos = getFileInfoStartPosition();
if (packageInfoDataStartPos < 0) {
m_errorMessage = QString("Gets file information start position of package %1 failure.").arg(packageFile.fileName());
return false;
}
int infoCount = m_fileInfoList.size();
if (infoCount == 0) {
m_errorMessage = QString("File information list of package %1 is empty.").arg(packageFile.fileName());
return false;
}
bool ok = packageFile.seek(packageInfoDataStartPos);
if (!ok) {
m_errorMessage = QString("Seeks position to %1 of file %2 failure.").arg(packageInfoDataStartPos).arg(packageFile.fileName());
return false;
}
QDataStream out(&packageFile);
out.setByteOrder(GmPackageManager::getLoPackageByteOrder());
bool originalSaveFlag = true;
if (m_compressFlag) {
QByteArray fileInfoListBuf;
QDataStream outb(&fileInfoListBuf, QIODevice::WriteOnly);
for (int i = 0; i < infoCount; i++) {
const GmPackageFileInfoItem & item = m_fileInfoList.at(i);
outb << item;
}
if (fileInfoListBuf.size() > 0) {
// compress file information list data
QByteArray cba = qCompress((const uchar *) fileInfoListBuf.constData(), fileInfoListBuf.size(), m_compressionLevel);
if (cba.size() > 0) {
originalSaveFlag = false;
// output compress flag
out << m_compressFlag;
// output LoPFileInfoItem list compressed data
ok = writeDataBlock(cba.constData(), (qint64) cba.size(), packageFile);
if (!ok) {
m_errorMessage = QString("Writes data block to package %1 failure.").arg(packageFile.fileName());
return false;
}
}
}
}
if (originalSaveFlag) {
// output compress flag
out << m_compressFlag;
// output LoPFileInfoItems
for (int i = 0; i < infoCount; i++) {
const GmPackageFileInfoItem & item = m_fileInfoList.at(i);
out << item;
}
}
// output information item count
out << infoCount;
// output package information data start position
out << packageInfoDataStartPos;
// output package file size
qint64 packageFileSize = packageFile.size() + sizeof (qint64);
out << packageFileSize;
// hear is package file tail, if some data exists after current position that must be truncated.
qint64 packageFileEndPos = packageFile.pos();
ok = packageFile.resize(packageFileEndPos);
if (!ok) {
m_errorMessage = QString("Resizes package file %1 failure.").arg(packageFile.fileName());
return false;
}
return true;
}
bool GmPackageManager::appendPackage(const QString & packageFilename)
{
if (m_packageFilename.isEmpty()) return false;
if (packageFilename.isEmpty()) return false;
if (m_packageFilename == packageFilename) return false;
bool ok = false;
if (!isValid()) ok = load();
if (!ok) return false;
// append package data into current package
// open current package file to append other package data
QFile packageFile(m_packageFilename);
ok = packageFile.open(QIODevice::ReadWrite);
if (!ok) {
m_errorMessage = QString("Opens package file %1 failure.").arg(m_packageFilename);
return false;
}
QFile packageFileAppend(packageFilename);
ok = packageFileAppend.open(QIODevice::ReadOnly);
if (!ok) {
m_errorMessage = QString("Opens package file %1 failure.").arg(packageFilename);
return false;
}
// seek package file position to pacakge data end position for output file data
qint64 appendFileDataStartPosition = getFileInfoStartPosition();
ok = packageFile.seek(appendFileDataStartPosition);
if (!ok) {
m_errorMessage = QString("Seeks position to %1 of file %2 failure.").arg(appendFileDataStartPosition).arg(packageFile.fileName());
return false;
}
GmPackageManager lopmAppend(packageFilename);
ok = lopmAppend.load();
if (!ok) return false;
// package information data of append package
const QList<GmPackageFileInfoItem> & lopFileInfoList = lopmAppend.getFileInfoList();
for (int i = 0; i < lopFileInfoList.size(); i++) {
// get file information item (filename, position, compressed length, original length) from package file information list
const GmPackageFileInfoItem & item = lopFileInfoList.at(i);
if (item.deleteFlag) continue;
// read file data
if (item.originalDataLength == 0) {
GmPackageFileInfoItem itemNull;
itemNull.filename = item.filename;
ok = appendFileInfo(itemNull);
if (!ok) return false;
} else {
char *data = lopmAppend.readDataFile(packageFileAppend, item);
if (data == NULL) return false;
GmPackageFileInfoItem itemAppend;
itemAppend.filename = item.filename;
ok = writeDataFile(data, packageFile, itemAppend);
delete []data;
// add file information to package file information list
ok = appendFileInfo(itemAppend);
if (!ok) return false;
}
}
// save package file information list to package file end
ok = saveFileInfo(packageFile);
if (!ok) return false;
return true;
}