File indexing completed on 2024-04-28 05:45:22

0001 /*
0002  *   SPDX-FileCopyrightText: 2010-2011 Frank Reininghaus <frank78ac@googlemail.com>
0003  *
0004  *   SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "testdir.h"
0008 
0009 #ifdef Q_OS_UNIX
0010 #include <utime.h>
0011 #else
0012 #include <sys/utime.h>
0013 #endif
0014 
0015 TestDir::TestDir(const QString &directoryPrefix)
0016     : QTemporaryDir(directoryPrefix)
0017 {
0018 }
0019 
0020 TestDir::~TestDir()
0021 {
0022 }
0023 
0024 QUrl TestDir::url() const
0025 {
0026     return QUrl::fromLocalFile(path());
0027 }
0028 
0029 /** The following function is taken from kdelibs/kio/tests/kiotesthelper.h, copyright (C) 2006 by David Faure */
0030 static void setTimeStamp(const QString &path, const QDateTime &mtime)
0031 {
0032 #ifdef Q_OS_UNIX
0033     struct utimbuf utbuf;
0034     utbuf.actime = mtime.toSecsSinceEpoch();
0035     utbuf.modtime = utbuf.actime;
0036     utime(QFile::encodeName(path), &utbuf);
0037 #elif defined(Q_OS_WIN)
0038     struct _utimbuf utbuf;
0039     utbuf.actime = mtime.toSecsSinceEpoch();
0040     utbuf.modtime = utbuf.actime;
0041     _wutime(reinterpret_cast<const wchar_t *>(path.utf16()), &utbuf);
0042 #endif
0043 }
0044 
0045 void TestDir::createFile(const QString &path, const QByteArray &data, const QDateTime &time)
0046 {
0047     QString absolutePath = path;
0048     makePathAbsoluteAndCreateParents(absolutePath);
0049 
0050     QFile f(absolutePath);
0051     f.open(QIODevice::WriteOnly);
0052     f.write(data);
0053     f.close();
0054 
0055     if (time.isValid()) {
0056         setTimeStamp(absolutePath, time);
0057     }
0058 
0059     Q_ASSERT(QFile::exists(absolutePath));
0060 }
0061 
0062 void TestDir::createFiles(const QStringList &files)
0063 {
0064     for (const QString &path : files) {
0065         createFile(path);
0066     }
0067 }
0068 
0069 void TestDir::createDir(const QString &path, const QDateTime &time)
0070 {
0071     QString absolutePath = path;
0072     makePathAbsoluteAndCreateParents(absolutePath);
0073     QDir(TestDir::path()).mkdir(absolutePath);
0074 
0075     if (time.isValid()) {
0076         setTimeStamp(absolutePath, time);
0077     }
0078 
0079     Q_ASSERT(QFile::exists(absolutePath));
0080 }
0081 
0082 void TestDir::removeFiles(const QStringList &files)
0083 {
0084     for (const QString &path : files) {
0085         removeFile(path);
0086     }
0087 }
0088 
0089 void TestDir::removeFile(const QString &path)
0090 {
0091     QString absolutePath = path;
0092     QFileInfo fileInfo(absolutePath);
0093     if (!fileInfo.isAbsolute()) {
0094         absolutePath = TestDir::path() + QLatin1Char('/') + path;
0095     }
0096     QFile::remove(absolutePath);
0097 }
0098 
0099 void TestDir::removeDir(const QString &path)
0100 {
0101     QString absolutePath = path;
0102     QFileInfo fileInfo(absolutePath);
0103     if (!fileInfo.isAbsolute()) {
0104         absolutePath = TestDir::path() + QLatin1Char('/') + path;
0105     }
0106     QDir dirToRemove = QDir(absolutePath);
0107     dirToRemove.removeRecursively();
0108 }
0109 
0110 void TestDir::makePathAbsoluteAndCreateParents(QString &path)
0111 {
0112     QFileInfo fileInfo(path);
0113     if (!fileInfo.isAbsolute()) {
0114         path = TestDir::path() + QLatin1Char('/') + path;
0115         fileInfo.setFile(path);
0116     }
0117 
0118     const QDir dir = fileInfo.dir();
0119     if (!dir.exists()) {
0120         createDir(dir.absolutePath());
0121     }
0122 
0123     Q_ASSERT(dir.exists());
0124 }