acquire-item.cc 48 KB

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