]> cygwin.com Git - cygwin-apps/setup.git/blobdiff - download.cc
Add an option to set the GUI language
[cygwin-apps/setup.git] / download.cc
index 0d839a62ef7e626e7945f4279edb6e89492ee841..fd1253b98f3cbc996826059aae38274186f5187e 100644 (file)
 /* The purpose of this file is to download all the files we need to
    do the installation. */
 
-#if 0
-static const char *cvsid =
-  "\n%%% $Id$\n";
-#endif
-
 #include "csu_util/rfc1738.h"
 
 #include "download.h"
@@ -30,96 +25,121 @@ static const char *cvsid =
 #include <stdio.h>
 #include <unistd.h>
 #include <process.h>
+#include <vector>
 
 #include "resource.h"
 #include "msg.h"
 #include "dialog.h"
 #include "geturl.h"
 #include "state.h"
-#include "LogSingleton.h"
+#include "LogFile.h"
 #include "filemanip.h"
 
 #include "io_stream.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"
-#include "csu_util/MD5Sum.h"
-
-using namespace std;
-
 extern ThreeBarProgressPage Progress;
 
+// 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)
+check_for_cached (packagesource & pkgsource, HWND owner, bool mirror_mode,
+                 bool check_hash)
 {
-  /* search algo:
-     1) is there a legacy version in the cache dir available.
-     (Note that the cache dir is represented by a mirror site of
-     file://local_dir
-   */
+  /* If the packagesource doesn't have a filename, it can't possibly be in the
+     cache */
+  if (!pkgsource.Canonical())
+    {
+      return 0;
+    }
 
-  // Already found one.
-  if (pkgsource.Cached())
-    return 1;
-  
+  /* Note that the cache dir is represented by a mirror site of file://local_dir */
   std::string prefix = "file://" + local_dir + "/";
-  /* FIXME: Nullness check can go away once packagesource is properly
-   * std::string-ified, and doesn't use overcomplex semantics. */
-  std::string fullname = prefix + 
-    (pkgsource.Canonical() ? 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;
-  }
+  std::string fullname = prefix + pkgsource.Canonical();
+
+  if (mirror_mode)
+    {
+      /* Just assume correctness of mirror. */
+      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, owner, check_hash, true))
+       return 1;
+      // If we get here, fullname was corrupt and deleted, but it
+      // might have been cached.
+      pkgsource.set_cached ("");
+    }
 
   /*
      2) is there a version from one of the selected mirror sites available ?
-     */
+  */
   for (packagesource::sitestype::const_iterator n = pkgsource.sites.begin();
        n != pkgsource.sites.end(); ++n)
   {
     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;
 }
@@ -130,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)
@@ -155,7 +175,7 @@ download_one (packagesource & pkgsource, HWND owner)
                                  pkgsource.Canonical ();
       io_stream::mkpath_p (PATH_TO_FILE, "file://" + local, 0);
 
-      if (get_url_to_file(n->key +  "/" + pkgsource.Canonical (),
+      if (get_url_to_file(n->key + pkgsource.Canonical (),
                          local + ".tmp", pkgsource.size, owner))
        {
          /* FIXME: note new source ? */
@@ -163,109 +183,145 @@ 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 (IDS_PROGRESS_CHECKING);
+  Progress.SetText2 ("");
+  Progress.SetText3 ("");
 
   packagedb db;
-  /* calculate the amount needed */
-  for (vector <packagemeta *>::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;
-      if (pkg.desired.picked () || pkg.desired.sourcePackage ().picked ())
-       {
-         packageversion version = pkg.desired;
-         packageversion sourceversion = version.sourcePackage();
-         try 
-           {
-             if (version.picked())
-               {
-                 for (vector<packagesource>::iterator i = 
-                      version.sources ()->begin(); 
-                      i != version.sources ()->end(); ++i)
-                   if (!check_for_cached (*i))
-                     total_download_bytes += i->size;
-               }
-             if (sourceversion.picked ())
-               {
-                 for (vector<packagesource>::iterator i =
-                      sourceversion.sources ()->begin();
-                      i != sourceversion.sources ()->end(); ++i)
-                   if (!check_for_cached (*i))
-                     total_download_bytes += i->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;
+        }
     }
 
   /* and do the download. FIXME: This here we assign a new name for the cached version
    * and check that above.
    */
-  for (vector <packagemeta *>::iterator i = db.packages.begin ();
-       i != db.packages.end (); ++i)
+  for (SolverTransactionList::const_iterator i = t.begin (); i != t.end (); ++i)
     {
-      packagemeta & pkg = **i;
-      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())
-           {
-             for (vector<packagesource>::iterator i =
-                  version.sources ()->begin();
-                  i != version.sources ()->end(); ++i)
-               e += download_one (*i, owner);
-           }
-         if (sourceversion && sourceversion.picked())
-           {
-             for (vector<packagesource>::iterator i =
-                  sourceversion.sources ()->begin();
-                  i != sourceversion.sources ()->end(); ++i)
-               e += download_one (*i, owner);
-           }
+          e += download_one (*version.source(), owner);
          errors += e;
+         if (e)
+           download_failures.push_back (version);
 #if 0
          if (e)
            pkg->action = ACTION_ERROR;
@@ -275,34 +331,45 @@ do_download_thread (HINSTANCE h, HWND owner)
 
   if (errors)
     {
-      /* In unattended mode, all dialog boxes automatically get
-         answered with a Yes/OK/other positive response.  This
-        means that if there's a download problem, setup will
-        potentially retry forever if we don't take care to give
-        up at some finite point.  */
-      static int retries = 4;
-      if (unattended_mode && retries-- <= 0)
+      // 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;
-         exit_msg = IDS_INSTALL_INCOMPLETE;
-         LogSingleton::GetInstance().exit (1);
+         Log (LOG_PLAIN) << "download error in unattended_mode: out of retries" << endLog;
+         rc = IDABORT;
        }
       else if (unattended_mode)
         {
-         log (LOG_PLAIN) << "download error in unattended_mode: " << retries
+         Log (LOG_PLAIN) << "download error in unattended_mode: " << retries
            << (retries > 1 ? " retries" : " retry") << " remaining." << endLog;
-         return IDD_SITE;
+         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)
-       exit_msg = IDS_DOWNLOAD_INCOMPLETE;
+       Logger ().setExitMsg (IDS_DOWNLOAD_INCOMPLETE_EXIT);
       else if (!unattended_mode)
-       exit_msg = IDS_DOWNLOAD_COMPLETE;
+       Logger ().setExitMsg (IDS_DOWNLOAD_COMPLETE);
       return IDD_DESKTOP;
     }
   else
@@ -315,6 +382,8 @@ do_download_reflector (void *p)
   HANDLE *context;
   context = (HANDLE *) p;
 
+  SetThreadUILanguage(langid);
+
   try
   {
     int next_dialog =
@@ -323,7 +392,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.036223 seconds and 5 git commands to generate.