status.h
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <fstream>
11 #include <iostream>
12 #include <string>
13 #include <assert.h>
14 #include <sstream>
15 #include <vector>
16 #include <map>
17 #include <boost/lexical_cast.hpp>
18 #include <boost/system/error_code.hpp>
19 #ifdef TESTING
20 #include "gtest/gtest.h"
21 #endif
22 
23 namespace dpyStatus{
24 
25 
27 enum Result
28 {
29  OK = 1,
30  ERROR = 2,
31  IN_PROCESS = 3,
32  UPDATED = 4,
33  BACKUP = 5
34 };
35 
36 }
37 
44 class Status
45 {
46 private:
50  static Status *instance;
51 
55  std::string statusFilePath;
56 
60  int _usedRFS = 1;
61 
65  int _activeRFS = 1;
66 
70  std::map<int, std::string> rfs_status;
71 
75  int _usedKernel = 1;
76 
80  int _activeKernel = 1;
81 
85  std::map<int, std::string> kernel_status;
86 
90  std::map<std::string, dpyStatus::Result> translationStatuMap;
91 
92  bool processUsedKernel(std::string command);
93  bool processUsedRfs(std::string command);
94 
95  void fixStatusFile(std::string path = "");
96  bool writeDefaultStatus(boost::system::error_code& ec, std::string path = "");
97 
98 protected:
102  explicit Status();
103 
104 public:
105  static Status *getInstance();
106  bool init(boost::system::error_code& ec, std::string path, std::string cmdline="cat /proc/cmdline");
107  bool readStatus(boost::system::error_code& ec, std::string path = "");
108  bool writeStatus(boost::system::error_code& ec, std::string path = "");
109 
110  //Status
111  dpyStatus::Result curRfsStatus();
112  dpyStatus::Result altRfsStatus();
113  dpyStatus::Result curKernelStatus();
114  dpyStatus::Result altKernelStatus();
115 
116  //Update OK
117  void setCurRFSStatus(dpyStatus::Result status);
118  void setAltRFSStatus(dpyStatus::Result status);
119 
120  void setCurKernelStatus(dpyStatus::Result status);
121  void setAltKernelStatus(dpyStatus::Result status);
122 
123  void repairActiveRFS();
124  void repairActiveKernel();
125 
126  void changeActiveRFS();
127  void changeActiveKernel();
128 
129  //Getters
134  int usedRFS() const
135  {
136  return _usedRFS;
137  }
138 
144  int activeRFS() const
145  {
146  return _activeRFS;
147  }
148 
154  {
155  if (_usedRFS == 1) {
156  return 2;
157  } else {
158  return 1;
159  }
160  }
161 
166  int usedKernel() const
167  {
168  return _usedKernel;
169  }
170 
176  int activeKernel() const
177  {
178  return _activeKernel;
179  }
180 
186  {
187  if (_usedKernel == 1) {
188  return 2;
189  } else {
190  return 1;
191  }
192  }
193 
194 #ifdef TESTING
195 public:
196  FRIEND_TEST(Status_test, readStatus_File_does_not_exist);
197  FRIEND_TEST(Status_test, writeDefaultStatusOK);
198  FRIEND_TEST(Status_test, writeStatusError);
199  FRIEND_TEST(Status_test,readStatusParseError);
200  FRIEND_TEST(Status_test,readStatusValueError);
201  FRIEND_TEST(Status_test,readStatusKeyError);
202  FRIEND_TEST(Status_test,readStatusWrongValueKernel);
203  FRIEND_TEST(Status_test,readStatusWrongValueRfs);
204  FRIEND_TEST(Status_test,processUsedKernel1Correct);
205  FRIEND_TEST(Status_test,processUsedRfs1Correct);
206  FRIEND_TEST(Status_test,processUsedRfs2Correct);
207  FRIEND_TEST(Status_test,processUsedKernel2Correct);
208  FRIEND_TEST(Status_test,changeStatus);
209  FRIEND_TEST(Status_test,currentDefaultStatus);
210  FRIEND_TEST(Status_test,translationMapCreation);
211  FRIEND_TEST(Status_test,StatusInitParseError);
212  FRIEND_TEST(Status_test,StatusInitFileDoesNotExit);
213  FRIEND_TEST(Status_test,StatusInitCorrect);
214  FRIEND_TEST(Status_test,processUsedRfsError);
215  FRIEND_TEST(Status_test,processUsedKernelError);
216  FRIEND_TEST(Status_test,changeActiveKernelCorrect);
217  FRIEND_TEST(Status_test,changeActiveRfsCorrect);
218  FRIEND_TEST(Status_test,repairActiveRfsCorrect);
219  FRIEND_TEST(Status_test,repairActiveKernelCorrect);
220 #endif
221 };
UPDATED.
Definition: status.h:32
Manages the status file.
Definition: status.h:44
int usedRFS() const
Definition: status.h:134
Definition: status.h:23
int alternativeKernel()
Definition: status.h:185
int activeRFS() const
Definition: status.h:144
int usedKernel() const
Definition: status.h:166
int activeKernel() const
Definition: status.h:176
Result
Defines the possible states.
Definition: status.h:27
int alternativeRFS()
Definition: status.h:153