diff --git a/include/DefaultQuotas.h b/include/DefaultQuotas.h index 892f23415c..22a81b875e 100644 --- a/include/DefaultQuotas.h +++ b/include/DefaultQuotas.h @@ -18,8 +18,10 @@ #define DEFAULT_QUOTAS_H_ #include "Quotas.h" +#include "SqlDB.h" +#include "ObjectSQL.h" -class DefaultQuotas : public Quotas +class DefaultQuotas : public Quotas, ObjectSQL { public: DefaultQuotas( @@ -54,14 +56,94 @@ public: string& to_xml(string& xml) const; /** - * Builds quota object from an ObjectXML - * @param xml The xml-formatted string - * @return 0 if success + * Writes the quotas in the database. + * @param db pointer to the db + * @return 0 on success */ - int from_xml(const string& xml); + int insert(SqlDB *db, string& error_str) + { + return insert_replace(db, false, error_str); + }; + + /** + * Writes/updates the quotas data fields in the database. + * @param db pointer to the db + * @return 0 on success + */ + int update(SqlDB *db) + { + string error_str; + return insert_replace(db, true, error_str); + }; + + /** + * Reads the Quotas from the database. + * @param db pointer to the db + * @return 0 on success + */ + int select(SqlDB *db); + + /** + * Bootstraps the database table(s) associated to the default quotas + * @return 0 on success + */ + static int bootstrap(SqlDB * db); private: const char * root_elem; + + // ************************************************************************* + // DataBase implementation + // ************************************************************************* + + static const char * db_names; + + static const char * db_bootstrap; + + static const char * table; + + /** + * Execute an INSERT or REPLACE Sql query. + * @param db The SQL DB + * @param replace Execute an INSERT or a REPLACE + * @param error_str Returns the error reason, if any + * @return 0 one success + */ + int insert_replace(SqlDB *db, bool replace, string& error_str); + + /** + * Callback function to unmarshall a PoolObjectSQL + * @param num the number of columns read from the DB + * @param names the column names + * @param vaues the column values + * @return 0 on success + */ + int select_cb(void *nil, int num, char **values, char **names) + { + if ( (!values[0]) || (num != 1) ) + { + return -1; + } + + return from_xml(values[0]); + }; + + /** + * Builds quota object from an ObjectXML + * @param xml The xml-formatted string + * @return 0 on success + */ + int from_xml(const string& xml); + + /** + * Removes the Quotas from the database. + * @param db pointer to the db + * @return 0 on success + */ + int drop(SqlDB * db) + { + return -1; // Not supported + }; }; #endif /*DEFAULT_QUOTAS_H_*/ diff --git a/include/Nebula.h b/include/Nebula.h index 39430cc5d5..ef30c0e32a 100644 --- a/include/Nebula.h +++ b/include/Nebula.h @@ -320,7 +320,15 @@ public: int set_default_user_quota(Template *tmpl, string& error) { - return default_user_quota.set(tmpl, error); + int rc; + rc = default_user_quota.set(tmpl, error); + + if ( rc == 0 ) + { + rc = default_user_quota.update(db); + } + + return rc; }; const DefaultQuotas& get_default_group_quota() @@ -330,7 +338,15 @@ public: int set_default_group_quota(Template *tmpl, string& error) { - return default_group_quota.set(tmpl, error); + int rc; + rc = default_group_quota.set(tmpl, error); + + if ( rc == 0 ) + { + rc = default_group_quota.update(db); + } + + return rc; }; private: diff --git a/src/nebula/Nebula.cc b/src/nebula/Nebula.cc index d7b5513d56..e6a6eaed9a 100644 --- a/src/nebula/Nebula.cc +++ b/src/nebula/Nebula.cc @@ -245,6 +245,8 @@ void Nebula::start() rc += ClusterPool::bootstrap(db); rc += DocumentPool::bootstrap(db); + rc += DefaultQuotas::bootstrap(db); + // Create the versioning table only if bootstrap went well if ( rc == 0 ) { @@ -340,6 +342,9 @@ void Nebula::start() tpool = new VMTemplatePool(db); dspool = new DatastorePool(db); + + default_user_quota.select(db); + default_group_quota.select(db); } catch (exception&) { @@ -681,6 +686,7 @@ int Nebula::bootstrap() { int rc; ostringstream oss; + string error; oss << "CREATE TABLE pool_control (tablename VARCHAR(32) PRIMARY KEY, " "last_oid BIGINT UNSIGNED)"; @@ -700,6 +706,9 @@ int Nebula::bootstrap() rc += db->exec(oss); + rc += default_user_quota.insert(db, error); + rc += default_group_quota.insert(db, error); + return rc; } diff --git a/src/um/DefaultQuotas.cc b/src/um/DefaultQuotas.cc index fc8877dc24..139e53e9aa 100644 --- a/src/um/DefaultQuotas.cc +++ b/src/um/DefaultQuotas.cc @@ -17,6 +17,15 @@ #include "DefaultQuotas.h" #include "ObjectXML.h" +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +const char * DefaultQuotas::table = "system_attributes"; +const char * DefaultQuotas::db_names = "name, body"; + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + string& DefaultQuotas::to_xml(string& xml) const { ostringstream oss; @@ -91,3 +100,103 @@ int DefaultQuotas::from_xml(const string& xml) return rc; } + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int DefaultQuotas::select(SqlDB *db) +{ + ostringstream oss; + + int rc; + + set_callback( + static_cast(&DefaultQuotas::select_cb)); + + oss << "SELECT body FROM " << table << " WHERE name = '" << root_elem << "'"; + + rc = db->exec(oss, this); + + unset_callback(); + + if (rc != 0) + { + return -1; + } + + return 0; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int DefaultQuotas::insert_replace(SqlDB *db, bool replace, string& error_str) +{ + ostringstream oss; + + int rc; + string xml_body; + + char * sql_xml; + + sql_xml = db->escape_str(to_xml(xml_body).c_str()); + + if ( sql_xml == 0 ) + { + goto error_body; + } +/* + if ( validate_xml(sql_xml) != 0 ) + { + goto error_xml; + } +*/ + if ( replace ) + { + oss << "REPLACE"; + } + else + { + oss << "INSERT"; + } + + // Construct the SQL statement to Insert or Replace + + oss <<" INTO "<exec(oss); + + db->free_str(sql_xml); + + return rc; +/* +error_xml: + db->free_str(sql_xml); + + error_str = "Error transforming the Quotas to XML."; + + goto error_common; +*/ +error_body: + goto error_generic; + +error_generic: + error_str = "Error inserting Quotas in DB."; +error_common: + return -1; +} + +/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ + +int DefaultQuotas::bootstrap(SqlDB * db) +{ + ostringstream oss; + + oss << "CREATE TABLE IF NOT EXISTS system_attributes (" + "name VARCHAR(128) PRIMARY KEY, body TEXT)"; + + return db->exec(oss); +}