File: history.cc

package info (click to toggle)
apt 3.1.13
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,764 kB
  • sloc: cpp: 71,085; sh: 31,750; xml: 5,553; perl: 217; python: 197; ansic: 191; makefile: 41
file content (191 lines) | stat: -rw-r--r-- 5,090 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// -*- mode: cpp; mode: fold -*-
// Description								/*{{{*/
/* ######################################################################
   Set of functions for parsing the history log
   ##################################################################### */
/*}}}*/
// Include Files							/*{{{*/

#include <config.h>

#include <apt-pkg/configuration.h>
#include <apt-pkg/error.h>
#include <apt-pkg/fileutl.h>
#include <apt-pkg/history.h>
#include <apt-pkg/strutl.h>
#include <apt-pkg/tagfile.h>

#include <algorithm>
#include <cassert>
#include <cctype>

#include <glob.h>
#include <apti18n.h> // for coloring

namespace APT::History
{

static Change ParsePackageEvent(const std::string &event)
{
   Change change{};
   // Remove all spaces
   std::string trimEvent = event;
   trimEvent.erase(std::remove_if(trimEvent.begin(), trimEvent.end(),
				  [](unsigned char c)
				  { return std::isspace(c); }),
		   trimEvent.end());
   auto openParen = trimEvent.find('(');
   if (openParen == std::string::npos)
      return change;

   change.package = trimEvent.substr(0, openParen);

   auto closeParen = trimEvent.find(')', openParen);
   if (closeParen == std::string::npos)
      return change;

   std::string versionStr = trimEvent.substr(openParen + 1, closeParen - openParen - 1);
   auto values = VectorizeString(versionStr, ',');
   for (size_t i = 0; i < values.size(); ++i)
   {
      if (i == 0 && std::isdigit(values[0][0]))
	 change.currentVersion = std::move(values[i]);
      else if (i == 1 && std::isdigit(values[1][0]))
	 change.candidateVersion = std::move(values[i]);
      else if (values[i] == "automatic")
	 change.automatic = true;
      else
	 _error->Warning(_("Unknown flag: %s"), values[i].c_str());
   }
   return change;
}

static std::vector<std::string> SplitPackagesInContent(const std::string &content)
{
   std::vector<std::string> result;
   std::size_t start = 0;

   if (content.length() == 0)
      return result;

   // Split at "), " but keep the parenthesis.
   while (true)
   {
      std::size_t pos = content.find("), ", start);
      if (pos == std::string::npos)
      {
	 result.push_back(content.substr(start));
	 break;
      }
      result.push_back(content.substr(start, pos - start + 1));
      // Increment by 3 since 3 == len("), ")
      start = pos + 3;
   }
   return result;
}

std::string KindToString(const Kind &kind)
{
   switch (kind)
   {
   case Kind::Install:
      return "Install";
   case Kind::Reinstall:
      return "Reinstall";
   case Kind::Upgrade:
      return "Upgrade";
   case Kind::Downgrade:
      return "Downgrade";
   case Kind::Remove:
      return "Remove";
   case Kind::Purge:
      return "Purge";
   default:
      return "Undefined";
   }
}

Entry ParseSection(
   const pkgTagSection &section)
{
   Entry entry{};
   entry.startDate = section.Find("Start-Date");
   entry.endDate = section.Find("End-Date");
   entry.cmdLine = section.Find("Commandline");
   entry.requestingUser = section.Find("Requested-By");
   entry.comment = section.Find("Comment");
   entry.error = section.Find("Error");

   std::string content = "";
   const Kind kinds[] =
      {
	 Kind::Install,
	 Kind::Reinstall,
	 Kind::Downgrade,
	 Kind::Upgrade,
	 Kind::Remove,
	 Kind::Purge,
      };
   for (const auto &kind : kinds)
   {
      content = section.Find(KindToString(kind));
      if (content.empty())
	 continue;

      std::vector<std::string> package_events = SplitPackagesInContent(content);
      std::vector<Change> changes = {};
      for (auto event : package_events)
      {
	 Change change = ParsePackageEvent(event);
	 change.kind = kind;
	 changes.push_back(change);
      }
      // Changed packages should be in order
      std::sort(changes.begin(), changes.end(), [](const Change &a, const Change &b)
		{ return a.package < b.package; });
      entry.changeMap[kind] = changes;
   }

   return entry;
}

bool ParseFile(FileFd &fd, HistoryBuffer &buf)
{
   pkgTagFile file(&fd, FileFd::ReadOnly);
   pkgTagSection tmpSection;
   while (file.Step(tmpSection))
      buf.push_back(ParseSection(tmpSection));
   return true;
}

bool ParseLogDir(HistoryBuffer &buf)
{
   std::string files = _config->FindFile("Dir::Log::History") + "*";
   const char *pattern = files.data();
   glob_t result;

   int ret = glob(pattern, GLOB_TILDE, nullptr, &result);
   if (ret != 0)
      return _error->Error(_("Cannot find history files: %s"), files.c_str());

   for (size_t i = 0; i < result.gl_pathc; ++i)
   {
      FileFd fd;
      if (not fd.Open(result.gl_pathv[i], FileFd::ReadOnly, FileFd::Extension))
	 return _error->Error(_("Could not open file: %s"), result.gl_pathv[i]);
      if (not ParseFile(fd, buf))
	 return _error->Error(_("Could not parse file: %s"), result.gl_pathv[i]);
      if (not fd.Close())
	 return _error->Error(_("Could not close file: %s"), result.gl_pathv[i]);
   }

   // Sort entries by time
   std::sort(buf.begin(), buf.end(),
	     [](const Entry &a, const Entry &b)
	     {
		return a.startDate < b.startDate;
	     });

   return true;
}
} // namespace APT::History