]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/acquire.cc
use the portable timegm shown in his manpage instead of a strange
[apt.git] / apt-pkg / acquire.cc
index fff1b2b6ad379b28603de987933b4523dc5c3f36..832eaa02cfc7224a3bee3f348b2f85ee1e2acf10 100644 (file)
    ##################################################################### */
                                                                        /*}}}*/
 // Include Files                                                       /*{{{*/
    ##################################################################### */
                                                                        /*}}}*/
 // Include Files                                                       /*{{{*/
-#ifdef __GNUG__
-#pragma implementation "apt-pkg/acquire.h"
-#endif       
 #include <apt-pkg/acquire.h>
 #include <apt-pkg/acquire-item.h>
 #include <apt-pkg/acquire-worker.h>
 #include <apt-pkg/configuration.h>
 #include <apt-pkg/error.h>
 #include <apt-pkg/strutl.h>
 #include <apt-pkg/acquire.h>
 #include <apt-pkg/acquire-item.h>
 #include <apt-pkg/acquire-worker.h>
 #include <apt-pkg/configuration.h>
 #include <apt-pkg/error.h>
 #include <apt-pkg/strutl.h>
+#include <apt-pkg/fileutl.h>
 
 #include <apti18n.h>
 
 #include <iostream>
 #include <sstream>
 
 #include <apti18n.h>
 
 #include <iostream>
 #include <sstream>
-    
+#include <stdio.h>
+
 #include <dirent.h>
 #include <sys/time.h>
 #include <errno.h>
 #include <dirent.h>
 #include <sys/time.h>
 #include <errno.h>
-#include <sys/stat.h>
                                                                        /*}}}*/
 
 using namespace std;
                                                                        /*}}}*/
 
 using namespace std;
@@ -39,32 +37,81 @@ using namespace std;
 // Acquire::pkgAcquire - Constructor                                   /*{{{*/
 // ---------------------------------------------------------------------
 /* We grab some runtime state from the configuration space */
 // Acquire::pkgAcquire - Constructor                                   /*{{{*/
 // ---------------------------------------------------------------------
 /* We grab some runtime state from the configuration space */
-pkgAcquire::pkgAcquire(pkgAcquireStatus *Log) : Log(Log)
+pkgAcquire::pkgAcquire() : Queues(0), Workers(0), Configs(0), Log(NULL), ToFetch(0),
+                          Debug(_config->FindB("Debug::pkgAcquire",false)),
+                          Running(false), LockFD(-1)
 {
 {
-   Queues = 0;
-   Configs = 0;
-   Workers = 0;
-   ToFetch = 0;
-   Running = false;
-   
-   string Mode = _config->Find("Acquire::Queue-Mode","host");
+   string const Mode = _config->Find("Acquire::Queue-Mode","host");
+   if (strcasecmp(Mode.c_str(),"host") == 0)
+      QueueMode = QueueHost;
+   if (strcasecmp(Mode.c_str(),"access") == 0)
+      QueueMode = QueueAccess;
+}
+pkgAcquire::pkgAcquire(pkgAcquireStatus *Progress) : Queues(0), Workers(0),
+                          Configs(0), Log(Progress), ToFetch(0),
+                          Debug(_config->FindB("Debug::pkgAcquire",false)),
+                          Running(false), LockFD(-1)
+{
+   string const Mode = _config->Find("Acquire::Queue-Mode","host");
    if (strcasecmp(Mode.c_str(),"host") == 0)
       QueueMode = QueueHost;
    if (strcasecmp(Mode.c_str(),"access") == 0)
    if (strcasecmp(Mode.c_str(),"host") == 0)
       QueueMode = QueueHost;
    if (strcasecmp(Mode.c_str(),"access") == 0)
-      QueueMode = QueueAccess;   
+      QueueMode = QueueAccess;
+   Setup(Progress, "");
+}
+                                                                       /*}}}*/
+// Acquire::Setup - Delayed Constructor                                        /*{{{*/
+// ---------------------------------------------------------------------
+/* Do everything needed to be a complete Acquire object and report the
+   success (or failure) back so the user knows that something is wrong… */
+bool pkgAcquire::Setup(pkgAcquireStatus *Progress, string const &Lock)
+{
+   Log = Progress;
 
 
-   Debug = _config->FindB("Debug::pkgAcquire",false);
-   
-   // This is really a stupid place for this
-   struct stat St;
-   if (stat((_config->FindDir("Dir::State::lists") + "partial/").c_str(),&St) != 0 ||
-       S_ISDIR(St.st_mode) == 0)
-      _error->Error(_("Lists directory %spartial is missing."),
-                   _config->FindDir("Dir::State::lists").c_str());
-   if (stat((_config->FindDir("Dir::Cache::Archives") + "partial/").c_str(),&St) != 0 ||
-       S_ISDIR(St.st_mode) == 0)
-      _error->Error(_("Archive directory %spartial is missing."),
-                   _config->FindDir("Dir::Cache::Archives").c_str());
+   // check for existence and possibly create auxiliary directories
+   string const listDir = _config->FindDir("Dir::State::lists");
+   string const partialListDir = listDir + "partial/";
+   string const archivesDir = _config->FindDir("Dir::Cache::Archives");
+   string const partialArchivesDir = archivesDir + "partial/";
+
+   if (CheckDirectory(_config->FindDir("Dir::State"), partialListDir) == false &&
+       CheckDirectory(listDir, partialListDir) == false)
+      return _error->Errno("Acquire", _("List directory %spartial is missing."), listDir.c_str());
+
+   if (CheckDirectory(_config->FindDir("Dir::Cache"), partialArchivesDir) == false &&
+       CheckDirectory(archivesDir, partialArchivesDir) == false)
+      return _error->Errno("Acquire", _("Archives directory %spartial is missing."), archivesDir.c_str());
+
+   if (Lock.empty() == true || _config->FindB("Debug::NoLocking", false) == true)
+      return true;
+
+   // Lock the directory this acquire object will work in
+   LockFD = GetLock(flCombine(Lock, "lock"));
+   if (LockFD == -1)
+      return _error->Error(_("Unable to lock directory %s"), Lock.c_str());
+
+   return true;
+}
+                                                                       /*}}}*/
+// Acquire::CheckDirectory - ensure that the given directory exists    /*{{{*/
+// ---------------------------------------------------------------------
+/* a small wrapper around CreateDirectory to check if it exists and to
+   remove the trailing "/apt/" from the parent directory if needed */
+bool pkgAcquire::CheckDirectory(string const &Parent, string const &Path) const
+{
+   if (DirectoryExists(Path) == true)
+      return true;
+
+   size_t const len = Parent.size();
+   if (len > 5 && Parent.find("/apt/", len - 6, 5) == len - 5)
+   {
+      if (CreateDirectory(Parent.substr(0,len-5), Path) == true)
+        return true;
+   }
+   else if (CreateDirectory(Parent, Path) == true)
+      return true;
+
+   return false;
 }
                                                                        /*}}}*/
 // Acquire::~pkgAcquire        - Destructor                                    /*{{{*/
 }
                                                                        /*}}}*/
 // Acquire::~pkgAcquire        - Destructor                                    /*{{{*/
@@ -73,7 +120,10 @@ pkgAcquire::pkgAcquire(pkgAcquireStatus *Log) : Log(Log)
 pkgAcquire::~pkgAcquire()
 {
    Shutdown();
 pkgAcquire::~pkgAcquire()
 {
    Shutdown();
-   
+
+   if (LockFD != -1)
+      close(LockFD);
+
    while (Configs != 0)
    {
       MethodConfig *Jnk = Configs;
    while (Configs != 0)
    {
       MethodConfig *Jnk = Configs;
@@ -193,9 +243,9 @@ void pkgAcquire::Enqueue(ItemDesc &Item)
    Item.Owner->Status = Item::StatIdle;
    
    // Queue it into the named queue
    Item.Owner->Status = Item::StatIdle;
    
    // Queue it into the named queue
-   I->Enqueue(Item);
-   ToFetch++;
-         
+   if(I->Enqueue(Item)) 
+      ToFetch++;
+            
    // Some trace stuff
    if (Debug == true)
    {
    // Some trace stuff
    if (Debug == true)
    {
@@ -268,7 +318,7 @@ pkgAcquire::MethodConfig *pkgAcquire::GetConfig(string Access)
       return 0;
 
    /* if a method uses DownloadLimit, we switch to SingleInstance mode */
       return 0;
 
    /* if a method uses DownloadLimit, we switch to SingleInstance mode */
-   if(_config->FindI("Acquire::"+Access+"::DlLimit",0) > 0)
+   if(_config->FindI("Acquire::"+Access+"::Dl-Limit",0) > 0)
       Conf->SingleInstance = true;
     
    return Conf;
       Conf->SingleInstance = true;
     
    return Conf;
@@ -447,8 +497,9 @@ bool pkgAcquire::Clean(string Dir)
         unlink(Dir->d_name);
    };
    
         unlink(Dir->d_name);
    };
    
-   chdir(StartDir.c_str());
    closedir(D);
    closedir(D);
+   if (chdir(StartDir.c_str()) != 0)
+      return _error->Errno("chdir",_("Unable to change to %s"),StartDir.c_str());
    return true;   
 }
                                                                        /*}}}*/
    return true;   
 }
                                                                        /*}}}*/
@@ -486,7 +537,7 @@ double pkgAcquire::PartialPresent()
         Total += (*I)->PartialSize;
    return Total;
 }
         Total += (*I)->PartialSize;
    return Total;
 }
-
+                                                                       /*}}}*/
 // Acquire::UriBegin - Start iterator for the uri list                 /*{{{*/
 // ---------------------------------------------------------------------
 /* */
 // Acquire::UriBegin - Start iterator for the uri list                 /*{{{*/
 // ---------------------------------------------------------------------
 /* */
@@ -503,7 +554,6 @@ pkgAcquire::UriIterator pkgAcquire::UriEnd()
    return UriIterator(0);
 }
                                                                        /*}}}*/
    return UriIterator(0);
 }
                                                                        /*}}}*/
-
 // Acquire::MethodConfig::MethodConfig - Constructor                   /*{{{*/
 // ---------------------------------------------------------------------
 /* */
 // Acquire::MethodConfig::MethodConfig - Constructor                   /*{{{*/
 // ---------------------------------------------------------------------
 /* */
@@ -517,7 +567,6 @@ pkgAcquire::MethodConfig::MethodConfig()
    Next = 0;
 }
                                                                        /*}}}*/
    Next = 0;
 }
                                                                        /*}}}*/
-
 // Queue::Queue - Constructor                                          /*{{{*/
 // ---------------------------------------------------------------------
 /* */
 // Queue::Queue - Constructor                                          /*{{{*/
 // ---------------------------------------------------------------------
 /* */
@@ -549,11 +598,17 @@ pkgAcquire::Queue::~Queue()
 // Queue::Enqueue - Queue an item to the queue                         /*{{{*/
 // ---------------------------------------------------------------------
 /* */
 // Queue::Enqueue - Queue an item to the queue                         /*{{{*/
 // ---------------------------------------------------------------------
 /* */
-void pkgAcquire::Queue::Enqueue(ItemDesc &Item)
+bool pkgAcquire::Queue::Enqueue(ItemDesc &Item)
 {
    QItem **I = &Items;
 {
    QItem **I = &Items;
-   for (; *I != 0; I = &(*I)->Next);
-   
+   // move to the end of the queue and check for duplicates here
+   for (; *I != 0; I = &(*I)->Next)
+      if (Item.URI == (*I)->URI) 
+      {
+        Item.Owner->Status = Item::StatDone;
+        return false;
+      }
+
    // Create a new item
    QItem *Itm = new QItem;
    *Itm = Item;
    // Create a new item
    QItem *Itm = new QItem;
    *Itm = Item;
@@ -563,6 +618,7 @@ void pkgAcquire::Queue::Enqueue(ItemDesc &Item)
    Item.Owner->QueueCounter++;   
    if (Items->Next == 0)
       Cycle();
    Item.Owner->QueueCounter++;   
    if (Items->Next == 0)
       Cycle();
+   return true;
 }
                                                                        /*}}}*/
 // Queue::Dequeue - Remove an item from the queue                      /*{{{*/
 }
                                                                        /*}}}*/
 // Queue::Dequeue - Remove an item from the queue                      /*{{{*/
@@ -615,7 +671,7 @@ bool pkgAcquire::Queue::Startup()
          added other source retry to have cycle maintain a pipeline depth
          on its own. */
       if (Cnf->Pipeline == true)
          added other source retry to have cycle maintain a pipeline depth
          on its own. */
       if (Cnf->Pipeline == true)
-        MaxPipeDepth = 10;
+        MaxPipeDepth = _config->FindI("Acquire::Max-Pipeline-Depth",10);
       else
         MaxPipeDepth = 1;
    }
       else
         MaxPipeDepth = 1;
    }
@@ -722,7 +778,6 @@ void pkgAcquire::Queue::Bump()
    Cycle();
 }
                                                                        /*}}}*/
    Cycle();
 }
                                                                        /*}}}*/
-
 // AcquireStatus::pkgAcquireStatus - Constructor                       /*{{{*/
 // ---------------------------------------------------------------------
 /* */
 // AcquireStatus::pkgAcquireStatus - Constructor                       /*{{{*/
 // ---------------------------------------------------------------------
 /* */
@@ -792,7 +847,7 @@ bool pkgAcquireStatus::Pulse(pkgAcquire *Owner)
    // Compute the CPS
    struct timeval NewTime;
    gettimeofday(&NewTime,0);
    // Compute the CPS
    struct timeval NewTime;
    gettimeofday(&NewTime,0);
-   if (NewTime.tv_sec - Time.tv_sec == 6 && NewTime.tv_usec > Time.tv_usec ||
+   if ((NewTime.tv_sec - Time.tv_sec == 6 && NewTime.tv_usec > Time.tv_usec) ||
        NewTime.tv_sec - Time.tv_sec > 6)
    {    
       double Delta = NewTime.tv_sec - Time.tv_sec + 
        NewTime.tv_sec - Time.tv_sec > 6)
    {    
       double Delta = NewTime.tv_sec - Time.tv_sec +