acquire-item.cc 46 KB

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