acquire-item.cc 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: acquire-item.cc,v 1.46.2.9 2004/01/16 18:51:11 mdz Exp $
  4. /* ######################################################################
  5. Acquire Item - Item to acquire
  6. Each item can download to exactly one file at a time. This means you
  7. cannot create an item that fetches two uri's to two files at the same
  8. time. The pkgAcqIndex class creates a second class upon instantiation
  9. to fetch the other index files because of this.
  10. ##################################################################### */
  11. /*}}}*/
  12. // Include Files /*{{{*/
  13. #include <apt-pkg/acquire-item.h>
  14. #include <apt-pkg/configuration.h>
  15. #include <apt-pkg/sourcelist.h>
  16. #include <apt-pkg/vendorlist.h>
  17. #include <apt-pkg/error.h>
  18. #include <apt-pkg/strutl.h>
  19. #include <apt-pkg/fileutl.h>
  20. #include <apt-pkg/md5.h>
  21. #include <apt-pkg/sha1.h>
  22. #include <apt-pkg/tagfile.h>
  23. #include <apti18n.h>
  24. #include <sys/stat.h>
  25. #include <unistd.h>
  26. #include <errno.h>
  27. #include <string>
  28. #include <sstream>
  29. #include <stdio.h>
  30. /*}}}*/
  31. using namespace std;
  32. // Acquire::Item::Item - Constructor /*{{{*/
  33. // ---------------------------------------------------------------------
  34. /* */
  35. pkgAcquire::Item::Item(pkgAcquire *Owner) : Owner(Owner), FileSize(0),
  36. PartialSize(0), Mode(0), ID(0), Complete(false),
  37. Local(false), QueueCounter(0)
  38. {
  39. Owner->Add(this);
  40. Status = StatIdle;
  41. }
  42. /*}}}*/
  43. // Acquire::Item::~Item - Destructor /*{{{*/
  44. // ---------------------------------------------------------------------
  45. /* */
  46. pkgAcquire::Item::~Item()
  47. {
  48. Owner->Remove(this);
  49. }
  50. /*}}}*/
  51. // Acquire::Item::Failed - Item failed to download /*{{{*/
  52. // ---------------------------------------------------------------------
  53. /* We return to an idle state if there are still other queues that could
  54. fetch this object */
  55. void pkgAcquire::Item::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  56. {
  57. Status = StatIdle;
  58. ErrorText = LookupTag(Message,"Message");
  59. if (QueueCounter <= 1)
  60. {
  61. /* This indicates that the file is not available right now but might
  62. be sometime later. If we do a retry cycle then this should be
  63. retried [CDROMs] */
  64. if (Cnf->LocalOnly == true &&
  65. StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
  66. {
  67. Status = StatIdle;
  68. Dequeue();
  69. return;
  70. }
  71. Status = StatError;
  72. Dequeue();
  73. }
  74. }
  75. /*}}}*/
  76. // Acquire::Item::Start - Item has begun to download /*{{{*/
  77. // ---------------------------------------------------------------------
  78. /* Stash status and the file size. Note that setting Complete means
  79. sub-phases of the acquire process such as decompresion are operating */
  80. void pkgAcquire::Item::Start(string /*Message*/,unsigned long Size)
  81. {
  82. Status = StatFetching;
  83. if (FileSize == 0 && Complete == false)
  84. FileSize = Size;
  85. }
  86. /*}}}*/
  87. // Acquire::Item::Done - Item downloaded OK /*{{{*/
  88. // ---------------------------------------------------------------------
  89. /* */
  90. void pkgAcquire::Item::Done(string Message,unsigned long Size,string,
  91. pkgAcquire::MethodConfig *Cnf)
  92. {
  93. // We just downloaded something..
  94. string FileName = LookupTag(Message,"Filename");
  95. // we only inform the Log class if it was actually not a local thing
  96. if (Complete == false && !Local && FileName == DestFile)
  97. {
  98. if (Owner->Log != 0)
  99. Owner->Log->Fetched(Size,atoi(LookupTag(Message,"Resume-Point","0").c_str()));
  100. }
  101. if (FileSize == 0)
  102. FileSize= Size;
  103. Status = StatDone;
  104. ErrorText = string();
  105. Owner->Dequeue(this);
  106. }
  107. /*}}}*/
  108. // Acquire::Item::Rename - Rename a file /*{{{*/
  109. // ---------------------------------------------------------------------
  110. /* This helper function is used by alot of item methods as thier final
  111. step */
  112. void pkgAcquire::Item::Rename(string From,string To)
  113. {
  114. if (rename(From.c_str(),To.c_str()) != 0)
  115. {
  116. char S[300];
  117. snprintf(S,sizeof(S),_("rename failed, %s (%s -> %s)."),strerror(errno),
  118. From.c_str(),To.c_str());
  119. Status = StatError;
  120. ErrorText = S;
  121. }
  122. }
  123. /*}}}*/
  124. // AcqDiffIndex::AcqDiffIndex - Constructor
  125. // ---------------------------------------------------------------------
  126. /* Get the DiffIndex file first and see if there are patches availabe
  127. * If so, create a pkgAcqIndexDiffs fetcher that will get and apply the
  128. * patches. If anything goes wrong in that process, it will fall back to
  129. * the original packages file
  130. */
  131. pkgAcqDiffIndex::pkgAcqDiffIndex(pkgAcquire *Owner,
  132. string URI,string URIDesc,string ShortDesc,
  133. string ExpectedMD5)
  134. : Item(Owner), RealURI(URI), ExpectedMD5(ExpectedMD5), Description(URIDesc)
  135. {
  136. Debug = _config->FindB("Debug::pkgAcquire::Diffs",false);
  137. Desc.Description = URIDesc + "/DiffIndex";
  138. Desc.Owner = this;
  139. Desc.ShortDesc = ShortDesc;
  140. Desc.URI = URI + ".diff/Index";
  141. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  142. DestFile += URItoFileName(URI) + string(".DiffIndex");
  143. if(Debug)
  144. std::clog << "pkgAcqDiffIndex: " << Desc.URI << std::endl;
  145. // look for the current package file
  146. CurrentPackagesFile = _config->FindDir("Dir::State::lists");
  147. CurrentPackagesFile += URItoFileName(RealURI);
  148. // FIXME: this file:/ check is a hack to prevent fetching
  149. // from local sources. this is really silly, and
  150. // should be fixed cleanly as soon as possible
  151. if(!FileExists(CurrentPackagesFile) ||
  152. Desc.URI.substr(0,strlen("file:/")) == "file:/")
  153. {
  154. // we don't have a pkg file or we don't want to queue
  155. if(Debug)
  156. std::clog << "No index file, local or canceld by user" << std::endl;
  157. Failed("", NULL);
  158. return;
  159. }
  160. if(Debug)
  161. std::clog << "pkgAcqIndexDiffs::pkgAcqIndexDiffs(): "
  162. << CurrentPackagesFile << std::endl;
  163. QueueURI(Desc);
  164. }
  165. // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
  166. // ---------------------------------------------------------------------
  167. /* The only header we use is the last-modified header. */
  168. string pkgAcqDiffIndex::Custom600Headers()
  169. {
  170. string Final = _config->FindDir("Dir::State::lists");
  171. Final += URItoFileName(RealURI) + string(".IndexDiff");
  172. if(Debug)
  173. std::clog << "Custom600Header-IMS: " << Final << std::endl;
  174. struct stat Buf;
  175. if (stat(Final.c_str(),&Buf) != 0)
  176. return "\nIndex-File: true";
  177. return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
  178. }
  179. bool pkgAcqDiffIndex::ParseDiffIndex(string IndexDiffFile)
  180. {
  181. if(Debug)
  182. std::clog << "pkgAcqIndexDiffs::ParseIndexDiff() " << IndexDiffFile
  183. << std::endl;
  184. pkgTagSection Tags;
  185. string ServerSha1;
  186. vector<DiffInfo> available_patches;
  187. FileFd Fd(IndexDiffFile,FileFd::ReadOnly);
  188. pkgTagFile TF(&Fd);
  189. if (_error->PendingError() == true)
  190. return false;
  191. if(TF.Step(Tags) == true)
  192. {
  193. string local_sha1;
  194. bool found = false;
  195. DiffInfo d;
  196. string size;
  197. string tmp = Tags.FindS("SHA1-Current");
  198. std::stringstream ss(tmp);
  199. ss >> ServerSha1;
  200. FileFd fd(CurrentPackagesFile, FileFd::ReadOnly);
  201. SHA1Summation SHA1;
  202. SHA1.AddFD(fd.Fd(), fd.Size());
  203. local_sha1 = string(SHA1.Result());
  204. if(local_sha1 == ServerSha1)
  205. {
  206. // we have the same sha1 as the server
  207. if(Debug)
  208. std::clog << "Package file is up-to-date" << std::endl;
  209. // set found to true, this will queue a pkgAcqIndexDiffs with
  210. // a empty availabe_patches
  211. found = true;
  212. }
  213. else
  214. {
  215. if(Debug)
  216. std::clog << "SHA1-Current: " << ServerSha1 << std::endl;
  217. // check the historie and see what patches we need
  218. string history = Tags.FindS("SHA1-History");
  219. std::stringstream hist(history);
  220. while(hist >> d.sha1 >> size >> d.file)
  221. {
  222. d.size = atoi(size.c_str());
  223. // read until the first match is found
  224. if(d.sha1 == local_sha1)
  225. found=true;
  226. // from that point on, we probably need all diffs
  227. if(found)
  228. {
  229. if(Debug)
  230. std::clog << "Need to get diff: " << d.file << std::endl;
  231. available_patches.push_back(d);
  232. }
  233. }
  234. }
  235. // no information how to get the patches, bail out
  236. if(!found)
  237. {
  238. if(Debug)
  239. std::clog << "Can't find a patch in the index file" << std::endl;
  240. // Failed will queue a big package file
  241. Failed("", NULL);
  242. }
  243. else
  244. {
  245. // queue the diffs
  246. new pkgAcqIndexDiffs(Owner, RealURI, Description, Desc.ShortDesc,
  247. ExpectedMD5, available_patches);
  248. Complete = false;
  249. Status = StatDone;
  250. Dequeue();
  251. return true;
  252. }
  253. }
  254. return false;
  255. }
  256. void pkgAcqDiffIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  257. {
  258. if(Debug)
  259. std::clog << "pkgAcqDiffIndex failed: " << Desc.URI << std::endl
  260. << "Falling back to normal index file aquire" << std::endl;
  261. new pkgAcqIndex(Owner, RealURI, Description, Desc.ShortDesc,
  262. ExpectedMD5);
  263. Complete = false;
  264. Status = StatDone;
  265. Dequeue();
  266. }
  267. void pkgAcqDiffIndex::Done(string Message,unsigned long Size,string Md5Hash,
  268. pkgAcquire::MethodConfig *Cnf)
  269. {
  270. if(Debug)
  271. std::clog << "pkgAcqDiffIndex::Done(): " << Desc.URI << std::endl;
  272. Item::Done(Message,Size,Md5Hash,Cnf);
  273. string FinalFile;
  274. FinalFile = _config->FindDir("Dir::State::lists")+URItoFileName(RealURI);
  275. // sucess in downloading the index
  276. // rename the index
  277. FinalFile += string(".IndexDiff");
  278. if(Debug)
  279. std::clog << "Renaming: " << DestFile << " -> " << FinalFile
  280. << std::endl;
  281. Rename(DestFile,FinalFile);
  282. chmod(FinalFile.c_str(),0644);
  283. DestFile = FinalFile;
  284. if(!ParseDiffIndex(DestFile))
  285. return Failed("", NULL);
  286. Complete = true;
  287. Status = StatDone;
  288. Dequeue();
  289. return;
  290. }
  291. // AcqIndexDiffs::AcqIndexDiffs - Constructor
  292. // ---------------------------------------------------------------------
  293. /* The package diff is added to the queue. one object is constructed
  294. * for each diff and the index
  295. */
  296. pkgAcqIndexDiffs::pkgAcqIndexDiffs(pkgAcquire *Owner,
  297. string URI,string URIDesc,string ShortDesc,
  298. string ExpectedMD5, vector<DiffInfo> diffs)
  299. : Item(Owner), RealURI(URI), ExpectedMD5(ExpectedMD5),
  300. available_patches(diffs)
  301. {
  302. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  303. DestFile += URItoFileName(URI);
  304. Debug = _config->FindB("Debug::pkgAcquire::Diffs",false);
  305. Desc.Description = URIDesc;
  306. Desc.Owner = this;
  307. Desc.ShortDesc = ShortDesc;
  308. if(available_patches.size() == 0)
  309. {
  310. // we are done (yeah!)
  311. Finish(true);
  312. }
  313. else
  314. {
  315. // get the next diff
  316. State = StateFetchDiff;
  317. QueueNextDiff();
  318. }
  319. }
  320. void pkgAcqIndexDiffs::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  321. {
  322. if(Debug)
  323. std::clog << "pkgAcqIndexDiffs failed: " << Desc.URI << std::endl
  324. << "Falling back to normal index file aquire" << std::endl;
  325. new pkgAcqIndex(Owner, RealURI, Description,Desc.ShortDesc,
  326. ExpectedMD5);
  327. Finish();
  328. }
  329. // helper that cleans the item out of the fetcher queue
  330. void pkgAcqIndexDiffs::Finish(bool allDone)
  331. {
  332. // we restore the original name, this is required, otherwise
  333. // the file will be cleaned
  334. if(allDone)
  335. {
  336. DestFile = _config->FindDir("Dir::State::lists");
  337. DestFile += URItoFileName(RealURI);
  338. // do the final md5sum checking
  339. MD5Summation sum;
  340. FileFd Fd(DestFile, FileFd::ReadOnly);
  341. sum.AddFD(Fd.Fd(), Fd.Size());
  342. Fd.Close();
  343. string MD5 = (string)sum.Result();
  344. if (!ExpectedMD5.empty() && MD5 != ExpectedMD5)
  345. {
  346. Status = StatAuthError;
  347. ErrorText = _("MD5Sum mismatch");
  348. Rename(DestFile,DestFile + ".FAILED");
  349. Dequeue();
  350. return;
  351. }
  352. // this is for the "real" finish
  353. Complete = true;
  354. Status = StatDone;
  355. Dequeue();
  356. if(Debug)
  357. std::clog << "\n\nallDone: " << DestFile << "\n" << std::endl;
  358. return;
  359. }
  360. if(Debug)
  361. std::clog << "Finishing: " << Desc.URI << std::endl;
  362. Complete = false;
  363. Status = StatDone;
  364. Dequeue();
  365. return;
  366. }
  367. bool pkgAcqIndexDiffs::QueueNextDiff()
  368. {
  369. // calc sha1 of the just patched file
  370. string FinalFile = _config->FindDir("Dir::State::lists");
  371. FinalFile += URItoFileName(RealURI);
  372. FileFd fd(FinalFile, FileFd::ReadOnly);
  373. SHA1Summation SHA1;
  374. SHA1.AddFD(fd.Fd(), fd.Size());
  375. string local_sha1 = string(SHA1.Result());
  376. if(Debug)
  377. std::clog << "QueueNextDiff: "
  378. << FinalFile << " (" << local_sha1 << ")"<<std::endl;
  379. // remove all patches until the next matching patch is found
  380. // this requires the Index file to be ordered
  381. for(vector<DiffInfo>::iterator I=available_patches.begin();
  382. available_patches.size() > 0 &&
  383. I != available_patches.end() &&
  384. (*I).sha1 != local_sha1;
  385. I++)
  386. {
  387. available_patches.erase(I);
  388. }
  389. // error checking and falling back if no patch was found
  390. if(available_patches.size() == 0)
  391. {
  392. Failed("", NULL);
  393. return false;
  394. }
  395. // queue the right diff
  396. Desc.URI = string(RealURI) + ".diff/" + available_patches[0].file + ".gz";
  397. Desc.Description = available_patches[0].file + string(".pdiff");
  398. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  399. DestFile += URItoFileName(RealURI + ".diff/" + available_patches[0].file);
  400. if(Debug)
  401. std::clog << "pkgAcqIndexDiffs::QueueNextDiff(): " << Desc.URI << std::endl;
  402. QueueURI(Desc);
  403. return true;
  404. }
  405. void pkgAcqIndexDiffs::Done(string Message,unsigned long Size,string Md5Hash,
  406. pkgAcquire::MethodConfig *Cnf)
  407. {
  408. if(Debug)
  409. std::clog << "pkgAcqIndexDiffs::Done(): " << Desc.URI << std::endl;
  410. Item::Done(Message,Size,Md5Hash,Cnf);
  411. string FinalFile;
  412. FinalFile = _config->FindDir("Dir::State::lists")+URItoFileName(RealURI);
  413. // sucess in downloading a diff, enter ApplyDiff state
  414. if(State == StateFetchDiff)
  415. {
  416. if(Debug)
  417. std::clog << "Sending to gzip method: " << FinalFile << std::endl;
  418. string FileName = LookupTag(Message,"Filename");
  419. State = StateUnzipDiff;
  420. Local = true;
  421. Desc.URI = "gzip:" + FileName;
  422. DestFile += ".decomp";
  423. QueueURI(Desc);
  424. Mode = "gzip";
  425. return;
  426. }
  427. // sucess in downloading a diff, enter ApplyDiff state
  428. if(State == StateUnzipDiff)
  429. {
  430. // rred excepts the patch as $FinalFile.ed
  431. Rename(DestFile,FinalFile+".ed");
  432. if(Debug)
  433. std::clog << "Sending to rred method: " << FinalFile << std::endl;
  434. State = StateApplyDiff;
  435. Local = true;
  436. Desc.URI = "rred:" + FinalFile;
  437. QueueURI(Desc);
  438. Mode = "rred";
  439. return;
  440. }
  441. // success in download/apply a diff, queue next (if needed)
  442. if(State == StateApplyDiff)
  443. {
  444. // remove the just applied patch
  445. available_patches.erase(available_patches.begin());
  446. // move into place
  447. if(Debug)
  448. {
  449. std::clog << "Moving patched file in place: " << std::endl
  450. << DestFile << " -> " << FinalFile << std::endl;
  451. }
  452. Rename(DestFile,FinalFile);
  453. chmod(FinalFile.c_str(),0644);
  454. // see if there is more to download
  455. if(available_patches.size() > 0) {
  456. new pkgAcqIndexDiffs(Owner, RealURI, Description, Desc.ShortDesc,
  457. ExpectedMD5, available_patches);
  458. return Finish();
  459. } else
  460. return Finish(true);
  461. }
  462. }
  463. // AcqIndex::AcqIndex - Constructor /*{{{*/
  464. // ---------------------------------------------------------------------
  465. /* The package file is added to the queue and a second class is
  466. instantiated to fetch the revision file */
  467. pkgAcqIndex::pkgAcqIndex(pkgAcquire *Owner,
  468. string URI,string URIDesc,string ShortDesc,
  469. string ExpectedMD5, string comprExt)
  470. : Item(Owner), RealURI(URI), ExpectedMD5(ExpectedMD5)
  471. {
  472. Decompression = false;
  473. Erase = false;
  474. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  475. DestFile += URItoFileName(URI);
  476. if(comprExt.empty())
  477. {
  478. // autoselect the compression method
  479. if(FileExists("/bin/bzip2"))
  480. CompressionExtension = ".bz2";
  481. else
  482. CompressionExtension = ".gz";
  483. } else {
  484. CompressionExtension = comprExt;
  485. }
  486. Desc.URI = URI + CompressionExtension;
  487. Desc.Description = URIDesc;
  488. Desc.Owner = this;
  489. Desc.ShortDesc = ShortDesc;
  490. QueueURI(Desc);
  491. }
  492. /*}}}*/
  493. // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
  494. // ---------------------------------------------------------------------
  495. /* The only header we use is the last-modified header. */
  496. string pkgAcqIndex::Custom600Headers()
  497. {
  498. string Final = _config->FindDir("Dir::State::lists");
  499. Final += URItoFileName(RealURI);
  500. struct stat Buf;
  501. if (stat(Final.c_str(),&Buf) != 0)
  502. return "\nIndex-File: true";
  503. return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
  504. }
  505. /*}}}*/
  506. void pkgAcqIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  507. {
  508. // no .bz2 found, retry with .gz
  509. if(Desc.URI.substr(Desc.URI.size()-3) == "bz2") {
  510. Desc.URI = Desc.URI.substr(0,Desc.URI.size()-3) + "gz";
  511. // retry with a gzip one
  512. new pkgAcqIndex(Owner, RealURI, Desc.Description,Desc.ShortDesc,
  513. ExpectedMD5, string(".gz"));
  514. Status = StatDone;
  515. Complete = false;
  516. Dequeue();
  517. return;
  518. }
  519. // on decompression failure, remove bad versions in partial/
  520. if(Decompression && Erase) {
  521. string s = _config->FindDir("Dir::State::lists") + "partial/";
  522. s += URItoFileName(RealURI);
  523. unlink(s.c_str());
  524. }
  525. Item::Failed(Message,Cnf);
  526. }
  527. // AcqIndex::Done - Finished a fetch /*{{{*/
  528. // ---------------------------------------------------------------------
  529. /* This goes through a number of states.. On the initial fetch the
  530. method could possibly return an alternate filename which points
  531. to the uncompressed version of the file. If this is so the file
  532. is copied into the partial directory. In all other cases the file
  533. is decompressed with a gzip uri. */
  534. void pkgAcqIndex::Done(string Message,unsigned long Size,string MD5,
  535. pkgAcquire::MethodConfig *Cfg)
  536. {
  537. Item::Done(Message,Size,MD5,Cfg);
  538. if (Decompression == true)
  539. {
  540. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  541. {
  542. std::cerr << std::endl << RealURI << ": Computed MD5: " << MD5;
  543. std::cerr << " Expected MD5: " << ExpectedMD5 << std::endl;
  544. }
  545. if (MD5.empty())
  546. {
  547. MD5Summation sum;
  548. FileFd Fd(DestFile, FileFd::ReadOnly);
  549. sum.AddFD(Fd.Fd(), Fd.Size());
  550. Fd.Close();
  551. MD5 = (string)sum.Result();
  552. }
  553. if (!ExpectedMD5.empty() && MD5 != ExpectedMD5)
  554. {
  555. Status = StatAuthError;
  556. ErrorText = _("MD5Sum mismatch");
  557. Rename(DestFile,DestFile + ".FAILED");
  558. return;
  559. }
  560. // Done, move it into position
  561. string FinalFile = _config->FindDir("Dir::State::lists");
  562. FinalFile += URItoFileName(RealURI);
  563. Rename(DestFile,FinalFile);
  564. chmod(FinalFile.c_str(),0644);
  565. /* We restore the original name to DestFile so that the clean operation
  566. will work OK */
  567. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  568. DestFile += URItoFileName(RealURI);
  569. // Remove the compressed version.
  570. if (Erase == true)
  571. unlink(DestFile.c_str());
  572. return;
  573. }
  574. Erase = false;
  575. Complete = true;
  576. // Handle the unzipd case
  577. string FileName = LookupTag(Message,"Alt-Filename");
  578. if (FileName.empty() == false)
  579. {
  580. // The files timestamp matches
  581. if (StringToBool(LookupTag(Message,"Alt-IMS-Hit"),false) == true)
  582. return;
  583. Decompression = true;
  584. Local = true;
  585. DestFile += ".decomp";
  586. Desc.URI = "copy:" + FileName;
  587. QueueURI(Desc);
  588. Mode = "copy";
  589. return;
  590. }
  591. FileName = LookupTag(Message,"Filename");
  592. if (FileName.empty() == true)
  593. {
  594. Status = StatError;
  595. ErrorText = "Method gave a blank filename";
  596. }
  597. // The files timestamp matches
  598. if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
  599. return;
  600. if (FileName == DestFile)
  601. Erase = true;
  602. else
  603. Local = true;
  604. string compExt = Desc.URI.substr(Desc.URI.size()-3);
  605. char *decompProg;
  606. if(compExt == "bz2")
  607. decompProg = "bzip2";
  608. else if(compExt == ".gz")
  609. decompProg = "gzip";
  610. else {
  611. _error->Error("Unsupported extension: %s", compExt.c_str());
  612. return;
  613. }
  614. Decompression = true;
  615. DestFile += ".decomp";
  616. Desc.URI = string(decompProg) + ":" + FileName;
  617. QueueURI(Desc);
  618. Mode = decompProg;
  619. }
  620. // AcqIndexTrans::pkgAcqIndexTrans - Constructor /*{{{*/
  621. // ---------------------------------------------------------------------
  622. /* The Translation file is added to the queue */
  623. pkgAcqIndexTrans::pkgAcqIndexTrans(pkgAcquire *Owner,
  624. string URI,string URIDesc,string ShortDesc) :
  625. pkgAcqIndex(Owner, URI, URIDesc, ShortDesc, "", "")
  626. {
  627. }
  628. /*}}}*/
  629. // AcqIndexTrans::Failed - Silence failure messages for missing files /*{{{*/
  630. // ---------------------------------------------------------------------
  631. /* */
  632. void pkgAcqIndexTrans::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  633. {
  634. if (Cnf->LocalOnly == true ||
  635. StringToBool(LookupTag(Message,"Transient-Failure"),false) == false)
  636. {
  637. // Ignore this
  638. Status = StatDone;
  639. Complete = false;
  640. Dequeue();
  641. return;
  642. }
  643. Item::Failed(Message,Cnf);
  644. }
  645. /*}}}*/
  646. pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire *Owner,
  647. string URI,string URIDesc,string ShortDesc,
  648. string MetaIndexURI, string MetaIndexURIDesc,
  649. string MetaIndexShortDesc,
  650. const vector<IndexTarget*>* IndexTargets,
  651. indexRecords* MetaIndexParser) :
  652. Item(Owner), RealURI(URI), MetaIndexURI(MetaIndexURI),
  653. MetaIndexURIDesc(MetaIndexURIDesc), MetaIndexShortDesc(MetaIndexShortDesc),
  654. MetaIndexParser(MetaIndexParser), IndexTargets(IndexTargets)
  655. {
  656. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  657. DestFile += URItoFileName(URI);
  658. // remove any partial downloaded sig-file in partial/.
  659. // it may confuse proxies and is too small to warrant a
  660. // partial download anyway
  661. unlink(DestFile.c_str());
  662. // Create the item
  663. Desc.Description = URIDesc;
  664. Desc.Owner = this;
  665. Desc.ShortDesc = ShortDesc;
  666. Desc.URI = URI;
  667. string Final = _config->FindDir("Dir::State::lists");
  668. Final += URItoFileName(RealURI);
  669. struct stat Buf;
  670. if (stat(Final.c_str(),&Buf) == 0)
  671. {
  672. // File was already in place. It needs to be re-verified
  673. // because Release might have changed, so Move it into partial
  674. Rename(Final,DestFile);
  675. }
  676. QueueURI(Desc);
  677. }
  678. /*}}}*/
  679. // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
  680. // ---------------------------------------------------------------------
  681. /* The only header we use is the last-modified header. */
  682. string pkgAcqMetaSig::Custom600Headers()
  683. {
  684. struct stat Buf;
  685. if (stat(DestFile.c_str(),&Buf) != 0)
  686. return "\nIndex-File: true";
  687. return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
  688. }
  689. void pkgAcqMetaSig::Done(string Message,unsigned long Size,string MD5,
  690. pkgAcquire::MethodConfig *Cfg)
  691. {
  692. Item::Done(Message,Size,MD5,Cfg);
  693. string FileName = LookupTag(Message,"Filename");
  694. if (FileName.empty() == true)
  695. {
  696. Status = StatError;
  697. ErrorText = "Method gave a blank filename";
  698. return;
  699. }
  700. if (FileName != DestFile)
  701. {
  702. // We have to copy it into place
  703. Local = true;
  704. Desc.URI = "copy:" + FileName;
  705. QueueURI(Desc);
  706. return;
  707. }
  708. Complete = true;
  709. // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
  710. new pkgAcqMetaIndex(Owner, MetaIndexURI, MetaIndexURIDesc, MetaIndexShortDesc,
  711. DestFile, IndexTargets, MetaIndexParser);
  712. }
  713. /*}}}*/
  714. void pkgAcqMetaSig::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  715. {
  716. string Final = _config->FindDir("Dir::State::lists") + URItoFileName(RealURI);
  717. // if we get a network error we fail gracefully
  718. if(Status == StatTransientNetworkError)
  719. {
  720. Item::Failed(Message,Cnf);
  721. // move the sigfile back on transient network failures
  722. if(FileExists(DestFile))
  723. Rename(DestFile,Final);
  724. // set the status back to , Item::Failed likes to reset it
  725. Status = pkgAcquire::Item::StatTransientNetworkError;
  726. return;
  727. }
  728. // Delete any existing sigfile when the acquire failed
  729. unlink(Final.c_str());
  730. // queue a pkgAcqMetaIndex with no sigfile
  731. new pkgAcqMetaIndex(Owner, MetaIndexURI, MetaIndexURIDesc, MetaIndexShortDesc,
  732. "", IndexTargets, MetaIndexParser);
  733. if (Cnf->LocalOnly == true ||
  734. StringToBool(LookupTag(Message,"Transient-Failure"),false) == false)
  735. {
  736. // Ignore this
  737. Status = StatDone;
  738. Complete = false;
  739. Dequeue();
  740. return;
  741. }
  742. Item::Failed(Message,Cnf);
  743. }
  744. pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire *Owner,
  745. string URI,string URIDesc,string ShortDesc,
  746. string SigFile,
  747. const vector<struct IndexTarget*>* IndexTargets,
  748. indexRecords* MetaIndexParser) :
  749. Item(Owner), RealURI(URI), SigFile(SigFile), AuthPass(false),
  750. MetaIndexParser(MetaIndexParser), IndexTargets(IndexTargets), IMSHit(false)
  751. {
  752. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  753. DestFile += URItoFileName(URI);
  754. // Create the item
  755. Desc.Description = URIDesc;
  756. Desc.Owner = this;
  757. Desc.ShortDesc = ShortDesc;
  758. Desc.URI = URI;
  759. QueueURI(Desc);
  760. }
  761. /*}}}*/
  762. // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
  763. // ---------------------------------------------------------------------
  764. /* The only header we use is the last-modified header. */
  765. string pkgAcqMetaIndex::Custom600Headers()
  766. {
  767. string Final = _config->FindDir("Dir::State::lists");
  768. Final += URItoFileName(RealURI);
  769. struct stat Buf;
  770. if (stat(Final.c_str(),&Buf) != 0)
  771. return "\nIndex-File: true";
  772. return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
  773. }
  774. void pkgAcqMetaIndex::Done(string Message,unsigned long Size,string MD5,
  775. pkgAcquire::MethodConfig *Cfg)
  776. {
  777. Item::Done(Message,Size,MD5,Cfg);
  778. // MetaIndexes are done in two passes: one to download the
  779. // metaindex with an appropriate method, and a second to verify it
  780. // with the gpgv method
  781. if (AuthPass == true)
  782. {
  783. AuthDone(Message);
  784. }
  785. else
  786. {
  787. RetrievalDone(Message);
  788. if (!Complete)
  789. // Still more retrieving to do
  790. return;
  791. if (SigFile == "")
  792. {
  793. // There was no signature file, so we are finished. Download
  794. // the indexes without verification.
  795. QueueIndexes(false);
  796. }
  797. else
  798. {
  799. // There was a signature file, so pass it to gpgv for
  800. // verification
  801. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  802. std::cerr << "Metaindex acquired, queueing gpg verification ("
  803. << SigFile << "," << DestFile << ")\n";
  804. AuthPass = true;
  805. Desc.URI = "gpgv:" + SigFile;
  806. QueueURI(Desc);
  807. Mode = "gpgv";
  808. }
  809. }
  810. }
  811. void pkgAcqMetaIndex::RetrievalDone(string Message)
  812. {
  813. // We have just finished downloading a Release file (it is not
  814. // verified yet)
  815. string FileName = LookupTag(Message,"Filename");
  816. if (FileName.empty() == true)
  817. {
  818. Status = StatError;
  819. ErrorText = "Method gave a blank filename";
  820. return;
  821. }
  822. if (FileName != DestFile)
  823. {
  824. Local = true;
  825. Desc.URI = "copy:" + FileName;
  826. QueueURI(Desc);
  827. return;
  828. }
  829. // see if the download was a IMSHit
  830. IMSHit = StringToBool(LookupTag(Message,"IMS-Hit"),false);
  831. Complete = true;
  832. string FinalFile = _config->FindDir("Dir::State::lists");
  833. FinalFile += URItoFileName(RealURI);
  834. // The files timestamp matches
  835. if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == false)
  836. {
  837. // Move it into position
  838. Rename(DestFile,FinalFile);
  839. }
  840. chmod(FinalFile.c_str(),0644);
  841. DestFile = FinalFile;
  842. }
  843. void pkgAcqMetaIndex::AuthDone(string Message)
  844. {
  845. // At this point, the gpgv method has succeeded, so there is a
  846. // valid signature from a key in the trusted keyring. We
  847. // perform additional verification of its contents, and use them
  848. // to verify the indexes we are about to download
  849. if (!MetaIndexParser->Load(DestFile))
  850. {
  851. Status = StatAuthError;
  852. ErrorText = MetaIndexParser->ErrorText;
  853. return;
  854. }
  855. if (!VerifyVendor(Message))
  856. {
  857. return;
  858. }
  859. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  860. std::cerr << "Signature verification succeeded: "
  861. << DestFile << std::endl;
  862. // Download further indexes with verification
  863. QueueIndexes(true);
  864. // Done, move signature file into position
  865. string VerifiedSigFile = _config->FindDir("Dir::State::lists") +
  866. URItoFileName(RealURI) + ".gpg";
  867. Rename(SigFile,VerifiedSigFile);
  868. chmod(VerifiedSigFile.c_str(),0644);
  869. }
  870. void pkgAcqMetaIndex::QueueIndexes(bool verify)
  871. {
  872. for (vector <struct IndexTarget*>::const_iterator Target = IndexTargets->begin();
  873. Target != IndexTargets->end();
  874. Target++)
  875. {
  876. string ExpectedIndexMD5;
  877. if (verify)
  878. {
  879. const indexRecords::checkSum *Record = MetaIndexParser->Lookup((*Target)->MetaKey);
  880. if (!Record)
  881. {
  882. Status = StatAuthError;
  883. ErrorText = "Unable to find expected entry "
  884. + (*Target)->MetaKey + " in Meta-index file (malformed Release file?)";
  885. return;
  886. }
  887. ExpectedIndexMD5 = Record->MD5Hash;
  888. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  889. {
  890. std::cerr << "Queueing: " << (*Target)->URI << std::endl;
  891. std::cerr << "Expected MD5: " << ExpectedIndexMD5 << std::endl;
  892. }
  893. if (ExpectedIndexMD5.empty())
  894. {
  895. Status = StatAuthError;
  896. ErrorText = "Unable to find MD5 sum for "
  897. + (*Target)->MetaKey + " in Meta-index file";
  898. return;
  899. }
  900. }
  901. // Queue Packages file (either diff or full packages files, depending
  902. // on the users option)
  903. if(_config->FindB("Acquire::PDiffs",true) == true)
  904. new pkgAcqDiffIndex(Owner, (*Target)->URI, (*Target)->Description,
  905. (*Target)->ShortDesc, ExpectedIndexMD5);
  906. else
  907. new pkgAcqIndex(Owner, (*Target)->URI, (*Target)->Description,
  908. (*Target)->ShortDesc, ExpectedIndexMD5);
  909. }
  910. }
  911. bool pkgAcqMetaIndex::VerifyVendor(string Message)
  912. {
  913. // // Maybe this should be made available from above so we don't have
  914. // // to read and parse it every time?
  915. // pkgVendorList List;
  916. // List.ReadMainList();
  917. // const Vendor* Vndr = NULL;
  918. // for (std::vector<string>::const_iterator I = GPGVOutput.begin(); I != GPGVOutput.end(); I++)
  919. // {
  920. // string::size_type pos = (*I).find("VALIDSIG ");
  921. // if (_config->FindB("Debug::Vendor", false))
  922. // std::cerr << "Looking for VALIDSIG in \"" << (*I) << "\": pos " << pos
  923. // << std::endl;
  924. // if (pos != std::string::npos)
  925. // {
  926. // string Fingerprint = (*I).substr(pos+sizeof("VALIDSIG"));
  927. // if (_config->FindB("Debug::Vendor", false))
  928. // std::cerr << "Looking for \"" << Fingerprint << "\" in vendor..." <<
  929. // std::endl;
  930. // Vndr = List.FindVendor(Fingerprint) != "";
  931. // if (Vndr != NULL);
  932. // break;
  933. // }
  934. // }
  935. string::size_type pos;
  936. // check for missing sigs (that where not fatal because otherwise we had
  937. // bombed earlier)
  938. string missingkeys;
  939. string msg = _("There is no public key available for the "
  940. "following key IDs:\n");
  941. pos = Message.find("NO_PUBKEY ");
  942. if (pos != std::string::npos)
  943. {
  944. string::size_type start = pos+strlen("NO_PUBKEY ");
  945. string Fingerprint = Message.substr(start, Message.find("\n")-start);
  946. missingkeys += (Fingerprint);
  947. }
  948. if(!missingkeys.empty())
  949. _error->Warning("%s", string(msg+missingkeys).c_str());
  950. string Transformed = MetaIndexParser->GetExpectedDist();
  951. if (Transformed == "../project/experimental")
  952. {
  953. Transformed = "experimental";
  954. }
  955. pos = Transformed.rfind('/');
  956. if (pos != string::npos)
  957. {
  958. Transformed = Transformed.substr(0, pos);
  959. }
  960. if (Transformed == ".")
  961. {
  962. Transformed = "";
  963. }
  964. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  965. {
  966. std::cerr << "Got Codename: " << MetaIndexParser->GetDist() << std::endl;
  967. std::cerr << "Expecting Dist: " << MetaIndexParser->GetExpectedDist() << std::endl;
  968. std::cerr << "Transformed Dist: " << Transformed << std::endl;
  969. }
  970. if (MetaIndexParser->CheckDist(Transformed) == false)
  971. {
  972. // This might become fatal one day
  973. // Status = StatAuthError;
  974. // ErrorText = "Conflicting distribution; expected "
  975. // + MetaIndexParser->GetExpectedDist() + " but got "
  976. // + MetaIndexParser->GetDist();
  977. // return false;
  978. if (!Transformed.empty())
  979. {
  980. _error->Warning("Conflicting distribution: %s (expected %s but got %s)",
  981. Desc.Description.c_str(),
  982. Transformed.c_str(),
  983. MetaIndexParser->GetDist().c_str());
  984. }
  985. }
  986. return true;
  987. }
  988. /*}}}*/
  989. // pkgAcqMetaIndex::Failed - no Release file present or no signature
  990. // file present /*{{{*/
  991. // ---------------------------------------------------------------------
  992. /* */
  993. void pkgAcqMetaIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  994. {
  995. if (AuthPass == true)
  996. {
  997. // if we fail the authentication but got the file via a IMS-Hit
  998. // this means that the file wasn't downloaded and that it might be
  999. // just stale (server problem, proxy etc). we delete what we have
  1000. // queue it again without i-m-s
  1001. // alternatively we could just unlink the file and let the user try again
  1002. if (IMSHit)
  1003. {
  1004. Complete = false;
  1005. Local = false;
  1006. AuthPass = false;
  1007. unlink(DestFile.c_str());
  1008. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  1009. DestFile += URItoFileName(RealURI);
  1010. Desc.URI = RealURI;
  1011. QueueURI(Desc);
  1012. return;
  1013. }
  1014. // gpgv method failed
  1015. _error->Warning("GPG error: %s: %s",
  1016. Desc.Description.c_str(),
  1017. LookupTag(Message,"Message").c_str());
  1018. }
  1019. // No Release file was present, or verification failed, so fall
  1020. // back to queueing Packages files without verification
  1021. QueueIndexes(false);
  1022. }
  1023. /*}}}*/
  1024. // AcqArchive::AcqArchive - Constructor /*{{{*/
  1025. // ---------------------------------------------------------------------
  1026. /* This just sets up the initial fetch environment and queues the first
  1027. possibilitiy */
  1028. pkgAcqArchive::pkgAcqArchive(pkgAcquire *Owner,pkgSourceList *Sources,
  1029. pkgRecords *Recs,pkgCache::VerIterator const &Version,
  1030. string &StoreFilename) :
  1031. Item(Owner), Version(Version), Sources(Sources), Recs(Recs),
  1032. StoreFilename(StoreFilename), Vf(Version.FileList()),
  1033. Trusted(false)
  1034. {
  1035. Retries = _config->FindI("Acquire::Retries",0);
  1036. if (Version.Arch() == 0)
  1037. {
  1038. _error->Error(_("I wasn't able to locate a file for the %s package. "
  1039. "This might mean you need to manually fix this package. "
  1040. "(due to missing arch)"),
  1041. Version.ParentPkg().Name());
  1042. return;
  1043. }
  1044. /* We need to find a filename to determine the extension. We make the
  1045. assumption here that all the available sources for this version share
  1046. the same extension.. */
  1047. // Skip not source sources, they do not have file fields.
  1048. for (; Vf.end() == false; Vf++)
  1049. {
  1050. if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0)
  1051. continue;
  1052. break;
  1053. }
  1054. // Does not really matter here.. we are going to fail out below
  1055. if (Vf.end() != true)
  1056. {
  1057. // If this fails to get a file name we will bomb out below.
  1058. pkgRecords::Parser &Parse = Recs->Lookup(Vf);
  1059. if (_error->PendingError() == true)
  1060. return;
  1061. // Generate the final file name as: package_version_arch.foo
  1062. StoreFilename = QuoteString(Version.ParentPkg().Name(),"_:") + '_' +
  1063. QuoteString(Version.VerStr(),"_:") + '_' +
  1064. QuoteString(Version.Arch(),"_:.") +
  1065. "." + flExtension(Parse.FileName());
  1066. }
  1067. // check if we have one trusted source for the package. if so, switch
  1068. // to "TrustedOnly" mode
  1069. for (pkgCache::VerFileIterator i = Version.FileList(); i.end() == false; i++)
  1070. {
  1071. pkgIndexFile *Index;
  1072. if (Sources->FindIndex(i.File(),Index) == false)
  1073. continue;
  1074. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  1075. {
  1076. std::cerr << "Checking index: " << Index->Describe()
  1077. << "(Trusted=" << Index->IsTrusted() << ")\n";
  1078. }
  1079. if (Index->IsTrusted()) {
  1080. Trusted = true;
  1081. break;
  1082. }
  1083. }
  1084. // "allow-unauthenticated" restores apts old fetching behaviour
  1085. // that means that e.g. unauthenticated file:// uris are higher
  1086. // priority than authenticated http:// uris
  1087. if (_config->FindB("APT::Get::AllowUnauthenticated",false) == true)
  1088. Trusted = false;
  1089. // Select a source
  1090. if (QueueNext() == false && _error->PendingError() == false)
  1091. _error->Error(_("I wasn't able to locate file for the %s package. "
  1092. "This might mean you need to manually fix this package."),
  1093. Version.ParentPkg().Name());
  1094. }
  1095. /*}}}*/
  1096. // AcqArchive::QueueNext - Queue the next file source /*{{{*/
  1097. // ---------------------------------------------------------------------
  1098. /* This queues the next available file version for download. It checks if
  1099. the archive is already available in the cache and stashs the MD5 for
  1100. checking later. */
  1101. bool pkgAcqArchive::QueueNext()
  1102. {
  1103. for (; Vf.end() == false; Vf++)
  1104. {
  1105. // Ignore not source sources
  1106. if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0)
  1107. continue;
  1108. // Try to cross match against the source list
  1109. pkgIndexFile *Index;
  1110. if (Sources->FindIndex(Vf.File(),Index) == false)
  1111. continue;
  1112. // only try to get a trusted package from another source if that source
  1113. // is also trusted
  1114. if(Trusted && !Index->IsTrusted())
  1115. continue;
  1116. // Grab the text package record
  1117. pkgRecords::Parser &Parse = Recs->Lookup(Vf);
  1118. if (_error->PendingError() == true)
  1119. return false;
  1120. string PkgFile = Parse.FileName();
  1121. MD5 = Parse.MD5Hash();
  1122. if (PkgFile.empty() == true)
  1123. return _error->Error(_("The package index files are corrupted. No Filename: "
  1124. "field for package %s."),
  1125. Version.ParentPkg().Name());
  1126. Desc.URI = Index->ArchiveURI(PkgFile);
  1127. Desc.Description = Index->ArchiveInfo(Version);
  1128. Desc.Owner = this;
  1129. Desc.ShortDesc = Version.ParentPkg().Name();
  1130. // See if we already have the file. (Legacy filenames)
  1131. FileSize = Version->Size;
  1132. string FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile);
  1133. struct stat Buf;
  1134. if (stat(FinalFile.c_str(),&Buf) == 0)
  1135. {
  1136. // Make sure the size matches
  1137. if ((unsigned)Buf.st_size == Version->Size)
  1138. {
  1139. Complete = true;
  1140. Local = true;
  1141. Status = StatDone;
  1142. StoreFilename = DestFile = FinalFile;
  1143. return true;
  1144. }
  1145. /* Hmm, we have a file and its size does not match, this means it is
  1146. an old style mismatched arch */
  1147. unlink(FinalFile.c_str());
  1148. }
  1149. // Check it again using the new style output filenames
  1150. FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename);
  1151. if (stat(FinalFile.c_str(),&Buf) == 0)
  1152. {
  1153. // Make sure the size matches
  1154. if ((unsigned)Buf.st_size == Version->Size)
  1155. {
  1156. Complete = true;
  1157. Local = true;
  1158. Status = StatDone;
  1159. StoreFilename = DestFile = FinalFile;
  1160. return true;
  1161. }
  1162. /* Hmm, we have a file and its size does not match, this shouldnt
  1163. happen.. */
  1164. unlink(FinalFile.c_str());
  1165. }
  1166. DestFile = _config->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename);
  1167. // Check the destination file
  1168. if (stat(DestFile.c_str(),&Buf) == 0)
  1169. {
  1170. // Hmm, the partial file is too big, erase it
  1171. if ((unsigned)Buf.st_size > Version->Size)
  1172. unlink(DestFile.c_str());
  1173. else
  1174. PartialSize = Buf.st_size;
  1175. }
  1176. // Create the item
  1177. Local = false;
  1178. Desc.URI = Index->ArchiveURI(PkgFile);
  1179. Desc.Description = Index->ArchiveInfo(Version);
  1180. Desc.Owner = this;
  1181. Desc.ShortDesc = Version.ParentPkg().Name();
  1182. QueueURI(Desc);
  1183. Vf++;
  1184. return true;
  1185. }
  1186. return false;
  1187. }
  1188. /*}}}*/
  1189. // AcqArchive::Done - Finished fetching /*{{{*/
  1190. // ---------------------------------------------------------------------
  1191. /* */
  1192. void pkgAcqArchive::Done(string Message,unsigned long Size,string Md5Hash,
  1193. pkgAcquire::MethodConfig *Cfg)
  1194. {
  1195. Item::Done(Message,Size,Md5Hash,Cfg);
  1196. // Check the size
  1197. if (Size != Version->Size)
  1198. {
  1199. Status = StatError;
  1200. ErrorText = _("Size mismatch");
  1201. return;
  1202. }
  1203. // Check the md5
  1204. if (Md5Hash.empty() == false && MD5.empty() == false)
  1205. {
  1206. if (Md5Hash != MD5)
  1207. {
  1208. Status = StatError;
  1209. ErrorText = _("MD5Sum mismatch");
  1210. if(FileExists(DestFile))
  1211. Rename(DestFile,DestFile + ".FAILED");
  1212. return;
  1213. }
  1214. }
  1215. // Grab the output filename
  1216. string FileName = LookupTag(Message,"Filename");
  1217. if (FileName.empty() == true)
  1218. {
  1219. Status = StatError;
  1220. ErrorText = "Method gave a blank filename";
  1221. return;
  1222. }
  1223. Complete = true;
  1224. // Reference filename
  1225. if (FileName != DestFile)
  1226. {
  1227. StoreFilename = DestFile = FileName;
  1228. Local = true;
  1229. return;
  1230. }
  1231. // Done, move it into position
  1232. string FinalFile = _config->FindDir("Dir::Cache::Archives");
  1233. FinalFile += flNotDir(StoreFilename);
  1234. Rename(DestFile,FinalFile);
  1235. StoreFilename = DestFile = FinalFile;
  1236. Complete = true;
  1237. }
  1238. /*}}}*/
  1239. // AcqArchive::Failed - Failure handler /*{{{*/
  1240. // ---------------------------------------------------------------------
  1241. /* Here we try other sources */
  1242. void pkgAcqArchive::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  1243. {
  1244. ErrorText = LookupTag(Message,"Message");
  1245. /* We don't really want to retry on failed media swaps, this prevents
  1246. that. An interesting observation is that permanent failures are not
  1247. recorded. */
  1248. if (Cnf->Removable == true &&
  1249. StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
  1250. {
  1251. // Vf = Version.FileList();
  1252. while (Vf.end() == false) Vf++;
  1253. StoreFilename = string();
  1254. Item::Failed(Message,Cnf);
  1255. return;
  1256. }
  1257. if (QueueNext() == false)
  1258. {
  1259. // This is the retry counter
  1260. if (Retries != 0 &&
  1261. Cnf->LocalOnly == false &&
  1262. StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
  1263. {
  1264. Retries--;
  1265. Vf = Version.FileList();
  1266. if (QueueNext() == true)
  1267. return;
  1268. }
  1269. StoreFilename = string();
  1270. Item::Failed(Message,Cnf);
  1271. }
  1272. }
  1273. /*}}}*/
  1274. // AcqArchive::IsTrusted - Determine whether this archive comes from a
  1275. // trusted source /*{{{*/
  1276. // ---------------------------------------------------------------------
  1277. bool pkgAcqArchive::IsTrusted()
  1278. {
  1279. return Trusted;
  1280. }
  1281. // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
  1282. // ---------------------------------------------------------------------
  1283. /* */
  1284. void pkgAcqArchive::Finished()
  1285. {
  1286. if (Status == pkgAcquire::Item::StatDone &&
  1287. Complete == true)
  1288. return;
  1289. StoreFilename = string();
  1290. }
  1291. /*}}}*/
  1292. // AcqFile::pkgAcqFile - Constructor /*{{{*/
  1293. // ---------------------------------------------------------------------
  1294. /* The file is added to the queue */
  1295. pkgAcqFile::pkgAcqFile(pkgAcquire *Owner,string URI,string MD5,
  1296. unsigned long Size,string Dsc,string ShortDesc,
  1297. const string &DestDir, const string &DestFilename) :
  1298. Item(Owner), Md5Hash(MD5)
  1299. {
  1300. Retries = _config->FindI("Acquire::Retries",0);
  1301. if(!DestFilename.empty())
  1302. DestFile = DestFilename;
  1303. else if(!DestDir.empty())
  1304. DestFile = DestDir + "/" + flNotDir(URI);
  1305. else
  1306. DestFile = flNotDir(URI);
  1307. // Create the item
  1308. Desc.URI = URI;
  1309. Desc.Description = Dsc;
  1310. Desc.Owner = this;
  1311. // Set the short description to the archive component
  1312. Desc.ShortDesc = ShortDesc;
  1313. // Get the transfer sizes
  1314. FileSize = Size;
  1315. struct stat Buf;
  1316. if (stat(DestFile.c_str(),&Buf) == 0)
  1317. {
  1318. // Hmm, the partial file is too big, erase it
  1319. if ((unsigned)Buf.st_size > Size)
  1320. unlink(DestFile.c_str());
  1321. else
  1322. PartialSize = Buf.st_size;
  1323. }
  1324. QueueURI(Desc);
  1325. }
  1326. /*}}}*/
  1327. // AcqFile::Done - Item downloaded OK /*{{{*/
  1328. // ---------------------------------------------------------------------
  1329. /* */
  1330. void pkgAcqFile::Done(string Message,unsigned long Size,string MD5,
  1331. pkgAcquire::MethodConfig *Cnf)
  1332. {
  1333. // Check the md5
  1334. if (Md5Hash.empty() == false && MD5.empty() == false)
  1335. {
  1336. if (Md5Hash != MD5)
  1337. {
  1338. Status = StatError;
  1339. ErrorText = "MD5Sum mismatch";
  1340. Rename(DestFile,DestFile + ".FAILED");
  1341. return;
  1342. }
  1343. }
  1344. Item::Done(Message,Size,MD5,Cnf);
  1345. string FileName = LookupTag(Message,"Filename");
  1346. if (FileName.empty() == true)
  1347. {
  1348. Status = StatError;
  1349. ErrorText = "Method gave a blank filename";
  1350. return;
  1351. }
  1352. Complete = true;
  1353. // The files timestamp matches
  1354. if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
  1355. return;
  1356. // We have to copy it into place
  1357. if (FileName != DestFile)
  1358. {
  1359. Local = true;
  1360. if (_config->FindB("Acquire::Source-Symlinks",true) == false ||
  1361. Cnf->Removable == true)
  1362. {
  1363. Desc.URI = "copy:" + FileName;
  1364. QueueURI(Desc);
  1365. return;
  1366. }
  1367. // Erase the file if it is a symlink so we can overwrite it
  1368. struct stat St;
  1369. if (lstat(DestFile.c_str(),&St) == 0)
  1370. {
  1371. if (S_ISLNK(St.st_mode) != 0)
  1372. unlink(DestFile.c_str());
  1373. }
  1374. // Symlink the file
  1375. if (symlink(FileName.c_str(),DestFile.c_str()) != 0)
  1376. {
  1377. ErrorText = "Link to " + DestFile + " failure ";
  1378. Status = StatError;
  1379. Complete = false;
  1380. }
  1381. }
  1382. }
  1383. /*}}}*/
  1384. // AcqFile::Failed - Failure handler /*{{{*/
  1385. // ---------------------------------------------------------------------
  1386. /* Here we try other sources */
  1387. void pkgAcqFile::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  1388. {
  1389. ErrorText = LookupTag(Message,"Message");
  1390. // This is the retry counter
  1391. if (Retries != 0 &&
  1392. Cnf->LocalOnly == false &&
  1393. StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
  1394. {
  1395. Retries--;
  1396. QueueURI(Desc);
  1397. return;
  1398. }
  1399. Item::Failed(Message,Cnf);
  1400. }
  1401. /*}}}*/