Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / tools / rbd / action / Diff.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
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"
10 #include <iostream>
11 #include <boost/program_options.hpp>
12
13 namespace rbd {
14 namespace action {
15 namespace diff {
16
17 namespace at = argument_types;
18 namespace po = boost::program_options;
19
20 struct output_method {
21   output_method() : f(NULL), t(NULL), empty(true) {}
22   Formatter *f;
23   TextTable *t;
24   bool empty;
25 };
26
27 static int diff_cb(uint64_t ofs, size_t len, int exists, void *arg)
28 {
29   output_method *om = static_cast<output_method *>(arg);
30   om->empty = false;
31   if (om->f) {
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();
37   } else {
38     assert(om->t);
39     *(om->t) << ofs << len << (exists ? "data" : "zero") << TextTable::endrow;
40   }
41   return 0;
42 }
43
44 static int do_diff(librbd::Image& image, const char *fromsnapname,
45                    bool whole_object, Formatter *f)
46 {
47   int r;
48   librbd::image_info_t info;
49
50   r = image.stat(info, sizeof(info));
51   if (r < 0)
52     return r;
53
54   output_method om;
55   if (f) {
56     om.f = f;
57     f->open_array_section("extents");
58   } else {
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);
63   }
64
65   r = image.diff_iterate2(fromsnapname, 0, info.size, true, whole_object,
66                           diff_cb, &om);
67   if (f) {
68     f->close_section();
69     f->flush(std::cout);
70   } else {
71     if (!om.empty)
72       std::cout << *om.t;
73     delete om.t;
74   }
75   return r;
76 }
77
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);
87 }
88
89 int execute(const po::variables_map &vm) {
90   size_t arg_index = 0;
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);
98   if (r < 0) {
99     return r;
100   }
101
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>();
105   }
106
107   bool diff_whole_object = vm[at::WHOLE_OBJECT].as<bool>();
108
109   at::Format::Formatter formatter;
110   r = utils::get_formatter(vm, &formatter);
111   if (r < 0) {
112     return r;
113   }
114
115   librados::Rados rados;
116   librados::IoCtx io_ctx;
117   librbd::Image image;
118   r = utils::init_and_open_image(pool_name, image_name, "", snap_name, true,
119                                  &rados, &io_ctx, &image);
120   if (r < 0) {
121     return r;
122   }
123
124   r = do_diff(image, from_snap_name.empty() ? nullptr : from_snap_name.c_str(),
125               diff_whole_object, formatter.get());
126   if (r < 0) {
127     std::cerr << "rbd: diff error: " << cpp_strerror(r) << std::endl;
128     return -r;
129   }
130   return 0;
131 }
132
133 Shell::SwitchArguments switched_arguments({at::WHOLE_OBJECT});
134 Shell::Action action(
135   {"diff"}, {},
136   "Print extents that differ since a previous snap, or image creation.", "",
137   &get_arguments, &execute);
138
139 } // namespace diff
140 } // namespace action
141 } // namespace rbd