acquire-item.h 40 KB

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