Skip to content
Permalink
Browse files

Add propget and ports_get method for a snapshot

1. propset are currently not supported for a snapshot
2. A method to convert a list of snapshot_ports to a
   list Tcl obj is currently underway
  • Loading branch information
umeshksingla authored and neverpanic committed Aug 18, 2017
1 parent 34a2162 commit e97116aeaec0d859dc2aeee14377ce4d639ceef5
Showing with 116 additions and 6 deletions.
  1. +81 −0 src/cregistry/snapshot.c
  2. +5 −0 src/cregistry/snapshot.h
  3. +30 −6 src/registry2.0/snapshotobj.c
@@ -475,3 +475,84 @@ int reg_snapshot_port_variants_get(reg_registry* reg, sqlite_int64 snapshot_port
return -1;
}
}

/**
* Gets a named property of a snapshot. The property named must be one
* that exists in the table and must not be one with internal meaning
* such as `id` or `state`.
*
* @param [in] snapshot snapshot to get property from
* @param [in] key property to get
* @param [out] value the value of the property
* @param [out] errPtr on error, a description of the error that occurred
* @return true if success; false if failure
*/
int reg_snapshot_propget(reg_snapshot* snapshot, char* key, char** value,
reg_error* errPtr) {
reg_registry* reg = snapshot->reg;
int result = 0;
sqlite3_stmt* stmt = NULL;
char* query;
const char *text;
query = sqlite3_mprintf("SELECT %q FROM registry.snapshots WHERE id=%lld", key,
snapshot->id);
if (sqlite3_prepare_v2(reg->db, query, -1, &stmt, NULL) == SQLITE_OK) {
int r;
do {
r = sqlite3_step(stmt);
switch (r) {
case SQLITE_ROW:
text = (const char*)sqlite3_column_text(stmt, 0);
if (text) {
*value = strdup(text);
result = 1;
} else {
reg_sqlite_error(reg->db, errPtr, query);
}
break;
case SQLITE_DONE:
errPtr->code = REG_INVALID;
errPtr->description = "an invalid snapshot was passed";
errPtr->free = NULL;
break;
case SQLITE_BUSY:
continue;
default:
reg_sqlite_error(reg->db, errPtr, query);
break;
}
} while (r == SQLITE_BUSY);
} else {
reg_sqlite_error(reg->db, errPtr, query);
}
if (stmt) {
sqlite3_finalize(stmt);
}
sqlite3_free(query);
return result;
}

/**
* Gets all the 'ports' of a snapshot. The property named must be one
* that exists in the table and must not be one with internal meaning
* such as `id` or `state`.
*
* A 'port' here is a row in registry.snapshot_ports table and its
* corresponding variants in registry.snapshot_port_variants table
*
* @param [in] snapshot snapshot to get property from
* @param [in] key property to get
* @param [out] value the value of the property
* @param [out] errPtr on error, a description of the error that occurred
* @return true if success; false if failure
*/
int reg_snapshot_ports_get(reg_snapshot* snapshot, port** ports,
reg_error* errPtr) {
reg_registry* reg = snapshot->reg;
int result = 0;
sqlite3_stmt* stmt = NULL;
char* query;
const char *text;
// TODO: get ports and their variants using snapshot->id as Fk
return result;
}
@@ -73,4 +73,9 @@ int snapshot_store_ports(reg_registry* reg, reg_snapshot* snapshot,
int snapshot_store_port_variants(reg_registry* reg, reg_entry* port_entry,
int snapshot_ports_id, reg_error* errPtr);

int reg_snapshot_propget(reg_snapshot* snapshot, char* key, char** value,
reg_error* errPtr);
int reg_snapshot_ports_get(reg_snapshot* snapshot, port** ports,
reg_error* errPtr);

#endif /* _CSNAPSHOT_H */
@@ -45,7 +45,7 @@ const char* snapshot_props[] = {
NULL
};

/* ${snapshot} prop ?value? */
/* ${snapshot} prop */
static int snapshot_obj_prop(Tcl_Interp* interp, reg_snapshot* snapshot, int objc,
Tcl_Obj* CONST objv[]) {
int index;
@@ -73,18 +73,42 @@ static int snapshot_obj_prop(Tcl_Interp* interp, reg_snapshot* snapshot, int obj
return registry_failed(interp, &error);
}
return TCL_ERROR;
} else {
/* ${snapshot} prop name value; set a new value */
}
}

/* ${snapshot} ports */
static int snapshot_obj_ports(Tcl_Interp* interp, reg_snapshot* snapshot, int objc,
Tcl_Obj* CONST objv[]) {
int index;
if (objc > 3) {
Tcl_WrongNumArgs(interp, 2, objv, "?value?");
return TCL_ERROR;
}
if (objc == 2) {
/* ${snapshot} prop; return the current value */
reg_registry* reg = registry_for(interp, reg_attached);
if (reg == NULL) {
return TCL_ERROR;
}
if (Tcl_GetIndexFromObj(interp, objv[1], snapshot_props, "prop", 0, &index)
== TCL_OK) {
char* key = Tcl_GetString(objv[1]);
char* value = Tcl_GetString(objv[2]);
port** ports;
reg_error error;
if (reg_snapshot_propset(snapshot, key, value, &error)) {
if (reg_snapshot_ports_get(snapshot, &ports, &error)) {

// TODO: correct the below for 'ports', added as a prototype for now
// Tcl_Obj** objs;
// int retval = TCL_ERROR;
// if (list_entry_to_obj(interp, &objs, entries, entry_count, &error)){
// Tcl_Obj* result = Tcl_NewListObj(entry_count, objs);
// Tcl_SetObjResult(interp, result);
// free(objs);
// retval = TCL_OK;
// } else {
// retval = registry_failed(interp, &error);
// }

free(ports);
return TCL_OK;
}
return registry_failed(interp, &error);

0 comments on commit e97116a

Please sign in to comment.
You can’t perform that action at this time.