fix insert to download_file_index 2

Change-Id: I2eeda060d8726d41fb1ddf1e4aaf20190263037b
Signed-off-by: Christopher Guindon <chris.guindon@eclipse-foundation.org>
diff --git a/download.php b/download.php
index a13633e..b51b2e7 100755
--- a/download.php
+++ b/download.php
@@ -292,7 +292,7 @@
     # using left joins for the download_file_index took .55 sec, two queries uses < .15 sec
 
     # q1: get file info from slave
-    $sql = "SELECT /* downnload.php lowest_drop */ IDX.file_id, IDX.timestamp_disk, IF(IDX.md5sum = '0', '', IDX.md5sum) AS md5sum, IF(IDX.sha1sum = '0', '', IDX.sha1sum) AS sha1sum, IF(IDX.sha512sum = '0', '', IDX.sha512sum) AS sha512sum FROM download_file_index AS IDX WHERE IDX.file_name = '$_file' ORDER BY file_id asc LIMIT 1";
+    $sql = "SELECT /* downnload.php lowest_drop */ file_id, timestamp_disk, IF(md5sum = '0', '', md5sum) AS md5sum, IF(sha1sum = '0', '', sha1sum) AS sha1sum, IF(sha512sum = '0', '', sha512sum) AS sha512sum FROM download_file_index WHERE file_name = '$_file' ORDER BY file_id asc LIMIT 1";
     if($_debug == 1) {
         echo $sql . "<br />";
     }
@@ -312,7 +312,7 @@
         echo "This is a new file.  Checking the Master DB.<br />";
       }
       # Lock tables for write
-      $rs = mysqli_query($dbh_RW, "LOCK TABLES download_file_index AS IDX WRITE, downloads READ, mirrors READ, mirror_protocols READ, mirror_drops READ, drops READ, SYS_countries READ, SYS_continents READ, mirror_trends READ, SYS_variables READ");
+      $rs = mysqli_query($dbh_RW, "LOCK TABLES download_file_index WRITE");
       $rs = mysqli_query($dbh_RW, $sql);
       if($myrow = mysqli_fetch_assoc($rs)) {
         $file_id   = $myrow['file_id'];
@@ -320,7 +320,6 @@
         $md5sum    = $myrow['md5sum'];
         $sha1sum  = $myrow['sha1sum'];
         $sha512sum  = $myrow['sha512sum'];
-        mysqli_query($dbh_RW, "UNLOCK TABLES");
       }
     }
 
@@ -335,38 +334,15 @@
       }
 
       if(!$filetime || $filetime == 0) {
-        # unable to open file
-        $dbc_RW->disconnect();
-
         # check on archive.eclipse.org
         $filename = $archiveBasePath . $_file;
         if(@fopen($filename, "r")) {
           $filetime = @filemtime($filename);
           $filesize = @filesize($filename);
-        }
-
-        if(!$filetime || $filetime == 0) {
-          # File isn't on download nor on archive.
-          $error = 1;
-          if($_format == "xml") {
-            $app->sendXMLHeader();
-            include("content/en_mir_list_xml.php");
-          }
-          else {
-            if($_send_404) {
-              header("HTTP/1.0 404 Not Found");
-              echo "<html><head><title>404 Not Found</title></head><body><h1>404 Not Found</h1></body></html>";
-            }
-            else {
-              include("content/en_mir_download_invalid_file.php");
-            }
-          }
-          return false;
-        }
-        else {
           # we finally have a filetime after examining disk.  Add it to the index if we're logging downloads.
           if($log_download) {
             $file_id = &addFileToIndex($_file);
+            $dbc_RW->disconnect();
           }
           # File exists on archive.eclipse.org.  No mirrors, or redirect to the actual file.
           if($_format == "xml") {
@@ -382,8 +358,30 @@
             exit;
           }
         }
+        else {
+          # File isn't on download nor on archive.
+          mysqli_query($dbh_RW, "UNLOCK TABLES");
+
+          $error = 1;
+          if($_format == "xml") {
+            $app->sendXMLHeader();
+            include("content/en_mir_list_xml.php");
+          }
+          else {
+            if($_send_404) {
+              header("HTTP/1.0 404 Not Found");
+              echo "<html><head><title>404 Not Found</title></head><body><h1>404 Not Found</h1></body></html>";
+            }
+            else {
+              include("content/en_mir_download_invalid_file.php");
+            }
+          }
+          return FALSE;
+        }
       }
       else {
+        # We found a new file, let's add it to download_file_index.
+        $file_id = addFileToIndex($_file);
         $filetime_update = true;
       }
     }
@@ -503,7 +501,6 @@
     echo "Pre phase-II<br /> URL: $_url <br />File: $_file:<br />Mirror: $_mirror_id";
   }
 
-
   # Phase 2 - a user picked a mirror for his file, or redirect, or there are no mirrors
   if(($_file != "" && $_mirror_id > 0 && $_url != "" && $_format != "xml") || $_debug) {
     $ip     = $remote_addr;
@@ -535,13 +532,7 @@
         }
       }
 
-
-      if ($log_download) {
-        # We couldn't find a file_id up there, so insert the actual file here
-        if($file_id == "") {
-          $file_id = &addFileToIndex($_file);
-        }
-
+      if ($log_download && !empty($file_id)) {
         $sql = "INSERT DELAYED INTO $log_download_table (file_id, download_date, remote_host, remote_addr, mirror_id, ccode)
           VALUES ( $file_id, NOW(), '$client_hostname', '$ip', $_mirror_id, '$_country_code')  ";
         mysqli_query($dbh_RW, $sql);