]> git.saurik.com Git - apt.git/blob - apt-pkg/acquire-item.h
refactor and add pkgAcqIndex::ValidateFile()
[apt.git] / apt-pkg / acquire-item.h
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: acquire-item.h,v 1.26.2.3 2004/01/02 18:51:00 mdz Exp $
4 /* ######################################################################
5
6 Acquire Item - Item to acquire
7
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.
11
12 Three item classes are provided to provide functionality for
13 downloading of Index, Translation and Packages files.
14
15 A Archive class is provided for downloading .deb files. It does Hash
16 checking and source location as well as a retry algorithm.
17
18 ##################################################################### */
19 /*}}}*/
20 #ifndef PKGLIB_ACQUIRE_ITEM_H
21 #define PKGLIB_ACQUIRE_ITEM_H
22
23 #include <apt-pkg/acquire.h>
24 #include <apt-pkg/hashes.h>
25 #include <apt-pkg/weakptr.h>
26 #include <apt-pkg/pkgcache.h>
27 #include <apt-pkg/cacheiterators.h>
28
29 #include <string>
30 #include <vector>
31
32 #ifndef APT_8_CLEANER_HEADERS
33 #include <apt-pkg/indexfile.h>
34 #include <apt-pkg/vendor.h>
35 #include <apt-pkg/sourcelist.h>
36 #include <apt-pkg/pkgrecords.h>
37 #include <apt-pkg/indexrecords.h>
38 #endif
39
40 /** \addtogroup acquire
41 * @{
42 *
43 * \file acquire-item.h
44 */
45
46 class indexRecords;
47 class pkgRecords;
48 class pkgSourceList;
49 class IndexTarget;
50 class pkgAcqMetaBase;
51
52 /** \brief Represents the process by which a pkgAcquire object should {{{
53 * retrieve a file or a collection of files.
54 *
55 * By convention, Item subclasses should insert themselves into the
56 * acquire queue when they are created by calling QueueURI(), and
57 * remove themselves by calling Dequeue() when either Done() or
58 * Failed() is invoked. Item objects are also responsible for
59 * notifying the download progress indicator (accessible via
60 * #Owner->Log) of their status.
61 *
62 * \see pkgAcquire
63 */
64 class pkgAcquire::Item : public WeakPointable
65 {
66 void *d;
67
68 protected:
69
70 /** \brief The acquire object with which this item is associated. */
71 pkgAcquire *Owner;
72
73 /** \brief Insert this item into its owner's queue.
74 *
75 * \param Item Metadata about this item (its URI and
76 * description).
77 */
78 inline void QueueURI(ItemDesc &Item)
79 {Owner->Enqueue(Item);};
80
81 /** \brief Remove this item from its owner's queue. */
82 inline void Dequeue() {Owner->Dequeue(this);};
83
84 /** \brief Rename a file without modifying its timestamp.
85 *
86 * Many item methods call this as their final action.
87 *
88 * \param From The file to be renamed.
89 *
90 * \param To The new name of \a From. If \a To exists it will be
91 * overwritten.
92 */
93 bool Rename(std::string From,std::string To);
94
95 public:
96
97 /** \brief The current status of this item. */
98 enum ItemState
99 {
100 /** \brief The item is waiting to be downloaded. */
101 StatIdle,
102
103 /** \brief The item is currently being downloaded. */
104 StatFetching,
105
106 /** \brief The item has been successfully downloaded. */
107 StatDone,
108
109 /** \brief An error was encountered while downloading this
110 * item.
111 */
112 StatError,
113
114 /** \brief The item was downloaded but its authenticity could
115 * not be verified.
116 */
117 StatAuthError,
118
119 /** \brief The item was could not be downloaded because of
120 * a transient network error (e.g. network down)
121 */
122 StatTransientNetworkError,
123 } Status;
124
125 /** \brief Contains a textual description of the error encountered
126 * if #ItemState is #StatError or #StatAuthError.
127 */
128 std::string ErrorText;
129
130 /** \brief The size of the object to fetch. */
131 unsigned long long FileSize;
132
133 /** \brief How much of the object was already fetched. */
134 unsigned long long PartialSize;
135
136 /** \brief If not \b NULL, contains the name of a subprocess that
137 * is operating on this object (for instance, "gzip" or "gpgv").
138 */
139 APT_DEPRECATED const char *Mode;
140
141 /** \brief contains the name of the subprocess that is operating on this object
142 * (for instance, "gzip", "rred" or "gpgv"). This is obsoleting #Mode from above
143 * as it can manage the lifetime of included string properly. */
144 std::string ActiveSubprocess;
145
146 /** \brief A client-supplied unique identifier.
147 *
148 * This field is initalized to 0; it is meant to be filled in by
149 * clients that wish to use it to uniquely identify items.
150 *
151 * \todo it's unused in apt itself
152 */
153 unsigned long ID;
154
155 /** \brief If \b true, the entire object has been successfully fetched.
156 *
157 * Subclasses should set this to \b true when appropriate.
158 */
159 bool Complete;
160
161 /** \brief If \b true, the URI of this object is "local".
162 *
163 * The only effect of this field is to exclude the object from the
164 * download progress indicator's overall statistics.
165 */
166 bool Local;
167 std::string UsedMirror;
168
169 /** \brief The number of fetch queues into which this item has been
170 * inserted.
171 *
172 * There is one queue for each source from which an item could be
173 * downloaded.
174 *
175 * \sa pkgAcquire
176 */
177 unsigned int QueueCounter;
178
179 /** \brief TransactionManager */
180 pkgAcqMetaBase *TransactionManager;
181
182 /** \brief The number of additional fetch items that are expected
183 * once this item is done.
184 *
185 * Some items like pkgAcqMeta{Index,Sig} will queue additional
186 * items. This variable can be set by the methods if it knows
187 * in advance how many items to expect to get a more accurate
188 * progress.
189 */
190 unsigned int ExpectedAdditionalItems;
191
192 /** \brief The name of the file into which the retrieved object
193 * will be written.
194 */
195 std::string DestFile;
196
197 /** \brief storge name until a transaction is finished */
198 std::string PartialFile;
199
200 /** \brief Invoked by the acquire worker when the object couldn't
201 * be fetched.
202 *
203 * This is a branch of the continuation of the fetch process.
204 *
205 * \param Message An RFC822-formatted message from the acquire
206 * method describing what went wrong. Use LookupTag() to parse
207 * it.
208 *
209 * \param Cnf The method via which the worker tried to fetch this object.
210 *
211 * \sa pkgAcqMethod
212 */
213 virtual void Failed(std::string Message,pkgAcquire::MethodConfig *Cnf);
214
215 /** \brief Invoked by the acquire worker when the object was
216 * fetched successfully.
217 *
218 * Note that the object might \e not have been written to
219 * DestFile; check for the presence of an Alt-Filename entry in
220 * Message to find the file to which it was really written.
221 *
222 * Done is often used to switch from one stage of the processing
223 * to the next (e.g. fetching, unpacking, copying). It is one
224 * branch of the continuation of the fetch process.
225 *
226 * \param Message Data from the acquire method. Use LookupTag()
227 * to parse it.
228 * \param Size The size of the object that was fetched.
229 * \param Hashes The HashSums of the object that was fetched.
230 * \param Cnf The method via which the object was fetched.
231 *
232 * \sa pkgAcqMethod
233 */
234 virtual void Done(std::string Message, unsigned long long Size, HashStringList const &Hashes,
235 pkgAcquire::MethodConfig *Cnf);
236
237 /** \brief Invoked when the worker starts to fetch this object.
238 *
239 * \param Message RFC822-formatted data from the worker process.
240 * Use LookupTag() to parse it.
241 *
242 * \param Size The size of the object being fetched.
243 *
244 * \sa pkgAcqMethod
245 */
246 virtual void Start(std::string Message,unsigned long long Size);
247
248 /** \brief Custom headers to be sent to the fetch process.
249 *
250 * \return a string containing RFC822-style headers that are to be
251 * inserted into the 600 URI Acquire message sent to the fetch
252 * subprocess. The headers are inserted after a newline-less
253 * line, so they should (if nonempty) have a leading newline and
254 * no trailing newline.
255 */
256 virtual std::string Custom600Headers() const {return std::string();};
257
258 /** \brief A "descriptive" URI-like string.
259 *
260 * \return a URI that should be used to describe what is being fetched.
261 */
262 virtual std::string DescURI() = 0;
263 /** \brief Short item description.
264 *
265 * \return a brief description of the object being fetched.
266 */
267 virtual std::string ShortDesc() {return DescURI();}
268
269 /** \brief Invoked by the worker when the download is completely done. */
270 virtual void Finished() {};
271
272 /** \brief HashSums
273 *
274 * \return the HashSums of this object, if applicable; otherwise, an
275 * empty list.
276 */
277 HashStringList HashSums() const {return ExpectedHashes;};
278 std::string HashSum() const {HashStringList const hashes = HashSums(); HashString const * const hs = hashes.find(NULL); return hs != NULL ? hs->toStr() : ""; };
279
280 /** \return the acquire process with which this item is associated. */
281 pkgAcquire *GetOwner() const {return Owner;};
282
283 /** \return \b true if this object is being fetched from a trusted source. */
284 virtual bool IsTrusted() const {return false;};
285
286 /** \brief Report mirror problem
287 *
288 * This allows reporting mirror failures back to a centralized
289 * server. The apt-report-mirror-failure script is called for this
290 *
291 * \param FailCode A short failure string that is send
292 */
293 void ReportMirrorFailure(std::string FailCode);
294
295 /** \brief Initialize an item.
296 *
297 * Adds the item to the list of items known to the acquire
298 * process, but does not place it into any fetch queues (you must
299 * manually invoke QueueURI() to do so).
300 *
301 * \param Owner The new owner of this item.
302 * \param ExpectedHashes of the file represented by this item
303 */
304 Item(pkgAcquire *Owner,
305 HashStringList const &ExpectedHashes=HashStringList(),
306 pkgAcqMetaBase *TransactionManager=NULL);
307
308 /** \brief Remove this item from its owner's queue by invoking
309 * pkgAcquire::Remove.
310 */
311 virtual ~Item();
312
313 protected:
314
315 enum RenameOnErrorState {
316 HashSumMismatch,
317 SizeMismatch,
318 InvalidFormat,
319 SignatureError,
320 NotClearsigned,
321 };
322
323 /** \brief Rename failed file and set error
324 *
325 * \param state respresenting the error we encountered
326 */
327 bool RenameOnError(RenameOnErrorState const state);
328
329 /** \brief The HashSums of the item is supposed to have than done */
330 HashStringList ExpectedHashes;
331
332 /** \brief The item that is currently being downloaded. */
333 pkgAcquire::ItemDesc Desc;
334 };
335 /*}}}*/
336 /** \brief Information about an index patch (aka diff). */ /*{{{*/
337 struct DiffInfo {
338 /** The filename of the diff. */
339 std::string file;
340
341 /** The sha1 hash of the diff. */
342 std::string sha1;
343
344 /** The size of the diff. */
345 unsigned long size;
346 };
347 /*}}}*/
348 /*}}}*/
349
350 class pkgAcqMetaBase : public pkgAcquire::Item
351 {
352 void *d;
353
354 protected:
355 std::vector<Item*> Transaction;
356
357 /** \brief A package-system-specific parser for the meta-index file. */
358 indexRecords *MetaIndexParser;
359
360 /** \brief The index files which should be looked up in the meta-index
361 * and then downloaded.
362 */
363 const std::vector<IndexTarget*>* IndexTargets;
364
365 /** \brief If \b true, the index's signature is currently being verified.
366 */
367 bool AuthPass;
368
369 // required to deal gracefully with problems caused by incorrect ims hits
370 bool IMSHit;
371
372 /** \brief Starts downloading the individual index files.
373 *
374 * \param verify If \b true, only indices whose expected hashsum
375 * can be determined from the meta-index will be downloaded, and
376 * the hashsums of indices will be checked (reporting
377 * #StatAuthError if there is a mismatch). If verify is \b false,
378 * no hashsum checking will be performed.
379 */
380 void QueueIndexes(bool verify);
381
382 public:
383 // transaction code
384 void Add(Item *I);
385 void AbortTransaction();
386 bool TransactionHasError() APT_PURE;
387 void CommitTransaction();
388
389 /** \brief Stage (queue) a copy action when the transaction is commited
390 */
391 void TransactionStageCopy(Item *I,
392 const std::string &From,
393 const std::string &To);
394 /** \brief Stage (queue) a removal action when the transaction is commited
395 */
396 void TransactionStageRemoval(Item *I, const std::string &FinalFile);
397
398 // helper for the signature warning
399 bool GenerateAuthWarning(const std::string &RealURI,
400 const std::string &Message);
401
402
403 pkgAcqMetaBase(pkgAcquire *Owner,
404 const std::vector<IndexTarget*>* IndexTargets,
405 indexRecords* MetaIndexParser,
406 HashStringList const &ExpectedHashes=HashStringList(),
407 pkgAcqMetaBase *TransactionManager=NULL)
408 : Item(Owner, ExpectedHashes, TransactionManager),
409 MetaIndexParser(MetaIndexParser), IndexTargets(IndexTargets),
410 AuthPass(false), IMSHit(false) {};
411 };
412
413 /** \brief An acquire item that downloads the detached signature {{{
414 * of a meta-index (Release) file, then queues up the release
415 * file itself.
416 *
417 * \todo Why protected members?
418 *
419 * \sa pkgAcqMetaIndex
420 */
421 class pkgAcqMetaSig : public pkgAcqMetaBase
422 {
423 void *d;
424
425 protected:
426
427 /** \brief The URI of the signature file. Unlike Desc.URI, this is
428 * never modified; it is used to determine the file that is being
429 * downloaded.
430 */
431 std::string RealURI;
432
433 /** \brief The file we need to verify */
434 std::string MetaIndexFile;
435
436 /** \brief Long URI description used in the acquire system */
437 std::string URIDesc;
438
439 /** \brief Short URI description used in the acquire system */
440 std::string ShortDesc;
441
442 public:
443
444 // Specialized action members
445 virtual void Failed(std::string Message,pkgAcquire::MethodConfig *Cnf);
446 virtual void Done(std::string Message,unsigned long long Size, HashStringList const &Hashes,
447 pkgAcquire::MethodConfig *Cnf);
448 virtual std::string Custom600Headers() const;
449 virtual std::string DescURI() {return RealURI; };
450
451 /** \brief Create a new pkgAcqMetaSig. */
452 pkgAcqMetaSig(pkgAcquire *Owner,
453 pkgAcqMetaBase *TransactionManager,
454 std::string URI,std::string URIDesc, std::string ShortDesc,
455 std::string MetaIndexFile,
456 const std::vector<IndexTarget*>* IndexTargets,
457 indexRecords* MetaIndexParser);
458 virtual ~pkgAcqMetaSig();
459 };
460 /*}}}*/
461
462 /** \brief An item that is responsible for downloading the meta-index {{{
463 * file (i.e., Release) itself and verifying its signature.
464 *
465 * Once the download and verification are complete, the downloads of
466 * the individual index files are queued up using pkgAcqDiffIndex.
467 * If the meta-index file had a valid signature, the expected hashsums
468 * of the index files will be the md5sums listed in the meta-index;
469 * otherwise, the expected hashsums will be "" (causing the
470 * authentication of the index files to be bypassed).
471 */
472 class pkgAcqMetaIndex : public pkgAcqMetaBase
473 {
474 void *d;
475
476 protected:
477 /** \brief The URI that is actually being downloaded; never
478 * modified by pkgAcqMetaIndex.
479 */
480 std::string RealURI;
481
482 /** \brief The file in which the signature for this index was stored.
483 *
484 * If empty, the signature and the md5sums of the individual
485 * indices will not be checked.
486 */
487 std::string SigFile;
488
489 /** \brief Check that the release file is a release file for the
490 * correct distribution.
491 *
492 * \return \b true if no fatal errors were encountered.
493 */
494 bool VerifyVendor(std::string Message);
495
496 /** \brief Called when a file is finished being retrieved.
497 *
498 * If the file was not downloaded to DestFile, a copy process is
499 * set up to copy it to DestFile; otherwise, Complete is set to \b
500 * true and the file is moved to its final location.
501 *
502 * \param Message The message block received from the fetch
503 * subprocess.
504 */
505 void RetrievalDone(std::string Message);
506
507 /** \brief Called when authentication succeeded.
508 *
509 * Sanity-checks the authenticated file, queues up the individual
510 * index files for download, and saves the signature in the lists
511 * directory next to the authenticated list file.
512 *
513 * \param Message The message block received from the fetch
514 * subprocess.
515 */
516 void AuthDone(std::string Message);
517
518 std::string URIDesc;
519 std::string ShortDesc;
520
521 /** \brief The URI of the meta-index file for the detached signature */
522 std::string MetaIndexSigURI;
523
524 /** \brief A "URI-style" description of the meta-index file */
525 std::string MetaIndexSigURIDesc;
526
527 /** \brief A brief description of the meta-index file */
528 std::string MetaIndexSigShortDesc;
529
530 /** \brief delayed constructor */
531 void Init(std::string URIDesc, std::string ShortDesc);
532
533 public:
534
535 // Specialized action members
536 virtual void Failed(std::string Message,pkgAcquire::MethodConfig *Cnf);
537 virtual void Done(std::string Message,unsigned long long Size, HashStringList const &Hashes,
538 pkgAcquire::MethodConfig *Cnf);
539 virtual std::string Custom600Headers() const;
540 virtual std::string DescURI() {return RealURI; };
541 virtual void Finished();
542
543 /** \brief Create a new pkgAcqMetaIndex. */
544 pkgAcqMetaIndex(pkgAcquire *Owner,
545 pkgAcqMetaBase *TransactionManager,
546 std::string URI,std::string URIDesc, std::string ShortDesc,
547 std::string MetaIndexSigURI, std::string MetaIndexSigURIDesc, std::string MetaIndexSigShortDesc,
548 const std::vector<IndexTarget*>* IndexTargets,
549 indexRecords* MetaIndexParser);
550 };
551 /*}}}*/
552 /** \brief An item repsonsible for downloading clearsigned metaindexes {{{*/
553 class pkgAcqMetaClearSig : public pkgAcqMetaIndex
554 {
555 void *d;
556
557 /** \brief The URI of the meta-index file for the detached signature */
558 std::string MetaIndexURI;
559
560 /** \brief A "URI-style" description of the meta-index file */
561 std::string MetaIndexURIDesc;
562
563 /** \brief A brief description of the meta-index file */
564 std::string MetaIndexShortDesc;
565
566 /** \brief The URI of the detached meta-signature file if the clearsigned one failed. */
567 std::string MetaSigURI;
568
569 /** \brief A "URI-style" description of the meta-signature file */
570 std::string MetaSigURIDesc;
571
572 /** \brief A brief description of the meta-signature file */
573 std::string MetaSigShortDesc;
574
575 public:
576 virtual void Failed(std::string Message,pkgAcquire::MethodConfig *Cnf);
577 virtual std::string Custom600Headers() const;
578 virtual void Done(std::string Message,unsigned long long Size, HashStringList const &Hashes,
579 pkgAcquire::MethodConfig *Cnf);
580
581 /** \brief Create a new pkgAcqMetaClearSig. */
582 pkgAcqMetaClearSig(pkgAcquire *Owner,
583 std::string const &URI, std::string const &URIDesc, std::string const &ShortDesc,
584 std::string const &MetaIndexURI, std::string const &MetaIndexURIDesc, std::string const &MetaIndexShortDesc,
585 std::string const &MetaSigURI, std::string const &MetaSigURIDesc, std::string const &MetaSigShortDesc,
586 const std::vector<IndexTarget*>* IndexTargets,
587 indexRecords* MetaIndexParser);
588 virtual ~pkgAcqMetaClearSig();
589 };
590 /*}}}*/
591
592
593 /** \brief Common base class for all classes that deal with fetching {{{
594 indexes
595 */
596 class pkgAcqBaseIndex : public pkgAcquire::Item
597 {
598 void *d;
599
600 protected:
601 /** \brief Pointer to the IndexTarget data
602 */
603 const struct IndexTarget * Target;
604 indexRecords *MetaIndexParser;
605 /** \brief The MetaIndex Key */
606 std::string MetaKey;
607
608 pkgAcqBaseIndex(pkgAcquire *Owner,
609 pkgAcqMetaBase *TransactionManager,
610 struct IndexTarget const * const Target,
611 HashStringList const &ExpectedHashes,
612 indexRecords *MetaIndexParser)
613 : Item(Owner, ExpectedHashes, TransactionManager), Target(Target),
614 MetaIndexParser(MetaIndexParser) {};
615 };
616 /*}}}*/
617 /** \brief An item that is responsible for fetching an index file of {{{
618 * package list diffs and starting the package list's download.
619 *
620 * This item downloads the Index file and parses it, then enqueues
621 * additional downloads of either the individual patches (using
622 * pkgAcqIndexDiffs) or the entire Packages file (using pkgAcqIndex).
623 *
624 * \sa pkgAcqIndexDiffs, pkgAcqIndex
625 */
626 class pkgAcqDiffIndex : public pkgAcqBaseIndex
627 {
628 void *d;
629
630 protected:
631 /** \brief If \b true, debugging information will be written to std::clog. */
632 bool Debug;
633
634 /** \brief The URI of the index file to recreate at our end (either
635 * by downloading it or by applying partial patches).
636 */
637 std::string RealURI;
638
639 /** \brief The index file which will be patched to generate the new
640 * file.
641 */
642 std::string CurrentPackagesFile;
643
644 /** \brief A description of the Packages file (stored in
645 * pkgAcquire::ItemDesc::Description).
646 */
647 std::string Description;
648
649 /** \brief If the copy step of the packages file is done
650 */
651 bool PackagesFileReadyInPartial;
652
653 public:
654 // Specialized action members
655 virtual void Failed(std::string Message,pkgAcquire::MethodConfig *Cnf);
656 virtual void Done(std::string Message,unsigned long long Size, HashStringList const &Hashes,
657 pkgAcquire::MethodConfig *Cnf);
658 virtual std::string DescURI() {return RealURI + "Index";};
659 virtual std::string Custom600Headers() const;
660
661 /** \brief Parse the Index file for a set of Packages diffs.
662 *
663 * Parses the Index file and creates additional download items as
664 * necessary.
665 *
666 * \param IndexDiffFile The name of the Index file.
667 *
668 * \return \b true if the Index file was successfully parsed, \b
669 * false otherwise.
670 */
671 bool ParseDiffIndex(std::string IndexDiffFile);
672
673
674 /** \brief Create a new pkgAcqDiffIndex.
675 *
676 * \param Owner The Acquire object that owns this item.
677 *
678 * \param URI The URI of the list file to download.
679 *
680 * \param URIDesc A long description of the list file to download.
681 *
682 * \param ShortDesc A short description of the list file to download.
683 *
684 * \param ExpectedHashes The list file's hashsums which are expected.
685 */
686 pkgAcqDiffIndex(pkgAcquire *Owner,
687 pkgAcqMetaBase *TransactionManager,
688 struct IndexTarget const * const Target,
689 HashStringList const &ExpectedHashes,
690 indexRecords *MetaIndexParser);
691 };
692 /*}}}*/
693 /** \brief An item that is responsible for fetching client-merge patches {{{
694 * that need to be applied to a given package index file.
695 *
696 * Instead of downloading and applying each patch one by one like its
697 * sister #pkgAcqIndexDiffs this class will download all patches at once
698 * and call rred with all the patches downloaded once. Rred will then
699 * merge and apply them in one go, which should be a lot faster – but is
700 * incompatible with server-based merges of patches like reprepro can do.
701 *
702 * \sa pkgAcqDiffIndex, pkgAcqIndex
703 */
704 class pkgAcqIndexMergeDiffs : public pkgAcqBaseIndex
705 {
706 void *d;
707
708 protected:
709
710 /** \brief If \b true, debugging output will be written to
711 * std::clog.
712 */
713 bool Debug;
714
715 /** \brief URI of the package index file that is being
716 * reconstructed.
717 */
718 std::string RealURI;
719
720 /** \brief description of the file being downloaded. */
721 std::string Description;
722
723 /** \brief information about the current patch */
724 struct DiffInfo const patch;
725
726 /** \brief list of all download items for the patches */
727 std::vector<pkgAcqIndexMergeDiffs*> const * const allPatches;
728
729 /** The current status of this patch. */
730 enum DiffState
731 {
732 /** \brief The diff is currently being fetched. */
733 StateFetchDiff,
734
735 /** \brief The diff is currently being applied. */
736 StateApplyDiff,
737
738 /** \brief the work with this diff is done */
739 StateDoneDiff,
740
741 /** \brief something bad happened and fallback was triggered */
742 StateErrorDiff
743 } State;
744
745 public:
746 /** \brief Called when the patch file failed to be downloaded.
747 *
748 * This method will fall back to downloading the whole index file
749 * outright; its arguments are ignored.
750 */
751 virtual void Failed(std::string Message,pkgAcquire::MethodConfig *Cnf);
752 virtual void Done(std::string Message,unsigned long long Size, HashStringList const &Hashes,
753 pkgAcquire::MethodConfig *Cnf);
754 virtual std::string DescURI() {return RealURI + "Index";};
755
756 /** \brief Create an index merge-diff item.
757 *
758 * \param Owner The pkgAcquire object that owns this item.
759 *
760 * \param URI The URI of the package index file being
761 * reconstructed.
762 *
763 * \param URIDesc A long description of this item.
764 *
765 * \param ShortDesc A brief description of this item.
766 *
767 * \param ExpectedHashes The expected md5sum of the completely
768 * reconstructed package index file; the index file will be tested
769 * against this value when it is entirely reconstructed.
770 *
771 * \param patch contains infos about the patch this item is supposed
772 * to download which were read from the index
773 *
774 * \param allPatches contains all related items so that each item can
775 * check if it was the last one to complete the download step
776 */
777 pkgAcqIndexMergeDiffs(pkgAcquire *Owner,
778 pkgAcqMetaBase *TransactionManager,
779 struct IndexTarget const * const Target,
780 HashStringList const &ExpectedHash,
781 indexRecords *MetaIndexParser,
782 DiffInfo const &patch,
783 std::vector<pkgAcqIndexMergeDiffs*> const * const allPatches);
784 };
785 /*}}}*/
786 /** \brief An item that is responsible for fetching server-merge patches {{{
787 * that need to be applied to a given package index file.
788 *
789 * After downloading and applying a single patch, this item will
790 * enqueue a new pkgAcqIndexDiffs to download and apply the remaining
791 * patches. If no patch can be found that applies to an intermediate
792 * file or if one of the patches cannot be downloaded, falls back to
793 * downloading the entire package index file using pkgAcqIndex.
794 *
795 * \sa pkgAcqDiffIndex, pkgAcqIndex
796 */
797 class pkgAcqIndexDiffs : public pkgAcqBaseIndex
798 {
799 void *d;
800
801 private:
802
803 /** \brief Queue up the next diff download.
804 *
805 * Search for the next available diff that applies to the file
806 * that currently exists on disk, and enqueue it by calling
807 * QueueURI().
808 *
809 * \return \b true if an applicable diff was found, \b false
810 * otherwise.
811 */
812 bool QueueNextDiff();
813
814 /** \brief Handle tasks that must be performed after the item
815 * finishes downloading.
816 *
817 * Dequeues the item and checks the resulting file's hashsums
818 * against ExpectedHashes after the last patch was applied.
819 * There is no need to check the md5/sha1 after a "normal"
820 * patch because QueueNextDiff() will check the sha1 later.
821 *
822 * \param allDone If \b true, the file was entirely reconstructed,
823 * and its md5sum is verified.
824 */
825 void Finish(bool allDone=false);
826
827 protected:
828
829 /** \brief If \b true, debugging output will be written to
830 * std::clog.
831 */
832 bool Debug;
833
834 /** \brief The URI of the package index file that is being
835 * reconstructed.
836 */
837 std::string RealURI;
838
839 /** A description of the file being downloaded. */
840 std::string Description;
841
842 /** The patches that remain to be downloaded, including the patch
843 * being downloaded right now. This list should be ordered so
844 * that each diff appears before any diff that depends on it.
845 *
846 * \todo These are indexed by sha1sum; why not use some sort of
847 * dictionary instead of relying on ordering and stripping them
848 * off the front?
849 */
850 std::vector<DiffInfo> available_patches;
851
852 /** Stop applying patches when reaching that sha1 */
853 std::string ServerSha1;
854
855 /** The current status of this patch. */
856 enum DiffState
857 {
858 /** \brief The diff is in an unknown state. */
859 StateFetchUnkown,
860
861 /** \brief The diff is currently being fetched. */
862 StateFetchDiff,
863
864 /** \brief The diff is currently being uncompressed. */
865 StateUnzipDiff, // FIXME: No longer used
866
867 /** \brief The diff is currently being applied. */
868 StateApplyDiff
869 } State;
870
871 public:
872
873 /** \brief Called when the patch file failed to be downloaded.
874 *
875 * This method will fall back to downloading the whole index file
876 * outright; its arguments are ignored.
877 */
878 virtual void Failed(std::string Message,pkgAcquire::MethodConfig *Cnf);
879
880 virtual void Done(std::string Message,unsigned long long Size, HashStringList const &Hashes,
881 pkgAcquire::MethodConfig *Cnf);
882 virtual std::string DescURI() {return RealURI + "IndexDiffs";};
883
884 /** \brief Create an index diff item.
885 *
886 * After filling in its basic fields, this invokes Finish(true) if
887 * \a diffs is empty, or QueueNextDiff() otherwise.
888 *
889 * \param Owner The pkgAcquire object that owns this item.
890 *
891 * \param URI The URI of the package index file being
892 * reconstructed.
893 *
894 * \param URIDesc A long description of this item.
895 *
896 * \param ShortDesc A brief description of this item.
897 *
898 * \param ExpectedHashes The expected md5sum of the completely
899 * reconstructed package index file; the index file will be tested
900 * against this value when it is entirely reconstructed.
901 *
902 * \param ServerSha1 is the sha1sum of the current file on the server
903 *
904 * \param diffs The remaining diffs from the index of diffs. They
905 * should be ordered so that each diff appears before any diff
906 * that depends on it.
907 */
908 pkgAcqIndexDiffs(pkgAcquire *Owner,
909 pkgAcqMetaBase *TransactionManager,
910 struct IndexTarget const * const Target,
911 HashStringList const &ExpectedHash,
912 indexRecords *MetaIndexParser,
913 std::string ServerSha1,
914 std::vector<DiffInfo> diffs=std::vector<DiffInfo>());
915 };
916 /*}}}*/
917 /** \brief An acquire item that is responsible for fetching an index {{{
918 * file (e.g., Packages or Sources).
919 *
920 * \sa pkgAcqDiffIndex, pkgAcqIndexDiffs, pkgAcqIndexTrans
921 *
922 * \todo Why does pkgAcqIndex have protected members?
923 */
924 class pkgAcqIndex : public pkgAcqBaseIndex
925 {
926 void *d;
927
928 protected:
929
930 /** \brief If \b true, the index file has been decompressed. */
931 bool Decompression;
932
933 /** \brief If \b true, the partially downloaded file will be
934 * removed when the download completes.
935 */
936 bool Erase;
937
938 /** \brief The object that is actually being fetched (minus any
939 * compression-related extensions).
940 */
941 std::string RealURI;
942
943 /** \brief The compression-related file extensions that are being
944 * added to the downloaded file one by one if first fails (e.g., "gz bz2").
945 */
946 std::string CompressionExtension;
947
948 /** \brief Do the changes needed to fetch via AptByHash (if needed) */
949 void InitByHashIfNeeded(const std::string MetaKey);
950
951 /** \brief Auto select the right compression to use */
952 void AutoSelectCompression();
953
954 /** \brief Get the full pathname of the final file for the current URI
955 */
956 std::string GetFinalFilename() const;
957
958 /** \brief Schedule file for verification after a IMS hit */
959 void ReverifyAfterIMS();
960
961 /** \brief Validate the downloaded index file */
962 bool ValidateFile(const std::string &FileName);
963
964 public:
965
966 // Specialized action members
967 virtual void Failed(std::string Message,pkgAcquire::MethodConfig *Cnf);
968 virtual void Done(std::string Message,unsigned long long Size,
969 HashStringList const &Hashes,
970 pkgAcquire::MethodConfig *Cnf);
971 virtual std::string Custom600Headers() const;
972 virtual std::string DescURI() {return Desc.URI;};
973
974 /** \brief Create a pkgAcqIndex.
975 *
976 * \param Owner The pkgAcquire object with which this item is
977 * associated.
978 *
979 * \param URI The URI of the index file that is to be downloaded.
980 *
981 * \param URIDesc A "URI-style" description of this index file.
982 *
983 * \param ShortDesc A brief description of this index file.
984 *
985 * \param ExpectedHashes The expected hashsum of this index file.
986 *
987 * \param compressExt The compression-related extension with which
988 * this index file should be downloaded, or "" to autodetect
989 * Compression types can be set with config Acquire::CompressionTypes,
990 * default is ".lzma" or ".bz2" (if the needed binaries are present)
991 * fallback is ".gz" or none.
992 */
993 pkgAcqIndex(pkgAcquire *Owner,std::string URI,std::string URIDesc,
994 std::string ShortDesc, HashStringList const &ExpectedHashes);
995 pkgAcqIndex(pkgAcquire *Owner, pkgAcqMetaBase *TransactionManager,
996 IndexTarget const * const Target,
997 HashStringList const &ExpectedHash,
998 indexRecords *MetaIndexParser);
999
1000 void Init(std::string const &URI, std::string const &URIDesc,
1001 std::string const &ShortDesc);
1002 };
1003 /*}}}*/
1004 /** \brief An acquire item that is responsible for fetching a {{{
1005 * translated index file.
1006 *
1007 * The only difference from pkgAcqIndex is that transient failures
1008 * are suppressed: no error occurs if the translated index file is
1009 * missing.
1010 */
1011 class pkgAcqIndexTrans : public pkgAcqIndex
1012 {
1013 void *d;
1014
1015 public:
1016
1017 virtual void Failed(std::string Message,pkgAcquire::MethodConfig *Cnf);
1018 virtual std::string Custom600Headers() const;
1019
1020 /** \brief Create a pkgAcqIndexTrans.
1021 *
1022 * \param Owner The pkgAcquire object with which this item is
1023 * associated.
1024 *
1025 * \param URI The URI of the index file that is to be downloaded.
1026 *
1027 * \param URIDesc A "URI-style" description of this index file.
1028 *
1029 * \param ShortDesc A brief description of this index file.
1030 */
1031 pkgAcqIndexTrans(pkgAcquire *Owner,
1032 std::string URI,std::string URIDesc,
1033 std::string ShortDesc);
1034 pkgAcqIndexTrans(pkgAcquire *Owner,
1035 pkgAcqMetaBase *TransactionManager,
1036 IndexTarget const * const Target,
1037 HashStringList const &ExpectedHashes,
1038 indexRecords *MetaIndexParser);
1039 };
1040 /*}}}*/
1041 /** \brief Information about an index file. */ /*{{{*/
1042 class IndexTarget
1043 {
1044 void *d;
1045
1046 public:
1047 /** \brief A URI from which the index file can be downloaded. */
1048 std::string URI;
1049
1050 /** \brief A description of the index file. */
1051 std::string Description;
1052
1053 /** \brief A shorter description of the index file. */
1054 std::string ShortDesc;
1055
1056 /** \brief The key by which this index file should be
1057 * looked up within the meta signature file.
1058 */
1059 std::string MetaKey;
1060
1061 virtual bool IsOptional() const {
1062 return false;
1063 }
1064 };
1065 /*}}}*/
1066 /** \brief Information about an optional index file. */ /*{{{*/
1067 class OptionalIndexTarget : public IndexTarget
1068 {
1069 void *d;
1070
1071 virtual bool IsOptional() const {
1072 return true;
1073 }
1074 };
1075 /*}}}*/
1076 /** \brief An item that is responsible for fetching a package file. {{{
1077 *
1078 * If the package file already exists in the cache, nothing will be
1079 * done.
1080 */
1081 class pkgAcqArchive : public pkgAcquire::Item
1082 {
1083 void *d;
1084
1085 protected:
1086 /** \brief The package version being fetched. */
1087 pkgCache::VerIterator Version;
1088
1089 /** \brief The list of sources from which to pick archives to
1090 * download this package from.
1091 */
1092 pkgSourceList *Sources;
1093
1094 /** \brief A package records object, used to look up the file
1095 * corresponding to each version of the package.
1096 */
1097 pkgRecords *Recs;
1098
1099 /** \brief A location in which the actual filename of the package
1100 * should be stored.
1101 */
1102 std::string &StoreFilename;
1103
1104 /** \brief The next file for this version to try to download. */
1105 pkgCache::VerFileIterator Vf;
1106
1107 /** \brief How many (more) times to try to find a new source from
1108 * which to download this package version if it fails.
1109 *
1110 * Set from Acquire::Retries.
1111 */
1112 unsigned int Retries;
1113
1114 /** \brief \b true if this version file is being downloaded from a
1115 * trusted source.
1116 */
1117 bool Trusted;
1118
1119 /** \brief Queue up the next available file for this version. */
1120 bool QueueNext();
1121
1122 public:
1123
1124 virtual void Failed(std::string Message,pkgAcquire::MethodConfig *Cnf);
1125 virtual void Done(std::string Message,unsigned long long Size, HashStringList const &Hashes,
1126 pkgAcquire::MethodConfig *Cnf);
1127 virtual std::string DescURI() {return Desc.URI;};
1128 virtual std::string ShortDesc() {return Desc.ShortDesc;};
1129 virtual void Finished();
1130 virtual bool IsTrusted() const;
1131
1132 /** \brief Create a new pkgAcqArchive.
1133 *
1134 * \param Owner The pkgAcquire object with which this item is
1135 * associated.
1136 *
1137 * \param Sources The sources from which to download version
1138 * files.
1139 *
1140 * \param Recs A package records object, used to look up the file
1141 * corresponding to each version of the package.
1142 *
1143 * \param Version The package version to download.
1144 *
1145 * \param[out] StoreFilename A location in which the actual filename of
1146 * the package should be stored. It will be set to a guessed
1147 * basename in the constructor, and filled in with a fully
1148 * qualified filename once the download finishes.
1149 */
1150 pkgAcqArchive(pkgAcquire *Owner,pkgSourceList *Sources,
1151 pkgRecords *Recs,pkgCache::VerIterator const &Version,
1152 std::string &StoreFilename);
1153 };
1154 /*}}}*/
1155 /** \brief Retrieve an arbitrary file to the current directory. {{{
1156 *
1157 * The file is retrieved even if it is accessed via a URL type that
1158 * normally is a NOP, such as "file". If the download fails, the
1159 * partial file is renamed to get a ".FAILED" extension.
1160 */
1161 class pkgAcqFile : public pkgAcquire::Item
1162 {
1163 void *d;
1164
1165 /** \brief How many times to retry the download, set from
1166 * Acquire::Retries.
1167 */
1168 unsigned int Retries;
1169
1170 /** \brief Should this file be considered a index file */
1171 bool IsIndexFile;
1172
1173 public:
1174
1175 // Specialized action members
1176 virtual void Failed(std::string Message,pkgAcquire::MethodConfig *Cnf);
1177 virtual void Done(std::string Message,unsigned long long Size, HashStringList const &CalcHashes,
1178 pkgAcquire::MethodConfig *Cnf);
1179 virtual std::string DescURI() {return Desc.URI;};
1180 virtual std::string Custom600Headers() const;
1181
1182 /** \brief Create a new pkgAcqFile object.
1183 *
1184 * \param Owner The pkgAcquire object with which this object is
1185 * associated.
1186 *
1187 * \param URI The URI to download.
1188 *
1189 * \param Hashes The hashsums of the file to download, if they are known;
1190 * otherwise empty list.
1191 *
1192 * \param Size The size of the file to download, if it is known;
1193 * otherwise 0.
1194 *
1195 * \param Desc A description of the file being downloaded.
1196 *
1197 * \param ShortDesc A brief description of the file being
1198 * downloaded.
1199 *
1200 * \param DestDir The directory the file should be downloaded into.
1201 *
1202 * \param DestFilename The filename+path the file is downloaded to.
1203 *
1204 * \param IsIndexFile The file is considered a IndexFile and cache-control
1205 * headers like "cache-control: max-age=0" are send
1206 *
1207 * If DestFilename is empty, download to DestDir/\<basename\> if
1208 * DestDir is non-empty, $CWD/\<basename\> otherwise. If
1209 * DestFilename is NOT empty, DestDir is ignored and DestFilename
1210 * is the absolute name to which the file should be downloaded.
1211 */
1212
1213 pkgAcqFile(pkgAcquire *Owner, std::string URI, HashStringList const &Hashes, unsigned long long Size,
1214 std::string Desc, std::string ShortDesc,
1215 const std::string &DestDir="", const std::string &DestFilename="",
1216 bool IsIndexFile=false);
1217 };
1218 /*}}}*/
1219 /** @} */
1220
1221 #endif