apt  0.9.12.1
acquire.h
Go to the documentation of this file.
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: acquire.h,v 1.29.2.1 2003/12/24 23:09:17 mdz Exp $
4 /* ######################################################################
5 
6  Acquire - File Acquiration
7 
8  This module contians the Acquire system. It is responsible for bringing
9  files into the local pathname space. It deals with URIs for files and
10  URI handlers responsible for downloading or finding the URIs.
11 
12  Each file to download is represented by an Acquire::Item class subclassed
13  into a specialization. The Item class can add itself to several URI
14  acquire queues each prioritized by the download scheduler. When the
15  system is run the proper URI handlers are spawned and the the acquire
16  queues are fed into the handlers by the schedular until the queues are
17  empty. This allows for an Item to be downloaded from an alternate source
18  if the first try turns out to fail. It also alows concurrent downloading
19  of multiple items from multiple sources as well as dynamic balancing
20  of load between the sources.
21 
22  Schedualing of downloads is done on a first ask first get basis. This
23  preserves the order of the download as much as possible. And means the
24  fastest source will tend to process the largest number of files.
25 
26  Internal methods and queues for performing gzip decompression,
27  md5sum hashing and file copying are provided to allow items to apply
28  a number of transformations to the data files they are working with.
29 
30  ##################################################################### */
31  /*}}}*/
32  /*}}}*/
58 
66 #ifndef PKGLIB_ACQUIRE_H
67 #define PKGLIB_ACQUIRE_H
68 
69 #include <apt-pkg/macros.h>
70 #include <apt-pkg/weakptr.h>
71 
72 #include <vector>
73 #include <string>
74 
75 #include <sys/time.h>
76 #include <unistd.h>
77 
78 #ifndef APT_8_CLEANER_HEADERS
79 using std::vector;
80 using std::string;
81 #endif
82 
83 class pkgAcquireStatus;
84 
94 {
95  private:
97  int LockFD;
99  void *d;
100 
101  public:
102 
103  class Item;
104  class Queue;
105  class Worker;
106  struct MethodConfig;
107  struct ItemDesc;
108  friend class Item;
109  friend class Queue;
110 
111  typedef std::vector<Item *>::iterator ItemIterator;
112  typedef std::vector<Item *>::const_iterator ItemCIterator;
113 
114  protected:
115 
121  std::vector<Item *> Items;
122 
129 
136 
148 
151 
153  unsigned long ToFetch;
154 
155  // Configurable parameters for the scheduler
156 
166  QueueAccess} QueueMode;
167 
169  bool const Debug;
171  bool Running;
172 
174  void Add(Item *Item);
175 
177  void Remove(Item *Item);
178 
180  void Add(Worker *Work);
181 
183  void Remove(Worker *Work);
184 
191  void Enqueue(ItemDesc &Item);
192 
194  void Dequeue(Item *Item);
195 
206  std::string QueueName(std::string URI,MethodConfig const *&Config);
207 
222  virtual void SetFds(int &Fd,fd_set *RSet,fd_set *WSet);
223 
234  virtual void RunFds(fd_set *RSet,fd_set *WSet);
235 
242  void Bump();
243 
244  public:
245 
252  MethodConfig *GetConfig(std::string Access);
253 
255  enum RunResult {
258 
261 
266 
278  RunResult Run(int PulseInterval=500000);
279 
283  void Shutdown();
284 
289  inline Worker *WorkersBegin() {return Workers;};
290 
296  Worker *WorkerStep(Worker *I);
297 
299  inline ItemIterator ItemsBegin() {return Items.begin();};
300 
302  inline ItemIterator ItemsEnd() {return Items.end();};
303 
304  // Iterate over queued Item URIs
305  class UriIterator;
311  UriIterator UriBegin();
313  UriIterator UriEnd();
314 
323  bool Clean(std::string Dir);
324 
328  unsigned long long TotalNeeded();
329 
333  unsigned long long FetchNeeded();
334 
338  unsigned long long PartialPresent();
339 
351  bool Setup(pkgAcquireStatus *Progress = NULL, std::string const &Lock = "");
352 
353  void SetLog(pkgAcquireStatus *Progress) { Log = Progress; }
354 
356  pkgAcquire(pkgAcquireStatus *Log) __deprecated;
357  pkgAcquire();
358 
364  virtual ~pkgAcquire();
365 
366 };
367 
374 {
376  std::string URI;
378  std::string Description;
380  std::string ShortDesc;
383 };
384  /*}}}*/
390 {
391  friend class pkgAcquire;
392  friend class pkgAcquire::UriIterator;
393  friend class pkgAcquire::Worker;
394 
396  void *d;
397 
399  Queue *Next;
400 
401  protected:
402 
405  {
410 
415  {
416  URI = I.URI;
418  ShortDesc = I.ShortDesc;
419  Owner = I.Owner;
420  };
421  };
422 
424  std::string Name;
425 
431 
442 
445 
449  signed long PipeDepth;
450 
454  unsigned long MaxPipeDepth;
455 
456  public:
457 
463  bool Enqueue(ItemDesc &Item);
464 
469  bool Dequeue(Item *Owner);
470 
479  QItem *FindItem(std::string URI,pkgAcquire::Worker *Owner);
480 
485  bool ItemStart(QItem *Itm,unsigned long long Size);
486 
497  bool ItemDone(QItem *Itm);
498 
506  bool Startup();
507 
517  bool Shutdown(bool Final);
518 
523  bool Cycle();
524 
535  void Bump();
536 
542  Queue(std::string Name,pkgAcquire *Owner);
543 
547  virtual ~Queue();
548 };
549  /*}}}*/
552 {
554  void *d;
555 
557  pkgAcquire::Queue *CurQ;
559  pkgAcquire::Queue::QItem *CurItem;
560 
561  public:
562 
563  inline void operator ++() {operator ++(0);};
564 
565  void operator ++(int)
566  {
567  CurItem = CurItem->Next;
568  while (CurItem == 0 && CurQ != 0)
569  {
570  CurItem = CurQ->Items;
571  CurQ = CurQ->Next;
572  }
573  };
574 
575  inline pkgAcquire::ItemDesc const *operator ->() const {return CurItem;};
576  inline bool operator !=(UriIterator const &rhs) const {return rhs.CurQ != CurQ || rhs.CurItem != CurItem;};
577  inline bool operator ==(UriIterator const &rhs) const {return rhs.CurQ == CurQ && rhs.CurItem == CurItem;};
578 
583  UriIterator(pkgAcquire::Queue *Q) : CurQ(Q), CurItem(0)
584  {
585  while (CurItem == 0 && CurQ != 0)
586  {
587  CurItem = CurQ->Items;
588  CurQ = CurQ->Next;
589  }
590  }
591  virtual ~UriIterator() {};
592 };
593  /*}}}*/
596 {
598  void *d;
599 
605 
607  std::string Access;
608 
610  std::string Version;
611 
616 
618  bool Pipeline;
619 
625 
629  bool LocalOnly;
630 
638 
640  bool Removable;
641 
647  MethodConfig();
648 
649  /* \brief Destructor, empty currently */
650  virtual ~MethodConfig() {};
651 };
652  /*}}}*/
658 {
660  void *d;
661 
662  protected:
663 
665  struct timeval Time;
666 
668  struct timeval StartTime;
669 
673  unsigned long long LastBytes;
674 
678  unsigned long long CurrentCPS;
679 
683  unsigned long long CurrentBytes;
684 
690  unsigned long long TotalBytes;
691 
695  unsigned long long FetchedBytes;
696 
700  unsigned long long ElapsedTime;
701 
707  unsigned long TotalItems;
708 
710  unsigned long CurrentItems;
711 
712  public:
713 
717  bool Update;
718 
726 
733  virtual void Fetched(unsigned long long Size,unsigned long long ResumePoint);
734 
752  virtual bool MediaChange(std::string Media,std::string Drive) = 0;
753 
759  virtual void IMSHit(pkgAcquire::ItemDesc &/*Itm*/) {};
760 
762  virtual void Fetch(pkgAcquire::ItemDesc &/*Itm*/) {};
763 
765  virtual void Done(pkgAcquire::ItemDesc &/*Itm*/) {};
766 
770  virtual void Fail(pkgAcquire::ItemDesc &/*Itm*/) {};
771 
782  virtual bool Pulse(pkgAcquire *Owner);
783 
785  virtual void Start();
786 
788  virtual void Stop();
789 
792  virtual ~pkgAcquireStatus() {};
793 };
794  /*}}}*/
797 #endif