Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qqmldomfilewriter.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5#include <QtCore/QRandomGenerator>
6#include <QtCore/QScopeGuard>
7
9namespace QQmlJS {
10namespace Dom {
11
13 int nBk)
14{
21 targetFile = tFile;
22 newBkFiles.clear();
23 warnings.clear();
24
25 int i = 0;
26 const int maxAttempts = 20;
27 for (; i < maxAttempts; ++i) {
29 + QString::number(QRandomGenerator::global()->generate(), 16).mid(0, 8)
30 + QStringLiteral(u".tmp"));
32 break;
33 }
34 if (i == maxAttempts) {
35 warnings.append(tr("Could not create temp file for %1").arg(targetFile));
37 return status;
38 }
40 bool success = false;
42 QT_TRY
43 {
44 auto cleanup = qScopeGuard([this, &inF, &success, nBk] {
45 inF.flush();
48 if (success) {
50 // compareFiles
52 auto closeTmpF = qScopeGuard([this] { tempFile.close(); });
53 QFile oldF(targetFile);
54 if (oldF.open(QIODevice::ReadOnly)) {
55 bool same = true;
56 while (!tempFile.atEnd() && !oldF.atEnd()) {
58 QByteArray l2 = oldF.readLine();
59 if (l1 != l2)
60 same = false;
61 }
62 if (tempFile.atEnd() && oldF.atEnd() && same) {
66 return;
67 }
68 }
69 }
70 }
71 // move to target
72 int i = 0;
73 const int maxAttempts = 10;
74 for (; i < maxAttempts; ++i) {
76 // make place for targetFile
77 QString bkFileName;
78 if (nBk < 1) {
80 } else if (nBk == 1) {
81 QString bkFileName = targetFile + QStringLiteral(u"~");
82 QFile::remove(bkFileName);
83 QFile::rename(targetFile, bkFileName);
84 } else {
85 // f~ is the oldest, further backups at f1~ .. f<nBk>~
86 // keeping an empty place for the "next" backup
87 // f~ is never overwritten
88 int iBk = 0;
89 QString bkFileName = targetFile + QStringLiteral(u"~");
90 while (++iBk < nBk) {
91 if (QFile::exists(bkFileName))
92 bkFileName = targetFile + QString::number(iBk)
93 + QStringLiteral(u"~");
94 }
95 if (iBk == nBk)
97 else
99 + QStringLiteral(u"~"));
100 QFile::remove(bkFileName);
101 QFile::rename(targetFile, bkFileName);
102 }
103 if (!bkFileName.isEmpty() && QFile::rename(targetFile, bkFileName))
104 newBkFiles.append(bkFileName);
105 }
108 shouldRemoveTempFile = false;
109 return;
110 }
111 }
112 warnings.append(
113 tr("Rename of file %1 to %2 failed").arg(tempFile.fileName(), targetFile));
115 } else {
116 warnings.append(tr("Error while writing"));
117 }
118 });
119 success = write(inF);
120 }
121 QT_CATCH(...)
122 {
123 warnings.append(tr("Exception trying to write file %1").arg(targetFile));
125 }
126 if (status == Status::ShouldWrite)
127 status = Status::SkippedDueToFailure;
128 return status;
129}
130
131} // namespace Dom
132} // namespace QQmlJS
134
135#include "moc_qqmldomfilewriter_p.cpp"
\inmodule QtCore
Definition qbytearray.h:57
bool flush()
Flushes any buffered data to the file.
bool atEnd() const override
Returns true if the end of the file has been reached; otherwise returns false.
void close() override
Calls QFileDevice::flush() and closes the file.
\inmodule QtCore
Definition qfile.h:93
QFILE_MAYBE_NODISCARD bool open(OpenMode flags) override
Opens the file using OpenMode mode, returning true if successful; otherwise false.
Definition qfile.cpp:904
bool remove()
Removes the file specified by fileName().
Definition qfile.cpp:419
void setFileName(const QString &name)
Sets the name of the file.
Definition qfile.cpp:302
QString fileName() const override
Returns the name set by setFileName() or to the QFile constructors.
Definition qfile.cpp:277
bool rename(const QString &newName)
Renames the file currently specified by fileName() to newName.
Definition qfile.cpp:554
bool exists() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qfile.cpp:351
qint64 readLine(char *data, qint64 maxlen)
This function reads a line of ASCII characters from the device, up to a maximum of maxSize - 1 bytes,...
Status write(const QString &targetFile, function_ref< bool(QTextStream &)> write, int nBk=2)
static Q_DECL_CONST_FUNCTION QRandomGenerator * global()
\threadsafe
Definition qrandom.h:275
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:8084
\inmodule QtCore
Combined button and popup list for selecting options.
#define QT_CATCH(A)
#define QT_TRY
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
QScopeGuard< typename std::decay< F >::type > qScopeGuard(F &&f)
[qScopeGuard]
Definition qscopeguard.h:60
SSL_CTX int void * arg
#define QStringLiteral(str)
#define tr(X)
gzip write("uncompressed data")