Index: branches/tap_branches/ippTools/src/pstamptool.c
===================================================================
--- branches/tap_branches/ippTools/src/pstamptool.c	(revision 25900)
+++ branches/tap_branches/ippTools/src/pstamptool.c	(revision 27838)
@@ -51,4 +51,5 @@
 static bool pendingdependentMode(pxConfig *config);
 static bool updatedependentMode(pxConfig *config);
+static bool revertdependentMode(pxConfig *config);
 
 # define MODECASE(caseName, func) \
@@ -91,4 +92,5 @@
         MODECASE(PSTAMPTOOL_MODE_PENDINGDEPENDENT, pendingdependentMode);
         MODECASE(PSTAMPTOOL_MODE_UPDATEDEPENDENT, updatedependentMode);
+        MODECASE(PSTAMPTOOL_MODE_REVERTDEPENDENT, revertdependentMode);
         default:
             psAbort("invalid option (this should not happen)");
@@ -116,8 +118,10 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    PXOPT_LOOKUP_STR(uri,         config->args, "-uri",           true, false);
-    PXOPT_LOOKUP_STR(outProduct,  config->args, "-out_product",   true, false);
-    PXOPT_LOOKUP_STR(lastFileset, config->args, "-last_fileset", false, false);
-    PXOPT_LOOKUP_STR(state,       config->args, "-state",         false, false);
+    PXOPT_LOOKUP_STR(uri,         config->args, "-set_uri",           true, false);
+    PXOPT_LOOKUP_STR(outProduct,  config->args, "-set_out_product",   true, false);
+    PXOPT_LOOKUP_STR(lastFileset, config->args, "-set_last_fileset", false, false);
+    PXOPT_LOOKUP_STR(state,       config->args, "-set_state",         false, false);
+    PXOPT_LOOKUP_STR(label,       config->args, "-set_label",         false, false);
+    PXOPT_LOOKUP_S32(pollInterval, config->args, "-set_poll_interval",false, false);
 
     if (!pstampDataStoreInsert(config->dbh,
@@ -125,6 +129,9 @@
             state,
             lastFileset,
+            NULL,       // timestamp
+            label,      // label
             outProduct,
-            uri
+            uri,
+            pollInterval
         )) {
         psError(PS_ERR_UNKNOWN, false, "database error");
@@ -142,9 +149,10 @@
     PXOPT_COPY_S64(config->args, where, "-ds_id", "ds_id", "==");
 
+    PXOPT_LOOKUP_BOOL(ready, config->args, "-ready", false);
     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
 
     psString query = pxDataGet("pstamptool_datastore.sql");
     if (!query) {
-        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
         return false;
     }
@@ -156,4 +164,7 @@
     }
     psFree(where);
+    if (ready) {
+        psStringAppend(&query, " %s", "\nAND TIMESTAMPDIFF(SECOND, timestamp, now()) > pollInterval");
+    }
 
     if (!p_psDBRunQuery(config->dbh, query)) {
@@ -189,25 +200,33 @@
 
     PXOPT_LOOKUP_S64(ds_id,       config->args, "-ds_id",         true, false);
-    PXOPT_LOOKUP_STR(lastFileset, config->args, "-last_fileset",  false, false);
-    PXOPT_LOOKUP_STR(state,       config->args, "-state",         false, false);
-
-    if (!state && !lastFileset) {
-        psError(PS_ERR_UNKNOWN, true, "at least one of -last_fileset or -state is required");
-        return false;
-    }
-
-    char *query = psStringCopy ("UPDATE pstampDataStore SET");
-    bool needComma = false;
-    
+    PXOPT_LOOKUP_STR(lastFileset, config->args, "-set_last_fileset",  false, false);
+    PXOPT_LOOKUP_STR(state,       config->args, "-set_state",         false, false);
+    PXOPT_LOOKUP_STR(label,       config->args, "-set_label",         false, false);
+    PXOPT_LOOKUP_S32(pollInterval, config->args, "-set_poll_interval",         false, false);
+    PXOPT_LOOKUP_BOOL(update_timestamp, config->args, "-update_timestamp", false);
+
+    if (!state && !lastFileset && !pollInterval && !update_timestamp && !label) {
+        psError(PS_ERR_UNKNOWN, true, "at least one of -last_fileset or -set_state is required");
+        return false;
+    }
+
+    char *query = psStringCopy ("UPDATE pstampDataStore SET timestamp = CURRENT_TIMESTAMP() ");
+
     if (lastFileset) {
-        psStringAppend(&query, " lastFileset = '%s'", lastFileset);
-        needComma = true;
+        psStringAppend(&query, " , lastFileset = '%s'", lastFileset);
     }
 
     if (state) {
-        psStringAppend(&query, "%s state = '%s'", needComma ? "," : " ", state);
-        needComma = true; // be ready in case we add another field
-    }
-                
+        psStringAppend(&query, ", state = '%s'", state);
+    }
+
+    if (label) {
+        psStringAppend(&query, ", label = '%s'", label);
+    }
+
+    if (pollInterval) {
+        psStringAppend(&query, ", pollInterval = %d", pollInterval);
+    }
+
     psStringAppend(&query, " WHERE ds_id = %" PRId64, ds_id);
 
@@ -220,5 +239,5 @@
     psU64 affected = psDBAffectedRows(config->dbh);
     if (affected != 1) {
-        psError(PS_ERR_UNKNOWN, false, "should have affected one row but %" 
+        psError(PS_ERR_UNKNOWN, false, "should have affected one row but %"
                                         PRIu64 " rows were modified", affected);
         return false;
@@ -245,6 +264,6 @@
         label,
         NULL,   // outProduct
-        uri,    
-        0       // fault 
+        uri,
+        0       // fault
         )) {
         psError(PS_ERR_UNKNOWN, false, "failed to insert request");
@@ -265,4 +284,5 @@
     psMetadata *where = psMetadataAlloc();
     PXOPT_COPY_S64(config->args, where, "-req_id", "req_id", "==");
+    pxAddLabelSearchArgs(config, where, "-label", "label", "LIKE");
 
     PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
@@ -271,5 +291,5 @@
     psString query = pxDataGet("pstamptool_pendingreq.sql");
     if (!query) {
-        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
         return false;
     }
@@ -342,5 +362,5 @@
     psFree(whereClause);
     psFree(where);
-    
+
     // treat limit == 0 as "no limit"
     if (limit) {
@@ -365,5 +385,5 @@
         psTrace("pstamptool", PS_LOG_INFO, "request not found");
         // This causes main to exit with PS_EXIT_DATA_ERROR which the script is looking for
-        psError(PXTOOLS_ERR_DATA, true, "request not found");
+        psError(PXTOOLS_ERR_CONFIG, true, "request not found");
         psFree(output);
         // we return false so that the caller can determine that a request does not exist
@@ -387,8 +407,18 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
+    psMetadata *where = psMetadataAlloc();
+    pxAddLabelSearchArgs(config, where, "-label", "label", "LIKE");
+
     PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
 
     psString query = pxDataGet("pstamptool_completedreq.sql");
+
+    if (psListLength(where->list)) {
+        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
+        psStringAppend(&query, " AND %s", whereClause);
+        psFree(whereClause);
+    }
+    psFree(where);
 
     // treat limit == 0 as "no limit"
@@ -436,7 +466,7 @@
     PXOPT_LOOKUP_STR(state,      config->args, "-state",      false, false);
     PXOPT_LOOKUP_STR(outProduct, config->args, "-outProduct", false, false);
-    PXOPT_LOOKUP_STR(fault, 	 config->args, "-fault",      false, false);
-    PXOPT_LOOKUP_STR(uri,   	 config->args, "-uri",        false, false);
-    PXOPT_LOOKUP_STR(name,  	 config->args, "-name",       false, false);
+    PXOPT_LOOKUP_S16(fault,      config->args, "-fault",      false, false);
+    PXOPT_LOOKUP_STR(uri,        config->args, "-uri",        false, false);
+    PXOPT_LOOKUP_STR(name,       config->args, "-name",       false, false);
     PXOPT_LOOKUP_STR(reqType,    config->args, "-reqType",    false, false);
 
@@ -454,5 +484,5 @@
     }
     if (fault) {
-        psStringAppend(&query, "%c fault = %s", c, fault);
+        psStringAppend(&query, "%c fault = %d", c, fault);
         c = ',';
     }
@@ -485,5 +515,5 @@
     // note zero is not an error
     if (affected > 1) {
-        psError(PS_ERR_UNKNOWN, false, "should have affected one row but %" 
+        psError(PS_ERR_UNKNOWN, false, "should have affected one row but %"
                                         PRIu64 " rows were modified", affected);
         return false;
@@ -500,10 +530,11 @@
 
     PXOPT_COPY_S64(config->args, where, "-req_id", "req_id", "==");
-    PXOPT_COPY_S64(config->args, where, "-fault", "req_id", "==");
+    PXOPT_COPY_S64(config->args, where, "-fault", "fault", "==");
     PXOPT_COPY_STR(config->args, where, "-state", "pstampRequest.state", "==");
+    pxAddLabelSearchArgs(config, where, "-label", "pstampRequest.label", "LIKE");
 
     if (!psListLength(where->list)) {
         psFree(where);
-        psError(PXTOOLS_ERR_DATA, false, "search parameters are required");
+        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
         return false;
     }
@@ -525,5 +556,5 @@
     query = pxDataGet("pstamptool_revertreq.sql");
     psStringAppend(&query, " AND %s", whereClause);
-    
+
     psFree(whereClause);
 
@@ -555,25 +586,25 @@
     // unless the job is being inserted with stop state require outputBase
     if (strcmp(stateString, "stop") && !outputBase) {
-	psError(PS_ERR_UNKNOWN, true, "-outputBase is required");
-	return false;
+        psError(PS_ERR_UNKNOWN, true, "-outputBase is required");
+        return false;
     }
 
     // default value for job_type is defined in pstamptoolConfig.c
     if (!strcmp(job_type, "get_image") || !strcmp(job_type, "detect_query") || !strcmp(job_type, "none")) {
-	stampJob = false;
+        stampJob = false;
     } else if (!strcmp(job_type, "stamp")) {
-	stampJob = true;
+        stampJob = true;
     } else {
-	psError(PS_ERR_UNKNOWN, false, "unknown value for -job_type: %s", job_type);
-	return false;
-    }
-    if (!pstampJobInsert(config->dbh, 
+        psError(PS_ERR_UNKNOWN, false, "unknown value for -job_type: %s", job_type);
+        return false;
+    }
+    if (!pstampJobInsert(config->dbh,
             0, // job_id
             req_id,
-            rownum, 
-            stateString, 
-            job_type, 
-            fault, 
-            exp_id, 
+            rownum,
+            stateString,
+            job_type,
+            fault,
+            exp_id,
             outputBase,
             options,
@@ -586,5 +617,5 @@
     psU64 affected = psDBAffectedRows(config->dbh);
     if (affected != 1) {
-        psError(PS_ERR_UNKNOWN, false, 
+        psError(PS_ERR_UNKNOWN, false,
             "should have affected one row but %" PRIu64 " rows were modified",
             affected);
@@ -617,5 +648,5 @@
     psString query = pxDataGet("pstamptool_listjob.sql");
     if (!query) {
-        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
         return false;
     }
@@ -674,4 +705,6 @@
     PXOPT_COPY_S64(config->args, where, "-job_id", "job_id", "==");
     PXOPT_COPY_S64(config->args, where, "-req_id", "req_id", "==");
+    pxAddLabelSearchArgs(config, where, "-label", "label", "LIKE");
+
     PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
@@ -679,5 +712,5 @@
     psString query = pxDataGet("pstamptool_pendingjob.sql");
     if (!query) {
-        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
         return false;
     }
@@ -685,5 +718,5 @@
     // use psDBGenerateWhereSQL because the SQL yields an intermediate table
     if (psListLength(where->list)) {
-        psString whereClause = psDBGenerateWhereConditionSQL(where, "pstampJob");
+        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
         psStringAppend(&query, " AND %s", whereClause);
         psFree(whereClause);
@@ -732,7 +765,18 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    PXOPT_LOOKUP_S64(job_id,    config->args, "-job_id", true, false);
+    PXOPT_LOOKUP_S64(job_id,    config->args, "-job_id", false, false);
+    PXOPT_LOOKUP_S64(dep_id,    config->args, "-dep_id", false, false);
+
+    if (!job_id && !dep_id) {
+        psError(PS_ERR_UNKNOWN, true, "at least -job_id or -dep_id is required");
+        return false;
+    }
     PXOPT_LOOKUP_STR(state,     config->args, "-state",  true, false);
-    PXOPT_LOOKUP_STR(fault,     config->args, "-fault",  false, false);
+    PXOPT_LOOKUP_S32(fault,     config->args, "-fault",  false, false);
+
+    psMetadata *where = psMetadataAlloc();
+
+    PXOPT_COPY_S64(config->args, where, "-job_id", "job_id", "==");
+    PXOPT_COPY_S64(config->args, where, "-dep_id", "dep_id", "==");
 
     psString faultStr = NULL;
@@ -740,12 +784,15 @@
         faultStr = psStringCopy("");
     } else {
-        psStringAppend(&faultStr, ", fault = '%s'", fault);
-    }
-
-    char *query ="UPDATE pstampJob"
-	" SET state = '%s' %s"
-	" WHERE job_id = %" PRId64;
-    
-    if (!p_psDBRunQueryF(config->dbh, query, state, faultStr, job_id)) {
+        psStringAppend(&faultStr, "\n, pstampJob.fault = %d", fault);
+    }
+
+    psString query = pxDataGet("pstamptool_updatejob.sql");
+
+    psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
+    psStringAppend(&query, " WHERE %s", whereClause);
+    psFree(whereClause);
+    psFree(where);
+
+    if (!p_psDBRunQueryF(config->dbh, query, state, faultStr)) {
         psError(PS_ERR_UNKNOWN, false, "database error");
         psFree(query);
@@ -753,11 +800,8 @@
     }
     psFree(faultStr);
+    psFree(query);
 
     psU64 affected = psDBAffectedRows(config->dbh);
-    if (affected != 1) {
-        psError(PS_ERR_UNKNOWN, false, "should have affected one row but %" 
-                                        PRIu64 " rows were modified", affected);
-        return false;
-    }
+    psLogMsg("pstamptool", PS_LOG_INFO, "Updated %" PRIu64 " pstampJobs", affected);
 
     return true;
@@ -772,23 +816,34 @@
     PXOPT_COPY_S64(config->args, where, "-job_id", "job_id", "==");
     PXOPT_COPY_S64(config->args, where, "-req_id", "req_id", "==");
-    PXOPT_COPY_S64(config->args, where, "-fault",  "fault", "==");
+    PXOPT_COPY_S64(config->args, where, "-fault",  "pstampJob.fault", "==");
     PXOPT_COPY_S64(config->args, where, "-req_id_min",  "req_id", ">=");
+    pxAddLabelSearchArgs(config, where, "-label", "pstampRequest.label", "LIKE");
+
     PXOPT_LOOKUP_BOOL(all, config->args, "-all", false);
     PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
+    PXOPT_LOOKUP_S16(fault, config->args, "-fault",  false, false);
+
+    // By default only revert faults < 10 which are our "ipp exit codes"
+    // codes larger than that are the pstamp request interface.
+    // Don't fault those unless -fault waa provided
+    psString faultClause = "";
+    if (!fault) {
+        faultClause = " \nAND (pstampJob.fault < 10)";
+    }
 
     psString query = pxDataGet("pstamptool_revertjob.sql");
     if (!psListLength(where->list) && !all) {
         psFree(where);
-        psError(PXTOOLS_ERR_DATA, false, "search parameters or -all are required");
+        psError(PXTOOLS_ERR_CONFIG, false, "search parameters or -all are required");
         return false;
     }
     if (psListLength(where->list)) {
-        psString whereClause = psDBGenerateWhereConditionSQL(where, "pstampJob");
+        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
         psStringAppend(&query, " AND %s", whereClause);
         psFree(whereClause);
     }
     psFree(where);
-    
-    if (!p_psDBRunQuery(config->dbh, query)) {
+
+    if (!p_psDBRunQueryF(config->dbh, query, faultClause)) {
         psError(PS_ERR_UNKNOWN, false, "database error");
         return false;
@@ -837,5 +892,5 @@
     psString query = pxDataGet("pstamptool_project.sql");
     if (!query) {
-        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
         return false;
     }
@@ -883,7 +938,7 @@
 
     char *query = psStringCopy ("UPDATE pstampProject SET");
-    
+
     psStringAppend(&query, " state = '%s'", state);
-                
+
     psStringAppend(&query, " WHERE proj_id = %" PRId64, proj_id);
 
@@ -896,5 +951,5 @@
     psU64 affected = psDBAffectedRows(config->dbh);
     if (affected != 1) {
-        psError(PS_ERR_UNKNOWN, false, "should have affected one row but %" 
+        psError(PS_ERR_UNKNOWN, false, "should have affected one row but %"
                                         PRIu64 " rows were modified", affected);
         return false;
@@ -910,14 +965,37 @@
     PXOPT_LOOKUP_STR(stage,       config->args, "-stage",   true, false);
     PXOPT_LOOKUP_S64(stage_id,    config->args, "-stage_id", true, false);
+    PXOPT_LOOKUP_STR(component,   config->args, "-component",  true, false);
     PXOPT_LOOKUP_STR(imagedb,     config->args, "-imagedb",  true, false);
-    PXOPT_LOOKUP_STR(rlabel,      config->args, "-rlabel",  false, false);
-    PXOPT_LOOKUP_BOOL(no_magic,   config->args, "-no_magic", false);
+    PXOPT_LOOKUP_STR(rlabel,      config->args, "-rlabel",  true, false);
+    PXOPT_LOOKUP_BOOL(need_magic, config->args, "-need_magic", false);
     PXOPT_LOOKUP_BOOL(no_create,  config->args, "-no_create", false);
 
-    psString query = pxDataGet("pstamptool_pendingdependent.sql");
+    psMetadata *where = psMetadataAlloc();
+    PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");
+    PXOPT_COPY_STR(config->args, where, "-imagedb", "imagedb", "==");
+    PXOPT_COPY_S64(config->args, where, "-stage_id", "stage_id", "==");
+    PXOPT_COPY_STR(config->args, where, "-component", "component", "==");
+
+    // start a transaction eraly so it will contain any row level locks
+    if (!psDBTransaction(config->dbh)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+
+    psString query = pxDataGet("pstamptool_getdependent.sql");
     if (!query) {
-        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
-        return false;
-    }
+        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
+        return false;
+    }
+    psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
+    if (!no_create) {
+        // This will lock the row until the transaction is committed
+        psStringAppend(&query, " AND %s FOR UPDATE", whereClause);
+    } else {
+        psStringAppend(&query, " AND %s", whereClause);
+    }
+    psFree(whereClause);
+    psFree(where);
+
     if (!p_psDBRunQuery(config->dbh, query)) {
         psError(PS_ERR_UNKNOWN, false, "database error");
@@ -937,13 +1015,24 @@
         if (!dep_id) {
             psError(PS_ERR_UNKNOWN, false, "database error");
+            psFree(output);
             return false;
         }
         printf("%" PRId64 "\n", dep_id);
+        psFree(output);
+        if (!psDBRollback(config->dbh)) {
+            psError(PS_ERR_UNKNOWN, false, "database error");
+        }
         return true;
     }
     if (no_create) {
+        if (!psDBRollback(config->dbh)) {
+            psError(PS_ERR_UNKNOWN, false, "database error");
+            return false;
+        }
         return true;
     }
     // no existing dependent that matches, insert one
+    // Since we have multiple processes running jobs we have a
+    // race condition here so that's why we need to lock the table
 
     if (!pstampDependentInsert(
@@ -953,13 +1042,35 @@
         stage,
         stage_id,
+        component,
         imagedb,
         rlabel,
-        no_magic
+        need_magic,
+        0               // fault
         )) {
+        if (!psDBRollback(config->dbh)) {
+            psError(PS_ERR_UNKNOWN, false, "database error");
+        }
         psError(PS_ERR_UNKNOWN, false, "failed to insert pstampDependent");
         return false;
     }
 
+    // if we try and get this after commit zero is returned
     psS64 dep_id = psDBLastInsertID(config->dbh);
+    if (!dep_id) {
+        psError(PS_ERR_UNKNOWN, false, "psDBLastInsertID returned NULL");
+        if (!psDBRollback(config->dbh)) {
+            psError(PS_ERR_UNKNOWN, false, "database error");
+        }
+        return false;
+    }
+
+    if (!psDBCommit(config->dbh)) {
+        // rollback
+        if (!psDBRollback(config->dbh)) {
+            psError(PS_ERR_UNKNOWN, false, "database error");
+        }
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
 
     printf("%" PRId64 "\n", dep_id);
@@ -973,4 +1084,9 @@
 
     psMetadata *where = psMetadataAlloc();
+    PXOPT_COPY_STR(config->args, where, "-stage", "stage", "==");
+    PXOPT_COPY_STR(config->args, where, "-imagedb", "imagedb", "==");
+    PXOPT_COPY_S64(config->args, where, "-stage_id", "stage_id", "==");
+    PXOPT_COPY_STR(config->args, where, "-rlabel", "rlabel", "==");
+    pxAddLabelSearchArgs(config, where, "-label", "pstampRequest.label", "==");
 
     PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
@@ -979,10 +1095,10 @@
     psString query = pxDataGet("pstamptool_pendingdependent.sql");
     if (!query) {
-        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
         return false;
     }
 
     if (psListLength(where->list)) {
-        psString whereClause = psDBGenerateWhereConditionSQL(where, "pstampDependent");
+        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
         psStringAppend(&query, " AND %s", whereClause);
         psFree(whereClause);
@@ -1031,11 +1147,24 @@
 
     PXOPT_LOOKUP_S64(dep_id,    config->args, "-dep_id", true, false);
-    PXOPT_LOOKUP_STR(state,     config->args, "-set_state",  true, false);
-
-    char *query ="UPDATE pstampDependent"
-	" SET state = '%s'"
-	" WHERE dep_id = %" PRId64;
-    
-    if (!p_psDBRunQueryF(config->dbh, query, state, dep_id)) {
+    PXOPT_LOOKUP_STR(state,     config->args, "-set_state",  false, false);
+    PXOPT_LOOKUP_S16(fault,     config->args, "-fault",  false, false);
+
+    if (!fault && !state) {
+        psError(PS_ERR_UNKNOWN, true, "at least one of -set_state or fault is required");
+        return false;
+    }
+    psString query = psStringCopy("UPDATE pstampDependent SET");
+    bool needComma = false;
+    if (state) {
+        psStringAppend(&query, " state = '%s'", state);
+        needComma = true;
+    }
+    if (fault) {
+        psStringAppend(&query, "%s fault = %d", needComma ? ", " : "", fault);
+        needComma = true;
+    }
+    psStringAppend(&query, " WHERE dep_id = %" PRId64, dep_id);
+
+    if (!p_psDBRunQuery(config->dbh, query)) {
         psError(PS_ERR_UNKNOWN, false, "database error");
         psFree(query);
@@ -1045,5 +1174,5 @@
     psU64 affected = psDBAffectedRows(config->dbh);
     if (affected != 1) {
-        psError(PS_ERR_UNKNOWN, false, "should have affected one row but %" 
+        psError(PS_ERR_UNKNOWN, false, "should have affected one row but %"
                                         PRIu64 " rows were modified", affected);
         return false;
@@ -1052,2 +1181,35 @@
     return true;
 }
+static bool revertdependentMode(pxConfig *config)
+{
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    psMetadata *where = psMetadataAlloc();
+    PXOPT_COPY_S64(config->args, where, "-fault", "pstampDependent.fault", "==");
+    PXOPT_COPY_S64(config->args, where, "-dep_id", "dep_id", "==");
+    pxAddLabelSearchArgs(config, where, "-label", "pstampRequest.label", "==");
+
+    if (!psListLength(where->list)) {
+        psFree(where);
+        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
+        return false;
+    }
+    psString query = pxDataGet("pstamptool_revertdependent.sql");
+    if (!query) {
+        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
+        return false;
+    }
+
+    psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
+    psStringAppend(&query, " AND %s", whereClause);
+    psFree(whereClause);
+    psFree(where);
+
+    if (!p_psDBRunQuery(config->dbh, query)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        psFree(query);
+        return false;
+    }
+
+    return true;
+}
