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