acquire-item.cc 47 KB

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