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 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
|
/*
* Copyright (c) [2011-2015] Novell, Inc.
* Copyright (c) [2016-2023] SUSE LLC
*
* All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of version 2 of the GNU General Public License as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, contact Novell, Inc.
*
* To contact Novell about this file by physical or electronic mail, you may
* find current contact information at www.novell.com.
*/
#include "config.h"
#include <iostream>
#include <boost/any.hpp>
#include <snapper/SnapperTmpl.h>
#include <snapper/BtrfsUtils.h>
#include "utils/text.h"
#include "GlobalOptions.h"
#include "proxy.h"
#include "misc.h"
#include "utils/TableFormatter.h"
#include "utils/CsvFormatter.h"
#include "utils/JsonFormatter.h"
#include "dbus/DBusMessage.h"
#include "utils/HumanString.h"
namespace snapper
{
using namespace std;
void
help_list()
{
cout << _(" List snapshots:") << '\n'
<< _("\tsnapper list") << '\n'
<< '\n'
<< _(" Options for 'list' command:") << '\n'
<< _("\t--type, -t <type>\t\tType of snapshots to list.") << '\n'
<< _("\t--disable-used-space\t\tDisable showing used space.") << '\n'
<< _("\t--all-configs, -a\t\tList snapshots from all accessible configs.") << '\n'
<< _("\t--columns <columns>\t\tColumns to show separated by comma.\n"
"\t\t\t\t\tPossible columns: config, subvolume, number, default, active,\n"
"\t\t\t\t\ttype, date, user, used-space, cleanup, description, userdata,\n"
"\t\t\t\t\tpre-number, post-number, post-date, read-only.") << '\n'
<< endl;
}
namespace
{
enum class Column
{
CONFIG, SUBVOLUME, NUMBER, DEFAULT, ACTIVE, TYPE, DATE, USER, USED_SPACE,
CLEANUP, DESCRIPTION, USERDATA, PRE_NUMBER, POST_NUMBER, POST_DATE, READ_ONLY
};
enum class ListMode { ALL, SINGLE, PRE_POST };
/**
* Just a collection of some variables defining the output.
*/
class OutputOptions
{
public:
OutputOptions(bool utc, bool iso, bool human)
: utc(utc), iso(iso), human(human)
{
}
const bool utc;
const bool iso;
const bool human;
};
/**
* Some help for the output, e.g. check if a snapshot is the default or active
* snapshot, check if a snapshot should be skipped in the output or check if the
* used-space works.
*/
class OutputHelper
{
public:
OutputHelper(const ProxySnapper* snapper, const vector<Column>& columns);
bool is_default(const ProxySnapshot& snapshot) const
{
return default_snapshot != snapshots.end() && default_snapshot->getNum() == snapshot.getNum();
}
bool is_active(const ProxySnapshot& snapshot) const
{
return active_snapshot != snapshots.end() && active_snapshot->getNum() == snapshot.getNum();
}
char extra_sign(const ProxySnapshot& snapshot) const
{
static const char sign[2][2] = { { ' ', '-' }, { '+', '*' } };
return sign[is_default(snapshot)][is_active(snapshot)];
}
ProxySnapshots::const_iterator find_post(const ProxySnapshot& snapshot) const
{
if (snapshot.getType() != SnapshotType::PRE)
return snapshots.end();
ProxySnapshots::const_iterator it = snapshots.find(snapshot.getNum());
return snapshots.findPost(it);
}
bool is_used_space_broken() const { return used_space_broken; }
bool skip_column(Column column) const { return column == Column::USED_SPACE && used_space_broken; }
bool skip_snapshot(const ProxySnapshot& snapshot, ListMode list_mode) const;
const ProxySnapper* snapper;
const ProxySnapshots& snapshots;
private:
ProxySnapshots::const_iterator default_snapshot;
ProxySnapshots::const_iterator active_snapshot;
bool used_space_broken = true;
#ifdef ENABLE_BTRFS
/**
* For all btrfses (by btrfs filesystem uuid) keep track of whether used space
* is broken.
*/
static map<Uuid, bool> used_space_broken_by_uuid;
#endif
};
#ifdef ENABLE_BTRFS
map<Uuid, bool> OutputHelper::used_space_broken_by_uuid;
#endif
OutputHelper::OutputHelper(const ProxySnapper* snapper, const vector<Column>& columns)
: snapper(snapper), snapshots(snapper->getSnapshots()), default_snapshot(snapshots.end()),
active_snapshot(snapshots.end())
{
try
{
default_snapshot = snapshots.getDefault();
active_snapshot = snapshots.getActive();
}
catch (const DBus::ErrorException& e)
{
SN_CAUGHT(e);
// If snapper was just updated and the old snapperd is still
// running it might not know the GetDefaultSnapshot and
// GetActiveSnapshot methods.
if (strcmp(e.name(), "error.unknown_method") != 0)
SN_RETHROW(e);
}
// Calculate the used space iff columns include USED_SPACE. Also sets
// used_space_broken (even cached) for use in skip_column.
if (find(columns.begin(), columns.end(), Column::USED_SPACE) != columns.end())
{
string subvolume = snapper->getConfig().getSubvolume();
#ifdef ENABLE_BTRFS
try
{
Uuid uuid = BtrfsUtils::get_uuid(subvolume);
map<Uuid, bool>::const_iterator pos = used_space_broken_by_uuid.find(uuid);
if (pos != used_space_broken_by_uuid.end())
{
used_space_broken = pos->second;
}
else
{
try
{
snapper->calculateUsedSpace();
used_space_broken = used_space_broken_by_uuid[uuid] = false;
}
catch (const QuotaException& e)
{
SN_CAUGHT(e);
used_space_broken_by_uuid[uuid] = true;
}
}
}
catch (const runtime_error& e)
{
// getting uuid failed, maybe it is a LVM config
}
#endif
}
}
bool
OutputHelper::skip_snapshot(const ProxySnapshot& snapshot, ListMode list_mode) const
{
switch (list_mode)
{
case ListMode::ALL:
return false;
case ListMode::SINGLE:
return snapshot.getType() != SINGLE;
case ListMode::PRE_POST:
return find_post(snapshot) == snapshots.end();
}
SN_THROW(Exception("invalid column value"));
__builtin_unreachable();
}
vector<Column>
default_columns(ListMode list_mode, GlobalOptions::OutputFormat format)
{
switch (list_mode)
{
case ListMode::ALL:
{
switch (format)
{
case GlobalOptions::OutputFormat::TABLE:
return { Column::NUMBER, Column::TYPE, Column::PRE_NUMBER, Column::DATE, Column::USER,
Column::USED_SPACE, Column::CLEANUP, Column::DESCRIPTION, Column::USERDATA };
case GlobalOptions::OutputFormat::CSV:
return { Column::CONFIG, Column::SUBVOLUME, Column::NUMBER, Column::DEFAULT,
Column::ACTIVE, Column::TYPE, Column::PRE_NUMBER, Column::DATE, Column::USER,
Column::USED_SPACE, Column::CLEANUP, Column::DESCRIPTION, Column::USERDATA };
case GlobalOptions::OutputFormat::JSON:
return { Column::SUBVOLUME, Column::NUMBER, Column::DEFAULT, Column::ACTIVE,
Column::TYPE, Column::PRE_NUMBER, Column::DATE, Column::USER,
Column::USED_SPACE, Column::CLEANUP, Column::DESCRIPTION, Column::USERDATA };
}
}
break;
case ListMode::SINGLE:
{
switch (format)
{
case GlobalOptions::OutputFormat::TABLE:
return { Column::NUMBER, Column::DATE, Column::USER, Column::USED_SPACE,
Column::DESCRIPTION, Column::USERDATA };
case GlobalOptions::OutputFormat::CSV:
return { Column::CONFIG, Column::SUBVOLUME, Column::NUMBER, Column::DEFAULT,
Column::ACTIVE, Column::DATE, Column::USER, Column::USED_SPACE,
Column::DESCRIPTION, Column::USERDATA };
case GlobalOptions::OutputFormat::JSON:
return { Column::SUBVOLUME, Column::NUMBER, Column::DEFAULT, Column::ACTIVE,
Column::DATE, Column::USER, Column::USED_SPACE, Column::DESCRIPTION,
Column::USERDATA };
}
}
break;
case ListMode::PRE_POST:
{
switch (format)
{
case GlobalOptions::OutputFormat::TABLE:
return { Column::NUMBER, Column::POST_NUMBER, Column::DATE, Column::POST_DATE,
Column::DESCRIPTION, Column::USERDATA };
case GlobalOptions::OutputFormat::CSV:
return { Column::CONFIG, Column::SUBVOLUME, Column::NUMBER, Column::DEFAULT,
Column::ACTIVE, Column::POST_NUMBER, Column::DATE, Column::POST_DATE,
Column::DESCRIPTION, Column::USERDATA };
case GlobalOptions::OutputFormat::JSON:
return { Column::SUBVOLUME, Column::NUMBER, Column::DEFAULT, Column::ACTIVE,
Column::POST_NUMBER, Column::DATE, Column::POST_DATE, Column::DESCRIPTION,
Column::USERDATA };
}
}
break;
}
SN_THROW(Exception("invalid list mode or output format"));
__builtin_unreachable();
}
pair<string, TableAlign>
header_for(ListMode list_mode, Column column)
{
switch (column)
{
case Column::CONFIG:
return make_pair(_("Config"), TableAlign::LEFT);
case Column::SUBVOLUME:
return make_pair(_("Subvolume"), TableAlign::LEFT);
case Column::NUMBER:
if (list_mode != ListMode::PRE_POST)
return make_pair(_("#"), TableAlign::RIGHT);
else
return make_pair(_("Pre #"), TableAlign::RIGHT);
case Column::DEFAULT:
return make_pair(_("Default"), TableAlign::LEFT);
case Column::ACTIVE:
return make_pair(_("Active"), TableAlign::LEFT);
case Column::TYPE:
return make_pair(_("Type"), TableAlign::LEFT);
case Column::DATE:
if (list_mode != ListMode::PRE_POST)
return make_pair(_("Date"), TableAlign::LEFT);
else
return make_pair(_("Pre Date"), TableAlign::LEFT);
case Column::USER:
return make_pair(_("User"), TableAlign::LEFT);
case Column::USED_SPACE:
return make_pair(_("Used Space"), TableAlign::RIGHT);;
case Column::CLEANUP:
return make_pair(_("Cleanup"), TableAlign::LEFT);
case Column::DESCRIPTION:
return make_pair(_("Description"), TableAlign::LEFT);
case Column::USERDATA:
return make_pair(_("Userdata"), TableAlign::LEFT);
case Column::PRE_NUMBER:
return make_pair(_("Pre #"), TableAlign::RIGHT);
case Column::POST_NUMBER:
return make_pair(_("Post #"), TableAlign::RIGHT);
case Column::POST_DATE:
return make_pair(_("Post Date"), TableAlign::LEFT);
case Column::READ_ONLY:
return make_pair(_("Read-Only"), TableAlign::LEFT);
}
SN_THROW(Exception("invalid column value"));
__builtin_unreachable();
}
boost::any
value_for_as_any(const OutputOptions& output_options, const OutputHelper& output_helper, Column column,
const ProxySnapshot& snapshot)
{
switch (column)
{
case Column::CONFIG:
return output_helper.snapper->configName();
case Column::SUBVOLUME:
return output_helper.snapper->getConfig().getSubvolume();
case Column::NUMBER:
{
if (output_options.human)
return decString(snapshot.getNum()) + output_helper.extra_sign(snapshot);
else
return snapshot.getNum();
}
case Column::DEFAULT:
return output_helper.is_default(snapshot);
case Column::ACTIVE:
return output_helper.is_active(snapshot);
case Column::TYPE:
return toString(snapshot.getType());
case Column::DATE:
return snapshot.isCurrent() ? "" : datetime(snapshot.getDate(), output_options.utc,
output_options.iso);
case Column::USER:
return username(snapshot.getUid());
case Column::USED_SPACE:
{
if (snapshot.isCurrent() || output_helper.is_used_space_broken())
return nullptr;
uint64_t used_space = snapshot.getUsedSpace();
if (output_options.human)
return byte_to_humanstring(used_space, false, 2);
else
return used_space;
}
case Column::CLEANUP:
return snapshot.getCleanup();
case Column::DESCRIPTION:
return snapshot.getDescription();
case Column::USERDATA:
return snapshot.getUserdata();
case Column::PRE_NUMBER:
{
if (snapshot.getType() != POST)
return nullptr;
return snapshot.getPreNum();
}
case Column::POST_NUMBER:
{
ProxySnapshots::const_iterator it = output_helper.find_post(snapshot);
if (it == output_helper.snapshots.end())
return nullptr;
if (output_options.human)
return decString(it->getNum()) + output_helper.extra_sign(*it);
else
return it->getNum();
}
case Column::POST_DATE:
{
ProxySnapshots::const_iterator it = output_helper.find_post(snapshot);
if (it == output_helper.snapshots.end())
return nullptr;
return datetime(it->getDate(), output_options.utc, output_options.iso);
}
case Column::READ_ONLY:
return snapshot.isReadOnly();
}
SN_THROW(Exception("invalid column value in value_for_as_any"));
__builtin_unreachable();
}
string
value_for_as_string(const OutputOptions& output_options, const OutputHelper& output_helper,
Column column, const ProxySnapshot& snapshot)
{
boost::any value = value_for_as_any(output_options, output_helper, column, snapshot);
if (value.type() == typeid(nullptr_t))
{
return "";
}
else if (value.type() == typeid(unsigned int))
{
return decString(boost::any_cast<unsigned int>(value));
}
else if (value.type() == typeid(bool))
{
if (output_options.human)
return boost::any_cast<bool>(value) ? _("yes") : _("no");
else
return boost::any_cast<bool>(value) ? "yes" : "no";
}
else if (value.type() == typeid(string))
{
return boost::any_cast<string>(value).c_str();
}
else if (value.type() == typeid(uint64_t))
{
return decString(boost::any_cast<uint64_t>(value));
}
else if (value.type() == typeid(map<string, string>))
{
return show_userdata(boost::any_cast<map<string, string>>(value));
}
SN_THROW(Exception("invalid column type in value_for_as_string"));
__builtin_unreachable();
}
json_object*
value_for_as_json(const OutputOptions& output_options, const OutputHelper& output_helper,
Column column, const ProxySnapshot& snapshot)
{
boost::any value = value_for_as_any(output_options, output_helper, column, snapshot);
if (value.type() == typeid(nullptr_t))
{
return nullptr;
}
else if (value.type() == typeid(unsigned int))
{
return json_object_new_int(boost::any_cast<unsigned int>(value));
}
else if (value.type() == typeid(bool))
{
return json_object_new_boolean(boost::any_cast<bool>(value));
}
else if (value.type() == typeid(string))
{
return json_object_new_string(boost::any_cast<string>(value).c_str());
}
else if (value.type() == typeid(uint64_t))
{
#if JSON_C_VERSION_NUM >= ((0 << 16) | (14 << 8) | 0)
return json_object_new_uint64(boost::any_cast<uint64_t>(value));
#else
return json_object_new_int64(boost::any_cast<uint64_t>(value));
#endif
}
else if (value.type() == typeid(map<string, string>))
{
map<string, string> tmp = boost::any_cast<map<string, string>>(value);
if (tmp.empty())
return nullptr;
json_object* json_userdata = json_object_new_object();
for (const map<string, string>::value_type& sub_value : tmp)
json_object_object_add(json_userdata, sub_value.first.c_str(),
json_object_new_string(sub_value.second.c_str()));
return json_userdata;
}
SN_THROW(Exception("invalid column type in value_for_as_json"));
__builtin_unreachable();
}
void
output(GlobalOptions& global_options, const vector<Column>& columns,
const vector<const ProxySnapper*>& snappers, ListMode list_mode)
{
switch (global_options.output_format())
{
case GlobalOptions::OutputFormat::TABLE:
{
OutputOptions output_options(global_options.utc(), global_options.iso(), true);
bool first_table = true;
for (const ProxySnapper* snapper : snappers)
{
if (!first_table)
cout << endl;
if (snappers.size() > 1)
{
cout << "Config: " << snapper->configName() << ", subvolume: "
<< snapper->getConfig().getSubvolume() << endl;
}
OutputHelper output_helper(snapper, columns);
TableFormatter formatter(global_options.table_style());
for (Column column : columns)
{
if (output_helper.skip_column(column))
continue;
formatter.header().push_back(header_for(list_mode, column));
formatter.abbrev().push_back(global_options.abbreviate() &&
column == Column::DESCRIPTION);
}
for (const ProxySnapshot& snapshot : output_helper.snapshots)
{
if (output_helper.skip_snapshot(snapshot, list_mode))
continue;
vector<string> row;
for (Column column : columns)
{
if (output_helper.skip_column(column))
continue;
row.push_back(value_for_as_string(output_options, output_helper, column,
snapshot));
}
formatter.rows().push_back(row);
}
cout << formatter;
first_table = false;
}
}
break;
case GlobalOptions::OutputFormat::CSV:
{
OutputOptions output_options(global_options.utc(), true, false);
CsvFormatter formatter(global_options.separator(), global_options.headers());
for (Column column : columns)
formatter.header().push_back(toString(column));
for (const ProxySnapper* snapper : snappers)
{
OutputHelper output_helper(snapper, columns);
for (const ProxySnapshot& snapshot : output_helper.snapshots)
{
if (output_helper.skip_snapshot(snapshot, list_mode))
continue;
vector<string> row;
for (Column column : columns)
row.push_back(value_for_as_string(output_options, output_helper, column,
snapshot));
formatter.rows().push_back(row);
}
}
cout << formatter;
}
break;
case GlobalOptions::OutputFormat::JSON:
{
OutputOptions output_options(global_options.utc(), true, false);
JsonFormatter formatter;
for (const ProxySnapper* snapper : snappers)
{
json_object* json_config = json_object_new_array();
json_object_object_add(formatter.root(), snapper->configName().c_str(), json_config);
OutputHelper output_helper(snapper, columns);
for (const ProxySnapshot& snapshot : output_helper.snapshots)
{
if (output_helper.skip_snapshot(snapshot, list_mode))
continue;
json_object* json_snapshot = json_object_new_object();
json_object_array_add(json_config, json_snapshot);
for (const Column& column : columns)
json_object_object_add(json_snapshot, toString(column).c_str(),
value_for_as_json(output_options, output_helper,
column, snapshot));
}
}
cout << formatter;
}
break;
}
}
}
void
command_list(GlobalOptions& global_options, GetOpts& get_opts, ProxySnappers* snappers, ProxySnapper* snapper)
{
const vector<Option> options = {
Option("type", required_argument, 't'),
Option("disable-used-space", no_argument),
Option("all-configs", no_argument, 'a'),
Option("columns", required_argument)
};
ParsedOpts opts = get_opts.parse("list", options);
ListMode list_mode = ListMode::ALL;
bool show_used_space = true;
vector<Column> columns;
ParsedOpts::const_iterator opt;
if ((opt = opts.find("type")) != opts.end())
{
if (!toValue(opt->second, list_mode, false))
{
string error = sformat(_("Unknown type '%s'."), opt->second.c_str()) + '\n' +
possible_enum_values<ListMode>();
SN_THROW(OptionsException(error));
}
}
if ((opt = opts.find("disable-used-space")) != opts.end())
{
show_used_space = false;
}
if ((opt = opts.find("columns")) != opts.end())
{
columns = parse_columns<Column>(opt->second);
}
else
{
columns = default_columns(list_mode, global_options.output_format());
}
if (!show_used_space)
{
columns.erase(remove(columns.begin(), columns.end(), Column::USED_SPACE), columns.end());
}
if (get_opts.has_args())
{
SN_THROW(OptionsException(_("Command 'list' does not take arguments.")));
}
if ((opt = opts.find("all-configs")) == opts.end())
{
output(global_options, columns, { snappers->getSnapper(global_options.config()) }, list_mode);
}
else
{
vector<const ProxySnapper*> tmp;
for (map<string, ProxyConfig>::value_type it : snappers->getConfigs())
tmp.push_back(snappers->getSnapper(it.first));
output(global_options, columns, tmp, list_mode);
}
}
template <> struct EnumInfo<Column> { static const vector<string> names; };
const vector<string> EnumInfo<Column>::names({
"config", "subvolume", "number", "default", "active", "type", "date", "user", "used-space", "cleanup",
"description", "userdata", "pre-number", "post-number", "post-date", "read-only"
});
template <> struct EnumInfo<ListMode> { static const vector<string> names; };
const vector<string> EnumInfo<ListMode>::names({ "all", "single", "pre-post" });
}
|