summaryrefslogtreecommitdiffstats
path: root/src/libs/kdtools/updateoperations.cpp
blob: 9301d4f1322a3cc154ee278ae6597d03521af67b (plain)
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
/****************************************************************************
**
** Copyright (C) 2013 Klaralvdalens Datakonsult AB (KDAB)
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Installer Framework.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** 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 The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "updateoperations.h"
#include "errors.h"
#include "fileutils.h"
#include "constants.h"
#include "packagemanagercore.h"

#include <QDir>
#include <QFile>
#include <QTextStream>
#include <QTemporaryFile>
#include <QFileInfo>

#include <cerrno>

using namespace KDUpdater;

static QString errnoToQString(int error)
{
#if defined(Q_OS_WIN) && !defined(Q_CC_MINGW)
    char msg[128];
    if (strerror_s(msg, sizeof msg, error) != 0)
        return QString::fromLocal8Bit(msg);
    return QString();
#else
    return QString::fromLocal8Bit(strerror(error));
#endif
}

static bool removeDirectory(const QString &path, QString *errorString, bool force, const QStringList &ignoreFiles = QStringList())
{
    Q_ASSERT(errorString);

    QDir dir = path;
    const QFileInfoList entries = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::AllEntries | QDir::Hidden);
    foreach (const QFileInfo &entry, entries) {
        if (entry.isDir() && (!entry.isSymLink()))
            removeDirectory(entry.filePath(), errorString, force, ignoreFiles);
        else if (ignoreFiles.contains(entry.absoluteFilePath()))
            continue;
        else if (force && (!QFile(entry.filePath()).remove()))
            return false;
    }

    // even remove some hidden, OS-created files in there
    QInstaller::removeSystemGeneratedFiles(path);

    errno = 0;
    const bool success = dir.rmdir(path);

    if (!success && (dir.entryList(QDir::NoDotAndDotDot | QDir::AllEntries | QDir::Hidden).count()
                     == ignoreFiles.count())){
        return true;
    }

    if (errno)
        *errorString = errnoToQString(errno);
    return success;
}

/*
 * \internal
 * Returns a filename for a temporary file based on \a templateName
 */
static QString backupFileName(const QString &templateName)
{
    QTemporaryFile file(templateName);
    file.open();
    const QString name = file.fileName();
    file.close();
    file.remove();
    return name;
}

////////////////////////////////////////////////////////////////////////////
// KDUpdater::CopyOperation
////////////////////////////////////////////////////////////////////////////

/*!
    \inmodule KDUpdater
    \class KDUpdater::CopyOperation
    \internal
*/

CopyOperation::CopyOperation(QInstaller::PackageManagerCore *core)
    : UpdateOperation(core)
{
    setName(QLatin1String("Copy"));
}

CopyOperation::~CopyOperation()
{
    deleteFileNowOrLater(value(QLatin1String("backupOfExistingDestination")).toString());
}

QString CopyOperation::sourcePath()
{
    return arguments().first();
}

QString CopyOperation::destinationPath()
{
    QString destination = arguments().last();

    // if the target is a directory use the source filename to complete the destination path
    if (QFileInfo(destination).isDir())
        destination = QDir(destination).filePath(QFileInfo(sourcePath()).fileName());
    return destination;
}


void CopyOperation::backup()
{
    QString destination = destinationPath();

    if (!QFile::exists(destination)) {
        clearValue(QLatin1String("backupOfExistingDestination"));
        return;
    }

    setValue(QLatin1String("backupOfExistingDestination"), backupFileName(destination));

    // race condition: The backup file could get created by another process right now. But this is the same
    // in QFile::copy...
    if (!QFile::rename(destination, value(QLatin1String("backupOfExistingDestination")).toString()))
        setError(UserDefinedError, tr("Cannot backup file \"%1\".").arg(QDir::toNativeSeparators(destination)));
}

bool CopyOperation::performOperation()
{
    // We need two args to complete the copy operation. First arg provides the complete file name of source
    // Second arg provides the complete file name of dest
    if (!checkArgumentCount(2))
        return false;

    QString source = sourcePath();
    QString destination = destinationPath();

    QFile sourceFile(source);
    if (!sourceFile.exists()) {
        setError(UserDefinedError);
        setErrorString(tr("Cannot copy a non-existent file: %1").arg(QDir::toNativeSeparators(source)));
        return false;
    }
    // If destination file exists, we cannot use QFile::copy() because it does not overwrite an existing
    // file. So we remove the destination file.
    QFile destinationFile(destination);
    if (destinationFile.exists()) {
        if (!destinationFile.remove()) {
            setError(UserDefinedError);
            setErrorString(tr("Cannot remove file \"%1\": %2").arg(
                               QDir::toNativeSeparators(destination), destinationFile.errorString()));
            return false;
        }
    }

    const bool copied = sourceFile.copy(destination);
    if (!copied) {
        setError(UserDefinedError);
        setErrorString(tr("Cannot copy file \"%1\" to \"%2\": %3").arg(
                           QDir::toNativeSeparators(source), QDir::toNativeSeparators(destination),
                           sourceFile.errorString()));
    }
    return copied;
}

bool CopyOperation::undoOperation()
{
    QString source = sourcePath();
    QString destination = destinationPath();

    // if the target is a directory use the source filename to complete the destination path
    if (QFileInfo(destination).isDir())
        destination = destination + QDir::separator() + QFileInfo(source).fileName();

    QFile destFile(destination);
    // first remove the dest
    if (destFile.exists() && !destFile.remove()) {
        setError(UserDefinedError, tr("Cannot delete file \"%1\": %2").arg(
                     QDir::toNativeSeparators(destination), destFile.errorString()));
        return false;
    }

    // no backup was done:
    // the copy destination file wasn't existing yet - that's no error
    if (!hasValue(QLatin1String("backupOfExistingDestination")))
        return true;

    QFile backupFile(value(QLatin1String("backupOfExistingDestination")).toString());
    // otherwise we have to copy the backup back:
    const bool success = backupFile.rename(destination);
    if (!success)
        setError(UserDefinedError, tr("Cannot restore backup file into \"%1\": %2").arg(
                     QDir::toNativeSeparators(destination), backupFile.errorString()));
    return success;
}

/*!
 \reimp
 */
QDomDocument CopyOperation::toXml() const
{
    // we don't want to save the backupOfExistingDestination
    if (!hasValue(QLatin1String("backupOfExistingDestination")))
        return UpdateOperation::toXml();

    CopyOperation *const me = const_cast<CopyOperation *>(this);

    const QVariant v = value(QLatin1String("backupOfExistingDestination"));
    me->clearValue(QLatin1String("backupOfExistingDestination"));
    const QDomDocument xml = UpdateOperation::toXml();
    me->setValue(QLatin1String("backupOfExistingDestination"), v);
    return xml;
}

bool CopyOperation::testOperation()
{
    // TODO
    return true;
}


////////////////////////////////////////////////////////////////////////////
// KDUpdater::MoveOperation
////////////////////////////////////////////////////////////////////////////

/*!
    \inmodule KDUpdater
    \class KDUpdater::MoveOperation
    \internal
*/

MoveOperation::MoveOperation(QInstaller::PackageManagerCore *core)
    : UpdateOperation(core)
{
    setName(QLatin1String("Move"));
}

MoveOperation::~MoveOperation()
{
    deleteFileNowOrLater(value(QLatin1String("backupOfExistingDestination")).toString());
}

void MoveOperation::backup()
{
    const QString dest = arguments().last();
    if (!QFile::exists(dest)) {
        clearValue(QLatin1String("backupOfExistingDestination"));
        return;
    }

    setValue(QLatin1String("backupOfExistingDestination"), backupFileName(dest));

    // race condition: The backup file could get created by another process right now. But this is the same
    // in QFile::copy...
    if (!QFile::rename(dest, value(QLatin1String("backupOfExistingDestination")).toString()))
        setError(UserDefinedError, tr("Cannot backup file \"%1\".").arg(QDir::toNativeSeparators(dest)));
}

bool MoveOperation::performOperation()
{
    // We need two args to complete the copy operation. // First arg provides the complete file name of
    // source, second arg provides the complete file name of dest
    if (!checkArgumentCount(2))
        return false;

    const QStringList args = arguments();
    const QString dest = args.at(1);
    // If destination file exists, then we cannot use QFile::copy() because it does not overwrite an existing
    // file. So we remove the destination file.
    if (QFile::exists(dest)) {
        QFile file(dest);
        if (!file.remove(dest)) {
            setError(UserDefinedError);
            setErrorString(tr("Cannot remove file \"%1\": %2").arg(
                               QDir::toNativeSeparators(dest), file.errorString()));
            return false;
        }
    }

    // Copy source to destination.
    QFile file(args.at(0));
    if (!file.copy(dest)) {
        setError(UserDefinedError);
        setErrorString(tr("Cannot copy file \"%1\" to \"%2\": %3").arg(QDir::toNativeSeparators(file.fileName()),
                                                                 QDir::toNativeSeparators(dest), file.errorString()));
        return false;
    }
    return deleteFileNowOrLater(file.fileName());
}

bool MoveOperation::undoOperation()
{
    const QStringList args = arguments();
    const QString dest = args.last();
    // first: copy back the destination to source
    QFile destF(dest);
    if (!destF.copy(args.first())) {
        setError(UserDefinedError, tr("Cannot copy file \"%1\" to \"%2\": %3").arg(
                     QDir::toNativeSeparators(dest), QDir::toNativeSeparators(args.first()), destF.errorString()));
        return false;
    }

    // second: delete the move destination
    if (!deleteFileNowOrLater(dest)) {
        setError(UserDefinedError, tr("Cannot remove file \"%1\".").arg(QDir::toNativeSeparators(dest)));
        return false;
    }

    // no backup was done:
    // the move destination file wasn't existing yet - that's no error
    if (!hasValue(QLatin1String("backupOfExistingDestination")))
        return true;

    // otherwise we have to copy the backup back:
    QFile backupF(value(QLatin1String("backupOfExistingDestination")).toString());
    const bool success = backupF.rename(dest);
    if (!success)
        setError(UserDefinedError, tr("Cannot restore backup file for \"%1\": %2").arg(
                     QDir::toNativeSeparators(dest), backupF.errorString()));

    return success;
}

bool MoveOperation::testOperation()
{
    // TODO
    return true;
}


////////////////////////////////////////////////////////////////////////////
// KDUpdater::DeleteOperation
////////////////////////////////////////////////////////////////////////////

/*!
    \inmodule KDUpdater
    \class KDUpdater::DeleteOperation
    \internal
*/

DeleteOperation::DeleteOperation(QInstaller::PackageManagerCore *core)
    : UpdateOperation(core)
{
    setName(QLatin1String("Delete"));
}

DeleteOperation::~DeleteOperation()
{
    deleteFileNowOrLater(value(QLatin1String("backupOfExistingFile")).toString());
}

void DeleteOperation::backup()
{
    const QString fileName = arguments().first();
    setValue(QLatin1String("backupOfExistingFile"), backupFileName(fileName));

    QFile file(fileName);
    if (!file.copy(value(QLatin1String("backupOfExistingFile")).toString()))
        setError(UserDefinedError, tr("Cannot create backup of file \"%1\": %2").arg(
                     QDir::toNativeSeparators(fileName), file.errorString()));
}

bool DeleteOperation::performOperation()
{
    // Requires only one parameter. That is the name of the file to remove.
    if (!checkArgumentCount(1))
        return false;

    return deleteFileNowOrLater(arguments().at(0));
}

bool DeleteOperation::undoOperation()
{
    if (!hasValue(QLatin1String("backupOfExistingFile")))
        return true;

    const QString fileName = arguments().first();
    QFile backupF(value(QLatin1String("backupOfExistingFile")).toString());
    const bool success = backupF.copy(fileName) && deleteFileNowOrLater(backupF.fileName());
    if (!success)
        setError(UserDefinedError, tr("Cannot restore backup file for \"%1\": %2").arg(
                     QDir::toNativeSeparators(fileName), backupF.errorString()));
    return success;
}

bool DeleteOperation::testOperation()
{
    // TODO
    return true;
}

/*!
 \reimp
 */
QDomDocument DeleteOperation::toXml() const
{
    // we don't want to save the backupOfExistingFile
    if (!hasValue(QLatin1String("backupOfExistingFile")))
        return UpdateOperation::toXml();

    DeleteOperation *const me = const_cast<DeleteOperation *>(this);

    const QVariant v = value(QLatin1String("backupOfExistingFile"));
    me->clearValue(QLatin1String("backupOfExistingFile"));
    const QDomDocument xml = UpdateOperation::toXml();
    me->setValue(QLatin1String("backupOfExistingFile"), v);
    return xml;
}

////////////////////////////////////////////////////////////////////////////
// KDUpdater::MkdirOperation
////////////////////////////////////////////////////////////////////////////

/*!
    \inmodule KDUpdater
    \class KDUpdater::MkdirOperation
    \internal
*/

MkdirOperation::MkdirOperation(QInstaller::PackageManagerCore *core)
    : UpdateOperation(core)
{
    setName(QLatin1String("Mkdir"));
}

void MkdirOperation::backup()
{
    QString path = arguments().first();
    path.replace(QLatin1Char('\\'), QLatin1Char('/'));

    QDir createdDir = QDir::root();

    // find out, which part of the path is the first one we actually need to create
    int end = 0;
    while (true) {
        QString p = path.section(QLatin1Char('/'), 0, ++end);
        createdDir = QDir(p);
        if (!createdDir.exists())
            break;
        if (p == path) {
            // everything did already exist -> nothing to do for us (nothing to revert then, either)
            createdDir = QDir::root();
            break;
        }
    }

    setValue(QLatin1String("createddir"), createdDir.absolutePath());
}

bool MkdirOperation::performOperation()
{
    // Requires only one parameter. That is the path which should be created
    if (!checkArgumentCount(1))
        return false;

    const QString dirName = arguments().at(0);
    const bool created = QDir::root().mkpath(dirName);
    if (!created) {
        setError(UserDefinedError);
        setErrorString(tr("Cannot create directory \"%1\": %2").arg(
                           QDir::toNativeSeparators(dirName), tr("Unknown error.")));
    }
    return created;
}

bool MkdirOperation::undoOperation()
{
    Q_ASSERT(arguments().count() == 1);

    QString createdDirValue = value(QLatin1String("createddir")).toString();
    if (packageManager()) {
        createdDirValue = QInstaller::replacePath(createdDirValue,
            QLatin1String(QInstaller::scRelocatable), packageManager()->value(QInstaller::scTargetDir));
    }

    if (createdDirValue.isEmpty())
        createdDirValue = arguments().first();
    QDir createdDir = QDir(createdDirValue);
    const bool forceremoval = QVariant(value(QLatin1String("forceremoval"))).toBool();

    // Since refactoring we know the mkdir operation which is creating the target path. If we do a full
    // uninstall prevent removing the full path including target, instead remove the target only. (QTIFW-46)
    QStringList excludeFiles;
    if (hasValue(QLatin1String("uninstall-only")) && value(QLatin1String("uninstall-only")).toBool()) {
        createdDir = QDir(arguments().first());
        // Do not try to remove maintenancetool in Windows as it will fail as it is running.
        // In Windows we use deferred delete for maintenancetool.
    #ifdef Q_OS_WIN
        if (packageManager())
            excludeFiles << packageManager()->maintenanceToolName();
    #endif
    }

    if (createdDir == QDir::root())
        return true;

    if (!createdDir.exists())
        return true;

    QString errorString;

    const bool result = removeDirectory(createdDir.path(), &errorString, forceremoval, excludeFiles);

    if (!result) {
        if (errorString.isEmpty())
            setError(UserDefinedError, tr("Cannot remove directory \"%1\": %2").arg(
                         QDir::toNativeSeparators(createdDir.path()), errorString));
        else
            setError(UserDefinedError, tr("Cannot remove directory \"%1\": %2").arg(
                         QDir::toNativeSeparators(createdDir.path()), errnoToQString(errno)));
    }
    return result;
}

bool KDUpdater::MkdirOperation::testOperation()
{
    // TODO
    return true;
}


////////////////////////////////////////////////////////////////////////////
// KDUpdater::RmdirOperation
////////////////////////////////////////////////////////////////////////////

/*!
    \inmodule KDUpdater
    \class KDUpdater::RmdirOperation
    \internal
*/

RmdirOperation::RmdirOperation(QInstaller::PackageManagerCore *core)
    : UpdateOperation(core)
{
    setName(QLatin1String("Rmdir"));
    setValue(QLatin1String("removed"), false);
}

void RmdirOperation::backup()
{
    // nothing to backup - rollback will just create the directory
}

bool RmdirOperation::performOperation()
{
    // Requires only one parameter. That is the name of the file to remove.
    if (!checkArgumentCount(1))
        return false;

    const QString firstArg = arguments().at(0);
    QDir dir(firstArg);
    if (!dir.exists()) {
        setError(UserDefinedError);
        setErrorString(tr("Cannot remove directory \"%1\": %2").arg(
                           QDir::toNativeSeparators(firstArg), tr("The directory does not exist.")));
        return false;
    }

    errno = 0;
    const bool removed = dir.rmdir(firstArg);
    setValue(QLatin1String("removed"), removed);
    if (!removed) {
        setError(UserDefinedError);
        setErrorString(tr("Cannot remove directory \"%1\": %2").arg(
                           QDir::toNativeSeparators(firstArg), errnoToQString(errno)));
    }
    return removed;
}

bool RmdirOperation::undoOperation()
{
   if (!value(QLatin1String("removed")).toBool())
        return true;

    errno = 0;
    const QFileInfo fi(arguments().first());
    const bool success = fi.dir().mkdir(fi.fileName());
    if( !success)
        setError(UserDefinedError, tr("Cannot recreate directory \"%1\": %2").arg(
                     QDir::toNativeSeparators(fi.fileName()), errnoToQString(errno)));

    return success;
}

bool RmdirOperation::testOperation()
{
    // TODO
    return true;
}


////////////////////////////////////////////////////////////////////////////
// KDUpdater::AppendFileOperation
////////////////////////////////////////////////////////////////////////////

/*!
    \inmodule KDUpdater
    \class KDUpdater::AppendFileOperation
    \internal
*/

AppendFileOperation::AppendFileOperation(QInstaller::PackageManagerCore *core)
    : UpdateOperation(core)
{
    setName(QLatin1String("AppendFile"));
}

void AppendFileOperation::backup()
{
    const QString filename = arguments().first();

    QFile file(filename);
    if (!file.exists())
        return; // nothing to backup

    setValue(QLatin1String("backupOfFile"), backupFileName(filename));
    if (!file.copy(value(QLatin1String("backupOfFile")).toString())) {
        setError(UserDefinedError, tr("Cannot backup file \"%1\": %2").arg(
                     QDir::toNativeSeparators(filename), file.errorString()));
        clearValue(QLatin1String("backupOfFile"));
    }
}

bool AppendFileOperation::performOperation()
{
    // This operation takes two arguments. First argument is the name of the file into which a text has to be
    // appended. Second argument is the text to append.
    if (!checkArgumentCount(2))
        return false;

    const QStringList args = this->arguments();
    const QString fName = args.at(0);
    QFile file(fName);
    if (!file.open(QFile::Append)) {
        // first we rename the file, then we copy it to the real target and open the copy - the renamed original is then marked for deletion
        bool error = false;
        const QString newName = backupFileName(fName);

        if (!QFile::rename(fName, newName))
            error = true;

        if (!error && !QFile::copy(newName, fName)) {
            error = true;
            QFile::rename(newName, fName);
        }

        if (!error && !file.open(QFile::Append)) {
            error = true;
            deleteFileNowOrLater(newName);
        }

        if (error) {
            setError(UserDefinedError);
            setErrorString(tr("Cannot open file \"%1\" for writing: %2").arg(
                               QDir::toNativeSeparators(file.fileName()), file.errorString()));
            return false;
        }
        deleteFileNowOrLater(newName);
    }

    QTextStream ts(&file);
    ts << args.at(1);
    file.close();

    return true;
}

bool AppendFileOperation::undoOperation()
{
   // backupOfFile being empty -> file didn't exist before -> no error
    const QString filename = arguments().first();
    const QString backupOfFile = value(QLatin1String("backupOfFile")).toString();
    if (!backupOfFile.isEmpty() && !QFile::exists(backupOfFile)) {
        setError(UserDefinedError, tr("Cannot find backup file for \"%1\".").arg(
                     QDir::toNativeSeparators(filename)));
        return false;
    }

    const bool removed = deleteFileNowOrLater(filename);
    if (!removed) {
        setError(UserDefinedError, tr("Cannot restore backup file for \"%1\".").arg(
                     QDir::toNativeSeparators(filename)));
        return false;
    }

    // got deleted? We might be done, if it didn't exist before
    if (backupOfFile.isEmpty())
        return true;

    QFile backupFile(backupOfFile);
    const bool success = backupFile.rename(filename);
    if (!success)
        setError(UserDefinedError, tr("Cannot restore backup file for \"%1\": %2").arg(
                     QDir::toNativeSeparators(filename), backupFile.errorString()));
    return success;
}

bool AppendFileOperation::testOperation()
{
    // TODO
    return true;
}


////////////////////////////////////////////////////////////////////////////
// KDUpdater::PrependFileOperation
////////////////////////////////////////////////////////////////////////////

/*!
    \inmodule KDUpdater
    \class KDUpdater::PrependFileOperation
    \internal
*/

PrependFileOperation::PrependFileOperation(QInstaller::PackageManagerCore *core)
    : UpdateOperation(core)
{
    setName(QLatin1String("PrependFile"));
}

void PrependFileOperation::backup()
{
    const QString filename = arguments().first();

    QFile file(filename);
    if (!file.exists())
        return; // nothing to backup

    setValue(QLatin1String("backupOfFile"), backupFileName(filename));
    if (!file.copy(value(QLatin1String("backupOfFile")).toString())) {
        setError(UserDefinedError, tr("Cannot backup file \"%1\": %2").arg(
                     QDir::toNativeSeparators(filename), file.errorString()));
        clearValue(QLatin1String("backupOfFile"));
    }
}

bool PrependFileOperation::performOperation()
{
    // This operation takes two arguments. First argument is the name
    // of the file into which a text has to be appended. Second argument
    // is the text to append.
    if (!checkArgumentCount(2))
        return false;

    const QStringList args = this->arguments();
    const QString fName = args.at(0);
    // Load the file first.
    QFile file(fName);
    if (!file.open(QFile::ReadOnly)) {
        setError(UserDefinedError);
        setErrorString(tr("Cannot open file \"%1\" for reading: %2").arg(
                           QDir::toNativeSeparators(file.fileName()), file.errorString()));
        return false;
    }

    // TODO: fix this, use a text stream
    QString fContents(QLatin1String(file.readAll()));
    file.close();

    // Prepend text to the file text
    fContents = args.at(1) + fContents;

    // Now re-open the file in write only mode.
    if (!file.open(QFile::WriteOnly)) {
        // first we rename the file, then we copy it to the real target and open the copy - the renamed original is then marked for deletion
        const QString newName = backupFileName(fName);
        if (!QFile::rename(fName, newName) && QFile::copy(newName, fName) && file.open(QFile::WriteOnly)) {
            QFile::rename(newName, fName);
            setError(UserDefinedError);
            setErrorString(tr("Cannot open file \"%1\" for writing: %2").arg(
                               QDir::toNativeSeparators(file.fileName()), file.errorString()));
            return false;
        }
        deleteFileNowOrLater(newName);
    }
    QTextStream ts(&file);
    ts << fContents;
    file.close();

    return true;
}

bool PrependFileOperation::undoOperation()
{
    // bockupOfFile being empty -> file didn't exist before -> no error
    const QString filename = arguments().first();
    const QString backupOfFile = value(QLatin1String("backupOfFile")).toString();
    if (!backupOfFile.isEmpty() && !QFile::exists(backupOfFile)) {
        setError(UserDefinedError,
                 tr("Cannot find backup file for \"%1\".").arg(QDir::toNativeSeparators(filename)));
        return false;
    }

    if (!deleteFileNowOrLater(filename)) {
        setError(UserDefinedError,
                 tr("Cannot restore backup file for \"%1\".").arg(QDir::toNativeSeparators(filename)));
        return false;
    }

    // got deleted? We might be done, if it didn't exist before
    if (backupOfFile.isEmpty())
        return true;

    QFile backupF(backupOfFile);
    const bool success = backupF.rename(filename);
    if (!success)
        setError(UserDefinedError, tr("Cannot restore backup file for \"%1\": %2").arg(
                     QDir::toNativeSeparators(filename), backupF.errorString()));

    return success;
}

bool PrependFileOperation::testOperation()
{
    // TODO
    return true;
}