1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #include "tools/rbd/ArgumentTypes.h"
5 #include "tools/rbd/Shell.h"
6 #include "tools/rbd/Utils.h"
7 #include "common/errno.h"
8 #include "common/Formatter.h"
9 #include "common/TextTable.h"
11 #include <boost/program_options.hpp>
17 namespace at = argument_types;
18 namespace po = boost::program_options;
20 struct output_method {
21 output_method() : f(NULL), t(NULL), empty(true) {}
27 static int diff_cb(uint64_t ofs, size_t len, int exists, void *arg)
29 output_method *om = static_cast<output_method *>(arg);
32 om->f->open_object_section("extent");
33 om->f->dump_unsigned("offset", ofs);
34 om->f->dump_unsigned("length", len);
35 om->f->dump_string("exists", exists ? "true" : "false");
36 om->f->close_section();
39 *(om->t) << ofs << len << (exists ? "data" : "zero") << TextTable::endrow;
44 static int do_diff(librbd::Image& image, const char *fromsnapname,
45 bool whole_object, Formatter *f)
48 librbd::image_info_t info;
50 r = image.stat(info, sizeof(info));
57 f->open_array_section("extents");
59 om.t = new TextTable();
60 om.t->define_column("Offset", TextTable::LEFT, TextTable::LEFT);
61 om.t->define_column("Length", TextTable::LEFT, TextTable::LEFT);
62 om.t->define_column("Type", TextTable::LEFT, TextTable::LEFT);
65 r = image.diff_iterate2(fromsnapname, 0, info.size, true, whole_object,
78 void get_arguments(po::options_description *positional,
79 po::options_description *options) {
80 at::add_image_or_snap_spec_options(positional, options,
81 at::ARGUMENT_MODIFIER_NONE);
82 options->add_options()
83 (at::FROM_SNAPSHOT_NAME.c_str(), po::value<std::string>(),
84 "snapshot starting point")
85 (at::WHOLE_OBJECT.c_str(), po::bool_switch(), "compare whole object");
86 at::add_format_options(options);
89 int execute(const po::variables_map &vm) {
91 std::string pool_name;
92 std::string image_name;
93 std::string snap_name;
94 int r = utils::get_pool_image_snapshot_names(
95 vm, at::ARGUMENT_MODIFIER_NONE, &arg_index, &pool_name, &image_name,
96 &snap_name, utils::SNAPSHOT_PRESENCE_PERMITTED,
97 utils::SPEC_VALIDATION_NONE);
102 std::string from_snap_name;
103 if (vm.count(at::FROM_SNAPSHOT_NAME)) {
104 from_snap_name = vm[at::FROM_SNAPSHOT_NAME].as<std::string>();
107 bool diff_whole_object = vm[at::WHOLE_OBJECT].as<bool>();
109 at::Format::Formatter formatter;
110 r = utils::get_formatter(vm, &formatter);
115 librados::Rados rados;
116 librados::IoCtx io_ctx;
118 r = utils::init_and_open_image(pool_name, image_name, "", snap_name, true,
119 &rados, &io_ctx, &image);
124 r = do_diff(image, from_snap_name.empty() ? nullptr : from_snap_name.c_str(),
125 diff_whole_object, formatter.get());
127 std::cerr << "rbd: diff error: " << cpp_strerror(r) << std::endl;
133 Shell::SwitchArguments switched_arguments({at::WHOLE_OBJECT});
134 Shell::Action action(
136 "Print extents that differ since a previous snap, or image creation.", "",
137 &get_arguments, &execute);
140 } // namespace action