]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/acquire.cc
Rework TransactionID stuff
[apt.git] / apt-pkg / acquire.cc
index a2da196be0bd51a6e0fa19673667d6afe180d45f..33afd8f1f8ffc37aee1b760ece99f07317bfe22a 100644 (file)
@@ -5,14 +5,16 @@
 
    Acquire - File Acquiration
 
-   The core element for the schedual system is the concept of a named
+   The core element for the schedule system is the concept of a named
    queue. Each queue is unique and each queue has a name derived from the
-   URI. The degree of paralization can be controled by how the queue
+   URI. The degree of paralization can be controlled by how the queue
    name is derived from the URI.
    
    ##################################################################### */
                                                                        /*}}}*/
 // Include Files                                                       /*{{{*/
+#include <config.h>
+
 #include <apt-pkg/acquire.h>
 #include <apt-pkg/acquire-item.h>
 #include <apt-pkg/acquire-worker.h>
 #include <apt-pkg/strutl.h>
 #include <apt-pkg/fileutl.h>
 
-#include <apti18n.h>
-
+#include <string>
+#include <vector>
 #include <iostream>
 #include <sstream>
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <iomanip>
 
 #include <dirent.h>
 #include <sys/time.h>
+#include <sys/select.h>
 #include <errno.h>
+#include <sys/stat.h>
+
+#include <apti18n.h>
                                                                        /*}}}*/
 
 using namespace std;
@@ -37,9 +47,9 @@ using namespace std;
 // Acquire::pkgAcquire - Constructor                                   /*{{{*/
 // ---------------------------------------------------------------------
 /* We grab some runtime state from the configuration space */
-pkgAcquire::pkgAcquire() : Queues(0), Workers(0), Configs(0), Log(NULL), ToFetch(0),
+pkgAcquire::pkgAcquire() : LockFD(-1), Queues(0), Workers(0), Configs(0), Log(NULL), ToFetch(0),
                           Debug(_config->FindB("Debug::pkgAcquire",false)),
-                          Running(false), LockFD(-1)
+                          Running(false)
 {
    string const Mode = _config->Find("Acquire::Queue-Mode","host");
    if (strcasecmp(Mode.c_str(),"host") == 0)
@@ -47,10 +57,10 @@ pkgAcquire::pkgAcquire() : Queues(0), Workers(0), Configs(0), Log(NULL), ToFetch
    if (strcasecmp(Mode.c_str(),"access") == 0)
       QueueMode = QueueAccess;
 }
-pkgAcquire::pkgAcquire(pkgAcquireStatus *Progress) : Queues(0), Workers(0),
+pkgAcquire::pkgAcquire(pkgAcquireStatus *Progress) :  LockFD(-1), Queues(0), Workers(0),
                           Configs(0), Log(Progress), ToFetch(0),
                           Debug(_config->FindB("Debug::pkgAcquire",false)),
-                          Running(false), LockFD(-1)
+                          Running(false)
 {
    string const Mode = _config->Find("Acquire::Queue-Mode","host");
    if (strcasecmp(Mode.c_str(),"host") == 0)
@@ -159,6 +169,74 @@ void pkgAcquire::Remove(Item *Itm)
    }
 }
                                                                        /*}}}*/
+// Acquire::AbortTransaction - Remove a transaction                    /*{{{*/
+void pkgAcquire::AbortTransaction(unsigned long TransactionID)
+{
+   if(_config->FindB("Debug::Acquire::Transaction", false) == true)
+      std::clog << "AbortTransaction: " << TransactionID << std::endl;
+
+   std::vector<Item*> Transaction;
+   for (ItemIterator I = Items.begin(); I != Items.end(); ++I)
+      if((*I)->TransactionID == TransactionID)
+         Transaction.push_back(*I);
+   
+   for (std::vector<Item*>::iterator I = Transaction.begin();
+        I != Transaction.end(); ++I)
+   {
+      if(_config->FindB("Debug::Acquire::Transaction", false) == true)
+         std::clog << "  Cancel: " << (*I)->DestFile << std::endl;
+      //Dequeue(*I);
+      (*I)->Status = pkgAcquire::Item::StatError;
+   }
+}
+                                                                       /*}}}*/
+bool pkgAcquire::TransactionHasError(unsigned long TransactionID)
+{
+   std::vector<Item*> Transaction;
+   for (ItemIterator I = Items.begin(); I != Items.end(); ++I)
+      if((*I)->TransactionID == TransactionID)
+         if((*I)->Status == pkgAcquire::Item::StatError ||
+            (*I)->Status == pkgAcquire::Item::StatAuthError)
+            return true;
+
+   return false;
+}
+// Acquire::CommitTransaction - Commit a transaction                   /*{{{*/
+void pkgAcquire::CommitTransaction(unsigned long TransactionID)
+{
+   if(_config->FindB("Debug::Acquire::Transaction", false) == true)
+      std::clog << "CommitTransaction: " << TransactionID << std::endl;
+
+   std::vector<Item*> Transaction;
+   for (ItemIterator I = Items.begin(); I != Items.end(); ++I)
+      if((*I)->TransactionID == TransactionID)
+         Transaction.push_back(*I);
+   
+   // move new files into place *and* remove files that are not
+   // part of the transaction but are still on disk
+   for (std::vector<Item*>::iterator I = Transaction.begin();
+        I != Transaction.end(); ++I)
+   {
+      if((*I)->PartialFile != "")
+      {
+         if(_config->FindB("Debug::Acquire::Transaction", false) == true)
+            std::clog << "mv " 
+                      << (*I)->PartialFile << " -> " 
+                      <<  (*I)->DestFile << std::endl;
+         Rename((*I)->PartialFile, (*I)->DestFile);
+         chmod((*I)->DestFile.c_str(),0644);
+      } else {
+         if(_config->FindB("Debug::Acquire::Transaction", false) == true)
+            std::clog << "rm " 
+                      <<  (*I)->DestFile << std::endl;
+         unlink((*I)->DestFile.c_str());
+      }
+      // mark that this transaction is finished
+      (*I)->TransactionID = 0;
+   }
+}
+                                                                       /*}}}*/
+
 // Acquire::Add - Add a worker                                         /*{{{*/
 // ---------------------------------------------------------------------
 /* A list of workers is kept so that the select loop can direct their FD
@@ -173,7 +251,7 @@ void pkgAcquire::Add(Worker *Work)
 // ---------------------------------------------------------------------
 /* A worker has died. This can not be done while the select loop is running
    as it would require that RunFds could handling a changing list state and
-   it cant.. */
+   it can't.. */
 void pkgAcquire::Remove(Worker *Work)
 {
    if (Running == true)
@@ -242,11 +320,19 @@ void pkgAcquire::Dequeue(Item *Itm)
 {
    Queue *I = Queues;
    bool Res = false;
-   for (; I != 0; I = I->Next)
-      Res |= I->Dequeue(Itm);
-   
    if (Debug == true)
       clog << "Dequeuing " << Itm->DestFile << endl;
+
+   for (; I != 0; I = I->Next)
+   {
+      if (I->Dequeue(Itm))
+      {
+         Res = true;
+        if (Debug == true)
+           clog << "Dequeued from " << I->Name << endl;
+      }
+   }
+
    if (Res == true)
       ToFetch--;
 }
@@ -267,9 +353,30 @@ string pkgAcquire::QueueName(string Uri,MethodConfig const *&Config)
    /* Single-Instance methods get exactly one queue per URI. This is
       also used for the Access queue method  */
    if (Config->SingleInstance == true || QueueMode == QueueAccess)
-       return U.Access;
+      return U.Access;
+
+   string AccessSchema = U.Access + ':',
+       FullQueueName = AccessSchema + U.Host;
+   unsigned int Instances = 0, SchemaLength = AccessSchema.length();
+
+   Queue *I = Queues;
+   for (; I != 0; I = I->Next) {
+      // if the queue already exists, re-use it
+      if (I->Name == FullQueueName)
+        return FullQueueName;
+
+      if (I->Name.compare(0, SchemaLength, AccessSchema) == 0)
+        Instances++;
+   }
+
+   if (Debug) {
+      clog << "Found " << Instances << " instances of " << U.Access << endl;
+   }
 
-   return U.Access + ':' + U.Host;
+   if (Instances >= (unsigned int)_config->FindI("Acquire::QueueHost::Limit",10))
+      return U.Access;
+
+   return FullQueueName;
 }
                                                                        /*}}}*/
 // Acquire::GetConfig - Fetch the configuration information            /*{{{*/
@@ -437,7 +544,7 @@ void pkgAcquire::Bump()
 pkgAcquire::Worker *pkgAcquire::WorkerStep(Worker *I)
 {
    return I->NextAcquire;
-};
+}
                                                                        /*}}}*/
 // Acquire::Clean - Cleans a directory                                 /*{{{*/
 // ---------------------------------------------------------------------
@@ -445,6 +552,13 @@ pkgAcquire::Worker *pkgAcquire::WorkerStep(Worker *I)
    if it is part of the download set. */
 bool pkgAcquire::Clean(string Dir)
 {
+   // non-existing directories are by definition clean…
+   if (DirectoryExists(Dir) == false)
+      return true;
+
+   if(Dir == "/")
+      return _error->Error(_("Clean of %s is not supported"), Dir.c_str());
+
    DIR *D = opendir(Dir.c_str());   
    if (D == 0)
       return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
@@ -485,7 +599,7 @@ bool pkgAcquire::Clean(string Dir)
 // Acquire::TotalNeeded - Number of bytes to fetch                     /*{{{*/
 // ---------------------------------------------------------------------
 /* This is the total number of bytes needed */
-unsigned long long pkgAcquire::TotalNeeded()
+APT_PURE unsigned long long pkgAcquire::TotalNeeded()
 {
    unsigned long long Total = 0;
    for (ItemCIterator I = ItemsBegin(); I != ItemsEnd(); ++I)
@@ -496,7 +610,7 @@ unsigned long long pkgAcquire::TotalNeeded()
 // Acquire::FetchNeeded - Number of bytes needed to get                        /*{{{*/
 // ---------------------------------------------------------------------
 /* This is the number of bytes that is not local */
-unsigned long long pkgAcquire::FetchNeeded()
+APT_PURE unsigned long long pkgAcquire::FetchNeeded()
 {
    unsigned long long Total = 0;
    for (ItemCIterator I = ItemsBegin(); I != ItemsEnd(); ++I)
@@ -508,7 +622,7 @@ unsigned long long pkgAcquire::FetchNeeded()
 // Acquire::PartialPresent - Number of partial bytes we already have   /*{{{*/
 // ---------------------------------------------------------------------
 /* This is the number of bytes that is not local */
-unsigned long long pkgAcquire::PartialPresent()
+APT_PURE unsigned long long pkgAcquire::PartialPresent()
 {
   unsigned long long Total = 0;
    for (ItemCIterator I = ItemsBegin(); I != ItemsEnd(); ++I)
@@ -760,7 +874,7 @@ void pkgAcquire::Queue::Bump()
 // AcquireStatus::pkgAcquireStatus - Constructor                       /*{{{*/
 // ---------------------------------------------------------------------
 /* */
-pkgAcquireStatus::pkgAcquireStatus() : Update(true), MorePulses(false)
+pkgAcquireStatus::pkgAcquireStatus() : d(NULL), Update(true), MorePulses(false)
 {
    Start();
 }
@@ -780,7 +894,9 @@ bool pkgAcquireStatus::Pulse(pkgAcquire *Owner)
    // Compute the total number of bytes to fetch
    unsigned int Unknown = 0;
    unsigned int Count = 0;
-   for (pkgAcquire::ItemCIterator I = Owner->ItemsBegin(); I != Owner->ItemsEnd();
+   bool UnfetchedReleaseFiles = false;
+   for (pkgAcquire::ItemCIterator I = Owner->ItemsBegin(); 
+        I != Owner->ItemsEnd();
        ++I, ++Count)
    {
       TotalItems++;
@@ -791,6 +907,13 @@ bool pkgAcquireStatus::Pulse(pkgAcquire *Owner)
       if ((*I)->Local == true)
         continue;
 
+      // see if the method tells us to expect more
+      TotalItems += (*I)->ExpectedAdditionalItems;
+
+      // check if there are unfetched Release files
+      if ((*I)->Complete == false && (*I)->ExpectedAdditionalItems > 0)
+         UnfetchedReleaseFiles = true;
+
       TotalBytes += (*I)->FileSize;
       if ((*I)->Complete == true)
         CurrentBytes += (*I)->FileSize;
@@ -799,9 +922,10 @@ bool pkgAcquireStatus::Pulse(pkgAcquire *Owner)
    }
    
    // Compute the current completion
-   unsigned long ResumeSize = 0;
+   unsigned long long ResumeSize = 0;
    for (pkgAcquire::Worker *I = Owner->WorkersBegin(); I != 0;
        I = Owner->WorkerStep(I))
+   {
       if (I->CurrentItem != 0 && I->CurrentItem->Owner->Complete == false)
       {
         CurrentBytes += I->CurrentSize;
@@ -812,6 +936,7 @@ bool pkgAcquireStatus::Pulse(pkgAcquire *Owner)
             I->CurrentItem->Owner->Complete == false)
            TotalBytes += I->CurrentSize;
       }
+   }
    
    // Normalize the figures and account for unknown size downloads
    if (TotalBytes <= 0)
@@ -822,6 +947,12 @@ bool pkgAcquireStatus::Pulse(pkgAcquire *Owner)
    // Wha?! Is not supposed to happen.
    if (CurrentBytes > TotalBytes)
       CurrentBytes = TotalBytes;
+
+   // debug
+   if (_config->FindB("Debug::acquire::progress", false) == true)
+      std::clog << " Bytes: " 
+                << SizeToStr(CurrentBytes) << " / " << SizeToStr(TotalBytes) 
+                << std::endl;
    
    // Compute the CPS
    struct timeval NewTime;
@@ -838,10 +969,18 @@ bool pkgAcquireStatus::Pulse(pkgAcquire *Owner)
       else
         CurrentCPS = ((CurrentBytes - ResumeSize) - LastBytes)/Delta;
       LastBytes = CurrentBytes - ResumeSize;
-      ElapsedTime = (unsigned long)Delta;
+      ElapsedTime = (unsigned long long)Delta;
       Time = NewTime;
    }
 
+   // calculate the percentage, if we have too little data assume 1%
+   if (TotalBytes > 0 && UnfetchedReleaseFiles)
+      Percent = 0;
+   else 
+      // use both files and bytes because bytes can be unreliable
+      Percent = (0.8 * (CurrentBytes/float(TotalBytes)*100.0) + 
+                 0.2 * (CurrentItems/float(TotalItems)*100.0));
+
    int fd = _config->FindI("APT::Status-Fd",-1);
    if(fd > 0) 
    {
@@ -849,8 +988,9 @@ bool pkgAcquireStatus::Pulse(pkgAcquire *Owner)
 
       char msg[200];
       long i = CurrentItems < TotalItems ? CurrentItems + 1 : CurrentItems;
-      unsigned long ETA =
-        (unsigned long)((TotalBytes - CurrentBytes) / CurrentCPS);
+      unsigned long long ETA = 0;
+      if(CurrentCPS > 0)
+         ETA = (TotalBytes - CurrentBytes) / CurrentCPS;
 
       // only show the ETA if it makes sense
       if (ETA > 0 && ETA < 172800 /* two days */ )
@@ -858,14 +998,14 @@ bool pkgAcquireStatus::Pulse(pkgAcquire *Owner)
       else
         snprintf(msg,sizeof(msg), _("Retrieving file %li of %li"), i, TotalItems);
         
-
-
       // build the status str
       status << "dlstatus:" << i
-            << ":"  << (CurrentBytes/float(TotalBytes)*100.0) 
-            << ":" << msg 
-            << endl;
-      write(fd, status.str().c_str(), status.str().size());
+             << ":"  << std::setprecision(3) << Percent
+             << ":" << msg 
+             << endl;
+
+      std::string const dlstatus = status.str();
+      FileFd::Write(fd, dlstatus.c_str(), dlstatus.size());
    }
 
    return true;
@@ -906,13 +1046,13 @@ void pkgAcquireStatus::Stop()
    else
       CurrentCPS = FetchedBytes/Delta;
    LastBytes = CurrentBytes;
-   ElapsedTime = (unsigned int)Delta;
+   ElapsedTime = (unsigned long long)Delta;
 }
                                                                        /*}}}*/
 // AcquireStatus::Fetched - Called when a byte set has been fetched    /*{{{*/
 // ---------------------------------------------------------------------
 /* This is used to get accurate final transfer rate reporting. */
-void pkgAcquireStatus::Fetched(unsigned long Size,unsigned long Resume)
+void pkgAcquireStatus::Fetched(unsigned long long Size,unsigned long long Resume)
 {   
    FetchedBytes += Size - Resume;
 }