1 // -*- mode: cpp; mode: fold -*-
3 // $Id: acquire-item.h,v 1.26.2.3 2004/01/02 18:51:00 mdz Exp $
4 /* ######################################################################
6 Acquire Item - Item to acquire
8 When an item is instantiated it will add it self to the local list in
9 the Owner Acquire class. Derived classes will then call QueueURI to
10 register all the URI's they wish to fetch at the initial moment.
12 Three item classes are provided to provide functionality for
13 downloading of Index, Translation and Packages files.
15 A Archive class is provided for downloading .deb files. It does Md5
16 checking and source location as well as a retry algorithm.
18 ##################################################################### */
20 #ifndef PKGLIB_ACQUIRE_ITEM_H
21 #define PKGLIB_ACQUIRE_ITEM_H
23 #include <apt-pkg/acquire.h>
24 #include <apt-pkg/indexfile.h>
25 #include <apt-pkg/vendor.h>
26 #include <apt-pkg/sourcelist.h>
27 #include <apt-pkg/pkgrecords.h>
28 #include <apt-pkg/indexrecords.h>
31 #pragma interface "apt-pkg/acquire-item.h"
34 /** \addtogroup acquire
37 * \file acquire-item.h
40 /** \brief Represents the process by which a pkgAcquire object should
41 * retrieve a file or a collection of files.
43 * By convention, Item subclasses should insert themselves into the
44 * acquire queue when they are created by calling QueueURI(), and
45 * remove themselves by calling Dequeue() when either Done() or
46 * Failed() is invoked. Item objects are also responsible for
47 * notifying the download progress indicator (accessible via
48 * #Owner->Log) of their status.
52 class pkgAcquire::Item
56 /** \brief The acquire object with which this item is associated. */
59 /** \brief Insert this item into its owner's queue.
61 * \param ItemDesc Metadata about this item (its URI and
64 inline void QueueURI(ItemDesc
&Item
)
65 {Owner
->Enqueue(Item
);};
67 /** \brief Remove this item from its owner's queue. */
68 inline void Dequeue() {Owner
->Dequeue(this);};
70 /** \brief Rename a file without modifying its timestamp.
72 * Many item methods call this as their final action.
74 * \param From The file to be renamed.
76 * \param To The new name of #From. If #To exists it will be
79 void Rename(string From
,string To
);
83 /** \brief The current status of this item. */
86 /** \brief The item is waiting to be downloaded. */
89 /** \brief The item is currently being downloaded. */
92 /** \brief The item has been successfully downloaded. */
95 /** \brief An error was encountered while downloading this
100 /** \brief The item was downloaded but its authenticity could
106 /** \brief Contains a textual description of the error encountered
107 * if #Status is #StatError or #StatAuthError.
111 /** \brief The size of the object to fetch. */
112 unsigned long FileSize
;
114 /** \brief How much of the object was already fetched. */
115 unsigned long PartialSize
;
117 /** \brief If not \b NULL, contains the name of a subprocess that
118 * is operating on this object (for instance, "gzip" or "gpgv").
122 /** \brief A client-supplied unique identifier.
124 * This field is initalized to 0; it is meant to be filled in by
125 * clients that wish to use it to uniquely identify items.
127 * \todo it's unused in apt itself
131 /** \brief If \b true, the entire object has been successfully fetched.
133 * Subclasses should set this to \b true when appropriate.
137 /** \brief If \b true, the URI of this object is "local".
139 * The only effect of this field is to exclude the object from the
140 * download progress indicator's overall statistics.
144 /** \brief The number of fetch queues into which this item has been
147 * There is one queue for each source from which an item could be
152 unsigned int QueueCounter
;
154 /** \brief The name of the file into which the retrieved object
159 /** \brief Invoked by the acquire worker when the object couldn't
162 * This is a branch of the continuation of the fetch process.
164 * \param Message An RFC822-formatted message from the acquire
165 * method describing what went wrong. Use LookupTag() to parse
168 * \param Cnf The method via which the worker tried to fetch this object.
172 virtual void Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
);
174 /** \brief Invoked by the acquire worker when the object was
175 * fetched successfully.
177 * Note that the object might \e not have been written to
178 * DestFile; check for the presence of an Alt-Filename entry in
179 * Message to find the file to which it was really written.
181 * Done is often used to switch from one stage of the processing
182 * to the next (e.g. fetching, unpacking, copying). It is one
183 * branch of the continuation of the fetch process.
185 * \param Message Data from the acquire method. Use LookupTag()
187 * \param Size The size of the object that was fetched.
188 * \param Md5Hash The MD5Sum of the object that was fetched.
189 * \param Cnf The method via which the object was fetched.
193 virtual void Done(string Message
,unsigned long Size
,string Md5Hash
,
194 pkgAcquire::MethodConfig
*Cnf
);
196 /** \brief Invoked when the worker starts to fetch this object.
198 * \param Message RFC822-formatted data from the worker process.
199 * Use LookupTag() to parse it.
201 * \param Size The size of the object being fetched.
205 virtual void Start(string Message
,unsigned long Size
);
207 /** \brief Custom headers to be sent to the fetch process.
209 * \return a string containing RFC822-style headers that are to be
210 * inserted into the 600 URI Acquire message sent to the fetch
211 * subprocess. The headers are inserted after a newline-less
212 * line, so they should (if nonempty) have a leading newline and
213 * no trailing newline.
215 virtual string
Custom600Headers() {return string();};
217 /** \brief A "descriptive" URI-like string.
219 * \return a URI that should be used to describe what is being fetched.
221 virtual string
DescURI() = 0;
222 /** \brief Short item description.
224 * \return a brief description of the object being fetched.
226 virtual string
ShortDesc() {return DescURI();}
228 /** \brief Invoked by the worker when the download is completely done. */
229 virtual void Finished() {};
233 * \return the MD5Sum of this object, if applicable; otherwise, an
236 virtual string
MD5Sum() {return string();};
238 /** \return the acquire process with which this item is associated. */
239 pkgAcquire
*GetOwner() {return Owner
;};
241 /** \return \b true if this object is being fetched from a trusted source. */
242 virtual bool IsTrusted() {return false;};
244 /** \brief Initialize an item.
246 * Adds the item to the list of items known to the acquire
247 * process, but does not place it into any fetch queues (you must
248 * manually invoke QueueURI() to do so).
250 * Initializes all fields of the item other than Owner to 0,
251 * false, or the empty string.
253 * \param Owner The new owner of this item.
255 Item(pkgAcquire
*Owner
);
257 /** \brief Remove this item from its owner's queue by invoking
258 * pkgAcquire::Remove.
263 /** \brief Information about an index patch (aka diff). */
265 /** The filename of the diff. */
268 /** The sha1 hash of the diff. */
271 /** The size of the diff. */
275 /** \brief An item that is responsible for fetching an index file of
276 * package list diffs and starting the package list's download.
278 * This item downloads the Index file and parses it, then enqueues
279 * additional downloads of either the individual patches (using
280 * pkgAcqIndexDiffs) or the entire Packages file (using pkgAcqIndex).
282 * \sa pkgAcqIndexDiffs, pkgAcqIndex
284 class pkgAcqDiffIndex
: public pkgAcquire::Item
287 /** \brief If \b true, debugging information will be written to std::clog. */
290 /** \brief The item that is currently being downloaded. */
291 pkgAcquire::ItemDesc Desc
;
293 /** \brief The URI of the index file to recreate at our end (either
294 * by downloading it or by applying partial patches).
298 /** \brief The MD5Sum that the real index file should have after
299 * all patches have been applied.
303 /** \brief The index file which will be patched to generate the new
306 string CurrentPackagesFile
;
308 /** \brief A description of the Packages file (stored in
309 * pkgAcquire::ItemDesc::Description).
314 // Specialized action members
315 virtual void Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
);
316 virtual void Done(string Message
,unsigned long Size
,string Md5Hash
,
317 pkgAcquire::MethodConfig
*Cnf
);
318 virtual string
DescURI() {return RealURI
+ "Index";};
319 virtual string
Custom600Headers();
321 /** \brief Parse the Index file for a set of Packages diffs.
323 * Parses the Index file and creates additional download items as
326 * \param IndexDiffFile The name of the Index file.
328 * \return \b true if the Index file was successfully parsed, \b
331 bool ParseDiffIndex(string IndexDiffFile
);
334 /** \brief Create a new pkgAcqDiffIndex.
336 * \param Owner The Acquire object that owns this item.
338 * \param URI The URI of the list file to download.
340 * \param URIDesc A long description of the list file to download.
342 * \param ShortDesc A short description of the list file to download.
344 * \param ExpectedMD5 The list file's MD5 signature.
346 pkgAcqDiffIndex(pkgAcquire
*Owner
,string URI
,string URIDesc
,
347 string ShortDesc
, string ExpectedMD5
);
350 /** \brief An item that is responsible for fetching all the patches
351 * that need to be applied to a given package index file.
353 * After downloading and applying a single patch, this item will
354 * enqueue a new pkgAcqIndexDiffs to download and apply the remaining
355 * patches. If no patch can be found that applies to an intermediate
356 * file or if one of the patches cannot be downloaded, falls back to
357 * downloading the entire package index file using pkgAcqIndex.
359 * \sa pkgAcqDiffIndex, pkgAcqIndex
361 class pkgAcqIndexDiffs
: public pkgAcquire::Item
365 /** \brief Queue up the next diff download.
367 * Search for the next available diff that applies to the file
368 * that currently exists on disk, and enqueue it by calling
371 * \return \b true if an applicable diff was found, \b false
374 bool QueueNextDiff();
376 /** \brief Handle tasks that must be performed after the item
377 * finishes downloading.
379 * Dequeues the item and checks the resulting file's md5sum
380 * against ExpectedMD5 after the last patch was applied.
381 * There is no need to check the md5/sha1 after a "normal"
382 * patch because QueueNextDiff() will check the sha1 later.
384 * \param allDone If \b true, the file was entirely reconstructed,
385 * and its md5sum is verified.
387 void Finish(bool allDone
=false);
391 /** \brief If \b true, debugging output will be written to
396 /** \brief A description of the item that is currently being
399 pkgAcquire::ItemDesc Desc
;
401 /** \brief The URI of the package index file that is being
406 /** \brief The MD5Sum of the package index file that is being
411 /** A description of the file being downloaded. */
414 /** The patches that remain to be downloaded, including the patch
415 * being downloaded right now. This list should be ordered so
416 * that each diff appears before any diff that depends on it.
418 * \todo These are indexed by sha1sum; why not use some sort of
419 * dictionary instead of relying on ordering and stripping them
422 vector
<DiffInfo
> available_patches
;
423 /** The current status of this patch. */
426 /** \brief The diff is in an unknown state. */
429 /** \brief The diff is currently being fetched. */
432 /** \brief The diff is currently being uncompressed. */
435 /** \brief The diff is currently being applied. */
441 /** \brief Called when the patch file failed to be downloaded.
443 * This method will fall back to downloading the whole index file
444 * outright; its arguments are ignored.
446 virtual void Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
);
448 virtual void Done(string Message
,unsigned long Size
,string Md5Hash
,
449 pkgAcquire::MethodConfig
*Cnf
);
450 virtual string
DescURI() {return RealURI
+ "Index";};
452 /** \brief Create an index diff item.
454 * After filling in its basic fields, this invokes Finish(true) if
455 * #diffs is empty, or QueueNextDiff() otherwise.
457 * \param Owner The pkgAcquire object that owns this item.
459 * \param URI The URI of the package index file being
462 * \param URIDesc A long description of this item.
464 * \param ShortDesc A brief description of this item.
466 * \param ExpectedMD5 The expected md5sum of the completely
467 * reconstructed package index file; the index file will be tested
468 * against this value when it is entirely reconstructed.
470 * \param diffs The remaining diffs from the index of diffs. They
471 * should be ordered so that each diff appears before any diff
472 * that depends on it.
474 pkgAcqIndexDiffs(pkgAcquire
*Owner
,string URI
,string URIDesc
,
475 string ShortDesc
, string ExpectedMD5
,
476 vector
<DiffInfo
> diffs
=vector
<DiffInfo
>());
479 /** \brief An acquire item that is responsible for fetching an index
480 * file (e.g., Packages or Sources).
482 * \sa pkgAcqDiffIndex, pkgAcqIndexDiffs, pkgAcqIndexTrans
484 * \todo Why does pkgAcqIndex have protected members?
486 class pkgAcqIndex
: public pkgAcquire::Item
490 /** \brief If \b true, the index file has been decompressed. */
493 /** \brief If \b true, the partially downloaded file will be
494 * removed when the download completes.
498 /** \brief The download request that is currently being
501 pkgAcquire::ItemDesc Desc
;
503 /** \brief The object that is actually being fetched (minus any
504 * compression-related extensions).
508 /** \brief The expected md5sum of the decompressed index file. */
511 /** \brief The compression-related file extension that is being
512 * added to the downloaded file (e.g., ".gz" or ".bz2").
514 string CompressionExtension
;
518 // Specialized action members
519 virtual void Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
);
520 virtual void Done(string Message
,unsigned long Size
,string Md5Hash
,
521 pkgAcquire::MethodConfig
*Cnf
);
522 virtual string
Custom600Headers();
523 virtual string
DescURI() {return RealURI
+ CompressionExtension
;};
525 /** \brief Create a pkgAcqIndex.
527 * \param Owner The pkgAcquire object with which this item is
530 * \param URI The URI of the index file that is to be downloaded.
532 * \param URIDesc A "URI-style" description of this index file.
534 * \param ShortDesc A brief description of this index file.
536 * \param ExpectedMD5 The expected md5sum of this index file.
538 * \param compressExt The compression-related extension with which
539 * this index file should be downloaded, or "" to autodetect
540 * (".bz2" is used if bzip2 is installed, ".gz" otherwise).
542 pkgAcqIndex(pkgAcquire
*Owner
,string URI
,string URIDesc
,
543 string ShortDesc
, string ExpectedMD5
, string compressExt
="");
546 /** \brief An acquire item that is responsible for fetching a
547 * translated index file.
549 * The only difference from pkgAcqIndex is that transient failures
550 * are suppressed: no error occurs if the translated index file is
553 class pkgAcqIndexTrans
: public pkgAcqIndex
557 virtual void Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
);
559 /** \brief Create a pkgAcqIndexTrans.
561 * \param Owner The pkgAcquire object with which this item is
564 * \param URI The URI of the index file that is to be downloaded.
566 * \param URIDesc A "URI-style" description of this index file.
568 * \param ShortDesc A brief description of this index file.
570 * \param ExpectedMD5 The expected md5sum of this index file.
572 * \param compressExt The compression-related extension with which
573 * this index file should be downloaded, or "" to autodetect
574 * (".bz2" is used if bzip2 is installed, ".gz" otherwise).
576 pkgAcqIndexTrans(pkgAcquire
*Owner
,string URI
,string URIDesc
,
580 /** \brief Information about an index file. */
583 /** \brief A URI from which the index file can be downloaded. */
586 /** \brief A description of the index file. */
589 /** \brief A shorter description of the index file. */
592 /** \brief The key by which this index file should be
593 * looked up within the meta signature file.
598 /** \brief An acquire item that downloads the detached signature
599 * of a meta-index (Release) file, then queues up the release
602 * \todo Why protected members?
604 * \sa pkgAcqMetaIndex
606 class pkgAcqMetaSig
: public pkgAcquire::Item
609 /** \brief The fetch request that is currently being processed. */
610 pkgAcquire::ItemDesc Desc
;
612 /** \brief The URI of the signature file. Unlike Desc.URI, this is
613 * never modified; it is used to determine the file that is being
618 /** \brief The URI of the meta-index file to be fetched after the signature. */
621 /** \brief A "URI-style" description of the meta-index file to be
622 * fetched after the signature.
624 string MetaIndexURIDesc
;
626 /** \brief A brief description of the meta-index file to be fetched
627 * after the signature.
629 string MetaIndexShortDesc
;
631 /** \brief A package-system-specific parser for the meta-index file. */
632 indexRecords
* MetaIndexParser
;
634 /** \brief The index files which should be looked up in the meta-index
635 * and then downloaded.
637 * \todo Why a list of pointers instead of a list of structs?
639 const vector
<struct IndexTarget
*>* IndexTargets
;
643 // Specialized action members
644 virtual void Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
);
645 virtual void Done(string Message
,unsigned long Size
,string Md5Hash
,
646 pkgAcquire::MethodConfig
*Cnf
);
647 virtual string
Custom600Headers();
648 virtual string
DescURI() {return RealURI
; };
650 /** \brief Create a new pkgAcqMetaSig. */
651 pkgAcqMetaSig(pkgAcquire
*Owner
,string URI
,string URIDesc
, string ShortDesc
,
652 string MetaIndexURI
, string MetaIndexURIDesc
, string MetaIndexShortDesc
,
653 const vector
<struct IndexTarget
*>* IndexTargets
,
654 indexRecords
* MetaIndexParser
);
657 /** \brief An item that is responsible for downloading the meta-index
658 * file (i.e., Release) itself and verifying its signature.
660 * Once the download and verification are complete, the downloads of
661 * the individual index files are queued up using pkgAcqDiffIndex.
662 * If the meta-index file had a valid signature, the expected md5sums
663 * of the index files will be the md5sums listed in the meta-index;
664 * otherwise, the expected md5sums will be "" (causing the
665 * authentication of the index files to be bypassed).
667 class pkgAcqMetaIndex
: public pkgAcquire::Item
670 /** \brief The fetch command that is currently being processed. */
671 pkgAcquire::ItemDesc Desc
;
673 /** \brief The URI that is actually being downloaded; never
674 * modified by pkgAcqMetaIndex.
678 /** \brief The file in which the signature for this index was stored.
680 * If empty, the signature and the md5sums of the individual
681 * indices will not be checked.
685 /** \brief The index files to download. */
686 const vector
<struct IndexTarget
*>* IndexTargets
;
688 /** \brief The parser for the meta-index file. */
689 indexRecords
* MetaIndexParser
;
691 /** \brief If \b true, the index's signature is currently being verified.
694 // required to deal gracefully with problems caused by incorrect ims hits
697 /** \brief Check that the release file is a release file for the
698 * correct distribution.
700 * \return \b true if no fatal errors were encountered.
702 bool VerifyVendor(string Message
);
704 /** \brief Called when a file is finished being retrieved.
706 * If the file was not downloaded to DestFile, a copy process is
707 * set up to copy it to DestFile; otherwise, Complete is set to \b
708 * true and the file is moved to its final location.
710 * \param Message The message block received from the fetch
713 void RetrievalDone(string Message
);
715 /** \brief Called when authentication succeeded.
717 * Sanity-checks the authenticated file, queues up the individual
718 * index files for download, and saves the signature in the lists
719 * directory next to the authenticated list file.
721 * \param Message The message block received from the fetch
724 void AuthDone(string Message
);
726 /** \brief Starts downloading the individual index files.
728 * \param verify If \b true, only indices whose expected md5sum
729 * can be determined from the meta-index will be downloaded, and
730 * the md5sums of indices will be checked (reporting
731 * #StatAuthError if there is a mismatch). If verify is \b false,
732 * no md5sum checking will be performed.
734 void QueueIndexes(bool verify
);
738 // Specialized action members
739 virtual void Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
);
740 virtual void Done(string Message
,unsigned long Size
,string Md5Hash
,
741 pkgAcquire::MethodConfig
*Cnf
);
742 virtual string
Custom600Headers();
743 virtual string
DescURI() {return RealURI
; };
745 /** \brief Create a new pkgAcqMetaIndex. */
746 pkgAcqMetaIndex(pkgAcquire
*Owner
,
747 string URI
,string URIDesc
, string ShortDesc
,
749 const vector
<struct IndexTarget
*>* IndexTargets
,
750 indexRecords
* MetaIndexParser
);
753 /** \brief An item that is responsible for fetching a package file.
755 * If the package file already exists in the cache, nothing will be
758 class pkgAcqArchive
: public pkgAcquire::Item
761 /** \brief The package version being fetched. */
762 pkgCache::VerIterator Version
;
764 /** \brief The fetch command that is currently being processed. */
765 pkgAcquire::ItemDesc Desc
;
767 /** \brief The list of sources from which to pick archives to
768 * download this package from.
770 pkgSourceList
*Sources
;
772 /** \brief A package records object, used to look up the file
773 * corresponding to each version of the package.
777 /** \brief The md5sum of this package. */
780 /** \brief A location in which the actual filename of the package
783 string
&StoreFilename
;
785 /** \brief The next file for this version to try to download. */
786 pkgCache::VerFileIterator Vf
;
788 /** \brief How many (more) times to try to find a new source from
789 * which to download this package version if it fails.
791 * Set from Acquire::Retries.
793 unsigned int Retries
;
795 /** \brief \b true if this version file is being downloaded from a
800 /** \brief Queue up the next available file for this version. */
805 virtual void Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
);
806 virtual void Done(string Message
,unsigned long Size
,string Md5Hash
,
807 pkgAcquire::MethodConfig
*Cnf
);
808 virtual string
MD5Sum() {return MD5
;};
809 virtual string
DescURI() {return Desc
.URI
;};
810 virtual string
ShortDesc() {return Desc
.ShortDesc
;};
811 virtual void Finished();
813 virtual bool IsTrusted();
815 /** \brief Create a new pkgAcqArchive.
817 * \param Owner The pkgAcquire object with which this item is
820 * \param Sources The sources from which to download version
823 * \param Recs A package records object, used to look up the file
824 * corresponding to each version of the package.
826 * \param Version The package version to download.
828 * \param StoreFilename A location in which the actual filename of
829 * the package should be stored. It will be set to a guessed
830 * basename in the constructor, and filled in with a fully
831 * qualified filename once the download finishes.
833 pkgAcqArchive(pkgAcquire
*Owner
,pkgSourceList
*Sources
,
834 pkgRecords
*Recs
,pkgCache::VerIterator
const &Version
,
835 string
&StoreFilename
);
838 /** \brief Retrieve an arbitrary file to the current directory.
840 * The file is retrieved even if it is accessed via a URL type that
841 * normally is a NOP, such as "file". If the download fails, the
842 * partial file is renamed to get a ".FAILED" extension.
844 class pkgAcqFile
: public pkgAcquire::Item
846 /** \brief The currently active download process. */
847 pkgAcquire::ItemDesc Desc
;
849 /** \brief The md5sum of the file to download, if it is known. */
852 /** \brief How many times to retry the download, set from
855 unsigned int Retries
;
859 // Specialized action members
860 virtual void Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
);
861 virtual void Done(string Message
,unsigned long Size
,string Md5Hash
,
862 pkgAcquire::MethodConfig
*Cnf
);
863 virtual string
MD5Sum() {return Md5Hash
;};
864 virtual string
DescURI() {return Desc
.URI
;};
866 /** \brief Create a new pkgAcqFile object.
868 * \param Owner The pkgAcquire object with which this object is
871 * \param URI The URI to download.
873 * \param MD5 The md5sum of the file to download, if it is known;
876 * \param Size The size of the file to download, if it is known;
879 * \param Desc A description of the file being downloaded.
881 * \param ShortDesc A brief description of the file being
884 * \param DestDir The directory the file should be downloaded into.
886 * \param DestFilename The filename+path the file is downloaded to.
889 * If DestFilename is empty, download to DestDir/<basename> if
890 * DestDir is non-empty, $CWD/<basename> otherwise. If
891 * DestFilename is NOT empty, DestDir is ignored and DestFilename
892 * is the absolute name to which the file should be downloaded.
895 pkgAcqFile(pkgAcquire
*Owner
, string URI
, string MD5
, unsigned long Size
,
896 string Desc
, string ShortDesc
,
897 const string
&DestDir
="", const string
&DestFilename
="");