GCC Code Coverage Report


Directory: ./
File: backends/filer_controller.cpp
Date: 2023-04-20 22:59:23
Exec Total Coverage
Lines: 23 44 52.3%
Branches: 18 38 47.4%

Line Branch Exec Source
1 /*************************************************************************
2 Copyright © 2020 Roman Gilg <subdiff@gmail.com>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 **************************************************************************/
18 #include "filer_controller.h"
19
20 #include "device.h"
21 #include "filer.h"
22 #include "logging.h"
23
24 namespace Disman
25 {
26
27 58 Filer_controller::Filer_controller(Device* device, QObject* parent)
28 : QObject(parent)
29 58 , m_device{device}
30 {
31 58 }
32
33 228 Filer_controller::~Filer_controller() = default;
34
35 217 bool Filer_controller::read(ConfigPtr& config)
36 {
37
12/12
✓ Branch 1 taken 159 times.
✓ Branch 2 taken 58 times.
✓ Branch 10 taken 2 times.
✓ Branch 11 taken 157 times.
✓ Branch 12 taken 159 times.
✓ Branch 13 taken 58 times.
✓ Branch 15 taken 159 times.
✓ Branch 16 taken 58 times.
✓ Branch 18 taken 159 times.
✓ Branch 19 taken 58 times.
✓ Branch 21 taken 60 times.
✓ Branch 22 taken 157 times.
217 if (!m_filer || m_filer->config()->hash() != config->hash()) {
38
2/8
✗ Branch 1 not taken.
✓ Branch 2 taken 60 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 60 times.
60 if (lid_file_exists(config) && m_device->lid_present() && m_device->lid_open()) {
39 // Can happen when while lid closed output combination changes or device is shut down.
40 move_lid_file(config);
41 }
42
43 // Different output combination means different combination of filers.
44 60 reset_filer(config);
45 }
46
47 217 auto const success = m_filer->get_values(config);
48
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 168 times.
217 if (success) {
49 49 config->set_cause(Config::Cause::file);
50 }
51 217 return success;
52 }
53
54 42 bool Filer_controller::write(ConfigPtr const& config)
55 {
56
1/2
✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
42 if (m_filer) {
57
1/2
✗ Branch 10 not taken.
✓ Branch 11 taken 42 times.
42 if (m_filer->config()->hash() != config->hash()) {
58 qCWarning(DISMAN_BACKEND)
59 << "Config control file not in sync. Was there a simultaneous hot-plug event?";
60 return false;
61 }
62 } else {
63 reset_filer(config);
64 }
65
66 42 return m_filer->write(config);
67 }
68
69 bool Filer_controller::load_lid_file(ConfigPtr& config)
70 {
71 if (!lid_file_exists(config)) {
72 qCWarning(DISMAN_BACKEND) << "Loading open-lid file failed: file does not exist.";
73 return false;
74 }
75 if (!move_lid_file(config)) {
76 qCWarning(DISMAN_BACKEND) << "Could not move open-lid file back to normal config file.";
77 }
78 reset_filer(config);
79 return read(config);
80 }
81
82 60 bool Filer_controller::lid_file_exists(ConfigPtr const& config)
83 {
84 60 auto const file_info = Filer(config, this, "open-lid").file_info();
85 60 return QFile(file_info.filePath()).exists();
86 60 }
87
88 bool Filer_controller::move_lid_file(ConfigPtr const& config)
89 {
90 assert(lid_file_exists(config));
91
92 auto const file_info = Filer(config, this).file_info();
93 QFile(file_info.filePath()).remove();
94
95 auto const lid_file_info = Filer(config, this, "open-lid").file_info();
96 return QFile::rename(lid_file_info.filePath(), file_info.filePath());
97 }
98
99 bool Filer_controller::save_lid_file(ConfigPtr const& config)
100 {
101 return Filer(config, this, "open-lid").write(config);
102 }
103
104 60 void Filer_controller::reset_filer(ConfigPtr const& config)
105 {
106 60 m_filer.reset(new Filer(config, this));
107 60 }
108
109 }
110