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.
 
 
 
 
 
 

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