From 26a20518aa74d4d038b24c126dcc7fb60d2403b4 Mon Sep 17 00:00:00 2001 From: Zhang Shaowen Date: Fri, 7 Jul 2017 14:17:38 +0800 Subject: [PATCH] rgw: add valid and empty in class LCExpiration. Signed-off-by: Zhang Shaowen --- src/rgw/rgw_lc.cc | 17 +++++------------ src/rgw/rgw_lc.h | 22 +++++++++++++++++----- src/rgw/rgw_lc_s3.cc | 9 +++++---- src/rgw/rgw_lc_s3.h | 6 +++--- 4 files changed, 30 insertions(+), 24 deletions(-) diff --git a/src/rgw/rgw_lc.cc b/src/rgw/rgw_lc.cc index 0a33d4716b9..1d91df58ce5 100644 --- a/src/rgw/rgw_lc.cc +++ b/src/rgw/rgw_lc.cc @@ -27,22 +27,15 @@ const char* LC_STATUS[] = { using namespace std; using namespace librados; -bool LCRule::validate() +bool LCRule::valid() { if (id.length() > MAX_ID_LEN) { return false; } - else if(!expiration.has_days() && !expiration.has_date() && - !noncur_expiration.has_days() && !mp_expiration.has_days() && !dm_expiration) { + else if(expiration.empty() && noncur_expiration.empty() && mp_expiration.empty() && !dm_expiration) { return false; } - else if (expiration.has_days() && expiration.get_days() <= 0) { - return false; - } - else if (noncur_expiration.has_days() && noncur_expiration.get_days() <=0) { - return false; - } - else if (mp_expiration.has_days() && mp_expiration.get_days() <= 0) { + else if (!expiration.valid() || !noncur_expiration.valid() || !mp_expiration.valid()) { return false; } return true; @@ -80,7 +73,7 @@ bool RGWLifecycleConfiguration::_add_rule(LCRule *rule) int RGWLifecycleConfiguration::check_and_add_rule(LCRule *rule) { - if (!rule->validate()) { + if (!rule->valid()) { return -EINVAL; } string id; @@ -111,7 +104,7 @@ bool RGWLifecycleConfiguration::has_same_action(const lc_op& first, const lc_op& //Rules are conflicted: if one rule's prefix starts with other rule's prefix, and these two rules //define same action. -bool RGWLifecycleConfiguration::validate() +bool RGWLifecycleConfiguration::valid() { if (prefix_map.size() < 2) { return true; diff --git a/src/rgw/rgw_lc.h b/src/rgw/rgw_lc.h index d9df3d2a476..fe96a7500d5 100644 --- a/src/rgw/rgw_lc.h +++ b/src/rgw/rgw_lc.h @@ -62,20 +62,32 @@ public: void dump(Formatter *f) const; // static void generate_test_instances(list& o); void set_days(const string& _days) { days = _days; } - string get_days_str() const{ + string get_days_str() const { return days; } - int get_days() {return atoi(days.c_str()); } + int get_days() const {return atoi(days.c_str()); } bool has_days() const { return !days.empty(); } void set_date(const string& _date) { date = _date; } - string get_date() const{ + string get_date() const { return date; } bool has_date() const { return !date.empty(); } + bool empty() const { + return days.empty() && date.empty(); + } + bool valid() const { + if (!days.empty() && !date.empty()) { + return false; + } else if (!days.empty() && get_days() <= 0) { + return false; + } + //We've checked date in xml parsing + return true; + } }; WRITE_CLASS_ENCODER(LCExpiration) @@ -152,7 +164,7 @@ public: dm_expiration = _dm_expiration; } - bool validate(); + bool valid(); void encode(bufferlist& bl) const { ENCODE_START(4, 1, bl); @@ -241,7 +253,7 @@ public: int check_and_add_rule(LCRule* rule); - bool validate(); + bool valid(); multimap& get_rule_map() { return rule_map; } map& get_prefix_map() { return prefix_map; } diff --git a/src/rgw/rgw_lc_s3.cc b/src/rgw/rgw_lc_s3.cc index 525cd0a4353..ea64847a0c7 100644 --- a/src/rgw/rgw_lc_s3.cc +++ b/src/rgw/rgw_lc_s3.cc @@ -31,6 +31,7 @@ bool LCExpiration_S3::xml_end(const char * el) { } } else { date = lc_date->get_data(); + //We need return xml error according to S3 if (boost::none == ceph::from_iso_8601(date)) { return false; } @@ -127,15 +128,15 @@ void LCRule_S3::to_xml(CephContext *cct, ostream& out) { out << "" << id << ""; out << "" << prefix << ""; out << "" << status << ""; - if (expiration.has_days() || expiration.has_date() || dm_expiration) { + if (!expiration.empty() || dm_expiration) { LCExpiration_S3 expir(expiration.get_days_str(), expiration.get_date(), dm_expiration); expir.to_xml(out); } - if (noncur_expiration.has_days()) { + if (!noncur_expiration.empty()) { LCNoncurExpiration_S3& noncur_expir = static_cast(noncur_expiration); noncur_expir.to_xml(out); } - if (mp_expiration.has_days()) { + if (!mp_expiration.empty()) { LCMPExpiration_S3& mp_expir = static_cast(mp_expiration); mp_expir.to_xml(out); } @@ -152,7 +153,7 @@ int RGWLifecycleConfiguration_S3::rebuild(RGWRados *store, RGWLifecycleConfigura if (ret < 0) return ret; } - if (!dest.validate()) { + if (!dest.valid()) { ret = -ERR_INVALID_REQUEST; } return ret; diff --git a/src/rgw/rgw_lc_s3.h b/src/rgw/rgw_lc_s3.h index 8d74bcaae79..ed1af0c0053 100644 --- a/src/rgw/rgw_lc_s3.h +++ b/src/rgw/rgw_lc_s3.h @@ -152,15 +152,15 @@ public: encode_xml("ID", id, f); encode_xml("Prefix", prefix, f); encode_xml("Status", status, f); - if (expiration.has_days() || expiration.has_date() || dm_expiration) { + if (!expiration.empty() || dm_expiration) { LCExpiration_S3 expir(expiration.get_days_str(), expiration.get_date(), dm_expiration); expir.dump_xml(f); } - if (noncur_expiration.has_days()) { + if (!noncur_expiration.empty()) { const LCNoncurExpiration_S3& noncur_expir = static_cast(noncur_expiration); noncur_expir.dump_xml(f); } - if (mp_expiration.has_days()) { + if (!mp_expiration.empty()) { const LCMPExpiration_S3& mp_expir = static_cast(mp_expiration); mp_expir.dump_xml(f); } -- 2.39.5