From 6875851892aa33392c7b62f094de1b89e57aa48d Mon Sep 17 00:00:00 2001 From: Drwsburah Date: Sun, 31 Mar 2024 19:48:57 +0100 Subject: [PATCH] Implemented database file hidden attribute preservation on Windows (#10343) * Implemented database file hidden attribute preservation on Windows Implemented database file hidden attribute preservation on Windows by modifying the save function to check the hidden attribute of the original database before saving and then reapply it post-saving if running on Windows so that users can easily store their database in a hidden file without having to re-hide it every time it's modified. Updated the TestDatabase::testSaveAs() unit test to first verify after the initial save that the database file is not hidden before hiding it then saving again and verifying that it is now hidden. Signed-off-by: Drwsburah Co-authored-by: Jonathan White --- src/core/Database.cpp | 16 ++++++++++++++++ tests/TestDatabase.cpp | 11 +++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/core/Database.cpp b/src/core/Database.cpp index 768d5e262..667a6a169 100644 --- a/src/core/Database.cpp +++ b/src/core/Database.cpp @@ -33,6 +33,10 @@ #include #include +#ifdef Q_OS_WIN +#include +#endif + QHash> Database::s_uuidMap; Database::Database() @@ -278,6 +282,11 @@ bool Database::saveAs(const QString& filePath, SaveAction action, const QString& QFileInfo fileInfo(filePath); auto realFilePath = fileInfo.exists() ? fileInfo.canonicalFilePath() : fileInfo.absoluteFilePath(); bool isNewFile = !QFile::exists(realFilePath); + +#ifdef Q_OS_WIN + bool isHidden = fileInfo.isHidden(); +#endif + bool ok = AsyncTask::runAndWaitForFuture([&] { return performSave(realFilePath, action, backupFilePath, error); }); if (ok) { setFilePath(filePath); @@ -285,6 +294,13 @@ bool Database::saveAs(const QString& filePath, SaveAction action, const QString& if (isNewFile) { QFile::setPermissions(realFilePath, QFile::ReadUser | QFile::WriteUser); } + +#ifdef Q_OS_WIN + if (isHidden) { + SetFileAttributes(realFilePath.toStdString().c_str(), FILE_ATTRIBUTE_HIDDEN); + } +#endif + m_fileWatcher->start(realFilePath, 30, 1); } else { // Saving failed, don't rewatch file since it does not represent our database diff --git a/tests/TestDatabase.cpp b/tests/TestDatabase.cpp index 9ab9de785..e3e957e79 100644 --- a/tests/TestDatabase.cpp +++ b/tests/TestDatabase.cpp @@ -30,6 +30,11 @@ #include "format/KeePass2Writer.h" #include "util/TemporaryFile.h" +#ifdef Q_OS_WIN +#include +#include +#endif + QTEST_GUILESS_MAIN(TestDatabase) static QString dbFileName = QStringLiteral(KEEPASSX_TEST_DATA_DIR).append("/NewDatabase.kdbx"); @@ -118,6 +123,12 @@ void TestDatabase::testSaveAs() QVERIFY(!db->isModified()); QCOMPARE(spyFilePathChanged.count(), 1); QVERIFY(QFile::exists(newDbFileName)); +#ifdef Q_OS_WIN + QVERIFY(!QFileInfo::QFileInfo(newDbFileName).isHidden()); + SetFileAttributes(newDbFileName.toStdString().c_str(), FILE_ATTRIBUTE_HIDDEN); + QVERIFY2(db->saveAs(newDbFileName, Database::Atomic, QString(), &error), error.toLatin1()); + QVERIFY(QFileInfo::QFileInfo(newDbFileName).isHidden()); +#endif QFile::remove(newDbFileName); QVERIFY(!QFile::exists(newDbFileName));