]> git.saurik.com Git - apt.git/blame - apt-pkg/acquire-item.h
rework hashsum verification in the acquire system
[apt.git] / apt-pkg / acquire-item.h
CommitLineData
0118833a
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
7db98ffc 3// $Id: acquire-item.h,v 1.26.2.3 2004/01/02 18:51:00 mdz Exp $
0118833a
AL
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
448c38bd
DK
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
770c32ec 12 Three item classes are provided to provide functionality for
a52f938b 13 downloading of Index, Translation and Packages files.
448c38bd 14
495e5cb2 15 A Archive class is provided for downloading .deb files. It does Hash
17caf1b1 16 checking and source location as well as a retry algorithm.
448c38bd 17
0118833a
AL
18 ##################################################################### */
19 /*}}}*/
20#ifndef PKGLIB_ACQUIRE_ITEM_H
21#define PKGLIB_ACQUIRE_ITEM_H
22
23#include <apt-pkg/acquire.h>
495e5cb2 24#include <apt-pkg/hashes.h>
229fb1a3 25#include <apt-pkg/weakptr.h>
472ff00e 26#include <apt-pkg/pkgcache.h>
453b82a3
DK
27#include <apt-pkg/cacheiterators.h>
28
29#include <string>
30#include <vector>
0118833a 31
b9dadc24
DK
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
3174e150
MV
40/** \addtogroup acquire
41 * @{
42 *
43 * \file acquire-item.h
44 */
45
472ff00e
DK
46class indexRecords;
47class pkgRecords;
48class pkgSourceList;
fa3b260f 49class IndexTarget;
715c65de 50class pkgAcqMetaBase;
472ff00e 51
448c38bd
DK
52class 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 /*}}}*/
79class 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 /*}}}*/
92class pkgAcquire::Item : public WeakPointable /*{{{*/
93/** \brief Represents the process by which a pkgAcquire object should
3174e150
MV
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 */
448c38bd 105{
0118833a
AL
106 public:
107
3174e150
MV
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 */
6ca714d5
MV
128 StatAuthError,
129
448c38bd 130 /** \brief The item was could not be downloaded because of
6ca714d5
MV
131 * a transient network error (e.g. network down)
132 */
56472095 133 StatTransientNetworkError,
3174e150
MV
134 } Status;
135
136 /** \brief Contains a textual description of the error encountered
255c9e4b 137 * if #ItemState is #StatError or #StatAuthError.
3174e150 138 */
472ff00e 139 std::string ErrorText;
3174e150
MV
140
141 /** \brief The size of the object to fetch. */
e2c66de5 142 unsigned long long FileSize;
3174e150
MV
143
144 /** \brief How much of the object was already fetched. */
e2c66de5 145 unsigned long long PartialSize;
3174e150
MV
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 */
ffbe056d
DK
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;
3174e150
MV
156
157 /** \brief A client-supplied unique identifier.
448c38bd 158 *
3174e150
MV
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 *
448c38bd 162 * APT progress reporting will store an ID there as shown in "Get:42 …"
3174e150 163 */
b98f2859 164 unsigned long ID;
3174e150
MV
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 */
8267fe24 170 bool Complete;
3174e150
MV
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 */
a6568219 177 bool Local;
448c38bd 178
472ff00e 179 std::string UsedMirror;
30e1eab5 180
3174e150
MV
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 */
0118833a 189 unsigned int QueueCounter;
d0cfa8ad
MV
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;
448c38bd 200
3174e150
MV
201 /** \brief The name of the file into which the retrieved object
202 * will be written.
203 */
472ff00e 204 std::string DestFile;
7d8afa39 205
3174e150
MV
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 */
448c38bd 219 virtual void Failed(std::string const &Message,pkgAcquire::MethodConfig const * const Cnf);
3174e150
MV
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.
b3501edb 234 * \param Hashes The HashSums of the object that was fetched.
3174e150
MV
235 * \param Cnf The method via which the object was fetched.
236 *
237 * \sa pkgAcqMethod
238 */
448c38bd
DK
239 virtual void Done(std::string const &Message, HashStringList const &Hashes,
240 pkgAcquire::MethodConfig const * const Cnf);
3174e150
MV
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 *
448c38bd 247 * \param Hashes The expected hashes of the object being fetched.
3174e150
MV
248 *
249 * \sa pkgAcqMethod
250 */
448c38bd 251 virtual void Start(std::string const &Message, unsigned long long const Size);
3174e150
MV
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 */
448c38bd 261 virtual std::string Custom600Headers() const;
3174e150
MV
262
263 /** \brief A "descriptive" URI-like string.
264 *
265 * \return a URI that should be used to describe what is being fetched.
266 */
448c38bd 267 virtual std::string DescURI() const = 0;
3174e150
MV
268 /** \brief Short item description.
269 *
270 * \return a brief description of the object being fetched.
271 */
448c38bd 272 virtual std::string ShortDesc() const;
3174e150
MV
273
274 /** \brief Invoked by the worker when the download is completely done. */
448c38bd
DK
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
3174e150 283 *
448c38bd
DK
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.
3174e150 287 */
448c38bd 288 virtual bool HashesRequired() const { return true; }
3174e150
MV
289
290 /** \return the acquire process with which this item is associated. */
448c38bd 291 pkgAcquire *GetOwner() const;
3174e150
MV
292
293 /** \return \b true if this object is being fetched from a trusted source. */
448c38bd
DK
294 virtual bool IsTrusted() const;
295
702c84fb 296 /** \brief Report mirror problem
448c38bd 297 *
702c84fb
MV
298 * This allows reporting mirror failures back to a centralized
299 * server. The apt-report-mirror-failure script is called for this
448c38bd 300 *
702c84fb
MV
301 * \param FailCode A short failure string that is send
302 */
448c38bd 303 void ReportMirrorFailure(std::string const &FailCode);
36280399 304
eeac6897
MV
305 /** \brief Set the name of the current active subprocess
306 *
307 * See also #ActiveSubprocess
308 */
448c38bd 309 void SetActiveSubprocess(std::string const &subprocess);
eeac6897 310
3174e150
MV
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 *
3174e150
MV
317 * \param Owner The new owner of this item.
318 */
448c38bd 319 Item(pkgAcquire * const Owner);
3174e150
MV
320
321 /** \brief Remove this item from its owner's queue by invoking
322 * pkgAcquire::Remove.
323 */
0118833a 324 virtual ~Item();
3c8030a4
DK
325
326 protected:
448c38bd
DK
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;
3c8030a4
DK
332
333 enum RenameOnErrorState {
334 HashSumMismatch,
335 SizeMismatch,
631a7dc7
MV
336 InvalidFormat,
337 SignatureError,
338 NotClearsigned,
146f7715
DK
339 MaximumSizeExceeded,
340 PDiffError,
3c8030a4
DK
341 };
342
343 /** \brief Rename failed file and set error
344 *
345 * \param state respresenting the error we encountered
3c8030a4
DK
346 */
347 bool RenameOnError(RenameOnErrorState const state);
fa3b260f 348
448c38bd
DK
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 /*}}}*/
384class 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
146f7715
DK
400 enum TransactionStates {
401 TransactionCommit,
402 TransactionAbort,
403 };
404 virtual bool TransactionState(TransactionStates const state);
405
448c38bd
DK
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;
fa3b260f 410
3174e150 411
448c38bd
DK
412 pkgAcqTransactionItem(pkgAcquire * const Owner, pkgAcqMetaBase * const TransactionManager, IndexTarget const * const Target);
413 virtual ~pkgAcqTransactionItem();
f6d4ab9a 414
448c38bd 415 friend class pkgAcqMetaBase;
002d9943 416};
92fcbfc1 417 /*}}}*/
448c38bd
DK
418class APT_HIDDEN pkgAcqMetaBase : public pkgAcqTransactionItem /*{{{*/
419/** \brief the manager of a transaction */
e6e89390 420{
60323ed7
MV
421 void *d;
422
e6e89390 423 protected:
448c38bd 424 std::vector<pkgAcqTransactionItem*> Transaction;
715c65de 425
448c38bd
DK
426 IndexTarget const DataTarget;
427 public:
c045cc02
MV
428 /** \brief A package-system-specific parser for the meta-index file. */
429 indexRecords *MetaIndexParser;
6bf93605 430 indexRecords *LastMetaIndexParser;
448c38bd 431 protected:
c045cc02
MV
432
433 /** \brief The index files which should be looked up in the meta-index
434 * and then downloaded.
435 */
448c38bd 436 const std::vector<IndexTarget*>* const IndexTargets;
c045cc02 437
fa3a96a1
MV
438 /** \brief If \b true, the index's signature is currently being verified.
439 */
440 bool AuthPass;
441
c045cc02
MV
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 */
448c38bd 450 void QueueIndexes(bool const verify);
c045cc02 451
f3097647
MV
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 */
448c38bd 461 bool CheckDownloadDone(pkgAcqTransactionItem * const I, const std::string &Message, HashStringList const &Hashes) const;
f3097647
MV
462
463 /** \brief Queue the downloaded Signature for verification */
448c38bd 464 void QueueForSignatureVerify(pkgAcqTransactionItem * const I, std::string const &File, std::string const &Signature);
f3097647 465
295d848b 466 virtual std::string Custom600Headers() const;
27e6c17a 467
f3097647
MV
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 */
448c38bd 477 bool CheckAuthDone(std::string const &Message);
f3097647 478
2d0a7bb4 479 /** Check if the current item should fail at this point */
6bf93605 480 bool CheckStopAuthentication(pkgAcquire::Item * const I, const std::string &Message);
2d0a7bb4 481
f3097647
MV
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 */
448c38bd 487 bool VerifyVendor(std::string const &Message);
295d848b 488
6bf93605 489 virtual bool TransactionState(TransactionStates const state);
295d848b 490
715c65de 491 public:
ba6b79bd
DK
492 // This refers more to the Transaction-Manager than the actual file
493 bool IMSHit;
494
ba6b79bd 495 virtual bool QueueURI(pkgAcquire::ItemDesc &Item);
448c38bd
DK
496 virtual HashStringList GetExpectedHashes() const;
497 virtual bool HashesRequired() const;
295d848b 498
715c65de 499 // transaction code
448c38bd 500 void Add(pkgAcqTransactionItem * const I);
715c65de 501 void AbortTransaction();
448c38bd 502 bool TransactionHasError() const;
715c65de
MV
503 void CommitTransaction();
504
dce45dbe 505 /** \brief Stage (queue) a copy action when the transaction is committed
fa3a96a1 506 */
448c38bd
DK
507 void TransactionStageCopy(pkgAcqTransactionItem * const I,
508 const std::string &From,
fa3a96a1 509 const std::string &To);
dce45dbe 510 /** \brief Stage (queue) a removal action when the transaction is committed
fa3a96a1 511 */
448c38bd 512 void TransactionStageRemoval(pkgAcqTransactionItem * const I, const std::string &FinalFile);
fa3a96a1 513
6bf93605
DK
514 /** \brief Get the full pathname of the final file for the current URI */
515 virtual std::string GetFinalFilename() const;
516
448c38bd
DK
517 pkgAcqMetaBase(pkgAcquire * const Owner, pkgAcqMetaBase * const TransactionManager,
518 std::vector<IndexTarget*> const * const IndexTargets,
519 IndexTarget const &DataTarget,
520 indexRecords* const MetaIndexParser);
e6e89390 521};
dce45dbe 522 /*}}}*/
56472095
MV
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 */
dce45dbe 533class APT_HIDDEN pkgAcqMetaIndex : public pkgAcqMetaBase
56472095 534{
60323ed7
MV
535 void *d;
536
56472095 537 protected:
448c38bd 538 IndexTarget const DetachedSigTarget;
e05672e8
MV
539
540 /** \brief delayed constructor */
448c38bd
DK
541 void Init(std::string const &URIDesc, std::string const &ShortDesc);
542
56472095 543 public:
448c38bd 544 virtual std::string DescURI() const;
631a7dc7 545
56472095 546 // Specialized action members
448c38bd
DK
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);
56472095
MV
550 virtual void Finished();
551
552 /** \brief Create a new pkgAcqMetaIndex. */
448c38bd
DK
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);
6bf93605
DK
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 */
448c38bd 568class APT_HIDDEN pkgAcqMetaSig : public pkgAcqTransactionItem
6bf93605
DK
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
6bf93605
DK
579 /** \brief Get the full pathname of the final file for the current URI */
580 virtual std::string GetFinalFilename() const;
581
582 public:
448c38bd 583 virtual bool HashesRequired() const { return false; }
6bf93605
DK
584
585 // Specialized action members
448c38bd
DK
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);
6bf93605
DK
589
590 /** \brief Create a new pkgAcqMetaSig. */
448c38bd
DK
591 pkgAcqMetaSig(pkgAcquire * const Owner, pkgAcqMetaBase * const TransactionManager, IndexTarget const * const Target,
592 pkgAcqMetaIndex * const MetaIndex);
6bf93605 593 virtual ~pkgAcqMetaSig();
56472095
MV
594};
595 /*}}}*/
596/** \brief An item repsonsible for downloading clearsigned metaindexes {{{*/
dce45dbe 597class APT_HIDDEN pkgAcqMetaClearSig : public pkgAcqMetaIndex
56472095 598{
60323ed7
MV
599 void *d;
600
448c38bd
DK
601 IndexTarget const ClearsignedTarget;
602 IndexTarget const DetachedDataTarget;
603 IndexTarget const DetachedSigTarget;
56472095
MV
604
605public:
448c38bd 606 virtual void Failed(std::string const &Message,pkgAcquire::MethodConfig const * const Cnf);
56472095 607 virtual std::string Custom600Headers() const;
448c38bd
DK
608 virtual void Done(std::string const &Message, HashStringList const &Hashes,
609 pkgAcquire::MethodConfig const * const Cnf);
56472095
MV
610
611 /** \brief Create a new pkgAcqMetaClearSig. */
448c38bd
DK
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);
56472095
MV
618 virtual ~pkgAcqMetaClearSig();
619};
620 /*}}}*/
448c38bd
DK
621/** \brief Common base class for all classes that deal with fetching indexes {{{*/
622class APT_HIDDEN pkgAcqBaseIndex : public pkgAcqTransactionItem
c2184314 623{
60323ed7
MV
624 void *d;
625
448c38bd 626 public:
295d848b
DK
627 /** \brief Get the full pathname of the final file for the current URI */
628 virtual std::string GetFinalFilename() const;
629
448c38bd
DK
630 pkgAcqBaseIndex(pkgAcquire * const Owner, pkgAcqMetaBase * const TransactionManager,
631 IndexTarget const * const Target);
c2184314 632};
0b58b3f8 633 /*}}}*/
92fcbfc1 634/** \brief An item that is responsible for fetching an index file of {{{
3174e150
MV
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 */
dce45dbe 643class APT_HIDDEN pkgAcqDiffIndex : public pkgAcqBaseIndex
2237bd01 644{
60323ed7
MV
645 void *d;
646
2237bd01 647 protected:
3174e150 648 /** \brief If \b true, debugging information will be written to std::clog. */
2237bd01 649 bool Debug;
3174e150 650
3174e150
MV
651 /** \brief The index file which will be patched to generate the new
652 * file.
653 */
472ff00e 654 std::string CurrentPackagesFile;
3174e150
MV
655
656 /** \brief A description of the Packages file (stored in
657 * pkgAcquire::ItemDesc::Description).
658 */
472ff00e 659 std::string Description;
2237bd01 660
295d848b
DK
661 /** \brief Get the full pathname of the final file for the current URI */
662 virtual std::string GetFinalFilename() const;
663
ba6b79bd 664 virtual bool QueueURI(pkgAcquire::ItemDesc &Item);
146f7715
DK
665
666 virtual bool TransactionState(TransactionStates const state);
2237bd01
MV
667 public:
668 // Specialized action members
448c38bd
DK
669 virtual void Failed(std::string const &Message, pkgAcquire::MethodConfig const * const Cnf);
670 virtual void Done(std::string const &Message, HashStringList const &Hashes,
671 pkgAcquire::MethodConfig const * const Cnf);
672 virtual std::string DescURI() const {return Target->URI + "Index";};
b3501edb 673 virtual std::string Custom600Headers() const;
448c38bd 674 virtual std::string GetMetaKey() const;
2237bd01 675
3174e150
MV
676 /** \brief Parse the Index file for a set of Packages diffs.
677 *
678 * Parses the Index file and creates additional download items as
679 * necessary.
680 *
681 * \param IndexDiffFile The name of the Index file.
682 *
683 * \return \b true if the Index file was successfully parsed, \b
684 * false otherwise.
685 */
448c38bd 686 bool ParseDiffIndex(std::string const &IndexDiffFile);
3174e150
MV
687
688 /** \brief Create a new pkgAcqDiffIndex.
689 *
690 * \param Owner The Acquire object that owns this item.
691 *
692 * \param URI The URI of the list file to download.
693 *
694 * \param URIDesc A long description of the list file to download.
695 *
696 * \param ShortDesc A short description of the list file to download.
3174e150 697 */
448c38bd
DK
698 pkgAcqDiffIndex(pkgAcquire * const Owner, pkgAcqMetaBase * const TransactionManager,
699 IndexTarget const * const Target);
ba6b79bd
DK
700 private:
701 APT_HIDDEN void QueueOnIMSHit() const;
002d9943 702};
92fcbfc1 703 /*}}}*/
448c38bd
DK
704struct APT_HIDDEN DiffInfo { /*{{{*/
705 /** The filename of the diff. */
706 std::string file;
707
708 /** The hashes of the diff */
709 HashStringList result_hashes;
710
711 /** The hashes of the file after the diff is applied */
712 HashStringList patch_hashes;
713
714 /** The size of the file after the diff is applied */
715 unsigned long long result_size;
716
717 /** The size of the diff itself */
718 unsigned long long patch_size;
719};
720 /*}}}*/
47d2bc78
DK
721/** \brief An item that is responsible for fetching client-merge patches {{{
722 * that need to be applied to a given package index file.
723 *
724 * Instead of downloading and applying each patch one by one like its
725 * sister #pkgAcqIndexDiffs this class will download all patches at once
726 * and call rred with all the patches downloaded once. Rred will then
727 * merge and apply them in one go, which should be a lot faster – but is
728 * incompatible with server-based merges of patches like reprepro can do.
729 *
730 * \sa pkgAcqDiffIndex, pkgAcqIndex
731 */
dce45dbe 732class APT_HIDDEN pkgAcqIndexMergeDiffs : public pkgAcqBaseIndex
47d2bc78 733{
60323ed7
MV
734 void *d;
735
47d2bc78
DK
736 protected:
737
738 /** \brief If \b true, debugging output will be written to
739 * std::clog.
740 */
741 bool Debug;
742
47d2bc78
DK
743 /** \brief description of the file being downloaded. */
744 std::string Description;
745
746 /** \brief information about the current patch */
747 struct DiffInfo const patch;
748
749 /** \brief list of all download items for the patches */
750 std::vector<pkgAcqIndexMergeDiffs*> const * const allPatches;
751
752 /** The current status of this patch. */
753 enum DiffState
754 {
755 /** \brief The diff is currently being fetched. */
756 StateFetchDiff,
757
758 /** \brief The diff is currently being applied. */
759 StateApplyDiff,
760
761 /** \brief the work with this diff is done */
762 StateDoneDiff,
763
764 /** \brief something bad happened and fallback was triggered */
765 StateErrorDiff
766 } State;
767
768 public:
769 /** \brief Called when the patch file failed to be downloaded.
770 *
771 * This method will fall back to downloading the whole index file
772 * outright; its arguments are ignored.
773 */
448c38bd
DK
774 virtual void Failed(std::string const &Message,pkgAcquire::MethodConfig const * const Cnf);
775 virtual void Done(std::string const &Message, HashStringList const &Hashes,
776 pkgAcquire::MethodConfig const * const Cnf);
777 virtual std::string DescURI() const {return Target->URI + "Index";};
778 virtual HashStringList GetExpectedHashes() const;
779 virtual bool HashesRequired() const;
47d2bc78
DK
780
781 /** \brief Create an index merge-diff item.
782 *
783 * \param Owner The pkgAcquire object that owns this item.
784 *
785 * \param URI The URI of the package index file being
786 * reconstructed.
787 *
788 * \param URIDesc A long description of this item.
789 *
790 * \param ShortDesc A brief description of this item.
791 *
47d2bc78
DK
792 * \param patch contains infos about the patch this item is supposed
793 * to download which were read from the index
794 *
795 * \param allPatches contains all related items so that each item can
796 * check if it was the last one to complete the download step
797 */
448c38bd
DK
798 pkgAcqIndexMergeDiffs(pkgAcquire * const Owner, pkgAcqMetaBase * const TransactionManager,
799 IndexTarget const * const Target,
800 DiffInfo const &patch,
c2184314 801 std::vector<pkgAcqIndexMergeDiffs*> const * const allPatches);
47d2bc78
DK
802};
803 /*}}}*/
804/** \brief An item that is responsible for fetching server-merge patches {{{
3174e150
MV
805 * that need to be applied to a given package index file.
806 *
807 * After downloading and applying a single patch, this item will
808 * enqueue a new pkgAcqIndexDiffs to download and apply the remaining
809 * patches. If no patch can be found that applies to an intermediate
810 * file or if one of the patches cannot be downloaded, falls back to
811 * downloading the entire package index file using pkgAcqIndex.
812 *
813 * \sa pkgAcqDiffIndex, pkgAcqIndex
814 */
dce45dbe 815class APT_HIDDEN pkgAcqIndexDiffs : public pkgAcqBaseIndex
ac5b205a 816{
60323ed7
MV
817 void *d;
818
3174e150
MV
819 private:
820
821 /** \brief Queue up the next diff download.
822 *
823 * Search for the next available diff that applies to the file
824 * that currently exists on disk, and enqueue it by calling
825 * QueueURI().
826 *
827 * \return \b true if an applicable diff was found, \b false
828 * otherwise.
829 */
3809194b 830 APT_HIDDEN bool QueueNextDiff();
3174e150
MV
831
832 /** \brief Handle tasks that must be performed after the item
833 * finishes downloading.
834 *
b3501edb
DK
835 * Dequeues the item and checks the resulting file's hashsums
836 * against ExpectedHashes after the last patch was applied.
3174e150
MV
837 * There is no need to check the md5/sha1 after a "normal"
838 * patch because QueueNextDiff() will check the sha1 later.
839 *
840 * \param allDone If \b true, the file was entirely reconstructed,
841 * and its md5sum is verified.
842 */
448c38bd 843 APT_HIDDEN void Finish(bool const allDone=false);
3174e150 844
ac5b205a 845 protected:
3174e150
MV
846
847 /** \brief If \b true, debugging output will be written to
848 * std::clog.
849 */
ac5b205a 850 bool Debug;
3174e150 851
3174e150 852 /** A description of the file being downloaded. */
472ff00e 853 std::string Description;
3174e150
MV
854
855 /** The patches that remain to be downloaded, including the patch
856 * being downloaded right now. This list should be ordered so
857 * that each diff appears before any diff that depends on it.
858 *
859 * \todo These are indexed by sha1sum; why not use some sort of
860 * dictionary instead of relying on ordering and stripping them
861 * off the front?
862 */
472ff00e 863 std::vector<DiffInfo> available_patches;
8a3207f4 864
3174e150
MV
865 /** The current status of this patch. */
866 enum DiffState
867 {
868 /** \brief The diff is in an unknown state. */
869 StateFetchUnkown,
870
871 /** \brief The diff is currently being fetched. */
872 StateFetchDiff,
3174e150
MV
873
874 /** \brief The diff is currently being applied. */
875 StateApplyDiff
876 } State;
6cb30d01 877
ac5b205a 878 public:
448c38bd 879
3174e150
MV
880 /** \brief Called when the patch file failed to be downloaded.
881 *
882 * This method will fall back to downloading the whole index file
883 * outright; its arguments are ignored.
884 */
448c38bd 885 virtual void Failed(std::string const &Message,pkgAcquire::MethodConfig const * const Cnf);
3174e150 886
448c38bd
DK
887 virtual void Done(std::string const &Message, HashStringList const &Hashes,
888 pkgAcquire::MethodConfig const * const Cnf);
889 virtual std::string DescURI() const {return Target->URI + "IndexDiffs";};
890 virtual HashStringList GetExpectedHashes() const;
891 virtual bool HashesRequired() const;
ac5b205a 892
3174e150
MV
893 /** \brief Create an index diff item.
894 *
895 * After filling in its basic fields, this invokes Finish(true) if
255c9e4b 896 * \a diffs is empty, or QueueNextDiff() otherwise.
3174e150
MV
897 *
898 * \param Owner The pkgAcquire object that owns this item.
899 *
900 * \param URI The URI of the package index file being
901 * reconstructed.
902 *
903 * \param URIDesc A long description of this item.
904 *
905 * \param ShortDesc A brief description of this item.
906 *
3174e150
MV
907 * \param diffs The remaining diffs from the index of diffs. They
908 * should be ordered so that each diff appears before any diff
909 * that depends on it.
910 */
448c38bd
DK
911 pkgAcqIndexDiffs(pkgAcquire * const Owner, pkgAcqMetaBase * const TransactionManager,
912 IndexTarget const * const Target,
913 std::vector<DiffInfo> const &diffs=std::vector<DiffInfo>());
ac5b205a 914};
92fcbfc1
DK
915 /*}}}*/
916/** \brief An acquire item that is responsible for fetching an index {{{
3174e150
MV
917 * file (e.g., Packages or Sources).
918 *
919 * \sa pkgAcqDiffIndex, pkgAcqIndexDiffs, pkgAcqIndexTrans
920 *
921 * \todo Why does pkgAcqIndex have protected members?
922 */
dce45dbe 923class APT_HIDDEN pkgAcqIndex : public pkgAcqBaseIndex
0118833a 924{
60323ed7
MV
925 void *d;
926
0118833a 927 protected:
3174e150 928
651bddad
MV
929 /** \brief The stages the method goes through
930 *
931 * The method first downloads the indexfile, then its decompressed (or
932 * copied) and verified
3174e150 933 */
651bddad
MV
934 enum AllStages {
935 STAGE_DOWNLOAD,
936 STAGE_DECOMPRESS_AND_VERIFY,
937 };
938 AllStages Stage;
3174e150 939
651bddad 940 /** \brief Handle what needs to be done when the download is done */
448c38bd 941 void StageDownloadDone(std::string const &Message,
651bddad 942 HashStringList const &Hashes,
448c38bd 943 pkgAcquire::MethodConfig const * const Cfg);
651bddad
MV
944
945 /** \brief Handle what needs to be done when the decompression/copy is
946 * done
3174e150 947 */
448c38bd 948 void StageDecompressDone(std::string const &Message,
651bddad 949 HashStringList const &Hashes,
448c38bd 950 pkgAcquire::MethodConfig const * const Cfg);
3174e150 951
1e8ba0d4
MV
952 /** \brief If \b set, this partially downloaded file will be
953 * removed when the download completes.
954 */
955 std::string EraseFileName;
956
5d885723
DK
957 /** \brief The compression-related file extensions that are being
958 * added to the downloaded file one by one if first fails (e.g., "gz bz2").
3174e150 959 */
651bddad
MV
960 std::string CompressionExtensions;
961
962 /** \brief The actual compression extension currently used */
1e8ba0d4 963 std::string CurrentCompressionExtension;
13e8426f 964
59194959 965 /** \brief Do the changes needed to fetch via AptByHash (if needed) */
448c38bd 966 void InitByHashIfNeeded();
59194959 967
56472095
MV
968 /** \brief Auto select the right compression to use */
969 void AutoSelectCompression();
970
63b7249e 971 /** \brief Schedule file for verification after a IMS hit */
916b8910 972 void ReverifyAfterIMS();
63b7249e 973
899e4ded 974 /** \brief Validate the downloaded index file */
448c38bd 975 bool ValidateFile(std::string const &FileName);
899e4ded 976
295d848b
DK
977 /** \brief Get the full pathname of the final file for the current URI */
978 virtual std::string GetFinalFilename() const;
979
146f7715
DK
980 virtual bool TransactionState(TransactionStates const state);
981
0118833a 982 public:
17caf1b1 983 // Specialized action members
448c38bd
DK
984 virtual void Failed(std::string const &Message,pkgAcquire::MethodConfig const * const Cnf);
985 virtual void Done(std::string const &Message, HashStringList const &Hashes,
986 pkgAcquire::MethodConfig const * const Cnf);
b3501edb 987 virtual std::string Custom600Headers() const;
448c38bd
DK
988 virtual std::string DescURI() const {return Desc.URI;};
989 virtual std::string GetMetaKey() const;
990
991 pkgAcqIndex(pkgAcquire * const Owner, pkgAcqMetaBase * const TransactionManager,
992 IndexTarget const * const Target);
0118833a 993
0b58b3f8
MV
994 void Init(std::string const &URI, std::string const &URIDesc,
995 std::string const &ShortDesc);
0118833a 996};
92fcbfc1 997 /*}}}*/
92fcbfc1 998/** \brief An item that is responsible for fetching a package file. {{{
3174e150
MV
999 *
1000 * If the package file already exists in the cache, nothing will be
1001 * done.
1002 */
03e39e59
AL
1003class pkgAcqArchive : public pkgAcquire::Item
1004{
60323ed7
MV
1005 void *d;
1006
448c38bd
DK
1007 bool LocalSource;
1008 HashStringList ExpectedHashes;
1009
03e39e59 1010 protected:
3174e150 1011 /** \brief The package version being fetched. */
03e39e59 1012 pkgCache::VerIterator Version;
3174e150 1013
3174e150
MV
1014 /** \brief The list of sources from which to pick archives to
1015 * download this package from.
1016 */
03e39e59 1017 pkgSourceList *Sources;
3174e150
MV
1018
1019 /** \brief A package records object, used to look up the file
1020 * corresponding to each version of the package.
1021 */
03e39e59 1022 pkgRecords *Recs;
3174e150 1023
3174e150
MV
1024 /** \brief A location in which the actual filename of the package
1025 * should be stored.
1026 */
472ff00e 1027 std::string &StoreFilename;
3174e150
MV
1028
1029 /** \brief The next file for this version to try to download. */
b185acc2 1030 pkgCache::VerFileIterator Vf;
3174e150
MV
1031
1032 /** \brief How many (more) times to try to find a new source from
1033 * which to download this package version if it fails.
1034 *
1035 * Set from Acquire::Retries.
1036 */
7d8afa39 1037 unsigned int Retries;
3174e150
MV
1038
1039 /** \brief \b true if this version file is being downloaded from a
1040 * trusted source.
1041 */
448c38bd 1042 bool Trusted;
17caf1b1 1043
3174e150 1044 /** \brief Queue up the next available file for this version. */
b185acc2 1045 bool QueueNext();
295d848b
DK
1046
1047 /** \brief Get the full pathname of the final file for the current URI */
1048 virtual std::string GetFinalFilename() const;
1049
03e39e59 1050 public:
295d848b 1051
448c38bd
DK
1052 virtual void Failed(std::string const &Message,pkgAcquire::MethodConfig const * const Cnf);
1053 virtual void Done(std::string const &Message, HashStringList const &Hashes,
1054 pkgAcquire::MethodConfig const * const Cnf);
1055 virtual std::string DescURI() const;
1056 virtual std::string ShortDesc() const;
ab559b35 1057 virtual void Finished();
b3501edb 1058 virtual bool IsTrusted() const;
448c38bd
DK
1059 virtual HashStringList GetExpectedHashes() const;
1060 virtual bool HashesRequired() const;
02e20767 1061
3174e150
MV
1062 /** \brief Create a new pkgAcqArchive.
1063 *
1064 * \param Owner The pkgAcquire object with which this item is
1065 * associated.
1066 *
1067 * \param Sources The sources from which to download version
1068 * files.
1069 *
1070 * \param Recs A package records object, used to look up the file
1071 * corresponding to each version of the package.
1072 *
1073 * \param Version The package version to download.
1074 *
3c8030a4 1075 * \param[out] StoreFilename A location in which the actual filename of
3174e150
MV
1076 * the package should be stored. It will be set to a guessed
1077 * basename in the constructor, and filled in with a fully
1078 * qualified filename once the download finishes.
1079 */
448c38bd
DK
1080 pkgAcqArchive(pkgAcquire * const Owner,pkgSourceList * const Sources,
1081 pkgRecords * const Recs,pkgCache::VerIterator const &Version,
472ff00e 1082 std::string &StoreFilename);
03e39e59 1083};
92fcbfc1
DK
1084 /*}}}*/
1085/** \brief Retrieve an arbitrary file to the current directory. {{{
3174e150
MV
1086 *
1087 * The file is retrieved even if it is accessed via a URL type that
1088 * normally is a NOP, such as "file". If the download fails, the
1089 * partial file is renamed to get a ".FAILED" extension.
1090 */
36375005
AL
1091class pkgAcqFile : public pkgAcquire::Item
1092{
60323ed7
MV
1093 void *d;
1094
3174e150
MV
1095 /** \brief How many times to retry the download, set from
1096 * Acquire::Retries.
1097 */
08cfc005 1098 unsigned int Retries;
448c38bd 1099
77278c2b
MV
1100 /** \brief Should this file be considered a index file */
1101 bool IsIndexFile;
1102
448c38bd 1103 HashStringList const ExpectedHashes;
36375005 1104 public:
448c38bd
DK
1105 virtual HashStringList GetExpectedHashes() const;
1106 virtual bool HashesRequired() const;
1107
36375005 1108 // Specialized action members
448c38bd
DK
1109 virtual void Failed(std::string const &Message,pkgAcquire::MethodConfig const * const Cnf);
1110 virtual void Done(std::string const &Message, HashStringList const &CalcHashes,
1111 pkgAcquire::MethodConfig const * const Cnf);
1112 virtual std::string DescURI() const {return Desc.URI;};
b3501edb 1113 virtual std::string Custom600Headers() const;
3174e150
MV
1114
1115 /** \brief Create a new pkgAcqFile object.
1116 *
1117 * \param Owner The pkgAcquire object with which this object is
1118 * associated.
1119 *
1120 * \param URI The URI to download.
1121 *
b3501edb
DK
1122 * \param Hashes The hashsums of the file to download, if they are known;
1123 * otherwise empty list.
3174e150
MV
1124 *
1125 * \param Size The size of the file to download, if it is known;
1126 * otherwise 0.
1127 *
1128 * \param Desc A description of the file being downloaded.
1129 *
1130 * \param ShortDesc A brief description of the file being
1131 * downloaded.
39c7baef
MV
1132 *
1133 * \param DestDir The directory the file should be downloaded into.
1134 *
1135 * \param DestFilename The filename+path the file is downloaded to.
1136 *
77278c2b
MV
1137 * \param IsIndexFile The file is considered a IndexFile and cache-control
1138 * headers like "cache-control: max-age=0" are send
39c7baef 1139 *
255c9e4b
DK
1140 * If DestFilename is empty, download to DestDir/\<basename\> if
1141 * DestDir is non-empty, $CWD/\<basename\> otherwise. If
39c7baef
MV
1142 * DestFilename is NOT empty, DestDir is ignored and DestFilename
1143 * is the absolute name to which the file should be downloaded.
3174e150 1144 */
39c7baef 1145
448c38bd
DK
1146 pkgAcqFile(pkgAcquire * const Owner, std::string const &URI, HashStringList const &Hashes, unsigned long long const Size,
1147 std::string const &Desc, std::string const &ShortDesc,
1148 std::string const &DestDir="", std::string const &DestFilename="",
1149 bool const IsIndexFile=false);
36375005 1150};
92fcbfc1 1151 /*}}}*/
3174e150
MV
1152/** @} */
1153
0118833a 1154#endif