w1146869587
2022-03-04 2efb30bdc5c62273d77443180aba586f64c097f9
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
#include "testquazipnewinfo.h"
 
#include "qztest.h"
 
#include <QDir>
#include <QFileInfo>
#include <QtTest/QtTest>
 
#include <quazip/quazip.h>
#include <quazip/quazipfile.h>
#include <quazip/quazipnewinfo.h>
#include <quazip/quazipfileinfo.h>
 
TestQuaZipNewInfo::TestQuaZipNewInfo(QObject *parent) :
    QObject(parent)
{
}
 
void TestQuaZipNewInfo::setFileNTFSTimes()
{
    QString zipName = "newtimes.zip";
    QStringList testFiles;
    testFiles << "test.txt";
    QDir curDir;
    if (curDir.exists(zipName)) {
        if (!curDir.remove(zipName))
            QFAIL("Can't remove zip file");
    }
    if (!createTestFiles(testFiles)) {
        QFAIL("Can't create test file");
    }
    QDateTime base(QDate(1601, 1, 1), QTime(0, 0), Qt::UTC);
    quint64 mTicks, aTicks, cTicks;
    {
        // create
        QuaZip zip(zipName);
        QVERIFY(zip.open(QuaZip::mdCreate));
        QuaZipFile zipFile(&zip);
        QFileInfo fileInfo("tmp/test.txt");
        QDateTime lm = fileInfo.lastModified().toUTC();
        QDateTime lr = fileInfo.lastRead().toUTC();
        QDateTime cr = fileInfo.created().toUTC();
        mTicks = (static_cast<qint64>(base.date().daysTo(lm.date()))
                * Q_UINT64_C(86400000)
                + static_cast<qint64>(base.time().msecsTo(lm.time())))
            * Q_UINT64_C(10000);
        aTicks = (static_cast<qint64>(base.date().daysTo(lr.date()))
                * Q_UINT64_C(86400000)
                + static_cast<qint64>(base.time().msecsTo(lr.time())))
            * Q_UINT64_C(10000);
        cTicks = (static_cast<qint64>(base.date().daysTo(cr.date()))
                * Q_UINT64_C(86400000)
                + static_cast<qint64>(base.time().msecsTo(cr.time())))
            * Q_UINT64_C(10000);
        QuaZipNewInfo newInfo("test.txt", "tmp/test.txt");
        newInfo.setFileNTFSTimes("tmp/test.txt");
        QVERIFY(zipFile.open(QIODevice::WriteOnly, newInfo));
        zipFile.close();
        zip.close();
    }
    {
        // check
        QuaZip zip(zipName);
        QVERIFY(zip.open(QuaZip::mdUnzip));
        zip.goToFirstFile();
        QuaZipFileInfo64 fileInfo;
        QVERIFY(zip.getCurrentFileInfo(&fileInfo));
        zip.close();
        QByteArray &extra = fileInfo.extra;
        bool ntfsFound = false;
        int timesFound = 0;
        for (int i = 0; i <= extra.size() - 4; ) {
            unsigned type = static_cast<unsigned>(static_cast<unsigned char>(
                                                      extra.at(i)))
                    | (static_cast<unsigned>(static_cast<unsigned char>(
                                                 extra.at(i + 1))) << 8);
            i += 2;
            unsigned length = static_cast<unsigned>(static_cast<unsigned char>(
                                                        extra.at(i)))
                    | (static_cast<unsigned>(static_cast<unsigned char>(
                                                 extra.at(i + 1))) << 8);
            i += 2;
            if (type == 0x000Au && length >= 32) {
                ntfsFound = true;
                i += 4; // reserved
                while (i <= extra.size() - 4) {
                    unsigned tag = static_cast<unsigned>(
                                static_cast<unsigned char>(extra.at(i)))
                            | (static_cast<unsigned>(
                                   static_cast<unsigned char>(extra.at(i + 1)))
                               << 8);
                    i += 2;
                    unsigned tagsize = static_cast<unsigned>(
                                static_cast<unsigned char>(extra.at(i)))
                            | (static_cast<unsigned>(
                                   static_cast<unsigned char>(extra.at(i + 1)))
                               << 8);
                    i += 2;
                    QCOMPARE(tagsize, static_cast<unsigned>(24));
                    if (tag == 0x0001u && tagsize >= 24) {
                        for (int timesPos = i;
                             i < timesPos + 24;
                             i += 8, tagsize -= 8) {
                            quint64 time = static_cast<quint64>(
                                static_cast<unsigned char>(extra.at(i)))
                            | (static_cast<quint64>(static_cast<unsigned char>(
                                                       extra.at(i + 1))) << 8)
                            | (static_cast<quint64>(static_cast<unsigned char>(
                                                       extra.at(i + 2))) << 16)
                            | (static_cast<quint64>(static_cast<unsigned char>(
                                                       extra.at(i + 3))) << 24)
                            | (static_cast<quint64>(static_cast<unsigned char>(
                                                       extra.at(i + 4))) << 32)
                            | (static_cast<quint64>(static_cast<unsigned char>(
                                                       extra.at(i + 5))) << 40)
                            | (static_cast<quint64>(static_cast<unsigned char>(
                                                       extra.at(i + 6))) << 48)
                            | (static_cast<quint64>(static_cast<unsigned char>(
                                                       extra.at(i + 7))) << 56);
                            ++timesFound;
                            if (i - timesPos == 0) {
                                QCOMPARE(time, mTicks);
                            } else if (i - timesPos == 8) {
                                QCOMPARE(time, aTicks);
                            } else if (i - timesPos == 16) {
                                QCOMPARE(time, cTicks);
                            } else {
                                QFAIL("Wrong position");
                            }
                        }
                        i += tagsize;
                    } else {
                        i += tagsize;
                    }
 
                }
            } else {
                i += length;
            }
        }
        QVERIFY(ntfsFound);
        QCOMPARE(timesFound, 3);
    }
    removeTestFiles(testFiles);
    curDir.remove(zipName);
}