]> cygwin.com Git - cygwin-apps/setup.git/blobdiff - download.cc
Also run stratum 'z' perpetual preremove scripts
[cygwin-apps/setup.git] / download.cc
index 8e2e6e9ad8b6b217462e315aacd8caa0d3d6d7f3..5bfcebf0957d26f122faa07324230946174d6c93 100644 (file)
@@ -25,6 +25,7 @@
 #include <stdio.h>
 #include <unistd.h>
 #include <process.h>
+#include <vector>
 
 #include "resource.h"
 #include "msg.h"
 
 #include "package_db.h"
 #include "package_meta.h"
-#include "package_version.h"
 #include "package_source.h"
 
 #include "threebar.h"
 
 #include "Exception.h"
 
-#include "getopt++/BoolOption.h"
-
-using namespace std;
-
 extern ThreeBarProgressPage Progress;
 
-BoolOption IncludeSource (false, 'I', "include-source", "Automatically include source download");
-
+// Return true if selected checks pass, false if they don't and the
+// user chooses to delete the file; otherwise throw an exception.
 static bool
-validateCachedPackage (const std::string& fullname, packagesource & pkgsource)
+validateCachedPackage (const std::string& fullname, packagesource & pkgsource,
+                      HWND owner, bool check_hash, bool check_size)
 {
-  DWORD size = get_file_size(fullname);
-  if (size != pkgsource.size)
-  {
-    Log (LOG_BABBLE) << "INVALID PACKAGE: " << fullname
-      << " - Size mismatch: Ini-file: " << pkgsource.size
-      << " != On-disk: " << size << endLog;
-    return false;
-  }
-  return true;
+  try
+    {
+      if (check_size)
+       pkgsource.check_size_and_cache (fullname);
+      if (check_hash)
+       pkgsource.check_hash ();
+      return true;
+    }
+  catch (Exception *e)
+    {
+      pkgsource.set_cached ("");
+      const char *filename = fullname.c_str ();
+      if (strncmp (filename, "file://", 7) == 0)
+       filename += 7;
+      if (e->errNo() == APPERR_CORRUPT_PACKAGE
+         && yesno (owner, IDS_QUERY_CORRUPT, filename) == IDYES)
+       remove (filename);
+      else
+       throw e;
+    }
+  return false;
 }
 
-/* 0 on failure
+/* 0 if not cached; may throw exception if validation fails.
  */
 int
-check_for_cached (packagesource & pkgsource, bool mirror_mode)
+check_for_cached (packagesource & pkgsource, HWND owner, bool mirror_mode,
+                 bool check_hash)
 {
-  // Already found one.
-  if (pkgsource.Cached())
-    return 1;
+  /* If the packagesource doesn't have a filename, it can't possibly be in the
+     cache */
+  if (!pkgsource.Canonical())
+    {
+      return 0;
+    }
 
   /* Note that the cache dir is represented by a mirror site of file://local_dir */
   std::string prefix = "file://" + local_dir + "/";
-  std::string fullname = prefix + (pkgsource.Canonical() ? pkgsource.Canonical() : "");
+  std::string fullname = prefix + pkgsource.Canonical();
 
   if (mirror_mode)
     {
       /* Just assume correctness of mirror. */
-      pkgsource.set_cached (fullname);
+      if (!pkgsource.Cached())
+       pkgsource.set_cached (fullname);
       return 1;
     }
 
+  // Already found one, which we can assume to have the right size.
+  if (pkgsource.Cached())
+    {
+      if (validateCachedPackage (pkgsource.Cached(), pkgsource, owner,
+                                check_hash, false))
+       return 1;
+      // If we get here, pkgsource.Cached() was corrupt and deleted.
+      pkgsource.set_cached ("");
+    }
+
   /*
      1) is there a legacy version in the cache dir available.
   */
   if (io_stream::exists (fullname))
     {
-      if (validateCachedPackage (fullname, pkgsource))
-        pkgsource.set_cached (fullname);
-      else
-        throw new Exception (TOSTRING(__LINE__) " " __FILE__,
-            "Package validation failure for " + fullname,
-            APPERR_CORRUPT_PACKAGE);
-      return 1;
+      if (validateCachedPackage (fullname, pkgsource, owner, check_hash, true))
+       return 1;
+      // If we get here, fullname was corrupt and deleted, but it
+      // might have been cached.
+      pkgsource.set_cached ("");
     }
 
   /*
@@ -110,15 +132,14 @@ check_for_cached (packagesource & pkgsource, bool mirror_mode)
     std::string fullname = prefix + rfc1738_escape_part (n->key) + "/" +
       pkgsource.Canonical ();
     if (io_stream::exists(fullname))
-    {
-      if (validateCachedPackage (fullname, pkgsource))
-        pkgsource.set_cached (fullname);
-      else
-        throw new Exception (TOSTRING(__LINE__) " " __FILE__,
-            "Package validation failure for " + fullname,
-            APPERR_CORRUPT_PACKAGE);
-      return 1;
-    }
+       {
+         if (validateCachedPackage (fullname, pkgsource, owner, check_hash,
+                                    true))
+           return 1;
+         // If we get here, fullname was corrupt and deleted, but it
+         // might have been cached.
+         pkgsource.set_cached ("");
+       }
   }
   return 0;
 }
@@ -129,7 +150,7 @@ download_one (packagesource & pkgsource, HWND owner)
 {
   try
     {
-      if (check_for_cached (pkgsource))
+      if (check_for_cached (pkgsource, owner))
         return 0;
     }
   catch (Exception * e)
@@ -162,101 +183,146 @@ download_one (packagesource & pkgsource, HWND owner)
        }
       else
        {
-         size_t size = get_file_size ("file://" + local + ".tmp");
-         if (size == pkgsource.size)
+         try
            {
-             Log (LOG_PLAIN) << "Downloaded " << local << endLog;
              if (_access (local.c_str(), 0) == 0)
                remove (local.c_str());
              rename ((local + ".tmp").c_str(), local.c_str());
+             pkgsource.check_size_and_cache ("file://" + local);
+             pkgsource.check_hash ();
+             Log (LOG_PLAIN) << "Downloaded " << local << endLog;
              success = 1;
-             pkgsource.set_cached ("file://" + local);
              // FIXME: move the downloaded file to the 
              //  original locations - without the mirror site dir in the way
              continue;
            }
-         else
+         catch (Exception *e)
            {
-             Log (LOG_PLAIN) << "Download " << local << " wrong size (" <<
-               size << " actual vs " << pkgsource.size << " expected)" << 
-               endLog;
-             remove ((local + ".tmp").c_str());
-             continue;
+             remove (local.c_str());
+             pkgsource.set_cached ("");
+             if (e->errNo() == APPERR_CORRUPT_PACKAGE)
+               {
+                 Log (LOG_PLAIN) << "Downloaded file " << local
+                                 << " is corrupt; deleting." << endLog;
+                 continue;
+               }
+             else
+               {
+                 Log (LOG_PLAIN) << "Unexpected exception while validating "
+                                 << "downloaded file " << local
+                                 << "; deleting." << endLog;
+                 throw e;
+               }
            }
        }
     }
   if (success)
     return 0;
-  /* FIXME: Do we want to note this? if so how? */
   return 1;
 }
 
+static std::vector <packageversion> download_failures;
+static std::string download_warn_pkgs;
+
+static INT_PTR CALLBACK
+download_error_proc (HWND h, UINT message, WPARAM wParam, LPARAM lParam)
+{
+  switch (message)
+    {
+    case WM_INITDIALOG:
+      eset (h, IDC_DOWNLOAD_EDIT, download_warn_pkgs);
+      SetFocus (GetDlgItem(h, IDRETRY));
+      return FALSE;
+
+    case WM_COMMAND:
+      switch (LOWORD (wParam))
+       {
+       case IDRETRY:
+       case IDC_BACK:
+       case IDIGNORE:
+       case IDABORT:
+         EndDialog (h, LOWORD (wParam));
+       default:
+         // Not reached.
+         return 0;
+       }
+
+    default:
+      // Not handled.
+      return FALSE;
+    }
+  return TRUE;
+}
+
+static int
+query_download_errors (HINSTANCE h, HWND owner)
+{
+  download_warn_pkgs = "";
+  Log (LOG_PLAIN) << "The following package(s) had download errors:" << endLog;
+  for (std::vector <packageversion>::const_iterator i = download_failures.begin (); i != download_failures.end (); i++)
+    {
+      packageversion pv = *i;
+      std::string pvs = pv.Name () + "-" + pv.Canonical_version ();
+      Log (LOG_PLAIN) << "  " << pvs << endLog;
+      download_warn_pkgs += pvs + "\r\n";
+    }
+  return DialogBox (h, MAKEINTRESOURCE (IDD_DOWNLOAD_ERROR), owner,
+                   download_error_proc);
+}
+
 static int
 do_download_thread (HINSTANCE h, HWND owner)
 {
   int errors = 0;
   total_download_bytes = 0;
   total_download_bytes_sofar = 0;
+  download_failures.clear ();
 
-  Progress.SetText1 ("Checking for packages to download...");
+  Progress.SetText1 (IDS_PROGRESS_CHECKING);
   Progress.SetText2 ("");
   Progress.SetText3 ("");
 
   packagedb db;
-  /* calculate the amount needed */
-  for (packagedb::packagecollection::iterator i = db.packages.begin ();
-       i != db.packages.end (); ++i)
+  const SolverTransactionList &t = db.solution.transactions();
+
+  /* calculate the total size of the download */
+  for (SolverTransactionList::const_iterator i = t.begin (); i != t.end (); ++i)
     {
-      packagemeta & pkg = *(i->second);
-      if (pkg.desired.picked () || pkg.desired.sourcePackage ().picked ())
-       {
-         packageversion version = pkg.desired;
-         packageversion sourceversion = version.sourcePackage();
-         try 
-           {
-             if (version.picked())
-               {
-                   if (!check_for_cached (*version.source()))
-                     total_download_bytes += version.source()->size;
-               }
-             if (sourceversion.picked () || IncludeSource)
-               {
-                   if (!check_for_cached (*sourceversion.source()))
-                     total_download_bytes += sourceversion.source()->size;
-               }
-           }
-         catch (Exception * e)
-           {
-             // We know what to do with these..
-             if (e->errNo() == APPERR_CORRUPT_PACKAGE)
-               fatal (owner, IDS_CORRUPT_PACKAGE, pkg.name.c_str());
-             // Unexpected exception.
-             throw e;
-           }
-       }
+      if (i->type != SolverTransaction::transInstall)
+        continue;
+      packageversion version = i->version;
+
+      try
+        {
+          if (!check_for_cached (*version.source(), owner))
+            total_download_bytes += version.source()->size;
+        }
+      catch (Exception * e)
+        {
+          // We know what to do with these..
+          if (e->errNo() == APPERR_CORRUPT_PACKAGE)
+            fatal (owner, IDS_CORRUPT_PACKAGE, version.Name().c_str());
+          // Unexpected exception.
+          throw e;
+        }
+      Progress.SetBar2(std::distance(t.begin(), i) + 1, t.size());
     }
 
   /* and do the download. FIXME: This here we assign a new name for the cached version
    * and check that above.
    */
-  for (packagedb::packagecollection::iterator i = db.packages.begin ();
-       i != db.packages.end (); ++i)
+  for (SolverTransactionList::const_iterator i = t.begin (); i != t.end (); ++i)
     {
-      packagemeta & pkg = *(i->second);
-      if (pkg.desired.picked () || pkg.desired.sourcePackage ().picked ())
+      if (i->type != SolverTransaction::transInstall)
+        continue;
+      packageversion version = i->version;
+
        {
          int e = 0;
-         packageversion version = pkg.desired;
-         packageversion sourceversion = version.sourcePackage();
-         if (version.picked())
-           {
-               e += download_one (*version.source(), owner);
-           }
-         if (sourceversion && (sourceversion.picked() || IncludeSource))
-           {
-               e += download_one (*sourceversion.source (), owner);
-           }
+          e += download_one (*version.source(), owner);
          errors += e;
+         if (e)
+           download_failures.push_back (version);
 #if 0
          if (e)
            pkg->action = ACTION_ERROR;
@@ -268,27 +334,41 @@ do_download_thread (HINSTANCE h, HWND owner)
     {
       // In unattended mode we retry the download, but not forever.
       static int retries = 5;
+      int rc;
       if (unattended_mode && --retries <= 0)
         {
          Log (LOG_PLAIN) << "download error in unattended_mode: out of retries" << endLog;
-         Logger ().setExitMsg (IDS_INSTALL_INCOMPLETE);
-         Logger ().exit (1);
+         rc = IDABORT;
        }
       else if (unattended_mode)
         {
          Log (LOG_PLAIN) << "download error in unattended_mode: " << retries
            << (retries > 1 ? " retries" : " retry") << " remaining." << endLog;
+         rc = IDRETRY;
+       }
+      else
+       rc = query_download_errors (h, owner);
+      switch (rc)
+       {
+       case IDRETRY:
          Progress.SetActivateTask (WM_APP_START_DOWNLOAD);
          return IDD_INSTATUS;
+       case IDC_BACK:
+         return IDD_CHOOSE;
+       case IDABORT:
+         Logger ().setExitMsg (IDS_DOWNLOAD_INCOMPLETE_EXIT);
+         Logger ().exit (1);
+       case IDIGNORE:
+         break;
+       default:
+         break;
        }
-      else if (yesno (owner, IDS_DOWNLOAD_INCOMPLETE) == IDYES)
-       return IDD_SITE;
     }
 
   if (source == IDC_SOURCE_DOWNLOAD)
     {
       if (errors)
-       Logger ().setExitMsg (IDS_DOWNLOAD_INCOMPLETE);
+       Logger ().setExitMsg (IDS_DOWNLOAD_INCOMPLETE_EXIT);
       else if (!unattended_mode)
        Logger ().setExitMsg (IDS_DOWNLOAD_COMPLETE);
       return IDD_DESKTOP;
@@ -303,6 +383,8 @@ do_download_reflector (void *p)
   HANDLE *context;
   context = (HANDLE *) p;
 
+  SetThreadUILanguage(langid);
+
   try
   {
     int next_dialog =
@@ -311,7 +393,7 @@ do_download_reflector (void *p)
     // Tell the progress page that we're done downloading
     Progress.PostMessageNow (WM_APP_DOWNLOAD_THREAD_COMPLETE, 0, next_dialog);
   }
-  TOPLEVEL_CATCH("download");
+  TOPLEVEL_CATCH((HWND) context[1], "download");
 
   ExitThread(0);
 }
This page took 0.032579 seconds and 5 git commands to generate.