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 Hash
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>
29 #include <apt-pkg/hashes.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
102 /** \brief The item was could not be downloaded because of
103 * a transient network error (e.g. network down)
105 StatTransientNetworkError
108 /** \brief Contains a textual description of the error encountered
109 * if #Status is #StatError or #StatAuthError.
113 /** \brief The size of the object to fetch. */
114 unsigned long FileSize
;
116 /** \brief How much of the object was already fetched. */
117 unsigned long PartialSize
;
119 /** \brief If not \b NULL, contains the name of a subprocess that
120 * is operating on this object (for instance, "gzip" or "gpgv").
124 /** \brief A client-supplied unique identifier.
126 * This field is initalized to 0; it is meant to be filled in by
127 * clients that wish to use it to uniquely identify items.
129 * \todo it's unused in apt itself
133 /** \brief If \b true, the entire object has been successfully fetched.
135 * Subclasses should set this to \b true when appropriate.
139 /** \brief If \b true, the URI of this object is "local".
141 * The only effect of this field is to exclude the object from the
142 * download progress indicator's overall statistics.
146 /** \brief The number of fetch queues into which this item has been
149 * There is one queue for each source from which an item could be
154 unsigned int QueueCounter
;
156 /** \brief The name of the file into which the retrieved object
161 /** \brief Invoked by the acquire worker when the object couldn't
164 * This is a branch of the continuation of the fetch process.
166 * \param Message An RFC822-formatted message from the acquire
167 * method describing what went wrong. Use LookupTag() to parse
170 * \param Cnf The method via which the worker tried to fetch this object.
174 virtual void Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
);
176 /** \brief Invoked by the acquire worker when the object was
177 * fetched successfully.
179 * Note that the object might \e not have been written to
180 * DestFile; check for the presence of an Alt-Filename entry in
181 * Message to find the file to which it was really written.
183 * Done is often used to switch from one stage of the processing
184 * to the next (e.g. fetching, unpacking, copying). It is one
185 * branch of the continuation of the fetch process.
187 * \param Message Data from the acquire method. Use LookupTag()
189 * \param Size The size of the object that was fetched.
190 * \param Hash The HashSum of the object that was fetched.
191 * \param Cnf The method via which the object was fetched.
195 virtual void Done(string Message
,unsigned long Size
,string Hash
,
196 pkgAcquire::MethodConfig
*Cnf
);
198 /** \brief Invoked when the worker starts to fetch this object.
200 * \param Message RFC822-formatted data from the worker process.
201 * Use LookupTag() to parse it.
203 * \param Size The size of the object being fetched.
207 virtual void Start(string Message
,unsigned long Size
);
209 /** \brief Custom headers to be sent to the fetch process.
211 * \return a string containing RFC822-style headers that are to be
212 * inserted into the 600 URI Acquire message sent to the fetch
213 * subprocess. The headers are inserted after a newline-less
214 * line, so they should (if nonempty) have a leading newline and
215 * no trailing newline.
217 virtual string
Custom600Headers() {return string();};
219 /** \brief A "descriptive" URI-like string.
221 * \return a URI that should be used to describe what is being fetched.
223 virtual string
DescURI() = 0;
224 /** \brief Short item description.
226 * \return a brief description of the object being fetched.
228 virtual string
ShortDesc() {return DescURI();}
230 /** \brief Invoked by the worker when the download is completely done. */
231 virtual void Finished() {};
235 * \return the HashSum of this object, if applicable; otherwise, an
238 virtual string
HashSum() {return string();};
240 /** \return the acquire process with which this item is associated. */
241 pkgAcquire
*GetOwner() {return Owner
;};
243 /** \return \b true if this object is being fetched from a trusted source. */
244 virtual bool IsTrusted() {return false;};
246 /** \brief Initialize an item.
248 * Adds the item to the list of items known to the acquire
249 * process, but does not place it into any fetch queues (you must
250 * manually invoke QueueURI() to do so).
252 * Initializes all fields of the item other than Owner to 0,
253 * false, or the empty string.
255 * \param Owner The new owner of this item.
257 Item(pkgAcquire
*Owner
);
259 /** \brief Remove this item from its owner's queue by invoking
260 * pkgAcquire::Remove.
265 /** \brief Information about an index patch (aka diff). */ /*{{{*/
267 /** The filename of the diff. */
270 /** The sha1 hash of the diff. */
273 /** The size of the diff. */
277 /** \brief An item that is responsible for fetching an index file of {{{
278 * package list diffs and starting the package list's download.
280 * This item downloads the Index file and parses it, then enqueues
281 * additional downloads of either the individual patches (using
282 * pkgAcqIndexDiffs) or the entire Packages file (using pkgAcqIndex).
284 * \sa pkgAcqIndexDiffs, pkgAcqIndex
286 class pkgAcqDiffIndex
: public pkgAcquire::Item
289 /** \brief If \b true, debugging information will be written to std::clog. */
292 /** \brief The item that is currently being downloaded. */
293 pkgAcquire::ItemDesc Desc
;
295 /** \brief The URI of the index file to recreate at our end (either
296 * by downloading it or by applying partial patches).
300 /** \brief The Hash that the real index file should have after
301 * all patches have been applied.
303 HashString ExpectedHash
;
305 /** \brief The index file which will be patched to generate the new
308 string CurrentPackagesFile
;
310 /** \brief A description of the Packages file (stored in
311 * pkgAcquire::ItemDesc::Description).
316 // Specialized action members
317 virtual void Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
);
318 virtual void Done(string Message
,unsigned long Size
,string Md5Hash
,
319 pkgAcquire::MethodConfig
*Cnf
);
320 virtual string
DescURI() {return RealURI
+ "Index";};
321 virtual string
Custom600Headers();
323 /** \brief Parse the Index file for a set of Packages diffs.
325 * Parses the Index file and creates additional download items as
328 * \param IndexDiffFile The name of the Index file.
330 * \return \b true if the Index file was successfully parsed, \b
333 bool ParseDiffIndex(string IndexDiffFile
);
336 /** \brief Create a new pkgAcqDiffIndex.
338 * \param Owner The Acquire object that owns this item.
340 * \param URI The URI of the list file to download.
342 * \param URIDesc A long description of the list file to download.
344 * \param ShortDesc A short description of the list file to download.
346 * \param ExpectedHash The list file's MD5 signature.
348 pkgAcqDiffIndex(pkgAcquire
*Owner
,string URI
,string URIDesc
,
349 string ShortDesc
, HashString ExpectedHash
);
352 /** \brief An item that is responsible for fetching all the patches {{{
353 * that need to be applied to a given package index file.
355 * After downloading and applying a single patch, this item will
356 * enqueue a new pkgAcqIndexDiffs to download and apply the remaining
357 * patches. If no patch can be found that applies to an intermediate
358 * file or if one of the patches cannot be downloaded, falls back to
359 * downloading the entire package index file using pkgAcqIndex.
361 * \sa pkgAcqDiffIndex, pkgAcqIndex
363 class pkgAcqIndexDiffs
: public pkgAcquire::Item
367 /** \brief Queue up the next diff download.
369 * Search for the next available diff that applies to the file
370 * that currently exists on disk, and enqueue it by calling
373 * \return \b true if an applicable diff was found, \b false
376 bool QueueNextDiff();
378 /** \brief Handle tasks that must be performed after the item
379 * finishes downloading.
381 * Dequeues the item and checks the resulting file's md5sum
382 * against ExpectedHash after the last patch was applied.
383 * There is no need to check the md5/sha1 after a "normal"
384 * patch because QueueNextDiff() will check the sha1 later.
386 * \param allDone If \b true, the file was entirely reconstructed,
387 * and its md5sum is verified.
389 void Finish(bool allDone
=false);
393 /** \brief If \b true, debugging output will be written to
398 /** \brief A description of the item that is currently being
401 pkgAcquire::ItemDesc Desc
;
403 /** \brief The URI of the package index file that is being
408 /** \brief The HashSum of the package index file that is being
411 HashString ExpectedHash
;
413 /** A description of the file being downloaded. */
416 /** The patches that remain to be downloaded, including the patch
417 * being downloaded right now. This list should be ordered so
418 * that each diff appears before any diff that depends on it.
420 * \todo These are indexed by sha1sum; why not use some sort of
421 * dictionary instead of relying on ordering and stripping them
424 vector
<DiffInfo
> available_patches
;
425 /** The current status of this patch. */
428 /** \brief The diff is in an unknown state. */
431 /** \brief The diff is currently being fetched. */
434 /** \brief The diff is currently being uncompressed. */
437 /** \brief The diff is currently being applied. */
443 /** \brief Called when the patch file failed to be downloaded.
445 * This method will fall back to downloading the whole index file
446 * outright; its arguments are ignored.
448 virtual void Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
);
450 virtual void Done(string Message
,unsigned long Size
,string Md5Hash
,
451 pkgAcquire::MethodConfig
*Cnf
);
452 virtual string
DescURI() {return RealURI
+ "Index";};
454 /** \brief Create an index diff item.
456 * After filling in its basic fields, this invokes Finish(true) if
457 * #diffs is empty, or QueueNextDiff() otherwise.
459 * \param Owner The pkgAcquire object that owns this item.
461 * \param URI The URI of the package index file being
464 * \param URIDesc A long description of this item.
466 * \param ShortDesc A brief description of this item.
468 * \param ExpectedHash The expected md5sum of the completely
469 * reconstructed package index file; the index file will be tested
470 * against this value when it is entirely reconstructed.
472 * \param diffs The remaining diffs from the index of diffs. They
473 * should be ordered so that each diff appears before any diff
474 * that depends on it.
476 pkgAcqIndexDiffs(pkgAcquire
*Owner
,string URI
,string URIDesc
,
477 string ShortDesc
, HashString ExpectedHash
,
478 vector
<DiffInfo
> diffs
=vector
<DiffInfo
>());
481 /** \brief An acquire item that is responsible for fetching an index {{{
482 * file (e.g., Packages or Sources).
484 * \sa pkgAcqDiffIndex, pkgAcqIndexDiffs, pkgAcqIndexTrans
486 * \todo Why does pkgAcqIndex have protected members?
488 class pkgAcqIndex
: public pkgAcquire::Item
492 /** \brief If \b true, the index file has been decompressed. */
495 /** \brief If \b true, the partially downloaded file will be
496 * removed when the download completes.
500 /** \brief The download request that is currently being
503 pkgAcquire::ItemDesc Desc
;
505 /** \brief The object that is actually being fetched (minus any
506 * compression-related extensions).
510 /** \brief The expected hashsum of the decompressed index file. */
511 HashString ExpectedHash
;
513 /** \brief The compression-related file extension that is being
514 * added to the downloaded file (e.g., ".gz" or ".bz2").
516 string CompressionExtension
;
520 // Specialized action members
521 virtual void Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
);
522 virtual void Done(string Message
,unsigned long Size
,string Md5Hash
,
523 pkgAcquire::MethodConfig
*Cnf
);
524 virtual string
Custom600Headers();
525 virtual string
DescURI() {return RealURI
+ CompressionExtension
;};
526 virtual string
HashSum() {return ExpectedHash
.toStr(); };
528 /** \brief Create a pkgAcqIndex.
530 * \param Owner The pkgAcquire object with which this item is
533 * \param URI The URI of the index file that is to be downloaded.
535 * \param URIDesc A "URI-style" description of this index file.
537 * \param ShortDesc A brief description of this index file.
539 * \param ExpectedHash The expected hashsum of this index file.
541 * \param compressExt The compression-related extension with which
542 * this index file should be downloaded, or "" to autodetect
543 * Compression types can be set with config Acquire::CompressionTypes,
544 * default is ".lzma" or ".bz2" (if the needed binaries are present)
545 * fallback is ".gz" or none.
547 pkgAcqIndex(pkgAcquire
*Owner
,string URI
,string URIDesc
,
548 string ShortDesc
, HashString ExpectedHash
, string compressExt
="");
551 /** \brief An acquire item that is responsible for fetching a {{{
552 * translated index file.
554 * The only difference from pkgAcqIndex is that transient failures
555 * are suppressed: no error occurs if the translated index file is
558 class pkgAcqIndexTrans
: public pkgAcqIndex
562 virtual void Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
);
564 /** \brief Create a pkgAcqIndexTrans.
566 * \param Owner The pkgAcquire object with which this item is
569 * \param URI The URI of the index file that is to be downloaded.
571 * \param URIDesc A "URI-style" description of this index file.
573 * \param ShortDesc A brief description of this index file.
575 pkgAcqIndexTrans(pkgAcquire
*Owner
,string URI
,string URIDesc
,
579 /** \brief Information about an index file. */ /*{{{*/
582 /** \brief A URI from which the index file can be downloaded. */
585 /** \brief A description of the index file. */
588 /** \brief A shorter description of the index file. */
591 /** \brief The key by which this index file should be
592 * looked up within the meta signature file.
597 /** \brief An acquire item that downloads the detached signature {{{
598 * of a meta-index (Release) file, then queues up the release
601 * \todo Why protected members?
603 * \sa pkgAcqMetaIndex
605 class pkgAcqMetaSig
: public pkgAcquire::Item
608 /** \brief The last good signature file */
612 /** \brief The fetch request that is currently being processed. */
613 pkgAcquire::ItemDesc Desc
;
615 /** \brief The URI of the signature file. Unlike Desc.URI, this is
616 * never modified; it is used to determine the file that is being
621 /** \brief The URI of the meta-index file to be fetched after the signature. */
624 /** \brief A "URI-style" description of the meta-index file to be
625 * fetched after the signature.
627 string MetaIndexURIDesc
;
629 /** \brief A brief description of the meta-index file to be fetched
630 * after the signature.
632 string MetaIndexShortDesc
;
634 /** \brief A package-system-specific parser for the meta-index file. */
635 indexRecords
* MetaIndexParser
;
637 /** \brief The index files which should be looked up in the meta-index
638 * and then downloaded.
640 * \todo Why a list of pointers instead of a list of structs?
642 const vector
<struct IndexTarget
*>* IndexTargets
;
646 // Specialized action members
647 virtual void Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
);
648 virtual void Done(string Message
,unsigned long Size
,string Md5Hash
,
649 pkgAcquire::MethodConfig
*Cnf
);
650 virtual string
Custom600Headers();
651 virtual string
DescURI() {return RealURI
; };
653 /** \brief Create a new pkgAcqMetaSig. */
654 pkgAcqMetaSig(pkgAcquire
*Owner
,string URI
,string URIDesc
, string ShortDesc
,
655 string MetaIndexURI
, string MetaIndexURIDesc
, string MetaIndexShortDesc
,
656 const vector
<struct IndexTarget
*>* IndexTargets
,
657 indexRecords
* MetaIndexParser
);
660 /** \brief An item that is responsible for downloading the meta-index {{{
661 * file (i.e., Release) itself and verifying its signature.
663 * Once the download and verification are complete, the downloads of
664 * the individual index files are queued up using pkgAcqDiffIndex.
665 * If the meta-index file had a valid signature, the expected hashsums
666 * of the index files will be the md5sums listed in the meta-index;
667 * otherwise, the expected hashsums will be "" (causing the
668 * authentication of the index files to be bypassed).
670 class pkgAcqMetaIndex
: public pkgAcquire::Item
673 /** \brief The fetch command that is currently being processed. */
674 pkgAcquire::ItemDesc Desc
;
676 /** \brief The URI that is actually being downloaded; never
677 * modified by pkgAcqMetaIndex.
681 /** \brief The file in which the signature for this index was stored.
683 * If empty, the signature and the md5sums of the individual
684 * indices will not be checked.
688 /** \brief The index files to download. */
689 const vector
<struct IndexTarget
*>* IndexTargets
;
691 /** \brief The parser for the meta-index file. */
692 indexRecords
* MetaIndexParser
;
694 /** \brief If \b true, the index's signature is currently being verified.
697 // required to deal gracefully with problems caused by incorrect ims hits
700 /** \brief Check that the release file is a release file for the
701 * correct distribution.
703 * \return \b true if no fatal errors were encountered.
705 bool VerifyVendor(string Message
);
707 /** \brief Called when a file is finished being retrieved.
709 * If the file was not downloaded to DestFile, a copy process is
710 * set up to copy it to DestFile; otherwise, Complete is set to \b
711 * true and the file is moved to its final location.
713 * \param Message The message block received from the fetch
716 void RetrievalDone(string Message
);
718 /** \brief Called when authentication succeeded.
720 * Sanity-checks the authenticated file, queues up the individual
721 * index files for download, and saves the signature in the lists
722 * directory next to the authenticated list file.
724 * \param Message The message block received from the fetch
727 void AuthDone(string Message
);
729 /** \brief Starts downloading the individual index files.
731 * \param verify If \b true, only indices whose expected hashsum
732 * can be determined from the meta-index will be downloaded, and
733 * the hashsums of indices will be checked (reporting
734 * #StatAuthError if there is a mismatch). If verify is \b false,
735 * no hashsum checking will be performed.
737 void QueueIndexes(bool verify
);
741 // Specialized action members
742 virtual void Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
);
743 virtual void Done(string Message
,unsigned long Size
, string Hash
,
744 pkgAcquire::MethodConfig
*Cnf
);
745 virtual string
Custom600Headers();
746 virtual string
DescURI() {return RealURI
; };
748 /** \brief Create a new pkgAcqMetaIndex. */
749 pkgAcqMetaIndex(pkgAcquire
*Owner
,
750 string URI
,string URIDesc
, string ShortDesc
,
752 const vector
<struct IndexTarget
*>* IndexTargets
,
753 indexRecords
* MetaIndexParser
);
756 /** \brief An item that is responsible for fetching a package file. {{{
758 * If the package file already exists in the cache, nothing will be
761 class pkgAcqArchive
: public pkgAcquire::Item
764 /** \brief The package version being fetched. */
765 pkgCache::VerIterator Version
;
767 /** \brief The fetch command that is currently being processed. */
768 pkgAcquire::ItemDesc Desc
;
770 /** \brief The list of sources from which to pick archives to
771 * download this package from.
773 pkgSourceList
*Sources
;
775 /** \brief A package records object, used to look up the file
776 * corresponding to each version of the package.
780 /** \brief The hashsum of this package. */
781 HashString ExpectedHash
;
783 /** \brief A location in which the actual filename of the package
786 string
&StoreFilename
;
788 /** \brief The next file for this version to try to download. */
789 pkgCache::VerFileIterator Vf
;
791 /** \brief How many (more) times to try to find a new source from
792 * which to download this package version if it fails.
794 * Set from Acquire::Retries.
796 unsigned int Retries
;
798 /** \brief \b true if this version file is being downloaded from a
803 /** \brief Queue up the next available file for this version. */
808 virtual void Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
);
809 virtual void Done(string Message
,unsigned long Size
,string Hash
,
810 pkgAcquire::MethodConfig
*Cnf
);
811 virtual string
DescURI() {return Desc
.URI
;};
812 virtual string
ShortDesc() {return Desc
.ShortDesc
;};
813 virtual void Finished();
814 virtual string
HashSum() {return ExpectedHash
.toStr(); };
815 virtual bool IsTrusted();
817 /** \brief Create a new pkgAcqArchive.
819 * \param Owner The pkgAcquire object with which this item is
822 * \param Sources The sources from which to download version
825 * \param Recs A package records object, used to look up the file
826 * corresponding to each version of the package.
828 * \param Version The package version to download.
830 * \param StoreFilename A location in which the actual filename of
831 * the package should be stored. It will be set to a guessed
832 * basename in the constructor, and filled in with a fully
833 * qualified filename once the download finishes.
835 pkgAcqArchive(pkgAcquire
*Owner
,pkgSourceList
*Sources
,
836 pkgRecords
*Recs
,pkgCache::VerIterator
const &Version
,
837 string
&StoreFilename
);
840 /** \brief Retrieve an arbitrary file to the current directory. {{{
842 * The file is retrieved even if it is accessed via a URL type that
843 * normally is a NOP, such as "file". If the download fails, the
844 * partial file is renamed to get a ".FAILED" extension.
846 class pkgAcqFile
: public pkgAcquire::Item
848 /** \brief The currently active download process. */
849 pkgAcquire::ItemDesc Desc
;
851 /** \brief The hashsum of the file to download, if it is known. */
852 HashString ExpectedHash
;
854 /** \brief How many times to retry the download, set from
857 unsigned int Retries
;
861 // Specialized action members
862 virtual void Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
);
863 virtual void Done(string Message
,unsigned long Size
,string CalcHash
,
864 pkgAcquire::MethodConfig
*Cnf
);
865 virtual string
DescURI() {return Desc
.URI
;};
866 virtual string
HashSum() {return ExpectedHash
.toStr(); };
868 /** \brief Create a new pkgAcqFile object.
870 * \param Owner The pkgAcquire object with which this object is
873 * \param URI The URI to download.
875 * \param Hash The hashsum of the file to download, if it is known;
878 * \param Size The size of the file to download, if it is known;
881 * \param Desc A description of the file being downloaded.
883 * \param ShortDesc A brief description of the file being
886 * \param DestDir The directory the file should be downloaded into.
888 * \param DestFilename The filename+path the file is downloaded to.
891 * If DestFilename is empty, download to DestDir/<basename> if
892 * DestDir is non-empty, $CWD/<basename> otherwise. If
893 * DestFilename is NOT empty, DestDir is ignored and DestFilename
894 * is the absolute name to which the file should be downloaded.
897 pkgAcqFile(pkgAcquire
*Owner
, string URI
, string Hash
, unsigned long Size
,
898 string Desc
, string ShortDesc
,
899 const string
&DestDir
="", const string
&DestFilename
="");