2019-10-14 08:37:26 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2019 KeePassXC Team <team@keepassxc.org>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 or (at your option)
|
|
|
|
* version 3 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-02-20 08:41:47 -05:00
|
|
|
#include "Import.h"
|
2019-10-14 08:37:26 -04:00
|
|
|
|
2022-10-05 07:30:15 -04:00
|
|
|
#include "DatabaseCreate.h"
|
2021-02-20 08:41:47 -05:00
|
|
|
#include "Utils.h"
|
2019-10-14 08:37:26 -04:00
|
|
|
|
2024-06-22 12:22:44 +01:00
|
|
|
#include "core/Global.h"
|
|
|
|
|
2021-07-11 22:10:29 -04:00
|
|
|
#include <QCommandLineParser>
|
2021-02-20 08:41:47 -05:00
|
|
|
#include <QFileInfo>
|
2019-10-14 08:37:26 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a database file from an XML export of another database.
|
|
|
|
* A password can be specified to encrypt the database.
|
|
|
|
* If none is specified the function will fail.
|
|
|
|
*
|
2023-01-29 09:38:44 -05:00
|
|
|
* If the database is being saved in a non existent directory, the
|
2019-10-14 08:37:26 -04:00
|
|
|
* function will fail.
|
|
|
|
*
|
|
|
|
* @return EXIT_SUCCESS on success, or EXIT_FAILURE on failure
|
|
|
|
*/
|
2020-10-10 02:31:29 +02:00
|
|
|
|
2019-10-14 08:37:26 -04:00
|
|
|
Import::Import()
|
|
|
|
{
|
|
|
|
name = QString("import");
|
|
|
|
description = QObject::tr("Import the contents of an XML database.");
|
|
|
|
positionalArguments.append({QString("xml"), QObject::tr("Path of the XML database export."), QString("")});
|
|
|
|
positionalArguments.append({QString("database"), QObject::tr("Path of the new database."), QString("")});
|
2022-10-05 07:30:15 -04:00
|
|
|
options.append(DatabaseCreate::SetKeyFileOption);
|
|
|
|
options.append(DatabaseCreate::SetKeyFileShortOption);
|
|
|
|
options.append(DatabaseCreate::SetPasswordOption);
|
|
|
|
options.append(DatabaseCreate::DecryptionTimeOption);
|
2019-10-14 08:37:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int Import::execute(const QStringList& arguments)
|
|
|
|
{
|
|
|
|
QSharedPointer<QCommandLineParser> parser = getCommandLineParser(arguments);
|
|
|
|
if (parser.isNull()) {
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2020-05-11 07:31:29 -04:00
|
|
|
auto& out = parser->isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT;
|
|
|
|
auto& err = Utils::STDERR;
|
2019-10-14 08:37:26 -04:00
|
|
|
|
|
|
|
const QStringList args = parser->positionalArguments();
|
2019-10-31 23:44:40 +03:00
|
|
|
const QString& xmlExportPath = args.at(0);
|
|
|
|
const QString& dbPath = args.at(1);
|
2019-10-14 08:37:26 -04:00
|
|
|
|
|
|
|
if (QFileInfo::exists(dbPath)) {
|
2024-06-22 12:22:44 +01:00
|
|
|
err << QObject::tr("File %1 already exists.").arg(dbPath) << Qt::endl;
|
2019-10-14 08:37:26 -04:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2022-10-05 07:30:15 -04:00
|
|
|
QSharedPointer<Database> db = DatabaseCreate::initializeDatabaseFromOptions(parser);
|
2020-10-10 02:31:29 +02:00
|
|
|
if (!db) {
|
2019-10-14 08:37:26 -04:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString errorMessage;
|
2020-10-10 02:31:29 +02:00
|
|
|
if (!db->import(xmlExportPath, &errorMessage)) {
|
2024-06-22 12:22:44 +01:00
|
|
|
err << QObject::tr("Unable to import XML database: %1").arg(errorMessage) << Qt::endl;
|
2019-10-14 08:37:26 -04:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2021-11-07 17:52:23 -05:00
|
|
|
if (!db->saveAs(dbPath, Database::Atomic, {}, &errorMessage)) {
|
2024-06-22 12:22:44 +01:00
|
|
|
err << QObject::tr("Failed to save the database: %1.").arg(errorMessage) << Qt::endl;
|
2019-10-14 08:37:26 -04:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2024-06-22 12:22:44 +01:00
|
|
|
out << QObject::tr("Successfully imported database.") << Qt::endl;
|
2019-10-14 08:37:26 -04:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|