You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1104 lines
37 KiB

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