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