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 /** \addtogroup acquire
34 * \file acquire-item.h
37 /** \brief Represents the process by which a pkgAcquire object should
38 * retrieve a file or a collection of files.
40 * By convention, Item subclasses should insert themselves into the
41 * acquire queue when they are created by calling QueueURI(), and
42 * remove themselves by calling Dequeue() when either Done() or
43 * Failed() is invoked. Item objects are also responsible for
44 * notifying the download progress indicator (accessible via
45 * #Owner->Log) of their status.
49 class pkgAcquire::Item
53 /** \brief The acquire object with which this item is associated. */
56 /** \brief Insert this item into its owner's queue.
58 * \param ItemDesc Metadata about this item (its URI and
61 inline void QueueURI(ItemDesc
&Item
)
62 {Owner
->Enqueue(Item
);};
64 /** \brief Remove this item from its owner's queue. */
65 inline void Dequeue() {Owner
->Dequeue(this);};
67 /** \brief Rename a file without modifying its timestamp.
69 * Many item methods call this as their final action.
71 * \param From The file to be renamed.
73 * \param To The new name of #From. If #To exists it will be
76 void Rename(string From
,string To
);
80 /** \brief The current status of this item. */
83 /** \brief The item is waiting to be downloaded. */
86 /** \brief The item is currently being downloaded. */
89 /** \brief The item has been successfully downloaded. */
92 /** \brief An error was encountered while downloading this
97 /** \brief The item was downloaded but its authenticity could
103 /** \brief Contains a textual description of the error encountered
104 * if #Status is #StatError or #StatAuthError.
108 /** \brief The size of the object to fetch. */
109 unsigned long FileSize
;
111 /** \brief How much of the object was already fetched. */
112 unsigned long PartialSize
;
114 /** \brief If not \b NULL, contains the name of a subprocess that
115 * is operating on this object (for instance, "gzip" or "gpgv").
119 /** \brief A client-supplied unique identifier.
121 * This field is initalized to 0; it is meant to be filled in by
122 * clients that wish to use it to uniquely identify items.
124 * \todo it's unused in apt itself
128 /** \brief If \b true, the entire object has been successfully fetched.
130 * Subclasses should set this to \b true when appropriate.
134 /** \brief If \b true, the URI of this object is "local".
136 * The only effect of this field is to exclude the object from the
137 * download progress indicator's overall statistics.
141 /** \brief The number of fetch queues into which this item has been
144 * There is one queue for each source from which an item could be
149 unsigned int QueueCounter
;
151 /** \brief The name of the file into which the retrieved object
156 /** \brief Invoked by the acquire worker when the object couldn't
159 * This is a branch of the continuation of the fetch process.
161 * \param Message An RFC822-formatted message from the acquire
162 * method describing what went wrong. Use LookupTag() to parse
165 * \param Cnf The method via which the worker tried to fetch this object.
169 virtual void Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
);
171 /** \brief Invoked by the acquire worker when the object was
172 * fetched successfully.
174 * Note that the object might \e not have been written to
175 * DestFile; check for the presence of an Alt-Filename entry in
176 * Message to find the file to which it was really written.
178 * Done is often used to switch from one stage of the processing
179 * to the next (e.g. fetching, unpacking, copying). It is one
180 * branch of the continuation of the fetch process.
182 * \param Message Data from the acquire method. Use LookupTag()
184 * \param Size The size of the object that was fetched.
185 * \param Md5Hash The MD5Sum of the object that was fetched.
186 * \param Cnf The method via which the object was fetched.
190 virtual void Done(string Message
,unsigned long Size
,string Md5Hash
,
191 pkgAcquire::MethodConfig
*Cnf
);
193 /** \brief Invoked when the worker starts to fetch this object.
195 * \param Message RFC822-formatted data from the worker process.
196 * Use LookupTag() to parse it.
198 * \param Size The size of the object being fetched.
202 virtual void Start(string Message
,unsigned long Size
);
204 /** \brief Custom headers to be sent to the fetch process.
206 * \return a string containing RFC822-style headers that are to be
207 * inserted into the 600 URI Acquire message sent to the fetch
208 * subprocess. The headers are inserted after a newline-less
209 * line, so they should (if nonempty) have a leading newline and
210 * no trailing newline.
212 virtual string
Custom600Headers() {return string();};
214 /** \brief A "descriptive" URI-like string.
216 * \return a URI that should be used to describe what is being fetched.
218 virtual string
DescURI() = 0;
219 /** \brief Short item description.
221 * \return a brief description of the object being fetched.
223 virtual string
ShortDesc() {return DescURI();}
225 /** \brief Invoked by the worker when the download is completely done. */
226 virtual void Finished() {};
230 * \return the MD5Sum of this object, if applicable; otherwise, an
233 virtual string
MD5Sum() {return string();};
235 /** \return the acquire process with which this item is associated. */
236 pkgAcquire
*GetOwner() {return Owner
;};
238 /** \return \b true if this object is being fetched from a trusted source. */
239 virtual bool IsTrusted() {return false;};
241 /** \brief Initialize an item.
243 * Adds the item to the list of items known to the acquire
244 * process, but does not place it into any fetch queues (you must
245 * manually invoke QueueURI() to do so).
247 * Initializes all fields of the item other than Owner to 0,
248 * false, or the empty string.
250 * \param Owner The new owner of this item.
252 Item(pkgAcquire
*Owner
);
254 /** \brief Remove this item from its owner's queue by invoking
255 * pkgAcquire::Remove.
260 /** \brief Information about an index patch (aka diff). */
262 /** The filename of the diff. */
265 /** The sha1 hash of the diff. */
268 /** The size of the diff. */
272 /** \brief An item that is responsible for fetching an index file of
273 * package list diffs and starting the package list's download.
275 * This item downloads the Index file and parses it, then enqueues
276 * additional downloads of either the individual patches (using
277 * pkgAcqIndexDiffs) or the entire Packages file (using pkgAcqIndex).
279 * \sa pkgAcqIndexDiffs, pkgAcqIndex
281 class pkgAcqDiffIndex
: public pkgAcquire::Item
284 /** \brief If \b true, debugging information will be written to std::clog. */
287 /** \brief The item that is currently being downloaded. */
288 pkgAcquire::ItemDesc Desc
;
290 /** \brief The URI of the index file to recreate at our end (either
291 * by downloading it or by applying partial patches).
295 /** \brief The MD5Sum that the real index file should have after
296 * all patches have been applied.
300 /** \brief The index file which will be patched to generate the new
303 string CurrentPackagesFile
;
305 /** \brief A description of the Packages file (stored in
306 * pkgAcquire::ItemDesc::Description).
311 // Specialized action members
312 virtual void Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
);
313 virtual void Done(string Message
,unsigned long Size
,string Md5Hash
,
314 pkgAcquire::MethodConfig
*Cnf
);
315 virtual string
DescURI() {return RealURI
+ "Index";};
316 virtual string
Custom600Headers();
318 /** \brief Parse the Index file for a set of Packages diffs.
320 * Parses the Index file and creates additional download items as
323 * \param IndexDiffFile The name of the Index file.
325 * \return \b true if the Index file was successfully parsed, \b
328 bool ParseDiffIndex(string IndexDiffFile
);
331 /** \brief Create a new pkgAcqDiffIndex.
333 * \param Owner The Acquire object that owns this item.
335 * \param URI The URI of the list file to download.
337 * \param URIDesc A long description of the list file to download.
339 * \param ShortDesc A short description of the list file to download.
341 * \param ExpectedMD5 The list file's MD5 signature.
343 pkgAcqDiffIndex(pkgAcquire
*Owner
,string URI
,string URIDesc
,
344 string ShortDesc
, string ExpectedMD5
);
347 /** \brief An item that is responsible for fetching all the patches
348 * that need to be applied to a given package index file.
350 * After downloading and applying a single patch, this item will
351 * enqueue a new pkgAcqIndexDiffs to download and apply the remaining
352 * patches. If no patch can be found that applies to an intermediate
353 * file or if one of the patches cannot be downloaded, falls back to
354 * downloading the entire package index file using pkgAcqIndex.
356 * \sa pkgAcqDiffIndex, pkgAcqIndex
358 class pkgAcqIndexDiffs
: public pkgAcquire::Item
362 /** \brief Queue up the next diff download.
364 * Search for the next available diff that applies to the file
365 * that currently exists on disk, and enqueue it by calling
368 * \return \b true if an applicable diff was found, \b false
371 bool QueueNextDiff();
373 /** \brief Handle tasks that must be performed after the item
374 * finishes downloading.
376 * Dequeues the item and checks the resulting file's md5sum
377 * against ExpectedMD5 after the last patch was applied.
378 * There is no need to check the md5/sha1 after a "normal"
379 * patch because QueueNextDiff() will check the sha1 later.
381 * \param allDone If \b true, the file was entirely reconstructed,
382 * and its md5sum is verified.
384 void Finish(bool allDone
=false);
388 /** \brief If \b true, debugging output will be written to
393 /** \brief A description of the item that is currently being
396 pkgAcquire::ItemDesc Desc
;
398 /** \brief The URI of the package index file that is being
403 /** \brief The MD5Sum of the package index file that is being
408 /** A description of the file being downloaded. */
411 /** The patches that remain to be downloaded, including the patch
412 * being downloaded right now. This list should be ordered so
413 * that each diff appears before any diff that depends on it.
415 * \todo These are indexed by sha1sum; why not use some sort of
416 * dictionary instead of relying on ordering and stripping them
419 vector
<DiffInfo
> available_patches
;
420 /** The current status of this patch. */
423 /** \brief The diff is in an unknown state. */
426 /** \brief The diff is currently being fetched. */
429 /** \brief The diff is currently being uncompressed. */
432 /** \brief The diff is currently being applied. */
438 /** \brief Called when the patch file failed to be downloaded.
440 * This method will fall back to downloading the whole index file
441 * outright; its arguments are ignored.
443 virtual void Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
);
445 virtual void Done(string Message
,unsigned long Size
,string Md5Hash
,
446 pkgAcquire::MethodConfig
*Cnf
);
447 virtual string
DescURI() {return RealURI
+ "Index";};
449 /** \brief Create an index diff item.
451 * After filling in its basic fields, this invokes Finish(true) if
452 * #diffs is empty, or QueueNextDiff() otherwise.
454 * \param Owner The pkgAcquire object that owns this item.
456 * \param URI The URI of the package index file being
459 * \param URIDesc A long description of this item.
461 * \param ShortDesc A brief description of this item.
463 * \param ExpectedMD5 The expected md5sum of the completely
464 * reconstructed package index file; the index file will be tested
465 * against this value when it is entirely reconstructed.
467 * \param diffs The remaining diffs from the index of diffs. They
468 * should be ordered so that each diff appears before any diff
469 * that depends on it.
471 pkgAcqIndexDiffs(pkgAcquire
*Owner
,string URI
,string URIDesc
,
472 string ShortDesc
, string ExpectedMD5
,
473 vector
<DiffInfo
> diffs
=vector
<DiffInfo
>());
476 /** \brief An acquire item that is responsible for fetching an index
477 * file (e.g., Packages or Sources).
479 * \sa pkgAcqDiffIndex, pkgAcqIndexDiffs, pkgAcqIndexTrans
481 * \todo Why does pkgAcqIndex have protected members?
483 class pkgAcqIndex
: public pkgAcquire::Item
487 /** \brief If \b true, the index file has been decompressed. */
490 /** \brief If \b true, the partially downloaded file will be
491 * removed when the download completes.
495 /** \brief The download request that is currently being
498 pkgAcquire::ItemDesc Desc
;
500 /** \brief The object that is actually being fetched (minus any
501 * compression-related extensions).
505 /** \brief The expected md5sum of the decompressed index file. */
508 /** \brief The compression-related file extension that is being
509 * added to the downloaded file (e.g., ".gz" or ".bz2").
511 string CompressionExtension
;
515 // Specialized action members
516 virtual void Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
);
517 virtual void Done(string Message
,unsigned long Size
,string Md5Hash
,
518 pkgAcquire::MethodConfig
*Cnf
);
519 virtual string
Custom600Headers();
520 virtual string
DescURI() {return RealURI
+ CompressionExtension
;};
522 /** \brief Create a pkgAcqIndex.
524 * \param Owner The pkgAcquire object with which this item is
527 * \param URI The URI of the index file that is to be downloaded.
529 * \param URIDesc A "URI-style" description of this index file.
531 * \param ShortDesc A brief description of this index file.
533 * \param ExpectedMD5 The expected md5sum of this index file.
535 * \param compressExt The compression-related extension with which
536 * this index file should be downloaded, or "" to autodetect
537 * (".bz2" is used if bzip2 is installed, ".gz" otherwise).
539 pkgAcqIndex(pkgAcquire
*Owner
,string URI
,string URIDesc
,
540 string ShortDesc
, string ExpectedMD5
, string compressExt
="");
543 /** \brief An acquire item that is responsible for fetching a
544 * translated index file.
546 * The only difference from pkgAcqIndex is that transient failures
547 * are suppressed: no error occurs if the translated index file is
550 class pkgAcqIndexTrans
: public pkgAcqIndex
554 virtual void Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
);
556 /** \brief Create a pkgAcqIndexTrans.
558 * \param Owner The pkgAcquire object with which this item is
561 * \param URI The URI of the index file that is to be downloaded.
563 * \param URIDesc A "URI-style" description of this index file.
565 * \param ShortDesc A brief description of this index file.
567 * \param ExpectedMD5 The expected md5sum of this index file.
569 * \param compressExt The compression-related extension with which
570 * this index file should be downloaded, or "" to autodetect
571 * (".bz2" is used if bzip2 is installed, ".gz" otherwise).
573 pkgAcqIndexTrans(pkgAcquire
*Owner
,string URI
,string URIDesc
,
577 /** \brief Information about an index file. */
580 /** \brief A URI from which the index file can be downloaded. */
583 /** \brief A description of the index file. */
586 /** \brief A shorter description of the index file. */
589 /** \brief The key by which this index file should be
590 * looked up within the meta signature file.
595 /** \brief An acquire item that downloads the detached signature
596 * of a meta-index (Release) file, then queues up the release
599 * \todo Why protected members?
601 * \sa pkgAcqMetaIndex
603 class pkgAcqMetaSig
: public pkgAcquire::Item
606 /** \brief The fetch request that is currently being processed. */
607 pkgAcquire::ItemDesc Desc
;
609 /** \brief The URI of the signature file. Unlike Desc.URI, this is
610 * never modified; it is used to determine the file that is being
615 /** \brief The URI of the meta-index file to be fetched after the signature. */
618 /** \brief A "URI-style" description of the meta-index file to be
619 * fetched after the signature.
621 string MetaIndexURIDesc
;
623 /** \brief A brief description of the meta-index file to be fetched
624 * after the signature.
626 string MetaIndexShortDesc
;
628 /** \brief A package-system-specific parser for the meta-index file. */
629 indexRecords
* MetaIndexParser
;
631 /** \brief The index files which should be looked up in the meta-index
632 * and then downloaded.
634 * \todo Why a list of pointers instead of a list of structs?
636 const vector
<struct IndexTarget
*>* IndexTargets
;
640 // Specialized action members
641 virtual void Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
);
642 virtual void Done(string Message
,unsigned long Size
,string Md5Hash
,
643 pkgAcquire::MethodConfig
*Cnf
);
644 virtual string
Custom600Headers();
645 virtual string
DescURI() {return RealURI
; };
647 /** \brief Create a new pkgAcqMetaSig. */
648 pkgAcqMetaSig(pkgAcquire
*Owner
,string URI
,string URIDesc
, string ShortDesc
,
649 string MetaIndexURI
, string MetaIndexURIDesc
, string MetaIndexShortDesc
,
650 const vector
<struct IndexTarget
*>* IndexTargets
,
651 indexRecords
* MetaIndexParser
);
654 /** \brief An item that is responsible for downloading the meta-index
655 * file (i.e., Release) itself and verifying its signature.
657 * Once the download and verification are complete, the downloads of
658 * the individual index files are queued up using pkgAcqDiffIndex.
659 * If the meta-index file had a valid signature, the expected md5sums
660 * of the index files will be the md5sums listed in the meta-index;
661 * otherwise, the expected md5sums will be "" (causing the
662 * authentication of the index files to be bypassed).
664 class pkgAcqMetaIndex
: public pkgAcquire::Item
667 /** \brief The fetch command that is currently being processed. */
668 pkgAcquire::ItemDesc Desc
;
670 /** \brief The URI that is actually being downloaded; never
671 * modified by pkgAcqMetaIndex.
675 /** \brief The file in which the signature for this index was stored.
677 * If empty, the signature and the md5sums of the individual
678 * indices will not be checked.
682 /** \brief The index files to download. */
683 const vector
<struct IndexTarget
*>* IndexTargets
;
685 /** \brief The parser for the meta-index file. */
686 indexRecords
* MetaIndexParser
;
688 /** \brief If \b true, the index's signature is currently being verified.
691 // required to deal gracefully with problems caused by incorrect ims hits
694 /** \brief Check that the release file is a release file for the
695 * correct distribution.
697 * \return \b true if no fatal errors were encountered.
699 bool VerifyVendor(string Message
);
701 /** \brief Called when a file is finished being retrieved.
703 * If the file was not downloaded to DestFile, a copy process is
704 * set up to copy it to DestFile; otherwise, Complete is set to \b
705 * true and the file is moved to its final location.
707 * \param Message The message block received from the fetch
710 void RetrievalDone(string Message
);
712 /** \brief Called when authentication succeeded.
714 * Sanity-checks the authenticated file, queues up the individual
715 * index files for download, and saves the signature in the lists
716 * directory next to the authenticated list file.
718 * \param Message The message block received from the fetch
721 void AuthDone(string Message
);
723 /** \brief Starts downloading the individual index files.
725 * \param verify If \b true, only indices whose expected md5sum
726 * can be determined from the meta-index will be downloaded, and
727 * the md5sums of indices will be checked (reporting
728 * #StatAuthError if there is a mismatch). If verify is \b false,
729 * no md5sum checking will be performed.
731 void QueueIndexes(bool verify
);
735 // Specialized action members
736 virtual void Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
);
737 virtual void Done(string Message
,unsigned long Size
,string Md5Hash
,
738 pkgAcquire::MethodConfig
*Cnf
);
739 virtual string
Custom600Headers();
740 virtual string
DescURI() {return RealURI
; };
742 /** \brief Create a new pkgAcqMetaIndex. */
743 pkgAcqMetaIndex(pkgAcquire
*Owner
,
744 string URI
,string URIDesc
, string ShortDesc
,
746 const vector
<struct IndexTarget
*>* IndexTargets
,
747 indexRecords
* MetaIndexParser
);
750 /** \brief An item that is responsible for fetching a package file.
752 * If the package file already exists in the cache, nothing will be
755 class pkgAcqArchive
: public pkgAcquire::Item
758 /** \brief The package version being fetched. */
759 pkgCache::VerIterator Version
;
761 /** \brief The fetch command that is currently being processed. */
762 pkgAcquire::ItemDesc Desc
;
764 /** \brief The list of sources from which to pick archives to
765 * download this package from.
767 pkgSourceList
*Sources
;
769 /** \brief A package records object, used to look up the file
770 * corresponding to each version of the package.
774 /** \brief The md5sum of this package. */
777 /** \brief A location in which the actual filename of the package
780 string
&StoreFilename
;
782 /** \brief The next file for this version to try to download. */
783 pkgCache::VerFileIterator Vf
;
785 /** \brief How many (more) times to try to find a new source from
786 * which to download this package version if it fails.
788 * Set from Acquire::Retries.
790 unsigned int Retries
;
792 /** \brief \b true if this version file is being downloaded from a
797 /** \brief Queue up the next available file for this version. */
802 virtual void Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
);
803 virtual void Done(string Message
,unsigned long Size
,string Md5Hash
,
804 pkgAcquire::MethodConfig
*Cnf
);
805 virtual string
MD5Sum() {return MD5
;};
806 virtual string
DescURI() {return Desc
.URI
;};
807 virtual string
ShortDesc() {return Desc
.ShortDesc
;};
808 virtual void Finished();
810 virtual bool IsTrusted();
812 /** \brief Create a new pkgAcqArchive.
814 * \param Owner The pkgAcquire object with which this item is
817 * \param Sources The sources from which to download version
820 * \param Recs A package records object, used to look up the file
821 * corresponding to each version of the package.
823 * \param Version The package version to download.
825 * \param StoreFilename A location in which the actual filename of
826 * the package should be stored. It will be set to a guessed
827 * basename in the constructor, and filled in with a fully
828 * qualified filename once the download finishes.
830 pkgAcqArchive(pkgAcquire
*Owner
,pkgSourceList
*Sources
,
831 pkgRecords
*Recs
,pkgCache::VerIterator
const &Version
,
832 string
&StoreFilename
);
835 /** \brief Retrieve an arbitrary file to the current directory.
837 * The file is retrieved even if it is accessed via a URL type that
838 * normally is a NOP, such as "file". If the download fails, the
839 * partial file is renamed to get a ".FAILED" extension.
841 class pkgAcqFile
: public pkgAcquire::Item
843 /** \brief The currently active download process. */
844 pkgAcquire::ItemDesc Desc
;
846 /** \brief The md5sum of the file to download, if it is known. */
849 /** \brief How many times to retry the download, set from
852 unsigned int Retries
;
856 // Specialized action members
857 virtual void Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
);
858 virtual void Done(string Message
,unsigned long Size
,string Md5Hash
,
859 pkgAcquire::MethodConfig
*Cnf
);
860 virtual string
MD5Sum() {return Md5Hash
;};
861 virtual string
DescURI() {return Desc
.URI
;};
863 /** \brief Create a new pkgAcqFile object.
865 * \param Owner The pkgAcquire object with which this object is
868 * \param URI The URI to download.
870 * \param MD5 The md5sum of the file to download, if it is known;
873 * \param Size The size of the file to download, if it is known;
876 * \param Desc A description of the file being downloaded.
878 * \param ShortDesc A brief description of the file being
881 * \param DestDir The directory the file should be downloaded into.
883 * \param DestFilename The filename+path the file is downloaded to.
886 * If DestFilename is empty, download to DestDir/<basename> if
887 * DestDir is non-empty, $CWD/<basename> otherwise. If
888 * DestFilename is NOT empty, DestDir is ignored and DestFilename
889 * is the absolute name to which the file should be downloaded.
892 pkgAcqFile(pkgAcquire
*Owner
, string URI
, string MD5
, unsigned long Size
,
893 string Desc
, string ShortDesc
,
894 const string
&DestDir
="", const string
&DestFilename
="");