READONLY: allow specifying the db name for setdbreadonly instead of just the hash
authorRonnie Sahlberg <ronniesahlberg@gmail.com>
Sun, 12 Feb 2012 23:27:59 +0000 (10:27 +1100)
committerRonnie Sahlberg <ronniesahlberg@gmail.com>
Thu, 1 Mar 2012 22:13:53 +0000 (09:13 +1100)
tools/ctdb.c

index c3f8a7a5ba7809d15c0fa5d41990f387688115cc..92ef63d10814e7278ab5f71822134b3ceb912a73 100644 (file)
@@ -4126,21 +4126,50 @@ static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **a
  */
 static int control_setdbreadonly(struct ctdb_context *ctdb, int argc, const char **argv)
 {
+       TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
        uint32_t db_id;
-       int ret;
+       struct ctdb_dbid_map *dbmap=NULL;
+       int i, ret;
 
        if (argc < 1) {
                usage();
        }
 
-       db_id = strtoul(argv[0], NULL, 0);
+       if (!strncmp(argv[0], "0x", 2)) {
+               db_id = strtoul(argv[0] + 2, NULL, 0);
+       } else {
+               ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
+               if (ret != 0) {
+                       DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
+                       talloc_free(tmp_ctx);
+                       return ret;
+               }
+               for(i=0;i<dbmap->num;i++){
+                       const char *name;
+
+                       ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
+                       if(!strcmp(argv[0], name)){
+                               talloc_free(discard_const(name));
+                               break;
+                       }
+                       talloc_free(discard_const(name));
+               }
+               if (i == dbmap->num) {
+                       DEBUG(DEBUG_ERR,("No database with name '%s' found\n", argv[0]));
+                       talloc_free(tmp_ctx);
+                       return -1;
+               }
+               db_id = dbmap->dbs[i].dbid;
+       }
 
        ret = ctdb_ctrl_set_db_readonly(ctdb, options.pnn, db_id);
        if (ret != 0) {
                DEBUG(DEBUG_ERR,("Unable to set db to support readonly\n"));
+               talloc_free(tmp_ctx);
                return -1;
        }
 
+       talloc_free(tmp_ctx);
        return 0;
 }