mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2025-03-13 01:26:37 -04:00
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 <Drwsburah@yahoo.com> Co-authored-by: Jonathan White <support@dmapps.us>
This commit is contained in:
parent
ff6c3d7d9a
commit
6875851892
@ -33,6 +33,10 @@
|
|||||||
#include <QTemporaryFile>
|
#include <QTemporaryFile>
|
||||||
#include <QTimer>
|
#include <QTimer>
|
||||||
|
|
||||||
|
#ifdef Q_OS_WIN
|
||||||
|
#include <Windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
QHash<QUuid, QPointer<Database>> Database::s_uuidMap;
|
QHash<QUuid, QPointer<Database>> Database::s_uuidMap;
|
||||||
|
|
||||||
Database::Database()
|
Database::Database()
|
||||||
@ -278,6 +282,11 @@ bool Database::saveAs(const QString& filePath, SaveAction action, const QString&
|
|||||||
QFileInfo fileInfo(filePath);
|
QFileInfo fileInfo(filePath);
|
||||||
auto realFilePath = fileInfo.exists() ? fileInfo.canonicalFilePath() : fileInfo.absoluteFilePath();
|
auto realFilePath = fileInfo.exists() ? fileInfo.canonicalFilePath() : fileInfo.absoluteFilePath();
|
||||||
bool isNewFile = !QFile::exists(realFilePath);
|
bool isNewFile = !QFile::exists(realFilePath);
|
||||||
|
|
||||||
|
#ifdef Q_OS_WIN
|
||||||
|
bool isHidden = fileInfo.isHidden();
|
||||||
|
#endif
|
||||||
|
|
||||||
bool ok = AsyncTask::runAndWaitForFuture([&] { return performSave(realFilePath, action, backupFilePath, error); });
|
bool ok = AsyncTask::runAndWaitForFuture([&] { return performSave(realFilePath, action, backupFilePath, error); });
|
||||||
if (ok) {
|
if (ok) {
|
||||||
setFilePath(filePath);
|
setFilePath(filePath);
|
||||||
@ -285,6 +294,13 @@ bool Database::saveAs(const QString& filePath, SaveAction action, const QString&
|
|||||||
if (isNewFile) {
|
if (isNewFile) {
|
||||||
QFile::setPermissions(realFilePath, QFile::ReadUser | QFile::WriteUser);
|
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);
|
m_fileWatcher->start(realFilePath, 30, 1);
|
||||||
} else {
|
} else {
|
||||||
// Saving failed, don't rewatch file since it does not represent our database
|
// Saving failed, don't rewatch file since it does not represent our database
|
||||||
|
@ -30,6 +30,11 @@
|
|||||||
#include "format/KeePass2Writer.h"
|
#include "format/KeePass2Writer.h"
|
||||||
#include "util/TemporaryFile.h"
|
#include "util/TemporaryFile.h"
|
||||||
|
|
||||||
|
#ifdef Q_OS_WIN
|
||||||
|
#include <QFileInfo>
|
||||||
|
#include <Windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
QTEST_GUILESS_MAIN(TestDatabase)
|
QTEST_GUILESS_MAIN(TestDatabase)
|
||||||
|
|
||||||
static QString dbFileName = QStringLiteral(KEEPASSX_TEST_DATA_DIR).append("/NewDatabase.kdbx");
|
static QString dbFileName = QStringLiteral(KEEPASSX_TEST_DATA_DIR).append("/NewDatabase.kdbx");
|
||||||
@ -118,6 +123,12 @@ void TestDatabase::testSaveAs()
|
|||||||
QVERIFY(!db->isModified());
|
QVERIFY(!db->isModified());
|
||||||
QCOMPARE(spyFilePathChanged.count(), 1);
|
QCOMPARE(spyFilePathChanged.count(), 1);
|
||||||
QVERIFY(QFile::exists(newDbFileName));
|
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);
|
QFile::remove(newDbFileName);
|
||||||
QVERIFY(!QFile::exists(newDbFileName));
|
QVERIFY(!QFile::exists(newDbFileName));
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user