Index: trunk/DataStoreServer/scripts/dsreg
===================================================================
--- trunk/DataStoreServer/scripts/dsreg	(revision 16867)
+++ trunk/DataStoreServer/scripts/dsreg	(revision 16872)
@@ -161,5 +161,5 @@
 
     if (!$fs_row) {
-        print "Fileset '$fileset' not found under $product.\n";
+        print STDERR "Fileset '$fileset' not found under $product.\n";
         exit 1;
     }
@@ -201,6 +201,4 @@
     #
 
-    # make a string out of the product specifc column values provided
-    my $prodcolstr = make_prodcol_str($ps0, $ps1, $ps2, $ps3, $ps4, $ps5, $ps6, $ps7);
 
     my $stmt = $dbh->prepare("SELECT prod_id FROM dsProduct WHERE prod_name = \'$product\'");
@@ -220,5 +218,9 @@
     }
 
+    # Note: There is a race condition here. Somebody could sneak in now and add a fileset with
+    # fileset_name = $fileset. So later we'll check again that only one fileset with the name exists
+
     # Read file data from STDIN
+    # XXX: Perhaps allow this to come from a file
 
     my $lineno = 0;
@@ -318,4 +320,5 @@
     # create the cgi script index.txt by making a copy of its parent's
     if (!copy("$ds_dir/$product/index.txt", $index_script_name)) {
+        cleanup();
         die("failed to copy index script to file set");
     }
@@ -326,10 +329,13 @@
         $dbh->{AutoCommit} = 0;
 
+        # note the resulting prodcolstr begins with a comma
+        my $prodcolstr = make_prodcol_str($ps0, $ps1, $ps2, $ps3, $ps4, $ps5, $ps6, $ps7);
+
         my $qstring = "INSERT into dsFileset" .
                 " (prod_id, fileset_name, reg_time, type," .
                 " prod_col_0, prod_col_1, prod_col_2, prod_col_3, prod_col_4, prod_col_5, " .
                 " prod_col_6, prod_col_7)" .
-                # note prodcolstr begins with a comma
                 " VALUES($prod_id, \'$fileset\', UTC_TIMESTAMP(), \'$fstype\' $prodcolstr)";
+
         $count = $dbh->do($qstring);
         if ($count == 0E0) {
@@ -339,5 +345,10 @@
         my $fileset_id = $dbh->last_insert_id(undef, undef, undef, undef);
 
-        # print STDERR "Inserted fileset_id $fileset_id\n";
+        # make sure that no-one got in and created a fileset with this name while we were busy
+        $count = $dbh->do("SELECT fileset_id FROM dsFileset WHERE fileset_name = '$fileset'" .
+                        " AND prod_id = $prod_id");
+        if ($count > 1) {
+            die("Fileset '$fileset' already exists under $product.");
+        }
 
         foreach my $ref (@files) {
@@ -371,9 +382,7 @@
         print STDERR "transactionfailed, rolling back error was:\n$@\n";
         eval {$dbh->rollback();};
-        unlink($index_script_name);
+        cleanup();
         exit 1;
     }
-
-    # print "Added $fileset to $product.\n";
 
     exit 0;
@@ -431,2 +440,13 @@
     return $string;
 }
+
+sub cleanup {
+
+    if ($linkfiles or $copyfiles) {
+        if (system "rm -r $fileset_dir") {
+            print STDERR "failed to remove $fileset_dir";
+        }
+    } else {
+        unlink($index_script_name);
+    }
+}
