Index: trunk/ippTools/src/pzgetimfiles.c
===================================================================
--- trunk/ippTools/src/pzgetimfiles.c	(revision 10021)
+++ trunk/ippTools/src/pzgetimfiles.c	(revision 10024)
@@ -139,6 +139,14 @@
     long imfiles = psArrayLength(newImfiles);
 
+    // start a transaction so it's all rows or nothing
+    if (!psDBTransaction(config->dbh)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+
     // if the fileset was empty (no files) then we can bail out early
     if (imfiles == 0) {
+        psFree(newImfiles);
+
         char *query = 
             "UPDATE summitExp"
@@ -148,16 +156,38 @@
             " AND telescope = '%s'";
         if (!p_psDBRunQuery(config->dbh, query, imfiles, filesetid, camera, telescope)) {
-            psError(PS_ERR_UNKNOWN, false, "database error");
-            return false;
-        }
-        psFree(newImfiles);
+            // rollback
+            if (!psDBRollback(config->dbh)) {
+                psError(PS_ERR_UNKNOWN, false, "database error");
+            }
+            psError(PS_ERR_UNKNOWN, false, "database error");
+            return false;
+        }
+
+        // remove the pzPendingExp entry for this exp (fileset)
+        {
+            char *query = 
+                "DELETE FROM pzPendingExp"
+                " WHERE"
+                "   exp_id = '%s'"
+                "   AND camera = '%s'"
+                "   AND telescope = '%s'";
+            if (!p_psDBRunQuery(config->dbh, query, filesetid, camera, telescope)) {
+                // rollback
+                if (!psDBRollback(config->dbh)) {
+                    psError(PS_ERR_UNKNOWN, false, "database error");
+                }
+                psError(PS_ERR_UNKNOWN, false, "database error");
+                return false;
+            }
+        }
+
+        // point of no return
+        if (!psDBCommit(config->dbh)) {
+            psError(PS_ERR_UNKNOWN, false, "database error");
+            return false;
+        }
+
         return true;
     } 
-
-    // start a transaction so it's all rows or nothing
-    if (!psDBTransaction(config->dbh)) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        return false;
-    }
 
     // try not to insert duplicate pzPendingExp/pzPendingImfile entries
@@ -179,4 +209,5 @@
     }
 
+    // load the imfiles (files) into the temp table
     {
         char *query = "INSERT INTO incoming (exp_id, camera, telescope, bytes, md5sum, class, class_id, uri) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
@@ -206,4 +237,5 @@
     psFree(newImfiles);
 
+    // queue the imfiles (files) in pzPendingImfile
     {
         char *query = 
@@ -238,5 +270,5 @@
     }
 
-
+    // move imfiles (files) from the temp table into summitImfiles
     {
         char *query = 
@@ -271,22 +303,47 @@
     }
 
-    // update summitExp.imfiles
+    // update summitExp.imfiles (should have been NULL)
     {
         char *query = 
             "UPDATE summitExp"
             " SET imfiles = (SELECT COUNT(*) FROM summitImfile"
-            "   WHERE exp_id = '%s'"
+            "   WHERE"
+            "       exp_id = '%s'"
+            "       AND camera = '%s'"
+            "       AND telescope = '%s'"
+            ")" 
+            " WHERE"
+            "   exp_id = '%s'"
             "   AND camera = '%s'"
-            "   AND telescope = '%s'"
-            ")" 
-            " WHERE exp_id = '%s'"
-            " AND camera = '%s'"
-            " AND telescope = '%s'";
+            "   AND telescope = '%s'";
         if (!p_psDBRunQuery(config->dbh, query, filesetid, camera, telescope, filesetid, camera, telescope)) {
-            psError(PS_ERR_UNKNOWN, false, "database error");
-            return false;
-        }
-    }
-
+            // rollback
+            if (!psDBRollback(config->dbh)) {
+                psError(PS_ERR_UNKNOWN, false, "database error");
+            }
+            psError(PS_ERR_UNKNOWN, false, "database error");
+            return false;
+        }
+    }
+
+    // remove the pzPendingExp entry for this exp (fileset)
+    {
+        char *query = 
+            "DELETE FROM pzPendingExp"
+            " WHERE"
+            "   exp_id = '%s'"
+            "   AND camera = '%s'"
+            "   AND telescope = '%s'";
+        if (!p_psDBRunQuery(config->dbh, query, filesetid, camera, telescope)) {
+            // rollback
+            if (!psDBRollback(config->dbh)) {
+                psError(PS_ERR_UNKNOWN, false, "database error");
+            }
+            psError(PS_ERR_UNKNOWN, false, "database error");
+            return false;
+        }
+    }
+
+    // point of no return
     if (!psDBCommit(config->dbh)) {
         psError(PS_ERR_UNKNOWN, false, "database error");
