acquire-item.h 39 KB

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