feat: add new interfaces for std::filesystem::path

Add for QFile::exists/symLinkTarget/remove/moveToTrash/
rename/link/copy

Change-Id: I4cbb908e945f043b2a5278a6d8d5149b2f20e871
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
JiDe Zhang 2021-07-10 09:57:22 +08:00 committed by Mårten Nordheim
parent ec3260e5c7
commit 413098c3e3
3 changed files with 126 additions and 3 deletions

View File

@ -1173,6 +1173,46 @@ qint64 QFile::size() const
\since 6.0
\overload
*/
/*!
\fn bool exists(const std::filesystem::path &fileName)
\since 6.3
\overload
*/
/*!
\fn std::filesystem::path QFile::filesystemSymLinkTarget() const
\since 6.3
Returns symLinkTarget() as \c{std::filesystem::path}.
*/
/*!
\fn std::filesystem::path QFile::filesystemSymLinkTarget(const std::filesystem::path &fileName)
\since 6.3
Returns symLinkTarget() as \c{std::filesystem::path} of \a fileName.
*/
/*!
\fn bool remove(const std::filesystem::path &fileName)
\since 6.3
\overload
*/
/*!
\fn bool moveToTrash(const std::filesystem::path &fileName, QString *pathInTrash)
\since 6.3
\overload
*/
/*!
\fn bool rename(const std::filesystem::path &oldName, const std::filesystem::path &newName)
\since 6.3
\overload
*/
/*!
\fn bool link(const std::filesystem::path &fileName, const std::filesystem::path &newName);
\since 6.3
\overload
*/
/*!
\fn bool copy(const std::filesystem::path &fileName, const std::filesystem::path &newName);
\since 6.3
\overload
*/
QT_END_NAMESPACE

View File

@ -172,51 +172,116 @@ public:
bool exists() const;
static bool exists(const QString &fileName);
#ifdef Q_CLANG_QDOC
static bool exists(const std::filesystem::path &fileName);
#elif QT_CONFIG(cxx17_filesystem)
template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
static bool exists(const T &fileName)
{
return exists(QtPrivate::fromFilesystemPath(fileName));
}
#endif // QT_CONFIG(cxx17_filesystem)
QString symLinkTarget() const;
static QString symLinkTarget(const QString &fileName);
#ifdef Q_CLANG_QDOC
std::filesystem::path filesystemSymLinkTarget() const;
static std::filesystem::path filesystemSymLinkTarget(const std::filesystem::path &fileName);
#elif QT_CONFIG(cxx17_filesystem)
std::filesystem::path filesystemSymLinkTarget() const
{
return QtPrivate::toFilesystemPath(symLinkTarget());
}
template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
static std::filesystem::path filesystemSymLinkTarget(const T &fileName)
{
return QtPrivate::toFilesystemPath(symLinkTarget(QtPrivate::fromFilesystemPath(fileName)));
}
#endif // QT_CONFIG(cxx17_filesystem)
bool remove();
static bool remove(const QString &fileName);
#ifdef Q_CLANG_QDOC
static bool remove(const std::filesystem::path &fileName);
#elif QT_CONFIG(cxx17_filesystem)
template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
static bool remove(const T &fileName)
{
return remove(QtPrivate::fromFilesystemPath(fileName));
}
#endif // QT_CONFIG(cxx17_filesystem)
bool moveToTrash();
static bool moveToTrash(const QString &fileName, QString *pathInTrash = nullptr);
#ifdef Q_CLANG_QDOC
static bool moveToTrash(const std::filesystem::path &fileName, QString *pathInTrash = nullptr);
#elif QT_CONFIG(cxx17_filesystem)
template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
static bool moveToTrash(const T &fileName, QString *pathInTrash = nullptr)
{
return moveToTrash(QtPrivate::fromFilesystemPath(fileName), pathInTrash);
}
#endif // QT_CONFIG(cxx17_filesystem)
bool rename(const QString &newName);
static bool rename(const QString &oldName, const QString &newName);
#ifdef Q_CLANG_QDOC
bool rename(const std::filesystem::path &newName);
static bool rename(const std::filesystem::path &oldName,
const std::filesystem::path &newName);
#elif QT_CONFIG(cxx17_filesystem)
template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
bool rename(const T &newName)
{
return rename(QtPrivate::fromFilesystemPath(newName));
}
template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
static bool rename(const T &oldName, const T &newName)
{
return rename(QtPrivate::fromFilesystemPath(oldName),
QtPrivate::fromFilesystemPath(newName));
}
#endif // QT_CONFIG(cxx17_filesystem)
static bool rename(const QString &oldName, const QString &newName);
bool link(const QString &newName);
static bool link(const QString &fileName, const QString &newName);
#ifdef Q_CLANG_QDOC
bool link(const std::filesystem::path &newName);
static bool link(const std::filesystem::path &fileName,
const std::filesystem::path &newName);
#elif QT_CONFIG(cxx17_filesystem)
template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
bool link(const T &newName)
{
return link(QtPrivate::fromFilesystemPath(newName));
}
template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
static bool link(const T &fileName, const T &newName)
{
return link(QtPrivate::fromFilesystemPath(fileName),
QtPrivate::fromFilesystemPath(newName));
}
#endif // QT_CONFIG(cxx17_filesystem)
static bool link(const QString &oldname, const QString &newName);
bool copy(const QString &newName);
static bool copy(const QString &fileName, const QString &newName);
#ifdef Q_CLANG_QDOC
bool copy(const std::filesystem::path &newName);
static bool copy(const std::filesystem::path &fileName,
const std::filesystem::path &newName);
#elif QT_CONFIG(cxx17_filesystem)
template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
bool copy(const T &newName)
{
return copy(QtPrivate::fromFilesystemPath(newName));
}
template<typename T, QtPrivate::ForceFilesystemPath<T> = 0>
static bool copy(const T &fileName, const T &newName)
{
return copy(QtPrivate::fromFilesystemPath(fileName),
QtPrivate::fromFilesystemPath(newName));
}
#endif // QT_CONFIG(cxx17_filesystem)
static bool copy(const QString &fileName, const QString &newName);
bool open(OpenMode flags) override;
bool open(FILE *f, OpenMode ioFlags, FileHandleFlags handleFlags=DontCloseHandle);

View File

@ -3920,13 +3920,27 @@ void tst_QFile::stdfilesystem()
path = "tile-fest";
QVERIFY(file.rename(path));
QVERIFY(fs::exists(path));
#ifdef Q_OS_WIN
fs::path linkfile { "test-link.lnk" };
#else
fs::path linkfile { "test-link" };
#endif
QVERIFY(file.link(linkfile));
QVERIFY(fs::exists(linkfile));
QVERIFY(QFile::remove(linkfile));
QVERIFY(QFile::link(file.filesystemFileName(), linkfile));
QVERIFY(fs::exists(linkfile));
QCOMPARE(QFileInfo(QFile::filesystemSymLinkTarget(linkfile)),
QFileInfo(file.filesystemFileName()));
QCOMPARE(QFileInfo(QFile(linkfile).filesystemSymLinkTarget()),
QFileInfo(file.filesystemFileName()));
fs::path copyfile { "copy-file" };
QVERIFY(file.copy(copyfile));
QVERIFY(fs::exists(copyfile));
QVERIFY(QFile::remove(copyfile));
QVERIFY(QFile::copy(file.filesystemFileName(), copyfile));
QVERIFY(fs::exists(copyfile));
QFileDevice::Permissions p = QFile::permissions(path);
QVERIFY(p.testFlag(QFile::WriteUser) || p.testFlag(QFile::WriteOwner)); // some we know for sure
@ -3935,6 +3949,10 @@ void tst_QFile::stdfilesystem()
else if (p.testFlag(QFile::ReadOwner))
p.setFlag(QFile::ReadOwner, false);
QVERIFY(QFile::setPermissions(path, p));
path = "test-exists";
fs::create_directory(path);
QVERIFY(QFile::exists(path) == fs::exists(path));
#else
QSKIP("Not supported");
#endif