acquire-item.h 39 KB

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