LeechCraft  0.6.70-18450-gabe19ee3b0
Modular cross-platform feature rich live environment.
dumper.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Distributed under the Boost Software License, Version 1.0.
6  * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7  **********************************************************************/
8 
9 #include "dumper.h"
10 
11 #include <QCoreApplication>
12 #include <QProcess>
13 #include <QtDebug>
14 #include <util/sll/qtutil.h>
15 #include <util/threads/coro.h>
16 
17 namespace LC::Util
18 {
19  namespace
20  {
21  struct Tr
22  {
23  Q_DECLARE_TR_FUNCTIONS (LC::Util::DumpSqlite)
24  };
25 
26  Either<QString, Void> CheckProcessFinishStatus (QProcess& process)
27  {
28  const auto& procStdErr = process.readAllStandardError ();
29  switch (process.exitStatus ())
30  {
31  case QProcess::CrashExit:
32  {
33  const auto& procErr = process.errorString ();
34 
35  if (process.error () == QProcess::FailedToStart)
36  return Left { Tr::tr ("Unable to start dumping process: %1. Do you have sqlite3 installed?").arg (procErr) };
37 
38  const auto& message = procStdErr.isEmpty () ?
39  Tr::tr ("Dumping process crashed: %1.").arg (procErr) :
40  Tr::tr ("Dumping process crashed: %1 (%2).").arg (procErr, procStdErr);
41  return Left { message };
42  }
43  case QProcess::NormalExit:
44  if (const auto ec = process.exitCode ())
45  {
46  const auto& message = Tr::tr ("Dumping process returned an error: %1 (%2).")
47  .arg (ec)
48  .arg (procStdErr);
49  return Left { message };
50  }
51  break;
52  }
53 
54  return Void {};
55  }
56  }
57 
58  Task<Either<QString, Void>> DumpSqlite (QString from, QString to)
59  {
60  QProcess dumper;
61  QProcess restorer;
62 
63  dumper.setStandardOutputProcess (&restorer);
64 
65  static const auto sqlite = "sqlite3"_qs;
66  dumper.start (sqlite, { from, ".dump"_qs });
67  restorer.start (sqlite, { to });
68 
69  co_await dumper;
70  co_await CheckProcessFinishStatus (dumper);
71 
72  co_await restorer;
73  co_await CheckProcessFinishStatus (restorer);
74 
75  co_return Void {};
76  }
77 }
Task< Either< QString, Void > > DumpSqlite(QString from, QString to)
Definition: dumper.cpp:58
A proper void type, akin to unit (or ()) type in functional languages.
Definition: void.h:20