From 12f50c40a5f256041d08c086464b6017944ee7fa Mon Sep 17 00:00:00 2001 From: Dongsheng Yang Date: Wed, 6 Jul 2016 05:24:57 -0400 Subject: [PATCH] rbd: use macro for rbd diff tags Signed-off-by: Dongsheng Yang --- src/tools/rbd/Utils.h | 7 +++++++ src/tools/rbd/action/ExportDiff.cc | 10 +++++----- src/tools/rbd/action/ImportDiff.cc | 12 ++++++------ src/tools/rbd/action/MergeDiff.cc | 30 +++++++++++++++--------------- 4 files changed, 33 insertions(+), 26 deletions(-) diff --git a/src/tools/rbd/Utils.h b/src/tools/rbd/Utils.h index aa75e06cddc5d..b13e555a8c16b 100644 --- a/src/tools/rbd/Utils.h +++ b/src/tools/rbd/Utils.h @@ -16,6 +16,13 @@ namespace utils { static const std::string RBD_DIFF_BANNER ("rbd diff v1\n"); +#define RBD_DIFF_FROM_SNAP 'f' +#define RBD_DIFF_TO_SNAP 't' +#define RBD_DIFF_IMAGE_SIZE 's' +#define RBD_DIFF_WRITE 'w' +#define RBD_DIFF_ZERO 'z' +#define RBD_DIFF_END 'e' + enum SnapshotPresence { SNAPSHOT_PRESENCE_NONE, SNAPSHOT_PRESENCE_PERMITTED, diff --git a/src/tools/rbd/action/ExportDiff.cc b/src/tools/rbd/action/ExportDiff.cc index df74eab32d386..8047b9a43dd78 100644 --- a/src/tools/rbd/action/ExportDiff.cc +++ b/src/tools/rbd/action/ExportDiff.cc @@ -98,7 +98,7 @@ private: uint64_t length, bool exists) { // extent bufferlist bl; - __u8 tag = exists ? 'w' : 'z'; + __u8 tag = exists ? RBD_DIFF_WRITE : RBD_DIFF_ZERO; ::encode(tag, bl); ::encode(offset, bl); ::encode(length, bl); @@ -142,20 +142,20 @@ static int do_export_diff(librbd::Image& image, const char *fromsnapname, __u8 tag; if (fromsnapname) { - tag = 'f'; + tag = RBD_DIFF_FROM_SNAP; ::encode(tag, bl); std::string from(fromsnapname); ::encode(from, bl); } if (endsnapname) { - tag = 't'; + tag = RBD_DIFF_TO_SNAP; ::encode(tag, bl); std::string to(endsnapname); ::encode(to, bl); } - tag = 's'; + tag = RBD_DIFF_IMAGE_SIZE; ::encode(tag, bl); uint64_t endsize = info.size; ::encode(endsize, bl); @@ -179,7 +179,7 @@ static int do_export_diff(librbd::Image& image, const char *fromsnapname, } { - __u8 tag = 'e'; + __u8 tag = RBD_DIFF_END; bufferlist bl; ::encode(tag, bl); r = bl.write_fd(fd); diff --git a/src/tools/rbd/action/ImportDiff.cc b/src/tools/rbd/action/ImportDiff.cc index 88032c74566b4..c9a6b5ee117a0 100644 --- a/src/tools/rbd/action/ImportDiff.cc +++ b/src/tools/rbd/action/ImportDiff.cc @@ -67,10 +67,10 @@ static int do_import_diff(librbd::Image &image, const char *path, goto done; } - if (tag == 'e') { + if (tag == RBD_DIFF_END) { dout(2) << " end diff" << dendl; break; - } else if (tag == 'f') { + } else if (tag == RBD_DIFF_FROM_SNAP) { r = utils::read_string(fd, 4096, &from); // 4k limit to make sure we don't get a garbage string if (r < 0) goto done; @@ -88,7 +88,7 @@ static int do_import_diff(librbd::Image &image, const char *path, goto done; } } - else if (tag == 't') { + else if (tag == RBD_DIFF_TO_SNAP) { r = utils::read_string(fd, 4096, &to); // 4k limit to make sure we don't get a garbage string if (r < 0) goto done; @@ -106,7 +106,7 @@ static int do_import_diff(librbd::Image &image, const char *path, r = -EEXIST; goto done; } - } else if (tag == 's') { + } else if (tag == RBD_DIFF_IMAGE_SIZE) { uint64_t end_size; char buf[8]; r = safe_read_exact(fd, buf, 8); @@ -126,7 +126,7 @@ static int do_import_diff(librbd::Image &image, const char *path, } if (from_stdin) size = end_size; - } else if (tag == 'w' || tag == 'z') { + } else if (tag == RBD_DIFF_WRITE || tag == RBD_DIFF_ZERO) { uint64_t len; char buf[16]; r = safe_read_exact(fd, buf, 16); @@ -138,7 +138,7 @@ static int do_import_diff(librbd::Image &image, const char *path, ::decode(off, p); ::decode(len, p); - if (tag == 'w') { + if (tag == RBD_DIFF_WRITE) { bufferptr bp = buffer::create(len); r = safe_read_exact(fd, bp.c_str(), len); if (r < 0) diff --git a/src/tools/rbd/action/MergeDiff.cc b/src/tools/rbd/action/MergeDiff.cc index c28cb1d266ccf..5c31769d0c408 100644 --- a/src/tools/rbd/action/MergeDiff.cc +++ b/src/tools/rbd/action/MergeDiff.cc @@ -48,17 +48,17 @@ static int parse_diff_header(int fd, __u8 *tag, string *from, string *to, uint64 if (r < 0) return r; - if (*tag == 'f') { + if (*tag == RBD_DIFF_FROM_SNAP) { r = utils::read_string(fd, 4096, from); // 4k limit to make sure we don't get a garbage string if (r < 0) return r; dout(2) << " from snap " << *from << dendl; - } else if (*tag == 't') { + } else if (*tag == RBD_DIFF_TO_SNAP) { r = utils::read_string(fd, 4096, to); // 4k limit to make sure we don't get a garbage string if (r < 0) return r; dout(2) << " to snap " << *to << dendl; - } else if (*tag == 's') { + } else if (*tag == RBD_DIFF_IMAGE_SIZE) { char buf[8]; r = safe_read_exact(fd, buf, 8); if (r < 0) @@ -86,13 +86,13 @@ static int parse_diff_body(int fd, __u8 *tag, uint64_t *offset, uint64_t *length return r; } - if (*tag == 'e') { + if (*tag == RBD_DIFF_END) { offset = 0; length = 0; return 0; } - if (*tag != 'w' && *tag != 'z') + if (*tag != RBD_DIFF_WRITE && *tag != RBD_DIFF_ZERO) return -ENOTSUP; char buf[16]; @@ -118,7 +118,7 @@ static int parse_diff_body(int fd, __u8 *tag, uint64_t *offset, uint64_t *length */ static int accept_diff_body(int fd, int pd, __u8 tag, uint64_t offset, uint64_t length) { - if (tag == 'e') + if (tag == RBD_DIFF_END) return 0; bufferlist bl; @@ -130,7 +130,7 @@ static int accept_diff_body(int fd, int pd, __u8 tag, uint64_t offset, uint64_t if (r < 0) return r; - if (tag == 'w') { + if (tag == RBD_DIFF_WRITE) { bufferptr bp = buffer::create(length); r = safe_read_exact(fd, bp.c_str(), length); if (r < 0) @@ -227,18 +227,18 @@ static int do_merge_diff(const char *first, const char *second, __u8 tag; if (f_from.size()) { - tag = 'f'; + tag = RBD_DIFF_FROM_SNAP; ::encode(tag, bl); ::encode(f_from, bl); } if (s_to.size()) { - tag = 't'; + tag = RBD_DIFF_TO_SNAP; ::encode(tag, bl); ::encode(s_to, bl); } - tag = 's'; + tag = RBD_DIFF_IMAGE_SIZE; ::encode(tag, bl); ::encode(s_size, bl); @@ -271,9 +271,9 @@ static int do_merge_diff(const char *first, const char *second, goto done; } - if (f_tag == 'e') { + if (f_tag == RBD_DIFF_END) { f_end = true; - f_tag = 'z'; + f_tag = RBD_DIFF_ZERO; f_off = f_size; if (f_size < s_size) f_len = s_size - f_size; @@ -302,7 +302,7 @@ static int do_merge_diff(const char *first, const char *second, goto done; } - if (s_tag == 'e') { + if (s_tag == RBD_DIFF_END) { s_end = true; s_off = s_size; if (s_size < f_size) @@ -342,7 +342,7 @@ static int do_merge_diff(const char *first, const char *second, uint64_t delta = s_off + s_len - f_off; if (delta > f_len) delta = f_len; - if (f_tag == 'w') { + if (f_tag == RBD_DIFF_WRITE) { if (first_stdin) { bufferptr bp = buffer::create(delta); r = safe_read_exact(fd, bp.c_str(), delta); @@ -380,7 +380,7 @@ static int do_merge_diff(const char *first, const char *second, } {//tail - __u8 tag = 'e'; + __u8 tag = RBD_DIFF_END; bufferlist bl; ::encode(tag, bl); r = bl.write_fd(pd); -- 2.39.5