]> git.saurik.com Git - apt.git/blob - apt-pkg/acquire-item.h
4d235dce2a5f36870d7fe70ee76afbf2a11b7eb1
[apt.git] / apt-pkg / acquire-item.h
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 /* ######################################################################
4
5 Acquire Item - Item to acquire
6
7 When an item is instantiated it will add it self to the local list in
8 the Owner Acquire class. Derived classes will then call QueueURI to
9 register all the URI's they wish to fetch at the initial moment.
10
11 Three item classes are provided to provide functionality for
12 downloading of Index, Translation and Packages files.
13
14 A Archive class is provided for downloading .deb files. It does Hash
15 checking and source location as well as a retry algorithm.
16
17 ##################################################################### */
18 /*}}}*/
19 #ifndef PKGLIB_ACQUIRE_ITEM_H
20 #define PKGLIB_ACQUIRE_ITEM_H
21
22 #include <apt-pkg/acquire.h>
23 #include <apt-pkg/indexfile.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 #include <map>
32
33 #ifndef APT_8_CLEANER_HEADERS
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 pkgAcqMetaClearSig;
50 class pkgAcqIndexMergeDiffs;
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 explicit 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 * const d;
341
342 friend class pkgAcqMetaBase;
343 friend class pkgAcqMetaClearSig;
344 };
345 /*}}}*/
346 class APT_HIDDEN pkgAcqTransactionItem: public pkgAcquire::Item /*{{{*/
347 /** \brief baseclass for the indexes files to manage them all together */
348 {
349 void * const d;
350 protected:
351 IndexTarget const Target;
352 HashStringList GetExpectedHashesFor(std::string const &MetaKey) const;
353
354 bool QueueURI(pkgAcquire::ItemDesc &Item);
355
356 public:
357 /** \brief storge name until a transaction is finished */
358 std::string PartialFile;
359
360 /** \brief TransactionManager */
361 pkgAcqMetaClearSig * const TransactionManager;
362
363 enum TransactionStates {
364 TransactionCommit,
365 TransactionAbort,
366 };
367 virtual bool TransactionState(TransactionStates const state);
368
369 virtual std::string DescURI() const { return Target.URI; }
370 virtual HashStringList GetExpectedHashes() const;
371 virtual std::string GetMetaKey() const;
372 virtual bool HashesRequired() const;
373
374
375 pkgAcqTransactionItem(pkgAcquire * const Owner, pkgAcqMetaClearSig * const TransactionManager, IndexTarget const &Target);
376 virtual ~pkgAcqTransactionItem();
377
378 friend class pkgAcqMetaBase;
379 friend class pkgAcqMetaClearSig;
380 };
381 /*}}}*/
382 class APT_HIDDEN pkgAcqMetaBase : public pkgAcqTransactionItem /*{{{*/
383 /** \brief the manager of a transaction */
384 {
385 void * const d;
386 protected:
387 std::vector<pkgAcqTransactionItem*> Transaction;
388
389 /** \brief The index files which should be looked up in the meta-index
390 * and then downloaded.
391 */
392 std::vector<IndexTarget> const IndexTargets;
393
394 /** \brief If \b true, the index's signature is currently being verified.
395 */
396 bool AuthPass;
397
398 /** \brief Starts downloading the individual index files.
399 *
400 * \param verify If \b true, only indices whose expected hashsum
401 * can be determined from the meta-index will be downloaded, and
402 * the hashsums of indices will be checked (reporting
403 * #StatAuthError if there is a mismatch). If verify is \b false,
404 * no hashsum checking will be performed.
405 */
406 void QueueIndexes(bool const verify);
407
408 /** \brief Called when a file is finished being retrieved.
409 *
410 * If the file was not downloaded to DestFile, a copy process is
411 * set up to copy it to DestFile; otherwise, Complete is set to \b
412 * true and the file is moved to its final location.
413 *
414 * \param Message The message block received from the fetch
415 * subprocess.
416 */
417 bool CheckDownloadDone(pkgAcqTransactionItem * const I, const std::string &Message, HashStringList const &Hashes) const;
418
419 /** \brief Queue the downloaded Signature for verification */
420 void QueueForSignatureVerify(pkgAcqTransactionItem * const I, std::string const &File, std::string const &Signature);
421
422 virtual std::string Custom600Headers() const;
423
424 /** \brief Called when authentication succeeded.
425 *
426 * Sanity-checks the authenticated file, queues up the individual
427 * index files for download, and saves the signature in the lists
428 * directory next to the authenticated list file.
429 *
430 * \param Message The message block received from the fetch
431 * subprocess.
432 */
433 bool CheckAuthDone(std::string const &Message);
434
435 /** Check if the current item should fail at this point */
436 bool CheckStopAuthentication(pkgAcquire::Item * const I, const std::string &Message);
437
438 /** \brief Check that the release file is a release file for the
439 * correct distribution.
440 *
441 * \return \b true if no fatal errors were encountered.
442 */
443 bool VerifyVendor(std::string const &Message);
444
445 virtual bool TransactionState(TransactionStates const state);
446
447 public:
448 // This refers more to the Transaction-Manager than the actual file
449 bool IMSHit;
450
451 virtual bool QueueURI(pkgAcquire::ItemDesc &Item);
452 virtual HashStringList GetExpectedHashes() const;
453 virtual bool HashesRequired() const;
454
455 // transaction code
456 void Add(pkgAcqTransactionItem * const I);
457 void AbortTransaction();
458 bool TransactionHasError() const;
459 void CommitTransaction();
460
461 /** \brief Stage (queue) a copy action when the transaction is committed
462 */
463 void TransactionStageCopy(pkgAcqTransactionItem * const I,
464 const std::string &From,
465 const std::string &To);
466 /** \brief Stage (queue) a removal action when the transaction is committed
467 */
468 void TransactionStageRemoval(pkgAcqTransactionItem * const I, const std::string &FinalFile);
469
470 /** \brief Get the full pathname of the final file for the current URI */
471 virtual std::string GetFinalFilename() const;
472
473 pkgAcqMetaBase(pkgAcquire * const Owner, pkgAcqMetaClearSig * const TransactionManager,
474 std::vector<IndexTarget> const &IndexTargets,
475 IndexTarget const &DataTarget);
476 virtual ~pkgAcqMetaBase();
477 };
478 /*}}}*/
479 /** \brief An item that is responsible for downloading the meta-index {{{
480 * file (i.e., Release) itself and verifying its signature.
481 *
482 * Once the download and verification are complete, the downloads of
483 * the individual index files are queued up using pkgAcqDiffIndex.
484 * If the meta-index file had a valid signature, the expected hashsums
485 * of the index files will be the md5sums listed in the meta-index;
486 * otherwise, the expected hashsums will be "" (causing the
487 * authentication of the index files to be bypassed).
488 */
489 class APT_HIDDEN pkgAcqMetaIndex : public pkgAcqMetaBase
490 {
491 void * const d;
492 protected:
493 IndexTarget const DetachedSigTarget;
494
495 /** \brief delayed constructor */
496 void Init(std::string const &URIDesc, std::string const &ShortDesc);
497
498 public:
499 virtual std::string DescURI() const;
500
501 // Specialized action members
502 virtual void Failed(std::string const &Message,pkgAcquire::MethodConfig const * const Cnf);
503 virtual void Done(std::string const &Message, HashStringList const &Hashes,
504 pkgAcquire::MethodConfig const * const Cnf);
505 virtual void Finished();
506
507 /** \brief Create a new pkgAcqMetaIndex. */
508 pkgAcqMetaIndex(pkgAcquire * const Owner, pkgAcqMetaClearSig * const TransactionManager,
509 IndexTarget const &DataTarget, IndexTarget const &DetachedSigTarget,
510 std::vector<IndexTarget> const &IndexTargets);
511 virtual ~pkgAcqMetaIndex();
512
513 friend class pkgAcqMetaSig;
514 };
515 /*}}}*/
516 /** \brief An acquire item that downloads the detached signature {{{
517 * of a meta-index (Release) file, then queues up the release
518 * file itself.
519 *
520 * \todo Why protected members?
521 *
522 * \sa pkgAcqMetaIndex
523 */
524 class APT_HIDDEN pkgAcqMetaSig : public pkgAcqTransactionItem
525 {
526 void * const d;
527
528 pkgAcqMetaIndex * const MetaIndex;
529
530 /** \brief The file we use to verify the MetaIndexFile with (not always set!) */
531 std::string MetaIndexFileSignature;
532
533 protected:
534
535 /** \brief Get the full pathname of the final file for the current URI */
536 virtual std::string GetFinalFilename() const;
537
538 public:
539 virtual bool HashesRequired() const { return false; }
540
541 // Specialized action members
542 virtual void Failed(std::string const &Message,pkgAcquire::MethodConfig const * const Cnf);
543 virtual void Done(std::string const &Message, HashStringList const &Hashes,
544 pkgAcquire::MethodConfig const * const Cnf);
545
546 /** \brief Create a new pkgAcqMetaSig. */
547 pkgAcqMetaSig(pkgAcquire * const Owner, pkgAcqMetaClearSig * const TransactionManager,
548 IndexTarget const &Target, pkgAcqMetaIndex * const MetaIndex);
549 virtual ~pkgAcqMetaSig();
550 };
551 /*}}}*/
552 /** \brief An item repsonsible for downloading clearsigned metaindexes {{{*/
553 class APT_HIDDEN pkgAcqMetaClearSig : public pkgAcqMetaIndex
554 {
555 void * const d;
556
557 IndexTarget const ClearsignedTarget;
558 IndexTarget const DetachedDataTarget;
559
560 public:
561 /** \brief A package-system-specific parser for the meta-index file. */
562 indexRecords *MetaIndexParser;
563 indexRecords *LastMetaIndexParser;
564
565 virtual void Failed(std::string const &Message,pkgAcquire::MethodConfig const * const Cnf);
566 virtual std::string Custom600Headers() const;
567 virtual void Done(std::string const &Message, HashStringList const &Hashes,
568 pkgAcquire::MethodConfig const * const Cnf);
569
570 /** \brief Create a new pkgAcqMetaClearSig. */
571 pkgAcqMetaClearSig(pkgAcquire * const Owner,
572 IndexTarget const &ClearsignedTarget,
573 IndexTarget const &DetachedDataTarget,
574 IndexTarget const &DetachedSigTarget,
575 std::vector<IndexTarget> const &IndexTargets,
576 indexRecords * const MetaIndexParser);
577 virtual ~pkgAcqMetaClearSig();
578 };
579 /*}}}*/
580 /** \brief Common base class for all classes that deal with fetching indexes {{{*/
581 class APT_HIDDEN pkgAcqBaseIndex : public pkgAcqTransactionItem
582 {
583 void * const d;
584
585 public:
586 /** \brief Get the full pathname of the final file for the current URI */
587 virtual std::string GetFinalFilename() const;
588
589 pkgAcqBaseIndex(pkgAcquire * const Owner, pkgAcqMetaClearSig * const TransactionManager,
590 IndexTarget const &Target);
591 virtual ~pkgAcqBaseIndex();
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 * const d;
606 std::vector<pkgAcqIndexMergeDiffs*> * diffs;
607
608 protected:
609 /** \brief If \b true, debugging information will be written to std::clog. */
610 bool Debug;
611
612 /** \brief A description of the Packages file (stored in
613 * pkgAcquire::ItemDesc::Description).
614 */
615 std::string Description;
616
617 /** \brief Get the full pathname of the final file for the current URI */
618 virtual std::string GetFinalFilename() const;
619
620 virtual bool QueueURI(pkgAcquire::ItemDesc &Item);
621
622 virtual bool TransactionState(TransactionStates const state);
623 public:
624 // Specialized action members
625 virtual void Failed(std::string const &Message, pkgAcquire::MethodConfig const * const Cnf);
626 virtual void Done(std::string const &Message, HashStringList const &Hashes,
627 pkgAcquire::MethodConfig const * const Cnf);
628 virtual std::string DescURI() const {return Target.URI + "Index";};
629 virtual std::string Custom600Headers() const;
630 virtual std::string GetMetaKey() const;
631
632 /** \brief Parse the Index file for a set of Packages diffs.
633 *
634 * Parses the Index file and creates additional download items as
635 * necessary.
636 *
637 * \param IndexDiffFile The name of the Index file.
638 *
639 * \return \b true if the Index file was successfully parsed, \b
640 * false otherwise.
641 */
642 bool ParseDiffIndex(std::string const &IndexDiffFile);
643
644 /** \brief Create a new pkgAcqDiffIndex.
645 *
646 * \param Owner The Acquire object that owns this item.
647 *
648 * \param URI The URI of the list file to download.
649 *
650 * \param URIDesc A long description of the list file to download.
651 *
652 * \param ShortDesc A short description of the list file to download.
653 */
654 pkgAcqDiffIndex(pkgAcquire * const Owner, pkgAcqMetaClearSig * const TransactionManager,
655 IndexTarget const &Target);
656 virtual ~pkgAcqDiffIndex();
657 private:
658 APT_HIDDEN void QueueOnIMSHit() const;
659 };
660 /*}}}*/
661 struct APT_HIDDEN DiffInfo { /*{{{*/
662 /** The filename of the diff. */
663 std::string file;
664
665 /** The hashes of the file after the diff is applied */
666 HashStringList result_hashes;
667
668 /** The hashes of the diff */
669 HashStringList patch_hashes;
670
671 /** The hashes of the compressed diff */
672 HashStringList download_hashes;
673 };
674 /*}}}*/
675 /** \brief An item that is responsible for fetching client-merge patches {{{
676 * that need to be applied to a given package index file.
677 *
678 * Instead of downloading and applying each patch one by one like its
679 * sister #pkgAcqIndexDiffs this class will download all patches at once
680 * and call rred with all the patches downloaded once. Rred will then
681 * merge and apply them in one go, which should be a lot faster – but is
682 * incompatible with server-based merges of patches like reprepro can do.
683 *
684 * \sa pkgAcqDiffIndex, pkgAcqIndex
685 */
686 class APT_HIDDEN pkgAcqIndexMergeDiffs : public pkgAcqBaseIndex
687 {
688 void * const d;
689
690 protected:
691
692 /** \brief If \b true, debugging output will be written to
693 * std::clog.
694 */
695 bool Debug;
696
697 /** \brief description of the file being downloaded. */
698 std::string Description;
699
700 /** \brief information about the current patch */
701 struct DiffInfo const patch;
702
703 /** \brief list of all download items for the patches */
704 std::vector<pkgAcqIndexMergeDiffs*> const * const allPatches;
705
706 /** The current status of this patch. */
707 enum DiffState
708 {
709 /** \brief The diff is currently being fetched. */
710 StateFetchDiff,
711
712 /** \brief The diff is currently being applied. */
713 StateApplyDiff,
714
715 /** \brief the work with this diff is done */
716 StateDoneDiff,
717
718 /** \brief something bad happened and fallback was triggered */
719 StateErrorDiff
720 } State;
721
722 public:
723 /** \brief Called when the patch file failed to be downloaded.
724 *
725 * This method will fall back to downloading the whole index file
726 * outright; its arguments are ignored.
727 */
728 virtual void Failed(std::string const &Message,pkgAcquire::MethodConfig const * const Cnf);
729 virtual void Done(std::string const &Message, HashStringList const &Hashes,
730 pkgAcquire::MethodConfig const * const Cnf);
731 virtual std::string Custom600Headers() const;
732 virtual std::string DescURI() const {return Target.URI + "Index";};
733 virtual HashStringList GetExpectedHashes() const;
734 virtual bool HashesRequired() const;
735
736 /** \brief Create an index merge-diff item.
737 *
738 * \param Owner The pkgAcquire object that owns this item.
739 *
740 * \param URI The URI of the package index file being
741 * reconstructed.
742 *
743 * \param URIDesc A long description of this item.
744 *
745 * \param ShortDesc A brief description of this item.
746 *
747 * \param patch contains infos about the patch this item is supposed
748 * to download which were read from the index
749 *
750 * \param allPatches contains all related items so that each item can
751 * check if it was the last one to complete the download step
752 */
753 pkgAcqIndexMergeDiffs(pkgAcquire * const Owner, pkgAcqMetaClearSig * const TransactionManager,
754 IndexTarget const &Target, DiffInfo const &patch,
755 std::vector<pkgAcqIndexMergeDiffs*> const * const allPatches);
756 virtual ~pkgAcqIndexMergeDiffs();
757 };
758 /*}}}*/
759 /** \brief An item that is responsible for fetching server-merge patches {{{
760 * that need to be applied to a given package index file.
761 *
762 * After downloading and applying a single patch, this item will
763 * enqueue a new pkgAcqIndexDiffs to download and apply the remaining
764 * patches. If no patch can be found that applies to an intermediate
765 * file or if one of the patches cannot be downloaded, falls back to
766 * downloading the entire package index file using pkgAcqIndex.
767 *
768 * \sa pkgAcqDiffIndex, pkgAcqIndex
769 */
770 class APT_HIDDEN pkgAcqIndexDiffs : public pkgAcqBaseIndex
771 {
772 void * const d;
773
774 private:
775
776 /** \brief Queue up the next diff download.
777 *
778 * Search for the next available diff that applies to the file
779 * that currently exists on disk, and enqueue it by calling
780 * QueueURI().
781 *
782 * \return \b true if an applicable diff was found, \b false
783 * otherwise.
784 */
785 APT_HIDDEN bool QueueNextDiff();
786
787 /** \brief Handle tasks that must be performed after the item
788 * finishes downloading.
789 *
790 * Dequeues the item and checks the resulting file's hashsums
791 * against ExpectedHashes after the last patch was applied.
792 * There is no need to check the md5/sha1 after a "normal"
793 * patch because QueueNextDiff() will check the sha1 later.
794 *
795 * \param allDone If \b true, the file was entirely reconstructed,
796 * and its md5sum is verified.
797 */
798 APT_HIDDEN void Finish(bool const allDone=false);
799
800 protected:
801
802 /** \brief If \b true, debugging output will be written to
803 * std::clog.
804 */
805 bool Debug;
806
807 /** A description of the file being downloaded. */
808 std::string Description;
809
810 /** The patches that remain to be downloaded, including the patch
811 * being downloaded right now. This list should be ordered so
812 * that each diff appears before any diff that depends on it.
813 *
814 * \todo These are indexed by sha1sum; why not use some sort of
815 * dictionary instead of relying on ordering and stripping them
816 * off the front?
817 */
818 std::vector<DiffInfo> available_patches;
819
820 /** The current status of this patch. */
821 enum DiffState
822 {
823 /** \brief The diff is in an unknown state. */
824 StateFetchUnkown,
825
826 /** \brief The diff is currently being fetched. */
827 StateFetchDiff,
828
829 /** \brief The diff is currently being applied. */
830 StateApplyDiff
831 } State;
832
833 public:
834
835 /** \brief Called when the patch file failed to be downloaded.
836 *
837 * This method will fall back to downloading the whole index file
838 * outright; its arguments are ignored.
839 */
840 virtual void Failed(std::string const &Message,pkgAcquire::MethodConfig const * const Cnf);
841
842 virtual void Done(std::string const &Message, HashStringList const &Hashes,
843 pkgAcquire::MethodConfig const * const Cnf);
844 virtual std::string Custom600Headers() const;
845 virtual std::string DescURI() const {return Target.URI + "IndexDiffs";};
846 virtual HashStringList GetExpectedHashes() const;
847 virtual bool HashesRequired() const;
848
849 /** \brief Create an index diff item.
850 *
851 * After filling in its basic fields, this invokes Finish(true) if
852 * \a diffs is empty, or QueueNextDiff() otherwise.
853 *
854 * \param Owner The pkgAcquire object that owns this item.
855 *
856 * \param URI The URI of the package index file being
857 * reconstructed.
858 *
859 * \param URIDesc A long description of this item.
860 *
861 * \param ShortDesc A brief description of this item.
862 *
863 * \param diffs The remaining diffs from the index of diffs. They
864 * should be ordered so that each diff appears before any diff
865 * that depends on it.
866 */
867 pkgAcqIndexDiffs(pkgAcquire * const Owner, pkgAcqMetaClearSig * const TransactionManager,
868 IndexTarget const &Target,
869 std::vector<DiffInfo> const &diffs=std::vector<DiffInfo>());
870 virtual ~pkgAcqIndexDiffs();
871 };
872 /*}}}*/
873 /** \brief An acquire item that is responsible for fetching an index {{{
874 * file (e.g., Packages or Sources).
875 *
876 * \sa pkgAcqDiffIndex, pkgAcqIndexDiffs, pkgAcqIndexTrans
877 *
878 * \todo Why does pkgAcqIndex have protected members?
879 */
880 class APT_HIDDEN pkgAcqIndex : public pkgAcqBaseIndex
881 {
882 void * const d;
883
884 protected:
885
886 /** \brief The stages the method goes through
887 *
888 * The method first downloads the indexfile, then its decompressed (or
889 * copied) and verified
890 */
891 enum AllStages {
892 STAGE_DOWNLOAD,
893 STAGE_DECOMPRESS_AND_VERIFY,
894 };
895 AllStages Stage;
896
897 /** \brief Handle what needs to be done when the download is done */
898 void StageDownloadDone(std::string const &Message,
899 HashStringList const &Hashes,
900 pkgAcquire::MethodConfig const * const Cfg);
901
902 /** \brief Handle what needs to be done when the decompression/copy is
903 * done
904 */
905 void StageDecompressDone(std::string const &Message,
906 HashStringList const &Hashes,
907 pkgAcquire::MethodConfig const * const Cfg);
908
909 /** \brief If \b set, this partially downloaded file will be
910 * removed when the download completes.
911 */
912 std::string EraseFileName;
913
914 /** \brief The compression-related file extensions that are being
915 * added to the downloaded file one by one if first fails (e.g., "gz bz2").
916 */
917 std::string CompressionExtensions;
918
919 /** \brief The actual compression extension currently used */
920 std::string CurrentCompressionExtension;
921
922 /** \brief Do the changes needed to fetch via AptByHash (if needed) */
923 void InitByHashIfNeeded();
924
925 /** \brief Auto select the right compression to use */
926 void AutoSelectCompression();
927
928 /** \brief Schedule file for verification after a IMS hit */
929 void ReverifyAfterIMS();
930
931 /** \brief Get the full pathname of the final file for the current URI */
932 virtual std::string GetFinalFilename() const;
933
934 virtual bool TransactionState(TransactionStates const state);
935
936 public:
937 // Specialized action members
938 virtual void Failed(std::string const &Message,pkgAcquire::MethodConfig const * const Cnf);
939 virtual void Done(std::string const &Message, HashStringList const &Hashes,
940 pkgAcquire::MethodConfig const * const Cnf);
941 virtual std::string Custom600Headers() const;
942 virtual std::string DescURI() const {return Desc.URI;};
943 virtual std::string GetMetaKey() const;
944
945 pkgAcqIndex(pkgAcquire * const Owner, pkgAcqMetaClearSig * const TransactionManager,
946 IndexTarget const &Target);
947 virtual ~pkgAcqIndex();
948
949 private:
950 APT_HIDDEN void Init(std::string const &URI, std::string const &URIDesc,
951 std::string const &ShortDesc);
952 };
953 /*}}}*/
954 /** \brief An item that is responsible for fetching a package file. {{{
955 *
956 * If the package file already exists in the cache, nothing will be
957 * done.
958 */
959 class pkgAcqArchive : public pkgAcquire::Item
960 {
961 void * const d;
962
963 bool LocalSource;
964 HashStringList ExpectedHashes;
965
966 protected:
967 /** \brief The package version being fetched. */
968 pkgCache::VerIterator Version;
969
970 /** \brief The list of sources from which to pick archives to
971 * download this package from.
972 */
973 pkgSourceList *Sources;
974
975 /** \brief A package records object, used to look up the file
976 * corresponding to each version of the package.
977 */
978 pkgRecords *Recs;
979
980 /** \brief A location in which the actual filename of the package
981 * should be stored.
982 */
983 std::string &StoreFilename;
984
985 /** \brief The next file for this version to try to download. */
986 pkgCache::VerFileIterator Vf;
987
988 /** \brief How many (more) times to try to find a new source from
989 * which to download this package version if it fails.
990 *
991 * Set from Acquire::Retries.
992 */
993 unsigned int Retries;
994
995 /** \brief \b true if this version file is being downloaded from a
996 * trusted source.
997 */
998 bool Trusted;
999
1000 /** \brief Queue up the next available file for this version. */
1001 bool QueueNext();
1002
1003 /** \brief Get the full pathname of the final file for the current URI */
1004 virtual std::string GetFinalFilename() const;
1005
1006 public:
1007
1008 virtual void Failed(std::string const &Message,pkgAcquire::MethodConfig const * const Cnf);
1009 virtual void Done(std::string const &Message, HashStringList const &Hashes,
1010 pkgAcquire::MethodConfig const * const Cnf);
1011 virtual std::string DescURI() const;
1012 virtual std::string ShortDesc() const;
1013 virtual void Finished();
1014 virtual bool IsTrusted() const;
1015 virtual HashStringList GetExpectedHashes() const;
1016 virtual bool HashesRequired() const;
1017
1018 /** \brief Create a new pkgAcqArchive.
1019 *
1020 * \param Owner The pkgAcquire object with which this item is
1021 * associated.
1022 *
1023 * \param Sources The sources from which to download version
1024 * files.
1025 *
1026 * \param Recs A package records object, used to look up the file
1027 * corresponding to each version of the package.
1028 *
1029 * \param Version The package version to download.
1030 *
1031 * \param[out] StoreFilename A location in which the actual filename of
1032 * the package should be stored. It will be set to a guessed
1033 * basename in the constructor, and filled in with a fully
1034 * qualified filename once the download finishes.
1035 */
1036 pkgAcqArchive(pkgAcquire * const Owner,pkgSourceList * const Sources,
1037 pkgRecords * const Recs,pkgCache::VerIterator const &Version,
1038 std::string &StoreFilename);
1039 virtual ~pkgAcqArchive();
1040 };
1041 /*}}}*/
1042 /** \brief Retrieve the changelog for the given version {{{
1043 *
1044 * Downloads the changelog to a temporary file it will also remove again
1045 * while it is deconstructed or downloads it to a named location.
1046 */
1047 class pkgAcqChangelog : public pkgAcquire::Item
1048 {
1049 void * const d;
1050 std::string TemporaryDirectory;
1051 std::string const SrcName;
1052 std::string const SrcVersion;
1053
1054 public:
1055 // we will never have hashes for changelogs.
1056 // If you need verified ones, download the deb and extract the changelog.
1057 virtual HashStringList GetExpectedHashes() const { return HashStringList(); }
1058 virtual bool HashesRequired() const { return false; }
1059
1060 // Specialized action members
1061 virtual void Failed(std::string const &Message,pkgAcquire::MethodConfig const * const Cnf);
1062 virtual void Done(std::string const &Message, HashStringList const &CalcHashes,
1063 pkgAcquire::MethodConfig const * const Cnf);
1064 virtual std::string DescURI() const {return Desc.URI;};
1065
1066 /** returns the URI to the changelog of this version
1067 *
1068 * @param Ver is the version to get the changelog for
1069 * @return the URI which will be used to acquire the changelog
1070 */
1071 static std::string URI(pkgCache::VerIterator const &Ver);
1072
1073 /** returns the URI to the changelog of this version
1074 *
1075 * \param Rls is the Release file the package comes from
1076 * \param Component in which the package resides, can be empty
1077 * \param SrcName is the source package name
1078 * \param SrcVersion is the source package version
1079 * @return the URI which will be used to acquire the changelog
1080 */
1081 static std::string URI(pkgCache::RlsFileIterator const &Rls,
1082 char const * const Component, char const * const SrcName,
1083 char const * const SrcVersion);
1084
1085 /** returns the URI to the changelog of this version
1086 *
1087 * \param Template URI where CHANGEPATH has to be filled in
1088 * \param Component in which the package resides, can be empty
1089 * \param SrcName is the source package name
1090 * \param SrcVersion is the source package version
1091 * @return the URI which will be used to acquire the changelog
1092 */
1093 static std::string URI(std::string const &Template,
1094 char const * const Component, char const * const SrcName,
1095 char const * const SrcVersion);
1096
1097 /** returns the URI template for this release file
1098 *
1099 * \param Rls is a Release file
1100 * @return the URI template to use for this release file
1101 */
1102 static std::string URITemplate(pkgCache::RlsFileIterator const &Rls);
1103
1104 /** \brief Create a new pkgAcqChangelog object.
1105 *
1106 * \param Owner The pkgAcquire object with which this object is
1107 * associated.
1108 * \param Ver is the version to get the changelog for
1109 * \param DestDir The directory the file should be downloaded into.
1110 * Will be an autocreated (and cleaned up) temporary directory if not set.
1111 * \param DestFilename The filename the file should have in #DestDir
1112 * Defaults to sourcepackagename.changelog if not set.
1113 */
1114 pkgAcqChangelog(pkgAcquire * const Owner, pkgCache::VerIterator const &Ver,
1115 std::string const &DestDir="", std::string const &DestFilename="");
1116
1117 /** \brief Create a new pkgAcqChangelog object.
1118 *
1119 * \param Owner The pkgAcquire object with which this object is
1120 * associated.
1121 * \param Rls is the Release file the package comes from
1122 * \param Component in which the package resides, can be empty
1123 * \param SrcName is the source package name
1124 * \param SrcVersion is the source package version
1125 * \param DestDir The directory the file should be downloaded into.
1126 * Will be an autocreated (and cleaned up) temporary directory if not set.
1127 * \param DestFilename The filename the file should have in #DestDir
1128 * Defaults to sourcepackagename.changelog if not set.
1129 */
1130 pkgAcqChangelog(pkgAcquire * const Owner, pkgCache::RlsFileIterator const &Rls,
1131 char const * const Component, char const * const SrcName, char const * const SrcVersion,
1132 std::string const &DestDir="", std::string const &DestFilename="");
1133
1134 /** \brief Create a new pkgAcqChangelog object.
1135 *
1136 * \param Owner The pkgAcquire object with which this object is
1137 * associated.
1138 * \param URI is to be used to get the changelog
1139 * \param SrcName is the source package name
1140 * \param SrcVersion is the source package version
1141 * \param DestDir The directory the file should be downloaded into.
1142 * Will be an autocreated (and cleaned up) temporary directory if not set.
1143 * \param DestFilename The filename the file should have in #DestDir
1144 * Defaults to sourcepackagename.changelog if not set.
1145 */
1146 pkgAcqChangelog(pkgAcquire * const Owner, std::string const &URI,
1147 char const * const SrcName, char const * const SrcVersion,
1148 std::string const &DestDir="", std::string const &DestFilename="");
1149
1150 virtual ~pkgAcqChangelog();
1151
1152 private:
1153 APT_HIDDEN void Init(std::string const &DestDir, std::string const &DestFilename);
1154 };
1155 /*}}}*/
1156 /** \brief Retrieve an arbitrary file to the current directory. {{{
1157 *
1158 * The file is retrieved even if it is accessed via a URL type that
1159 * normally is a NOP, such as "file". If the download fails, the
1160 * partial file is renamed to get a ".FAILED" extension.
1161 */
1162 class pkgAcqFile : public pkgAcquire::Item
1163 {
1164 void * const d;
1165
1166 /** \brief How many times to retry the download, set from
1167 * Acquire::Retries.
1168 */
1169 unsigned int Retries;
1170
1171 /** \brief Should this file be considered a index file */
1172 bool IsIndexFile;
1173
1174 HashStringList const ExpectedHashes;
1175 public:
1176 virtual HashStringList GetExpectedHashes() const;
1177 virtual bool HashesRequired() const;
1178
1179 // Specialized action members
1180 virtual void Failed(std::string const &Message,pkgAcquire::MethodConfig const * const Cnf);
1181 virtual void Done(std::string const &Message, HashStringList const &CalcHashes,
1182 pkgAcquire::MethodConfig const * const Cnf);
1183 virtual std::string DescURI() const {return Desc.URI;};
1184 virtual std::string Custom600Headers() const;
1185
1186 /** \brief Create a new pkgAcqFile object.
1187 *
1188 * \param Owner The pkgAcquire object with which this object is
1189 * associated.
1190 *
1191 * \param URI The URI to download.
1192 *
1193 * \param Hashes The hashsums of the file to download, if they are known;
1194 * otherwise empty list.
1195 *
1196 * \param Size The size of the file to download, if it is known;
1197 * otherwise 0.
1198 *
1199 * \param Desc A description of the file being downloaded.
1200 *
1201 * \param ShortDesc A brief description of the file being
1202 * downloaded.
1203 *
1204 * \param DestDir The directory the file should be downloaded into.
1205 *
1206 * \param DestFilename The filename+path the file is downloaded to.
1207 *
1208 * \param IsIndexFile The file is considered a IndexFile and cache-control
1209 * headers like "cache-control: max-age=0" are send
1210 *
1211 * If DestFilename is empty, download to DestDir/\<basename\> if
1212 * DestDir is non-empty, $CWD/\<basename\> otherwise. If
1213 * DestFilename is NOT empty, DestDir is ignored and DestFilename
1214 * is the absolute name to which the file should be downloaded.
1215 */
1216
1217 pkgAcqFile(pkgAcquire * const Owner, std::string const &URI, HashStringList const &Hashes, unsigned long long const Size,
1218 std::string const &Desc, std::string const &ShortDesc,
1219 std::string const &DestDir="", std::string const &DestFilename="",
1220 bool const IsIndexFile=false);
1221 virtual ~pkgAcqFile();
1222 };
1223 /*}}}*/
1224 /** @} */
1225
1226 #endif