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.
 
 
 
 
 
 

1233 lines
44 KiB

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