From 5e2a82675eb50f2e9f0fd8f27fb0e5fed6c97688 Mon Sep 17 00:00:00 2001 From: Loic Dachary Date: Wed, 15 Oct 2014 17:06:12 -0700 Subject: [PATCH] crush: add CrushWrapper::rename_item and can_rename_item The can_rename_item is a const method checking if renaming an item could succeed. If not it returns a unique -errno code and a human readable message message. Trying to rename a non existent item into an existent item returns -EALREADY which can be treated as success if renaming is to be idempotent. Signed-off-by: Loic Dachary --- src/crush/CrushWrapper.cc | 37 +++++++++++++++++++++++++++++++++++++ src/crush/CrushWrapper.h | 6 ++++++ 2 files changed, 43 insertions(+) diff --git a/src/crush/CrushWrapper.cc b/src/crush/CrushWrapper.cc index e605aa32d79f4..dbd50d165d5ee 100644 --- a/src/crush/CrushWrapper.cc +++ b/src/crush/CrushWrapper.cc @@ -63,6 +63,43 @@ bool CrushWrapper::is_v3_rule(unsigned ruleid) const return false; } +int CrushWrapper::can_rename_item(const string& srcname, + const string& dstname, + ostream *ss) const +{ + if (name_exists(srcname)) { + if (name_exists(dstname)) { + *ss << "dstname = '" << dstname << "' already exists"; + return -EEXIST; + } + if (is_valid_crush_name(dstname)) { + return 0; + } else { + *ss << "srcname = '" << srcname << "' does not match [-_.0-9a-zA-Z]+"; + return -EINVAL; + } + } else { + if (name_exists(dstname)) { + *ss << "srcname = '" << srcname << "' does not exist " + << "and dstname = '" << dstname << "' already exists"; + return -EALREADY; + } else { + *ss << "srcname = '" << srcname << "' does not exist"; + return -ENOENT; + } + } +} + +int CrushWrapper::rename_item(const string& srcname, + const string& dstname, + ostream *ss) +{ + int ret = can_rename_item(srcname, dstname, ss); + if (ret < 0) + return ret; + int oldid = get_item_id(srcname); + return set_item_name(oldid, dstname); +} void CrushWrapper::find_takes(set& roots) const { diff --git a/src/crush/CrushWrapper.h b/src/crush/CrushWrapper.h index 1e01d74c7af5a..459d761071d65 100644 --- a/src/crush/CrushWrapper.h +++ b/src/crush/CrushWrapper.h @@ -269,6 +269,12 @@ public: return 0; } + int can_rename_item(const string& srcname, + const string& dstname, + ostream *ss) const; + int rename_item(const string& srcname, + const string& dstname, + ostream *ss); // rule names bool rule_exists(string name) const { build_rmaps(); -- 2.39.5