Index: /tags/ipp-20101029/ippScripts/scripts/magic_process.pl
===================================================================
--- /tags/ipp-20101029/ippScripts/scripts/magic_process.pl	(revision 29636)
+++ /tags/ipp-20101029/ippScripts/scripts/magic_process.pl	(revision 29637)
@@ -325,24 +325,4 @@
 }
 
-### Input result into database
-{
-    my $command = "$magictool -addresult";
-    $command   .= " -magic_id $magic_id";
-    $command   .= " -node $node";
-    $command   .= " -path_base $outroot";
-    $command   .= " -dbname $dbname" if defined $dbname;
-
-    # Add the processed file to the database
-    unless ($no_update) {
-        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-            run(command => $command, verbose => $verbose);
-        unless ($success) {
-            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-            &my_die("Unable to perform magictool -addresult: $error_code", $magic_id, $node, $error_code);
-        }
-    } else {
-        print "Skipping command: $command\n";
-    }
-}
 
 if ($node eq "root") {
@@ -376,4 +356,7 @@
         my $exposures = $mdcParser->parse_list(join "", @$stdout_buf);
         my $exp = $$exposures[0]; # Exposure of interest (should only be one)
+        if (!$exp) {
+            &my_die("magictool -exposure returned no output", $magic_id, $node, $PS_EXIT_UNKNOWN_ERROR);
+        }
 
         $exp_id = $exp->{exp_id};
@@ -429,9 +412,4 @@
             unless ($success) {
                 $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-                # XXX: This my_die isn't going to work because the result for the root node was already
-                # added to the database up above.
-                # There is a magicMask has a fault column. Maybe we should 
-                # use that and add a new revert mode that deletes the magicMask and the magicNodeResult
-                # for the root node.
                 &my_die("Unable to perform magictool -addmask: $error_code", $magic_id, $node, $error_code);
             }
@@ -439,4 +417,27 @@
             print "Skipping command: $command\n";
         }
+    }
+}
+
+    ### Input result into database
+{
+    my $command = "$magictool -addresult";
+    $command   .= " -magic_id $magic_id";
+    $command   .= " -node $node";
+    $command   .= " -path_base $outroot";
+    $command   .= " -dbname $dbname" if defined $dbname;
+
+    # Add the processed file to the database
+    unless ($no_update) {
+        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+            run(command => $command, verbose => $verbose);
+        unless ($success) {
+            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+            # XXX: if this is the root node we need to revert delete the magicMask object
+            # inserted above
+            &my_die("Unable to perform magictool -addresult: $error_code", $magic_id, $node, $error_code);
+        }
+    } else {
+        print "Skipping command: $command\n";
     }
 }
Index: /tags/ipp-20101029/ippTools/share/Makefile.am
===================================================================
--- /tags/ipp-20101029/ippTools/share/Makefile.am	(revision 29636)
+++ /tags/ipp-20101029/ippTools/share/Makefile.am	(revision 29637)
@@ -213,5 +213,5 @@
 	flatcorr_dropchip.sql \
 	flatcorr_dropcamera.sql \
-	magictool_addmask.sql \
+	magictool_deletemask.sql \
 	magictool_restore_camera.sql \
 	magictool_restore_chip.sql \
@@ -237,4 +237,5 @@
 	magictool_revertnode.sql \
 	magictool_exposure.sql \
+	magictool_setfull.sql \
 	magicdstool_clearstatefaults.sql \
 	magicdstool_change_file_data_state.sql \
@@ -272,4 +273,5 @@
 	minidvodbtool_find_minidvodb_id_dvo.sql \
 	minidvodbtool_find_minidvodbcopy.sql \
+	minidvodbtool_queue_minidvodb_id.sql \
 	minidvodbtool_revertminidvodbcopy.sql \
 	pstamptool_completedreq.sql \
Index: gs/ipp-20101029/ippTools/share/magictool_addmask.sql
===================================================================
--- /tags/ipp-20101029/ippTools/share/magictool_addmask.sql	(revision 29636)
+++ 	(revision )
@@ -1,6 +1,0 @@
-UPDATE
-    magicRun
-SET
-    state = 'full'
-WHERE
-    state != 'full'
Index: /tags/ipp-20101029/ippTools/share/magictool_deletemask.sql
===================================================================
--- /tags/ipp-20101029/ippTools/share/magictool_deletemask.sql	(revision 29637)
+++ /tags/ipp-20101029/ippTools/share/magictool_deletemask.sql	(revision 29637)
@@ -0,0 +1,9 @@
+DELETE magicMask
+FROM magicRun
+    JOIN magicNodeResult USING(magic_id)
+    JOIN magicMask USING(magic_id)
+WHERE
+    magicRun.state = 'new'
+    AND magicNodeResult.node = 'root'
+    AND magicNodeResult.fault != 0
+    AND magicRun.state = 'new'
Index: /tags/ipp-20101029/ippTools/share/magictool_setfull.sql
===================================================================
--- /tags/ipp-20101029/ippTools/share/magictool_setfull.sql	(revision 29637)
+++ /tags/ipp-20101029/ippTools/share/magictool_setfull.sql	(revision 29637)
@@ -0,0 +1,6 @@
+UPDATE
+    magicRun
+SET
+    state = 'full'
+WHERE
+    state != 'full'
Index: /tags/ipp-20101029/ippTools/src/magictool.c
===================================================================
--- /tags/ipp-20101029/ippTools/src/magictool.c	(revision 29636)
+++ /tags/ipp-20101029/ippTools/src/magictool.c	(revision 29637)
@@ -1059,4 +1059,9 @@
     PXOPT_LOOKUP_S16(fault, config->args, "-fault", false, false);
 
+    if (!psDBTransaction(config->dbh)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+
     if (!magicNodeResultInsert(config->dbh,
                                magic_id,
@@ -1069,4 +1074,33 @@
     }
 
+    if (fault == 0 && !strcmp(node, "root")) {
+        // Set the magicRun state
+        psString query = pxDataGet("magictool_setfull.sql");
+        if (!query) {
+            psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
+            if (!psDBRollback(config->dbh)) {
+                psError(PS_ERR_UNKNOWN, false, "database error");
+            }
+            return false;
+        }
+
+        // manually add constraint
+        psStringAppend(&query, " AND magic_id = %" PRId64, magic_id);
+
+        if (!p_psDBRunQuery(config->dbh, query)) {
+            psError(PS_ERR_UNKNOWN, false, "database error");
+            psFree(query);
+            if (!psDBRollback(config->dbh)) {
+                psError(PS_ERR_UNKNOWN, false, "database error");
+            }
+            return false;
+        }
+        psFree(query);
+    }
+    if (!psDBCommit(config->dbh)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+
     return true;
 }
@@ -1082,25 +1116,43 @@
     pxAddLabelSearchArgs (config, where, "-label", "magicRun.label", "==");
 
-    psString query = pxDataGet("magictool_revertnode.sql");
+    if (!psListLength(where->list)) {
+        psError(PS_ERR_UNKNOWN, false, "search parameters are required");
+        psFree(where);
+        return false;
+    }
+    psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
+    psFree(where);
+
+    // don't need a transaction because it is ok if the first query succeeds
+    // but the second query does not
+    psString query = pxDataGet("magictool_deletemask.sql");
     if (!query) {
         psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
         return false;
     }
-
-    if (psListLength(where->list)) {
-        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
-        psStringAppend(&query, " AND %s", whereClause);
+    psStringAppend(&query, " AND %s", whereClause);
+    if (!p_psDBRunQuery(config->dbh, query)) {
         psFree(whereClause);
-    } else {
-        psError(PS_ERR_UNKNOWN, false, "search parameters are required");
-        psFree(where);
-        return false;
-    }
-    psFree(where);
+        psFree(query);
+        psError(PS_ERR_UNKNOWN, false, "failed to delete faulted masks");
+        return false;
+    }
+    psFree(query);
+
+    query = pxDataGet("magictool_revertnode.sql");
+    if (!query) {
+        psFree(whereClause);
+        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
+        return false;
+    }
+    psStringAppend(&query, " AND %s", whereClause);
+    psFree(whereClause);
 
     if (!p_psDBRunQuery(config->dbh, query)) {
+        psFree(query);
         psError(PS_ERR_UNKNOWN, false, "failed to revert");
         return false;
     }
+    psFree(query);
 
     psS32 numUpdated = psDBAffectedRows(config->dbh);
@@ -1180,17 +1232,9 @@
     // required
     PXOPT_LOOKUP_S64(magic_id, config->args, "-magic_id", true, false);
+    PXOPT_LOOKUP_STR(path_base, config->args, "-path_base", true, false);
 
     // optional
-//    PXOPT_LOOKUP_STR(uri, config->args, "-uri", false, false);
-    PXOPT_LOOKUP_STR(path_base, config->args, "-path_base", true, false);
     PXOPT_LOOKUP_S32(streaks, config->args, "-streaks", false, false);
-
-    // default values
     PXOPT_LOOKUP_S16(fault, config->args, "-fault", false, false);
-
-    if (!psDBTransaction(config->dbh)) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        return false;
-    }
 
     if (!magicMaskInsert(config->dbh,
@@ -1205,32 +1249,4 @@
             psError(PS_ERR_UNKNOWN, false, "database error");
         }
-        return false;
-    }
-
-    // Set the magicRun state
-    psString query = pxDataGet("magictool_addmask.sql");
-    if (!query) {
-        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
-        if (!psDBRollback(config->dbh)) {
-            psError(PS_ERR_UNKNOWN, false, "database error");
-        }
-        return false;
-    }
-
-    // manually add constraint
-    psStringAppend(&query, " AND magic_id = %" PRId64, magic_id);
-
-    if (!p_psDBRunQuery(config->dbh, query)) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        psFree(query);
-        if (!psDBRollback(config->dbh)) {
-            psError(PS_ERR_UNKNOWN, false, "database error");
-        }
-        return false;
-    }
-    psFree(query);
-
-    if (!psDBCommit(config->dbh)) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
         return false;
     }
