]> cygwin.com Git - cygwin-apps/setup.git/blobdiff - download.cc
2002-07-15 Robert Collins <rbtcollins@hotmail.com>
[cygwin-apps/setup.git] / download.cc
index 2e695e8dd0b77c332eed132e39802577d3409476..6a375a9c84172106369d039e1c58ee41388bfefb 100644 (file)
@@ -21,6 +21,8 @@ static const char *cvsid =
   "\n%%% $Id$\n";
 #endif
 
+#include "download.h"
+  
 #include "win32.h"
 
 #include <stdio.h>
@@ -33,10 +35,8 @@ static const char *cvsid =
 #include "String++.h"
 #include "geturl.h"
 #include "state.h"
-#include "mkdir.h"
-#include "log.h"
+#include "LogSingleton.h"
 #include "filemanip.h"
-#include "port.h"
 
 #include "io_stream.h"
 
@@ -53,10 +53,53 @@ static const char *cvsid =
 
 #include "Exception.h"
 
-#include "download.h"
-  
 extern ThreeBarProgressPage Progress;
 
+
+bool
+validateCachedPackage (String const &fullname, packagesource & pkgsource)
+{
+  if (pkgsource.md5.isSet())
+    {
+      // check the MD5 sum of the cached file here
+      io_stream *thefile = io_stream::open (fullname, "rb");
+      if (!thefile)
+       return 0;
+      md5_state_t pns;
+      md5_init (&pns);
+
+      log (LOG_BABBLE) << "Checking MD5 for " << fullname << endLog;
+
+      Progress.SetText1 ((String ("Checking MD5 for ") + pkgsource.Base()).cstr_oneuse());
+      Progress.SetText4 ("Progress:");
+      Progress.SetBar1 (0);
+      
+      unsigned char buffer[16384];
+      ssize_t count;
+      while ((count = thefile->read (buffer, 16384)) > 0)
+       {
+         md5_append (&pns, buffer, count);
+         Progress.SetBar1 (thefile->tell(), thefile->get_size());
+       }
+      delete thefile;
+      if (count < 0)
+       throw new Exception ("__LINE__ __FILE__", (String ("IO Error reading ") + pkgsource.Cached()).cstr_oneuse(), APPERR_IO_ERROR);
+      
+      md5_byte_t tempdigest[16];
+      md5_finish(&pns, tempdigest);
+      md5 tempMD5;
+      tempMD5.set (tempdigest);
+      
+      log (LOG_BABBLE) << "For file '" << fullname << 
+          " ini digest is " << pkgsource.md5.print() <<
+          " file digest is " << tempMD5.print() << endLog;
+      
+      if (pkgsource.md5 != tempMD5)
+       return false;
+    }
+  return true;
+}
+
 /* 0 on failure
  */
 int
@@ -77,55 +120,31 @@ check_for_cached (packagesource & pkgsource)
   if ((size = get_file_size (prefix + pkgsource.Canonical ())) > 0)
     if (size == pkgsource.size)
       {
-       pkgsource.set_cached (prefix + pkgsource.Canonical ());
+       if (validateCachedPackage (prefix + pkgsource.Canonical (), pkgsource))
+         pkgsource.set_cached (prefix + pkgsource.Canonical ());
+       else
+         throw new Exception ("__LINE__ __FILE__", (String ("Package validation failure for ") + prefix + pkgsource.Canonical ()).cstr_oneuse(), APPERR_CORRUPT_PACKAGE);
        return 1;
       }
 
   /*
      2) is there a version from one of the selected mirror sites available ?
    */
-  for (size_t n = 1; n <= pkgsource.sites.number (); n++)
+  for (packagesource::sitestype::const_iterator n = pkgsource.sites.begin();
+       n != pkgsource.sites.end(); ++n)
     {
-      String fullname = prefix +
-       rfc1738_escape_part (pkgsource.sites[n]->key) + "/" +
+      String fullname = prefix + rfc1738_escape_part (n->key) + "/" +
        pkgsource.Canonical ();
     if ((size = get_file_size (fullname)) > 0)
       if (size == pkgsource.size)
        {
-         if (pkgsource.md5.isSet())
-           {
-             // check the MD5 sum of the cached file here
-             io_stream *thefile = io_stream::open (String("file://") + fullname, "rb");
-             if (!thefile)
-               return 0;
-             md5_state_t pns;
-             md5_init (&pns);
-
-             unsigned char buffer[16384];
-             ssize_t count;
-             while ((count = thefile->read (buffer, 16384)) > 0)
-               md5_append (&pns, buffer, count);
-             delete thefile;
-             if (count < 0)
-               throw new Exception ("__LINE__ __FILE__", (String ("IO Error reading ") + pkgsource.Cached()).cstr_oneuse(), APPERR_IO_ERROR);
-
-             md5_byte_t tempdigest[16];
-             md5_finish(&pns, tempdigest);
-             md5 tempMD5;
-             tempMD5.set (tempdigest);
-             
-             log (LOG_BABBLE, String ("For file") + pkgsource.Cached() + 
-                  " ini digest is" + pkgsource.md5.print() + 
-                  " file digest is " + tempMD5.print());
-             
-             if (pkgsource.md5 != tempMD5)
-               throw new Exception ("__LINE__ __FILE__", (String ("MD5 Checksum failure for ") + pkgsource.Cached()).cstr_oneuse(), APPERR_CORRUPT_PACKAGE);
-           }
-         pkgsource.
-           set_cached (String ("file://") + fullname );
+         if (validateCachedPackage (fullname, pkgsource))
+           pkgsource.set_cached (fullname );
+         else
+           throw new Exception ("__LINE__ __FILE__", (String ("Package validation failure for ") + fullname).cstr_oneuse(), APPERR_CORRUPT_PACKAGE);
          return 1;
        }
-       }
+    }
   return 0;
 }
 
@@ -152,16 +171,15 @@ download_one (packagesource & pkgsource, HWND owner)
   /* try the download sites one after another */
 
   int success = 0;
-  for (size_t n = 1; n <= pkgsource.sites.number () && !success; n++)
+  for (packagesource::sitestype::const_iterator n = pkgsource.sites.begin();
+       n != pkgsource.sites.end() && !success; ++n)
     {
       String const local = local_dir + "/" +
-                                 rfc1738_escape_part (pkgsource.sites[n]->
-                                                      key) + "/" +
+                                 rfc1738_escape_part (n->key) + "/" +
                                  pkgsource.Canonical ();
       io_stream::mkpath_p (PATH_TO_FILE, String ("file://") + local);
 
-      if (get_url_to_file(pkgsource.sites[n]->key +  "/" +
-                         pkgsource.Canonical (),
+      if (get_url_to_file(n->key +  "/" + pkgsource.Canonical (),
                          local + ".tmp", pkgsource.size, owner))
        {
          /* FIXME: note new source ? */
@@ -172,7 +190,7 @@ download_one (packagesource & pkgsource, HWND owner)
          size_t size = get_file_size (String("file://") + local + ".tmp");
          if (size == pkgsource.size)
            {
-             log (LOG_PLAIN, String ("Downloaded ") + local);
+             log (LOG_PLAIN) << "Downloaded " << local << endLog;
              if (_access (local.cstr_oneuse(), 0) == 0)
                remove (local.cstr_oneuse());
              rename ((local + ".tmp").cstr_oneuse(), local.cstr_oneuse());
@@ -184,9 +202,9 @@ download_one (packagesource & pkgsource, HWND owner)
            }
          else
            {
-             log (LOG_PLAIN,
-                  "Download %s wrong size (%u actual vs %d expected)",
-                  local.cstr_oneuse(), size, pkgsource.size);
+             log (LOG_PLAIN) << "Download " << local << " wrong size (" <<
+               size << " actual vs " << pkgsource.size << " expected)" << 
+               endLog;
              remove ((local + ".tmp").cstr_oneuse());
              continue;
            }
@@ -207,18 +225,32 @@ do_download_thread (HINSTANCE h, HWND owner)
 
   packagedb db;
   /* calculate the amount needed */
-  for (size_t n = 1; n <= db.packages.number (); n++)
+  for (vector <packagemeta *>::iterator i = db.packages.begin ();
+       i != db.packages.end (); ++i)
     {
-      packagemeta & pkg = *db.packages[n];
-      if (pkg.desired && (pkg.desired->srcpicked || pkg.desired->binpicked))
+      packagemeta & pkg = **i;
+      if (pkg.desired.changeRequested())
        {
-         packageversion *version = pkg.desired;
+         packageversion version = pkg.desired;
+         packageversion sourceversion = version.sourcePackage();
          try 
            {
-             if (!check_for_cached (version->bin) && pkg.desired->binpicked)
-                 total_download_bytes += version->bin.size;
-             if (!check_for_cached (version->src) && pkg.desired->srcpicked)
-                 total_download_bytes += version->src.size;
+             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)
            {
@@ -234,17 +266,29 @@ do_download_thread (HINSTANCE h, HWND owner)
   /* and do the download. FIXME: This here we assign a new name for the cached version
    * and check that above.
    */
-  for (size_t n = 1; n <= db.packages.number (); n++)
+  for (vector <packagemeta *>::iterator i = db.packages.begin ();
+       i != db.packages.end (); ++i)
     {
-      packagemeta & pkg = *db.packages[n];
-      if (pkg.desired && (pkg.desired->srcpicked || pkg.desired->binpicked))
+      packagemeta & pkg = **i;
+      if (pkg.desired.changeRequested())
        {
          int e = 0;
-         packageversion *version = pkg.desired;
-         if (version->binpicked)
-           e += download_one (version->bin, owner);
-         if (version->srcpicked)
-           e += download_one (version->src, owner);
+         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);
+           }
          errors += e;
 #if 0
          if (e)
This page took 0.030999 seconds and 5 git commands to generate.