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