Index: /trunk/DataStoreServer/scripts/dsreg
===================================================================
--- /trunk/DataStoreServer/scripts/dsreg	(revision 16871)
+++ /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);
+    }
+}
Index: /trunk/DataStoreServer/scripts/tabledefs.sql
===================================================================
--- /trunk/DataStoreServer/scripts/tabledefs.sql	(revision 16871)
+++ /trunk/DataStoreServer/scripts/tabledefs.sql	(revision 16872)
@@ -5,7 +5,5 @@
 
 #
-# TODO: arguably we should put prod_col_n in another table, but since the
-# number of products will be small, it is simpler to just keep them here
-#
+# 
 
 CREATE TABLE dsProduct (
@@ -16,7 +14,6 @@
     type        VARCHAR(64),
     description VARCHAR(255),
-    path_name   VARCHAR(255),
-    prod_col_0  VARCHAR(64),        # names of product specific columns
-    prod_col_1  VARCHAR(64),        # shows up in the header list
+    prod_col_0  VARCHAR(64),        # labels for product specific columns
+    prod_col_1  VARCHAR(64),        # these appear in the header list
     prod_col_2  VARCHAR(64),
     prod_col_3  VARCHAR(64),
@@ -31,5 +28,5 @@
     prod_id     BIGINT(20),
     fileset_id  BIGINT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
-    fileset_name VARCHAR(64) NOT NULL UNIQUE,
+    fileset_name VARCHAR(64) NOT NULL,
     reg_time    datetime NOT NULL,
     type        VARCHAR(64),
@@ -65,7 +62,5 @@
 
 #
-# names of the type specific columns for a data store file type
-# 
-# if not null, the cols will show up in the indexes
+# labels for the type specific columns for each Data Store file type
 #
 
@@ -78,35 +73,9 @@
 ) Engine=InnoDB DEFAULT CHARSET=latin1;
 
-# type specific columns for type chip
+# type specific column for type chip
 INSERT INTO dsFileType (type, type_col_0) VALUES('chip', 'chipname');
+
+# none of these types have any type specific columns
 INSERT INTO dsFileType (type) VALUES('psrequest');
 INSERT INTO dsFileType (type) VALUES('psresults');
 INSERT INTO dsFileType (type) VALUES('pstamp');
-
-INSERT INTO dsProduct (prod_id, prod_name, last_update, last_fs, type, description, path_name)
-        VALUES (0, 'pstamprequest', UTC_TIMESTAMP(), NULL, 'psrequest', 'Postage Stamp Request',
-        '/var/www/html/ds/dsroot');
-
-INSERT INTO dsProduct (prod_id, prod_name, last_update, last_fs, type, description, path_name)
-        VALUES (0, 'sample', UTC_TIMESTAMP(), NULL, 'psresult', 'Postage Stamp Request', '/var/www/html/ds/dsroot');
-
-#
-# the width of the product specific column header strings should match the usual width of the columns
-# to make the fileset lists look neat (note 'telescope pointing') 
-#
-
-INSERT INTO dsProduct VALUES (0, 'gpc1', UTC_TIMESTAMP(), NULL, 'image',
-        'Gigapixel Camera 1', '/var/www/html/ds/dsroot', 'telescope pointing        ', 'etime', 'f',
-        'airm', 'comments', NULL, NULL, NULL);
-
-INSERT INTO dsFileset VALUES(1, 0, 'fileset1', UTC_TIMESTAMP(), 'PSTAMP');
-INSERT INTO dsFileset VALUES(1, 0, 'fileset2', UTC_TIMESTAMP(), 'PSTAMP');
-INSERT INTO dsFileset VALUES(1, 0, 'fileset13', UTC_TIMESTAMP(), 'PSTAMP');
-
-INSERT INTO dsFile VALUES(LAST_INSERT_ID(), 0, 'fileset13.fits', 8640, 'b8217dcb393de3af02d915d77025021d',
-    'psrequest');
-
-    
-
-
-
