tarpackagecreationstep.cpp 11.4 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8
9
10
11
12
13
14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
18
19
20
21
22
23
24
25
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
29
30
#include "tarpackagecreationstep.h"

31
#include <projectexplorer/deploymentdata.h>
32
33
34
#include <projectexplorer/project.h>
#include <projectexplorer/target.h>

35
36
37
38
#include <QDateTime>
#include <QDir>
#include <QFile>
#include <QFileInfo>
39

40
41
#include <cstring>

42
43
44
45
46
using namespace ProjectExplorer;

namespace RemoteLinux {
namespace {

47
class CreateTarStepWidget : public SimpleBuildStepConfigWidget
48
49
50
{
    Q_OBJECT
public:
51
    CreateTarStepWidget(TarPackageCreationStep *step) : SimpleBuildStepConfigWidget(step)
52
    {
53
        connect(step, SIGNAL(packageFilePathChanged()), SIGNAL(updateSummary()));
54
55
56
57
    }

    QString summaryText() const
    {
58
        TarPackageCreationStep * const step = static_cast<TarPackageCreationStep *>(this->step());
59
        if (step->packageFilePath().isEmpty()) {
60
61
62
63
            return QLatin1String("<font color=\"red\">")
                + tr("Tarball creation not possible.") + QLatin1String("</font>");
        }
        return QLatin1String("<b>") + tr("Create tarball:") + QLatin1String("</b> ")
64
            + step->packageFilePath();
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
    }
};


const int TarBlockSize = 512;
struct TarFileHeader {
    char fileName[100];
    char fileMode[8];
    char uid[8];
    char gid[8];
    char length[12];
    char mtime[12];
    char chksum[8];
    char typeflag;
    char linkname[100];
    char magic[6];
    char version[2];
    char uname[32];
    char gname[32];
    char devmajor[8];
    char devminor[8];
    char fileNamePrefix[155];
    char padding[12];
};

} // Anonymous namespace.

TarPackageCreationStep::TarPackageCreationStep(BuildStepList *bsl)
    : AbstractPackagingStep(bsl, stepId())
{
    ctor();
}

TarPackageCreationStep::TarPackageCreationStep(BuildStepList *bsl, TarPackageCreationStep *other)
    : AbstractPackagingStep(bsl, other)
{
    ctor();
}

void TarPackageCreationStep::ctor()
{
    setDefaultDisplayName(displayName());
}

109
110
111
112
113
bool TarPackageCreationStep::init()
{
    if (!AbstractPackagingStep::init())
        return false;
    m_packagingNeeded = isPackagingNeeded();
114
115
    if (m_packagingNeeded)
        m_files = target()->deploymentData().allFiles();
116
117
118
    return true;
}

119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
void TarPackageCreationStep::run(QFutureInterface<bool> &fi)
{
    setPackagingStarted();
    const bool success = doPackage(fi);
    setPackagingFinished(success);
    if (success)
        emit addOutput(tr("Packaging finished successfully."), MessageOutput);
    else
        emit addOutput(tr("Packaging failed."), ErrorMessageOutput);
    fi.reportResult(success);
}

bool TarPackageCreationStep::doPackage(QFutureInterface<bool> &fi)
{
    emit addOutput(tr("Creating tarball..."), MessageOutput);
134
    if (!m_packagingNeeded) {
135
136
137
138
139
        emit addOutput(tr("Tarball up to date, skipping packaging."), MessageOutput);
        return true;
    }

    // TODO: Optimization: Only package changed files
140
    QFile tarFile(cachedPackageFilePath());
141
142
143

    if (!tarFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
        raiseError(tr("Error: tar file %1 cannot be opened (%2).")
144
            .arg(QDir::toNativeSeparators(cachedPackageFilePath()), tarFile.errorString()));
145
146
147
        return false;
    }

148
    foreach (const DeployableFile &d, m_files) {
149
        if (d.remoteDirectory().isEmpty()) {
150
            emit addOutput(tr("No remote path specified for file '%1', skipping.")
151
                .arg(d.localFilePath().toUserOutput()), ErrorMessageOutput);
152
153
            continue;
        }
154
155
        QFileInfo fileInfo = d.localFilePath().toFileInfo();
        if (!appendFile(tarFile, fileInfo, d.remoteDirectory() + QLatin1Char('/')
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
                + fileInfo.fileName(), fi)) {
            return false;
        }
    }

    const QByteArray eofIndicator(2*sizeof(TarFileHeader), 0);
    if (tarFile.write(eofIndicator) != eofIndicator.length()) {
        raiseError(tr("Error writing tar file '%1': %2.")
            .arg(QDir::toNativeSeparators(tarFile.fileName()), tarFile.errorString()));
        return false;
    }

    return true;
}

bool TarPackageCreationStep::appendFile(QFile &tarFile, const QFileInfo &fileInfo,
    const QString &remoteFilePath, const QFutureInterface<bool> &fi)
{
    if (!writeHeader(tarFile, fileInfo, remoteFilePath))
        return false;
    if (fileInfo.isDir()) {
        QDir dir(fileInfo.absoluteFilePath());
        foreach (const QString &fileName,
                 dir.entryList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot)) {
            const QString thisLocalFilePath = dir.path() + QLatin1Char('/') + fileName;
            const QString thisRemoteFilePath  = remoteFilePath + QLatin1Char('/') + fileName;
            if (!appendFile(tarFile, QFileInfo(thisLocalFilePath), thisRemoteFilePath, fi))
                return false;
        }
        return true;
    }

    const QString nativePath = QDir::toNativeSeparators(fileInfo.filePath());
    QFile file(fileInfo.filePath());
    if (!file.open(QIODevice::ReadOnly)) {
        raiseError(tr("Error reading file '%1': %2.").arg(nativePath, file.errorString()));
        return false;
    }

    const int chunkSize = 1024*1024;

    emit addOutput(tr("Adding file '%1' to tarball...").arg(nativePath), MessageOutput);

    // TODO: Wasteful. Work with fixed-size buffer.
200
    while (!file.atEnd() && file.error() == QFile::NoError && tarFile.error() == QFile::NoError) {
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
        const QByteArray data = file.read(chunkSize);
        tarFile.write(data);
        if (fi.isCanceled())
            return false;
    }
    if (file.error() != QFile::NoError) {
        raiseError(tr("Error reading file '%1': %2.").arg(nativePath, file.errorString()));
        return false;
    }

    const int blockModulo = file.size() % TarBlockSize;
    if (blockModulo != 0)
        tarFile.write(QByteArray(TarBlockSize - blockModulo, 0));

    if (tarFile.error() != QFile::NoError) {
        raiseError(tr("Error writing tar file '%1': %2.")
            .arg(QDir::toNativeSeparators(tarFile.fileName()), tarFile.errorString()));
        return false;
    }
    return true;
}

bool TarPackageCreationStep::writeHeader(QFile &tarFile, const QFileInfo &fileInfo,
    const QString &remoteFilePath)
{
    TarFileHeader header;
227
    std::memset(&header, '\0', sizeof header);
228
229
230
231
    const QByteArray &filePath = remoteFilePath.toUtf8();
    const int maxFilePathLength = sizeof header.fileNamePrefix + sizeof header.fileName;
    if (filePath.count() > maxFilePathLength) {
        raiseError(tr("Cannot add file '%1' to tar-archive: path too long.")
232
            .arg(QDir::toNativeSeparators(remoteFilePath)));
233
234
235
236
237
        return false;
    }

    const int fileNameBytesToWrite = qMin<int>(filePath.length(), sizeof header.fileName);
    const int fileNameOffset = filePath.length() - fileNameBytesToWrite;
238
    std::memcpy(&header.fileName, filePath.data() + fileNameOffset, fileNameBytesToWrite);
239
    if (fileNameOffset > 0)
240
        std::memcpy(&header.fileNamePrefix, filePath.data(), fileNameOffset);
241
242
243
244
245
246
247
248
249
    int permissions = (0400 * fileInfo.permission(QFile::ReadOwner))
        | (0200 * fileInfo.permission(QFile::WriteOwner))
        | (0100 * fileInfo.permission(QFile::ExeOwner))
        | (040 * fileInfo.permission(QFile::ReadGroup))
        | (020 * fileInfo.permission(QFile::WriteGroup))
        | (010 * fileInfo.permission(QFile::ExeGroup))
        | (04 * fileInfo.permission(QFile::ReadOther))
        | (02 * fileInfo.permission(QFile::WriteOther))
        | (01 * fileInfo.permission(QFile::ExeOther));
250
    const QByteArray permissionString = QString::fromLatin1("%1").arg(permissions,
251
        sizeof header.fileMode - 1, 8, QLatin1Char('0')).toLatin1();
252
    std::memcpy(&header.fileMode, permissionString.data(), permissionString.length());
253
    const QByteArray uidString = QString::fromLatin1("%1").arg(fileInfo.ownerId(),
254
        sizeof header.uid - 1, 8, QLatin1Char('0')).toLatin1();
255
    std::memcpy(&header.uid, uidString.data(), uidString.length());
256
    const QByteArray gidString = QString::fromLatin1("%1").arg(fileInfo.groupId(),
257
        sizeof header.gid - 1, 8, QLatin1Char('0')).toLatin1();
258
    std::memcpy(&header.gid, gidString.data(), gidString.length());
259
    const QByteArray sizeString = QString::fromLatin1("%1").arg(fileInfo.size(),
260
        sizeof header.length - 1, 8, QLatin1Char('0')).toLatin1();
261
    std::memcpy(&header.length, sizeString.data(), sizeString.length());
262
    const QByteArray mtimeString = QString::fromLatin1("%1").arg(fileInfo.lastModified().toTime_t(),
263
        sizeof header.mtime - 1, 8, QLatin1Char('0')).toLatin1();
264
    std::memcpy(&header.mtime, mtimeString.data(), mtimeString.length());
265
266
    if (fileInfo.isDir())
        header.typeflag = '5';
267
268
    std::memcpy(&header.magic, "ustar", sizeof "ustar");
    std::memcpy(&header.version, "00", 2);
269
    const QByteArray &owner = fileInfo.owner().toUtf8();
270
    std::memcpy(&header.uname, owner.data(), qMin<int>(owner.length(), sizeof header.uname - 1));
271
    const QByteArray &group = fileInfo.group().toUtf8();
272
273
    std::memcpy(&header.gname, group.data(), qMin<int>(group.length(), sizeof header.gname - 1));
    std::memset(&header.chksum, ' ', sizeof header.chksum);
274
275
276
    quint64 checksum = 0;
    for (size_t i = 0; i < sizeof header; ++i)
        checksum += reinterpret_cast<char *>(&header)[i];
277
    const QByteArray checksumString = QString::fromLatin1("%1").arg(checksum,
278
        sizeof header.chksum - 1, 8, QLatin1Char('0')).toLatin1();
279
    std::memcpy(&header.chksum, checksumString.data(), checksumString.length());
280
281
282
    header.chksum[sizeof header.chksum-1] = 0;
    if (!tarFile.write(reinterpret_cast<char *>(&header), sizeof header)) {
        raiseError(tr("Error writing tar file '%1': %2")
283
           .arg(QDir::toNativeSeparators(cachedPackageFilePath()), tarFile.errorString()));
284
285
286
287
288
289
290
        return false;
    }
    return true;
}

QString TarPackageCreationStep::packageFileName() const
{
291
    return project()->displayName() + QLatin1String(".tar");
292
293
294
295
296
297
298
}

BuildStepConfigWidget *TarPackageCreationStep::createConfigWidget()
{
    return new CreateTarStepWidget(this);
}

299
Core::Id TarPackageCreationStep::stepId()
300
{
301
    return Core::Id("MaemoTarPackageCreationStep");
302
303
304
305
306
307
308
309
310
311
}

QString TarPackageCreationStep::displayName()
{
    return tr("Create tarball");
}

} // namespace RemoteLinux

#include "tarpackagecreationstep.moc"