tst_QFile: Introduce StdioFileGuard
Guard the FILE * obtained by fopen() by a RAI class ensuring the file is closed on destruction. Change-Id: I9297f91ca2120238f3a44bad92bca5f920e01aa8 Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@qt.io>
This commit is contained in:
parent
e8d2e831a4
commit
cd45d0f712
@ -109,6 +109,30 @@ QT_END_NAMESPACE
|
||||
|
||||
Q_DECLARE_METATYPE(QFile::FileError)
|
||||
|
||||
|
||||
class StdioFileGuard
|
||||
{
|
||||
Q_DISABLE_COPY(StdioFileGuard)
|
||||
public:
|
||||
explicit StdioFileGuard(FILE *f = nullptr) : m_file(f) {}
|
||||
~StdioFileGuard() { close(); }
|
||||
|
||||
operator FILE *() const { return m_file; }
|
||||
|
||||
void close();
|
||||
|
||||
private:
|
||||
FILE * m_file;
|
||||
};
|
||||
|
||||
void StdioFileGuard::close()
|
||||
{
|
||||
if (m_file != nullptr) {
|
||||
fclose(m_file);
|
||||
m_file = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
class tst_QFile : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
@ -660,14 +684,13 @@ void tst_QFile::size()
|
||||
}
|
||||
|
||||
{
|
||||
QFile f;
|
||||
FILE* stream = QT_FOPEN(filename.toLocal8Bit().constData(), "rb");
|
||||
StdioFileGuard stream(QT_FOPEN(filename.toLocal8Bit().constData(), "rb"));
|
||||
QVERIFY( stream );
|
||||
QFile f;
|
||||
QVERIFY( f.open(stream, QIODevice::ReadOnly) );
|
||||
QCOMPARE( f.size(), size );
|
||||
|
||||
f.close();
|
||||
fclose(stream);
|
||||
}
|
||||
|
||||
{
|
||||
@ -1598,6 +1621,7 @@ void tst_QFile::largeUncFileSupport()
|
||||
qint64 dataOffset = Q_INT64_C(8589914592);
|
||||
QByteArray knownData("LargeFile content at offset 8589914592");
|
||||
QString largeFile("//" + QtNetworkSettings::winServerName() + "/testsharelargefile/file.bin");
|
||||
const QByteArray largeFileEncoded = QFile::encodeName(largeFile);
|
||||
|
||||
{
|
||||
// 1) Native file handling.
|
||||
@ -1612,24 +1636,24 @@ void tst_QFile::largeUncFileSupport()
|
||||
}
|
||||
{
|
||||
// 2) stdlib file handling.
|
||||
StdioFileGuard fh(fopen(largeFileEncoded.constData(), "rb"));
|
||||
QVERIFY(fh);
|
||||
QFile file;
|
||||
FILE *fh = fopen(QFile::encodeName(largeFile).data(), "rb");
|
||||
QVERIFY(file.open(fh, QIODevice::ReadOnly));
|
||||
QCOMPARE(file.size(), size);
|
||||
QVERIFY(file.seek(dataOffset));
|
||||
QCOMPARE(file.read(knownData.size()), knownData);
|
||||
fclose(fh);
|
||||
}
|
||||
{
|
||||
// 3) stdio file handling.
|
||||
QFile file;
|
||||
FILE *fh = fopen(QFile::encodeName(largeFile).data(), "rb");
|
||||
StdioFileGuard fh(fopen(largeFileEncoded.constData(), "rb"));
|
||||
QVERIFY(fh);
|
||||
int fd = int(_fileno(fh));
|
||||
QFile file;
|
||||
QVERIFY(file.open(fd, QIODevice::ReadOnly));
|
||||
QCOMPARE(file.size(), size);
|
||||
QVERIFY(file.seek(dataOffset));
|
||||
QCOMPARE(file.read(knownData.size()), knownData);
|
||||
fclose(fh);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@ -1670,7 +1694,7 @@ void tst_QFile::bufferedRead()
|
||||
file.write("abcdef");
|
||||
file.close();
|
||||
|
||||
FILE *stdFile = fopen("stdfile.txt", "r");
|
||||
StdioFileGuard stdFile(fopen("stdfile.txt", "r"));
|
||||
QVERIFY(stdFile);
|
||||
char c;
|
||||
QCOMPARE(int(fread(&c, 1, 1, stdFile)), 1);
|
||||
@ -1685,8 +1709,6 @@ void tst_QFile::bufferedRead()
|
||||
QCOMPARE(c, 'b');
|
||||
QCOMPARE(file.pos(), qlonglong(2));
|
||||
}
|
||||
|
||||
fclose(stdFile);
|
||||
}
|
||||
|
||||
#ifdef Q_OS_UNIX
|
||||
@ -1815,7 +1837,7 @@ void tst_QFile::FILEReadWrite()
|
||||
f.close();
|
||||
}
|
||||
|
||||
FILE *fp = fopen("FILEReadWrite.txt", "r+b");
|
||||
StdioFileGuard fp(fopen("FILEReadWrite.txt", "r+b"));
|
||||
QVERIFY(fp);
|
||||
QFile file;
|
||||
QVERIFY2(file.open(fp, QFile::ReadWrite), msgOpenFailed(file).constData());
|
||||
@ -1850,7 +1872,7 @@ void tst_QFile::FILEReadWrite()
|
||||
|
||||
}
|
||||
file.close();
|
||||
fclose(fp);
|
||||
fp.close();
|
||||
|
||||
// check modified file
|
||||
{
|
||||
@ -2435,11 +2457,10 @@ void tst_QFile::virtualFile()
|
||||
|
||||
void tst_QFile::textFile()
|
||||
{
|
||||
#if defined(Q_OS_WIN)
|
||||
FILE *fs = ::fopen("writeabletextfile", "wt");
|
||||
#else
|
||||
FILE *fs = ::fopen("writeabletextfile", "w");
|
||||
#endif
|
||||
const char *openMode = QOperatingSystemVersion::current().type() != QOperatingSystemVersion::Windows
|
||||
? "w" : "wt";
|
||||
StdioFileGuard fs(fopen("writeabletextfile", openMode));
|
||||
QVERIFY(fs);
|
||||
QFile f;
|
||||
QByteArray part1("This\nis\na\nfile\nwith\nnewlines\n");
|
||||
QByteArray part2("Add\nsome\nmore\nnewlines\n");
|
||||
@ -2448,7 +2469,7 @@ void tst_QFile::textFile()
|
||||
f.write(part1);
|
||||
f.write(part2);
|
||||
f.close();
|
||||
::fclose(fs);
|
||||
fs.close();
|
||||
|
||||
QFile file("writeabletextfile");
|
||||
QVERIFY2(file.open(QIODevice::ReadOnly), msgOpenFailed(file).constData());
|
||||
@ -2704,11 +2725,12 @@ void tst_QFile::handle()
|
||||
|
||||
//test round trip of adopted stdio file handle
|
||||
QFile file2;
|
||||
FILE *fp = fopen(qPrintable(m_testSourceFile), "r");
|
||||
StdioFileGuard fp(fopen(qPrintable(m_testSourceFile), "r"));
|
||||
QVERIFY(fp);
|
||||
file2.open(fp, QIODevice::ReadOnly);
|
||||
QCOMPARE(int(file2.handle()), int(fileno(fp)));
|
||||
QCOMPARE(int(file2.handle()), int(fileno(fp)));
|
||||
fclose(fp);
|
||||
fp.close();
|
||||
|
||||
//test round trip of adopted posix file handle
|
||||
#ifdef Q_OS_UNIX
|
||||
|
Loading…
x
Reference in New Issue
Block a user