acquire-item.h 38 KB

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