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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
|
// Include files
#include <config.h>
#include <apt-pkg/algorithms.h>
#include <apt-pkg/cachefile.h>
#include <apt-pkg/cacheset.h>
#include <apt-pkg/cmndline.h>
#include <apt-pkg/configuration.h>
#include <apt-pkg/depcache.h>
#include <apt-pkg/error.h>
#include <apt-pkg/fileutl.h>
#include <apt-pkg/history.h>
#include <apt-pkg/pkgcache.h>
#include <apt-pkg/tagfile.h>
#include <apt-private/private-cachefile.h>
#include <apt-private/private-history.h>
#include <apt-private/private-install.h>
#include <apt-private/private-output.h>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <span>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <glob.h>
#include <apti18n.h> // for coloring
/*}}}*/
using namespace APT::History;
// =============================================================================
// Internal API extensions
// =============================================================================
//
static Kind ReflectKind(const Kind &kind)
{
switch (kind)
{
case Kind::Install:
case Kind::Reinstall:
return Kind::Remove;
case Kind::Upgrade:
return Kind::Downgrade;
case Kind::Downgrade:
return Kind::Upgrade;
case Kind::Remove:
case Kind::Purge:
return Kind::Install;
default:
assert(false);
return kind;
}
}
std::vector<Change> APT::Internal::FlattenChanges(const Entry &entry)
{
std::vector<Change> flattened;
size_t total_size = 0;
for (const auto &[_, changes] : entry.changeMap)
total_size += changes.size();
flattened.reserve(total_size);
for (const auto &[_, changes] : entry.changeMap)
flattened.insert(flattened.end(), changes.begin(), changes.end());
return flattened;
}
Change APT::Internal::InvertChange(const Change &change)
{
Change inverse = change;
inverse.kind = ReflectKind(change.kind);
// tailor change after what action was performed
switch (change.kind)
{
case Kind::Upgrade:
std::swap(inverse.currentVersion, inverse.candidateVersion);
break;
case Kind::Downgrade:
std::swap(inverse.currentVersion, inverse.candidateVersion);
break;
default:
break;
}
return inverse;
}
// =============================================================================
// Output formatting
// =============================================================================
//
// ShortenCommand - Take a command and shorten it such that it adheres
// to the given maximum length.
static std::string ShortenCommand(std::string cmd, const std::size_t maxLen)
{
if (cmd.starts_with("/usr/bin/"))
cmd.erase(0,9);
else if (cmd.starts_with("apt "))
cmd.erase(0, 4);
if (cmd.length() <= maxLen)
return cmd;
if (maxLen <= 3)
return cmd.substr(0, maxLen);
cmd.resize(maxLen - 3);
cmd.append("...");
return cmd;
}
static std::string LocalizeKindToString(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");
}
}
// GetKindString - Take a history entry and construct the string
// corresponding to the actions performed. Multpile actions are
// alphabetically grouped such as:
// Install, Remove, and Reinstall -> I,R,rI
//
// Shorthand legend:
// Install -> I
// Reinstall -> rI
// Upgrade -> U
// Downgrade -> D
// Remove -> R
// Purge -> P
//
static std::string GetKindString(const Entry &entry)
{
// We want full output if there is only one action
if (entry.changeMap.size() == 1)
return LocalizeKindToString(entry.changeMap.begin()->first);
std::string kindGroup = "";
// add localization later
for (const auto &[action, _] : entry.changeMap)
{
switch (action)
{
case Kind::Install:
kindGroup += "I";
break;
case Kind::Reinstall:
kindGroup += "rI";
break;
case Kind::Upgrade:
kindGroup += "U";
break;
case Kind::Downgrade:
kindGroup += "D";
break;
case Kind::Remove:
kindGroup += "R";
break;
case Kind::Purge:
kindGroup += "P";
break;
default:
kindGroup += "INVALID";
}
kindGroup += ",";
}
// remove trailing ","
if (not kindGroup.empty())
kindGroup.pop_back();
return kindGroup;
}
// =============================================================================
// Output printing
// =============================================================================
//
static void PrintHistoryVector(const HistoryBuffer &buf)
{
// TRANSLATORS: a number used as identifier.
const std::string idHeader = _("ID");
// TRANSLATORS: input on the terminal console.
const std::string cmdHeader = _("Command line");
// TRANSLATORS: indicates when the entry occurred.
const std::string dateHeader = _("Date and Time");
// TRANSLATORS: the type of operation that was performed.
const std::string actionHeader = _("Action");
// TRANSLATORS: changes as a quantity.
const std::string changesHeader = _("Changes");
constexpr size_t pad = 2;
size_t idWidth = std::to_string(buf.size()).size();
idWidth = std::max(idHeader.size(), idWidth) + pad;
size_t cmdLineWidth = cmdHeader.size() + pad;
size_t dateWidth = dateHeader.size() + pad;
size_t actionWidth = actionHeader.size() + pad;
size_t changesWidth = changesHeader.size();
// Scale to screen width
// ID and Changes are numerical and cannot be shortened
size_t remainingWidth = ::ScreenWidth - idWidth - changesWidth;
size_t minRequiredCmdWidth = cmdLineWidth;
std::vector<std::string> kindStrings;
std::vector<size_t> changeCounts;
kindStrings.reserve(buf.size());
changeCounts.reserve(buf.size());
if (!buf.empty())
{
dateWidth = std::max(buf.front().startDate.size() + pad, dateWidth);
for (const auto &entry : buf)
{
std::string kindString = GetKindString(entry);
if (kindString.size() > actionWidth)
actionWidth = kindString.size();
kindStrings.push_back(kindString);
size_t numChanges = 0;
for (const auto &[_, changes] : entry.changeMap)
numChanges += changes.size();
changeCounts.push_back(numChanges);
if (entry.cmdLine.length() > minRequiredCmdWidth)
minRequiredCmdWidth = entry.cmdLine.length() + pad;
}
remainingWidth -= (dateWidth + actionWidth);
cmdLineWidth = std::min(remainingWidth, minRequiredCmdWidth);
}
constexpr auto writeEntry = [](const auto &entry, size_t width)
{
std::cout << std::left << std::setw(width) << entry;
};
writeEntry(idHeader, idWidth);
writeEntry(cmdHeader, cmdLineWidth);
writeEntry(dateHeader, dateWidth);
writeEntry(actionHeader, actionWidth);
writeEntry(changesHeader, changesWidth);
std::cout << "\n\n";
// Each entry corresponds to a row
for (size_t id = 0; id < buf.size(); ++id)
{
const auto &entry = buf[id];
writeEntry(id, idWidth);
writeEntry(ShortenCommand(entry.cmdLine, cmdLineWidth), cmdLineWidth);
writeEntry(entry.startDate, dateWidth);
// If there are multiple actions, we want to group them
writeEntry(kindStrings[id], actionWidth);
writeEntry(changeCounts[id], changesWidth);
std::cout << "\n";
}
}
// PrintChange - Take a change and print the event for that package.
// Example:
// "package (0.1.0, 0.2.0)" and "Upgrade" -> "Upgrade package (0.1.0 -> 0.2.0)"
// "package (0.1.0)" and "Install" -> "Install package (0.1.0)"
static void PrintChange(const Change &change)
{
std::cout << " " << LocalizeKindToString(change.kind) << " " << change.package;
std::cout << " (" << change.currentVersion;
if (not change.candidateVersion.empty())
std::cout << " -> " << change.candidateVersion;
if (change.automatic)
std::cout << ", " << _("automatic");
std::cout << ")";
}
// PrintDetailedEntry - Take a history buffer and print the detailed
// transaction details for the given transaction id.
static void PrintDetailedEntry(const HistoryBuffer &buf, const size_t id)
{
Entry entry = buf[id];
std::cout << _("Transaction ID") << ": " << id << "\n";
std::cout << _("Start time") << ": " << entry.startDate << "\n";
std::cout << _("End time") << ": " << entry.endDate << "\n";
std::cout << _("Requested by") << ": " << entry.requestingUser << "\n";
std::cout << _("Command line") << ": " << entry.cmdLine << "\n";
if (not entry.error.empty())
{
std::cout << _config->Find("APT::Color::Red") << _("Error") << ": ";
std::cout << entry.error << _config->Find("APT::Color::Neutral") << "\n";
}
if (not entry.comment.empty())
std::cout << _("Comment") << ": " << entry.comment << "\n";
// For each performed action, print what it did to each package
std::cout << _("Packages changed") << ":" << "\n";
for (const auto &[_, changes] : entry.changeMap)
{
for (const auto &change : changes)
{
PrintChange(change);
std::cout << "\n";
}
std::cout << "\n";
}
}
// =============================================================================
// Transaction helpers
// =============================================================================
//
class TransactionController
{
public:
TransactionController(CacheFile &cache) : Cache(cache),
Fix(Cache.GetDepCache()),
InstallAction(Cache, &Fix, false),
RemoveAction(Cache, &Fix),
HeldBackPackages()
{
}
bool AppendChange(const Change &change)
{
auto pkg = Cache->FindPkg(change.package);
if (pkg.end())
return _error->Error(_("Could not find package %s"), change.package.data());
if (IsRemoval(change.kind))
{
auto ver = pkg.CurrentVer();
if (ver == nullptr)
{
_error->Warning(_("Tried to remove %s, but it is not installed"), change.package.data());
return true;
}
RemoveAction(ver);
return true;
}
auto ver = pkg.VersionList();
for (; not ver.end(); ++ver)
if (strcmp(ver.VerStr(), change.currentVersion.data()) == 0)
break;
if (ver.end())
return _error->Error(_("Could not find given version %s of %s"), change.currentVersion.data(),
change.package.data());
InstallAction(ver);
return true;
}
bool CommitChanges()
{
InstallAction.doAutoInstall();
OpTextProgress Progress(*_config);
bool const resolver_fail = Fix.Resolve(true, &Progress);
if (resolver_fail == false && Cache->BrokenCount() == 0)
return false;
if (CheckNothingBroken(Cache) == false)
return false;
return InstallPackages(Cache, HeldBackPackages, false, true);
}
private:
// This must be a reference for lifetime safety
CacheFile &Cache;
pkgProblemResolver Fix;
TryToInstall InstallAction;
TryToRemove RemoveAction;
APT::PackageVector HeldBackPackages;
};
static bool ParseId(const char *str, size_t &id, size_t max)
{
if (str == nullptr)
return _error->Error(_("Incorrect usage, no ID was given."));
try
{
id = std::stoi(str);
}
catch (const std::invalid_argument &e)
{
return _error->Error(_("'%s' not a valid ID."), str);
}
if (max < id)
return _error->Error(_("Transaction ID '%ld' out of bounds."), id);
return true;
}
// =============================================================================
// Entrypoints
// =============================================================================
//
bool DoHistoryUndo(CommandLine &Cmd)
{
HistoryBuffer buf = {};
if (not ParseLogDir(buf))
return _error->Error(_("Could not read %s"),
_config->FindFile("Dir::Log::History").data());
size_t id = 0;
if (not ParseId(*(Cmd.FileList + 1), id, buf.size() - 1))
return false;
CacheFile Cache;
if (Cache.BuildCaches(true) == false)
return false;
TransactionController Controller(Cache);
for (const auto &change : APT::Internal::FlattenChanges(buf[id]))
if (not Controller.AppendChange(APT::Internal::InvertChange(change)))
return false;
return Controller.CommitChanges();
}
bool DoHistoryRollback(CommandLine &Cmd)
{
HistoryBuffer buf = {};
if (not ParseLogDir(buf))
return _error->Error(_("Could not read %s"),
_config->FindFile("Dir::Log::History").data());
size_t rollbackId = 0;
if (not ParseId(*(Cmd.FileList + 1), rollbackId, buf.size()))
return false;
CacheFile Cache;
if (Cache.BuildCaches(true) == false)
return false;
TransactionController Controller(Cache);
// Map to keep the effective change of each package
std::unordered_map<std::string, Change> effectiveChangeMap;
// Plus 1 for zero indexing
size_t numChanges = buf.size() - (rollbackId + 1);
std::span<Entry> bufSpan(buf.end() - numChanges, numChanges);
// Iterate in reverse to process changes LIFO
for (auto it = bufSpan.rbegin(); it != bufSpan.rend(); ++it)
for (const auto &change : APT::Internal::FlattenChanges(*it))
effectiveChangeMap[change.package] = APT::Internal::InvertChange(change);
for (const auto &[_, change] : effectiveChangeMap)
{
if (not Controller.AppendChange(change))
return false;
}
return Controller.CommitChanges();
}
bool DoHistoryRedo(CommandLine &Cmd)
{
HistoryBuffer buf = {};
if (not ParseLogDir(buf))
return _error->Error(_("Could not read %s"),
_config->FindFile("Dir::Log::History").data());
size_t id = 0;
if (not ParseId(*(Cmd.FileList + 1), id, buf.size() - 1))
return false;
CacheFile Cache;
if (Cache.BuildCaches(true) == false)
return false;
TransactionController Controller(Cache);
for (const auto &change : APT::Internal::FlattenChanges(buf[id]))
if (not Controller.AppendChange(change))
return false;
return Controller.CommitChanges();
}
bool DoHistoryList(CommandLine &Cmd)
{
HistoryBuffer buf = {};
if (Cmd.FileSize() != 1)
return _error->Error("This command does not support any arguments");
if (not ParseLogDir(buf))
return _error->Error(_("Could not read: %s"),
_config->FindFile("Dir::Log::History").data());
PrintHistoryVector(buf);
return true;
}
bool DoHistoryInfo(CommandLine &Cmd)
{
HistoryBuffer buf = {};
if (not ParseLogDir(buf))
return _error->Error(_("Could not read: %s"),
_config->FindFile("Dir::Log::History").data());
size_t id = 0;
for (size_t i = 1; i < Cmd.FileSize(); i++)
{
if (not ParseId(*(Cmd.FileList + i), id, buf.size() - 1))
return false;
PrintDetailedEntry(buf, id);
}
return true;
}
|