acquire-item.cc 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369
  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 <config.h>
  14. #include <apt-pkg/acquire-item.h>
  15. #include <apt-pkg/configuration.h>
  16. #include <apt-pkg/aptconfiguration.h>
  17. #include <apt-pkg/sourcelist.h>
  18. #include <apt-pkg/error.h>
  19. #include <apt-pkg/strutl.h>
  20. #include <apt-pkg/fileutl.h>
  21. #include <apt-pkg/sha1.h>
  22. #include <apt-pkg/tagfile.h>
  23. #include <apt-pkg/indexrecords.h>
  24. #include <apt-pkg/acquire.h>
  25. #include <apt-pkg/hashes.h>
  26. #include <apt-pkg/indexfile.h>
  27. #include <apt-pkg/pkgcache.h>
  28. #include <apt-pkg/cacheiterators.h>
  29. #include <apt-pkg/pkgrecords.h>
  30. #include <stddef.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <iostream>
  34. #include <vector>
  35. #include <sys/stat.h>
  36. #include <unistd.h>
  37. #include <errno.h>
  38. #include <string>
  39. #include <sstream>
  40. #include <stdio.h>
  41. #include <ctime>
  42. #include <apti18n.h>
  43. /*}}}*/
  44. using namespace std;
  45. // Acquire::Item::Item - Constructor /*{{{*/
  46. // ---------------------------------------------------------------------
  47. /* */
  48. pkgAcquire::Item::Item(pkgAcquire *Owner) : Owner(Owner), FileSize(0),
  49. PartialSize(0), Mode(0), ID(0), Complete(false),
  50. Local(false), QueueCounter(0)
  51. {
  52. Owner->Add(this);
  53. Status = StatIdle;
  54. }
  55. /*}}}*/
  56. // Acquire::Item::~Item - Destructor /*{{{*/
  57. // ---------------------------------------------------------------------
  58. /* */
  59. pkgAcquire::Item::~Item()
  60. {
  61. Owner->Remove(this);
  62. }
  63. /*}}}*/
  64. // Acquire::Item::Failed - Item failed to download /*{{{*/
  65. // ---------------------------------------------------------------------
  66. /* We return to an idle state if there are still other queues that could
  67. fetch this object */
  68. void pkgAcquire::Item::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  69. {
  70. Status = StatIdle;
  71. ErrorText = LookupTag(Message,"Message");
  72. UsedMirror = LookupTag(Message,"UsedMirror");
  73. if (QueueCounter <= 1)
  74. {
  75. /* This indicates that the file is not available right now but might
  76. be sometime later. If we do a retry cycle then this should be
  77. retried [CDROMs] */
  78. if (Cnf->LocalOnly == true &&
  79. StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
  80. {
  81. Status = StatIdle;
  82. Dequeue();
  83. return;
  84. }
  85. Status = StatError;
  86. Dequeue();
  87. }
  88. // report mirror failure back to LP if we actually use a mirror
  89. string FailReason = LookupTag(Message, "FailReason");
  90. if(FailReason.size() != 0)
  91. ReportMirrorFailure(FailReason);
  92. else
  93. ReportMirrorFailure(ErrorText);
  94. }
  95. /*}}}*/
  96. // Acquire::Item::Start - Item has begun to download /*{{{*/
  97. // ---------------------------------------------------------------------
  98. /* Stash status and the file size. Note that setting Complete means
  99. sub-phases of the acquire process such as decompresion are operating */
  100. void pkgAcquire::Item::Start(string /*Message*/,unsigned long long Size)
  101. {
  102. Status = StatFetching;
  103. if (FileSize == 0 && Complete == false)
  104. FileSize = Size;
  105. }
  106. /*}}}*/
  107. // Acquire::Item::Done - Item downloaded OK /*{{{*/
  108. // ---------------------------------------------------------------------
  109. /* */
  110. void pkgAcquire::Item::Done(string Message,unsigned long long Size,string /*Hash*/,
  111. pkgAcquire::MethodConfig * /*Cnf*/)
  112. {
  113. // We just downloaded something..
  114. string FileName = LookupTag(Message,"Filename");
  115. UsedMirror = LookupTag(Message,"UsedMirror");
  116. if (Complete == false && !Local && FileName == DestFile)
  117. {
  118. if (Owner->Log != 0)
  119. Owner->Log->Fetched(Size,atoi(LookupTag(Message,"Resume-Point","0").c_str()));
  120. }
  121. if (FileSize == 0)
  122. FileSize= Size;
  123. Status = StatDone;
  124. ErrorText = string();
  125. Owner->Dequeue(this);
  126. }
  127. /*}}}*/
  128. // Acquire::Item::Rename - Rename a file /*{{{*/
  129. // ---------------------------------------------------------------------
  130. /* This helper function is used by a lot of item methods as their final
  131. step */
  132. void pkgAcquire::Item::Rename(string From,string To)
  133. {
  134. if (rename(From.c_str(),To.c_str()) != 0)
  135. {
  136. char S[300];
  137. snprintf(S,sizeof(S),_("rename failed, %s (%s -> %s)."),strerror(errno),
  138. From.c_str(),To.c_str());
  139. Status = StatError;
  140. ErrorText = S;
  141. }
  142. }
  143. /*}}}*/
  144. bool pkgAcquire::Item::RenameOnError(pkgAcquire::Item::RenameOnErrorState const error)/*{{{*/
  145. {
  146. if(FileExists(DestFile))
  147. Rename(DestFile, DestFile + ".FAILED");
  148. switch (error)
  149. {
  150. case HashSumMismatch:
  151. ErrorText = _("Hash Sum mismatch");
  152. Status = StatAuthError;
  153. ReportMirrorFailure("HashChecksumFailure");
  154. break;
  155. case SizeMismatch:
  156. ErrorText = _("Size mismatch");
  157. Status = StatAuthError;
  158. ReportMirrorFailure("SizeFailure");
  159. break;
  160. case InvalidFormat:
  161. ErrorText = _("Invalid file format");
  162. Status = StatError;
  163. // do not report as usually its not the mirrors fault, but Portal/Proxy
  164. break;
  165. }
  166. return false;
  167. }
  168. /*}}}*/
  169. // Acquire::Item::ReportMirrorFailure /*{{{*/
  170. // ---------------------------------------------------------------------
  171. void pkgAcquire::Item::ReportMirrorFailure(string FailCode)
  172. {
  173. // we only act if a mirror was used at all
  174. if(UsedMirror.empty())
  175. return;
  176. #if 0
  177. std::cerr << "\nReportMirrorFailure: "
  178. << UsedMirror
  179. << " Uri: " << DescURI()
  180. << " FailCode: "
  181. << FailCode << std::endl;
  182. #endif
  183. const char *Args[40];
  184. unsigned int i = 0;
  185. string report = _config->Find("Methods::Mirror::ProblemReporting",
  186. "/usr/lib/apt/apt-report-mirror-failure");
  187. if(!FileExists(report))
  188. return;
  189. Args[i++] = report.c_str();
  190. Args[i++] = UsedMirror.c_str();
  191. Args[i++] = DescURI().c_str();
  192. Args[i++] = FailCode.c_str();
  193. Args[i++] = NULL;
  194. pid_t pid = ExecFork();
  195. if(pid < 0)
  196. {
  197. _error->Error("ReportMirrorFailure Fork failed");
  198. return;
  199. }
  200. else if(pid == 0)
  201. {
  202. execvp(Args[0], (char**)Args);
  203. std::cerr << "Could not exec " << Args[0] << std::endl;
  204. _exit(100);
  205. }
  206. if(!ExecWait(pid, "report-mirror-failure"))
  207. {
  208. _error->Warning("Couldn't report problem to '%s'",
  209. _config->Find("Methods::Mirror::ProblemReporting").c_str());
  210. }
  211. }
  212. /*}}}*/
  213. // AcqSubIndex::AcqSubIndex - Constructor /*{{{*/
  214. // ---------------------------------------------------------------------
  215. /* Get a sub-index file based on checksums from a 'master' file and
  216. possibly query additional files */
  217. pkgAcqSubIndex::pkgAcqSubIndex(pkgAcquire *Owner, string const &URI,
  218. string const &URIDesc, string const &ShortDesc,
  219. HashString const &ExpectedHash)
  220. : Item(Owner), ExpectedHash(ExpectedHash)
  221. {
  222. /* XXX: Beware: Currently this class does nothing (of value) anymore ! */
  223. Debug = _config->FindB("Debug::pkgAcquire::SubIndex",false);
  224. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  225. DestFile += URItoFileName(URI);
  226. Desc.URI = URI;
  227. Desc.Description = URIDesc;
  228. Desc.Owner = this;
  229. Desc.ShortDesc = ShortDesc;
  230. QueueURI(Desc);
  231. if(Debug)
  232. std::clog << "pkgAcqSubIndex: " << Desc.URI << std::endl;
  233. }
  234. /*}}}*/
  235. // AcqSubIndex::Custom600Headers - Insert custom request headers /*{{{*/
  236. // ---------------------------------------------------------------------
  237. /* The only header we use is the last-modified header. */
  238. string pkgAcqSubIndex::Custom600Headers()
  239. {
  240. string Final = _config->FindDir("Dir::State::lists");
  241. Final += URItoFileName(Desc.URI);
  242. struct stat Buf;
  243. if (stat(Final.c_str(),&Buf) != 0)
  244. return "\nIndex-File: true\nFail-Ignore: true\n";
  245. return "\nIndex-File: true\nFail-Ignore: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
  246. }
  247. /*}}}*/
  248. void pkgAcqSubIndex::Failed(string Message,pkgAcquire::MethodConfig * /*Cnf*/)/*{{{*/
  249. {
  250. if(Debug)
  251. std::clog << "pkgAcqSubIndex failed: " << Desc.URI << " with " << Message << std::endl;
  252. Complete = false;
  253. Status = StatDone;
  254. Dequeue();
  255. // No good Index is provided
  256. }
  257. /*}}}*/
  258. void pkgAcqSubIndex::Done(string Message,unsigned long long Size,string Md5Hash, /*{{{*/
  259. pkgAcquire::MethodConfig *Cnf)
  260. {
  261. if(Debug)
  262. std::clog << "pkgAcqSubIndex::Done(): " << Desc.URI << std::endl;
  263. string FileName = LookupTag(Message,"Filename");
  264. if (FileName.empty() == true)
  265. {
  266. Status = StatError;
  267. ErrorText = "Method gave a blank filename";
  268. return;
  269. }
  270. if (FileName != DestFile)
  271. {
  272. Local = true;
  273. Desc.URI = "copy:" + FileName;
  274. QueueURI(Desc);
  275. return;
  276. }
  277. Item::Done(Message,Size,Md5Hash,Cnf);
  278. string FinalFile = _config->FindDir("Dir::State::lists")+URItoFileName(Desc.URI);
  279. /* Downloaded invalid transindex => Error (LP: #346386) (Closes: #627642) */
  280. indexRecords SubIndexParser;
  281. if (FileExists(DestFile) == true && !SubIndexParser.Load(DestFile)) {
  282. Status = StatError;
  283. ErrorText = SubIndexParser.ErrorText;
  284. return;
  285. }
  286. // success in downloading the index
  287. // rename the index
  288. if(Debug)
  289. std::clog << "Renaming: " << DestFile << " -> " << FinalFile << std::endl;
  290. Rename(DestFile,FinalFile);
  291. chmod(FinalFile.c_str(),0644);
  292. DestFile = FinalFile;
  293. if(ParseIndex(DestFile) == false)
  294. return Failed("", NULL);
  295. Complete = true;
  296. Status = StatDone;
  297. Dequeue();
  298. return;
  299. }
  300. /*}}}*/
  301. bool pkgAcqSubIndex::ParseIndex(string const &IndexFile) /*{{{*/
  302. {
  303. indexRecords SubIndexParser;
  304. if (FileExists(IndexFile) == false || SubIndexParser.Load(IndexFile) == false)
  305. return false;
  306. // so something with the downloaded index
  307. return true;
  308. }
  309. /*}}}*/
  310. // AcqDiffIndex::AcqDiffIndex - Constructor /*{{{*/
  311. // ---------------------------------------------------------------------
  312. /* Get the DiffIndex file first and see if there are patches available
  313. * If so, create a pkgAcqIndexDiffs fetcher that will get and apply the
  314. * patches. If anything goes wrong in that process, it will fall back to
  315. * the original packages file
  316. */
  317. pkgAcqDiffIndex::pkgAcqDiffIndex(pkgAcquire *Owner,
  318. string URI,string URIDesc,string ShortDesc,
  319. HashString ExpectedHash)
  320. : Item(Owner), RealURI(URI), ExpectedHash(ExpectedHash),
  321. Description(URIDesc)
  322. {
  323. Debug = _config->FindB("Debug::pkgAcquire::Diffs",false);
  324. Desc.Description = URIDesc + "/DiffIndex";
  325. Desc.Owner = this;
  326. Desc.ShortDesc = ShortDesc;
  327. Desc.URI = URI + ".diff/Index";
  328. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  329. DestFile += URItoFileName(Desc.URI);
  330. if(Debug)
  331. std::clog << "pkgAcqDiffIndex: " << Desc.URI << std::endl;
  332. // look for the current package file
  333. CurrentPackagesFile = _config->FindDir("Dir::State::lists");
  334. CurrentPackagesFile += URItoFileName(RealURI);
  335. // FIXME: this file:/ check is a hack to prevent fetching
  336. // from local sources. this is really silly, and
  337. // should be fixed cleanly as soon as possible
  338. if(!FileExists(CurrentPackagesFile) ||
  339. Desc.URI.substr(0,strlen("file:/")) == "file:/")
  340. {
  341. // we don't have a pkg file or we don't want to queue
  342. if(Debug)
  343. std::clog << "No index file, local or canceld by user" << std::endl;
  344. Failed("", NULL);
  345. return;
  346. }
  347. if(Debug)
  348. std::clog << "pkgAcqDiffIndex::pkgAcqDiffIndex(): "
  349. << CurrentPackagesFile << std::endl;
  350. QueueURI(Desc);
  351. }
  352. /*}}}*/
  353. // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
  354. // ---------------------------------------------------------------------
  355. /* The only header we use is the last-modified header. */
  356. string pkgAcqDiffIndex::Custom600Headers()
  357. {
  358. string Final = _config->FindDir("Dir::State::lists");
  359. Final += URItoFileName(Desc.URI);
  360. if(Debug)
  361. std::clog << "Custom600Header-IMS: " << Final << std::endl;
  362. struct stat Buf;
  363. if (stat(Final.c_str(),&Buf) != 0)
  364. return "\nIndex-File: true";
  365. return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
  366. }
  367. /*}}}*/
  368. bool pkgAcqDiffIndex::ParseDiffIndex(string IndexDiffFile) /*{{{*/
  369. {
  370. if(Debug)
  371. std::clog << "pkgAcqDiffIndex::ParseIndexDiff() " << IndexDiffFile
  372. << std::endl;
  373. pkgTagSection Tags;
  374. string ServerSha1;
  375. vector<DiffInfo> available_patches;
  376. FileFd Fd(IndexDiffFile,FileFd::ReadOnly);
  377. pkgTagFile TF(&Fd);
  378. if (_error->PendingError() == true)
  379. return false;
  380. if(TF.Step(Tags) == true)
  381. {
  382. bool found = false;
  383. DiffInfo d;
  384. string size;
  385. string const tmp = Tags.FindS("SHA1-Current");
  386. std::stringstream ss(tmp);
  387. ss >> ServerSha1 >> size;
  388. unsigned long const ServerSize = atol(size.c_str());
  389. FileFd fd(CurrentPackagesFile, FileFd::ReadOnly);
  390. SHA1Summation SHA1;
  391. SHA1.AddFD(fd);
  392. string const local_sha1 = SHA1.Result();
  393. if(local_sha1 == ServerSha1)
  394. {
  395. // we have the same sha1 as the server so we are done here
  396. if(Debug)
  397. std::clog << "Package file is up-to-date" << std::endl;
  398. // list cleanup needs to know that this file as well as the already
  399. // present index is ours, so we create an empty diff to save it for us
  400. new pkgAcqIndexDiffs(Owner, RealURI, Description, Desc.ShortDesc,
  401. ExpectedHash, ServerSha1, available_patches);
  402. return true;
  403. }
  404. else
  405. {
  406. if(Debug)
  407. std::clog << "SHA1-Current: " << ServerSha1 << " and we start at "<< fd.Name() << " " << fd.Size() << " " << local_sha1 << std::endl;
  408. // check the historie and see what patches we need
  409. string const history = Tags.FindS("SHA1-History");
  410. std::stringstream hist(history);
  411. while(hist >> d.sha1 >> size >> d.file)
  412. {
  413. // read until the first match is found
  414. // from that point on, we probably need all diffs
  415. if(d.sha1 == local_sha1)
  416. found=true;
  417. else if (found == false)
  418. continue;
  419. if(Debug)
  420. std::clog << "Need to get diff: " << d.file << std::endl;
  421. available_patches.push_back(d);
  422. }
  423. if (available_patches.empty() == false)
  424. {
  425. // patching with too many files is rather slow compared to a fast download
  426. unsigned long const fileLimit = _config->FindI("Acquire::PDiffs::FileLimit", 0);
  427. if (fileLimit != 0 && fileLimit < available_patches.size())
  428. {
  429. if (Debug)
  430. std::clog << "Need " << available_patches.size() << " diffs (Limit is " << fileLimit
  431. << ") so fallback to complete download" << std::endl;
  432. return false;
  433. }
  434. // see if the patches are too big
  435. found = false; // it was true and it will be true again at the end
  436. d = *available_patches.begin();
  437. string const firstPatch = d.file;
  438. unsigned long patchesSize = 0;
  439. std::stringstream patches(Tags.FindS("SHA1-Patches"));
  440. while(patches >> d.sha1 >> size >> d.file)
  441. {
  442. if (firstPatch == d.file)
  443. found = true;
  444. else if (found == false)
  445. continue;
  446. patchesSize += atol(size.c_str());
  447. }
  448. unsigned long const sizeLimit = ServerSize * _config->FindI("Acquire::PDiffs::SizeLimit", 100);
  449. if (sizeLimit > 0 && (sizeLimit/100) < patchesSize)
  450. {
  451. if (Debug)
  452. std::clog << "Need " << patchesSize << " bytes (Limit is " << sizeLimit/100
  453. << ") so fallback to complete download" << std::endl;
  454. return false;
  455. }
  456. }
  457. }
  458. // we have something, queue the next diff
  459. if(found)
  460. {
  461. // queue the diffs
  462. string::size_type const last_space = Description.rfind(" ");
  463. if(last_space != string::npos)
  464. Description.erase(last_space, Description.size()-last_space);
  465. /* decide if we should download patches one by one or in one go:
  466. The first is good if the server merges patches, but many don't so client
  467. based merging can be attempt in which case the second is better.
  468. "bad things" will happen if patches are merged on the server,
  469. but client side merging is attempt as well */
  470. bool pdiff_merge = _config->FindB("Acquire::PDiffs::Merge", true);
  471. if (pdiff_merge == true)
  472. {
  473. // reprepro adds this flag if it has merged patches on the server
  474. std::string const precedence = Tags.FindS("X-Patch-Precedence");
  475. pdiff_merge = (precedence != "merged");
  476. }
  477. if (pdiff_merge == false)
  478. new pkgAcqIndexDiffs(Owner, RealURI, Description, Desc.ShortDesc,
  479. ExpectedHash, ServerSha1, available_patches);
  480. else
  481. {
  482. std::vector<pkgAcqIndexMergeDiffs*> *diffs = new std::vector<pkgAcqIndexMergeDiffs*>(available_patches.size());
  483. for(size_t i = 0; i < available_patches.size(); ++i)
  484. (*diffs)[i] = new pkgAcqIndexMergeDiffs(Owner, RealURI, Description, Desc.ShortDesc, ExpectedHash,
  485. available_patches[i], diffs);
  486. }
  487. Complete = false;
  488. Status = StatDone;
  489. Dequeue();
  490. return true;
  491. }
  492. }
  493. // Nothing found, report and return false
  494. // Failing here is ok, if we return false later, the full
  495. // IndexFile is queued
  496. if(Debug)
  497. std::clog << "Can't find a patch in the index file" << std::endl;
  498. return false;
  499. }
  500. /*}}}*/
  501. void pkgAcqDiffIndex::Failed(string Message,pkgAcquire::MethodConfig * /*Cnf*/)/*{{{*/
  502. {
  503. if(Debug)
  504. std::clog << "pkgAcqDiffIndex failed: " << Desc.URI << " with " << Message << std::endl
  505. << "Falling back to normal index file acquire" << std::endl;
  506. new pkgAcqIndex(Owner, RealURI, Description, Desc.ShortDesc,
  507. ExpectedHash);
  508. Complete = false;
  509. Status = StatDone;
  510. Dequeue();
  511. }
  512. /*}}}*/
  513. void pkgAcqDiffIndex::Done(string Message,unsigned long long Size,string Md5Hash, /*{{{*/
  514. pkgAcquire::MethodConfig *Cnf)
  515. {
  516. if(Debug)
  517. std::clog << "pkgAcqDiffIndex::Done(): " << Desc.URI << std::endl;
  518. Item::Done(Message,Size,Md5Hash,Cnf);
  519. string FinalFile;
  520. FinalFile = _config->FindDir("Dir::State::lists")+URItoFileName(RealURI);
  521. // success in downloading the index
  522. // rename the index
  523. FinalFile += string(".IndexDiff");
  524. if(Debug)
  525. std::clog << "Renaming: " << DestFile << " -> " << FinalFile
  526. << std::endl;
  527. Rename(DestFile,FinalFile);
  528. chmod(FinalFile.c_str(),0644);
  529. DestFile = FinalFile;
  530. if(!ParseDiffIndex(DestFile))
  531. return Failed("", NULL);
  532. Complete = true;
  533. Status = StatDone;
  534. Dequeue();
  535. return;
  536. }
  537. /*}}}*/
  538. // AcqIndexDiffs::AcqIndexDiffs - Constructor /*{{{*/
  539. // ---------------------------------------------------------------------
  540. /* The package diff is added to the queue. one object is constructed
  541. * for each diff and the index
  542. */
  543. pkgAcqIndexDiffs::pkgAcqIndexDiffs(pkgAcquire *Owner,
  544. string URI,string URIDesc,string ShortDesc,
  545. HashString ExpectedHash,
  546. string ServerSha1,
  547. vector<DiffInfo> diffs)
  548. : Item(Owner), RealURI(URI), ExpectedHash(ExpectedHash),
  549. available_patches(diffs), ServerSha1(ServerSha1)
  550. {
  551. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  552. DestFile += URItoFileName(URI);
  553. Debug = _config->FindB("Debug::pkgAcquire::Diffs",false);
  554. Description = URIDesc;
  555. Desc.Owner = this;
  556. Desc.ShortDesc = ShortDesc;
  557. if(available_patches.empty() == true)
  558. {
  559. // we are done (yeah!)
  560. Finish(true);
  561. }
  562. else
  563. {
  564. // get the next diff
  565. State = StateFetchDiff;
  566. QueueNextDiff();
  567. }
  568. }
  569. /*}}}*/
  570. void pkgAcqIndexDiffs::Failed(string Message,pkgAcquire::MethodConfig * /*Cnf*/)/*{{{*/
  571. {
  572. if(Debug)
  573. std::clog << "pkgAcqIndexDiffs failed: " << Desc.URI << " with " << Message << std::endl
  574. << "Falling back to normal index file acquire" << std::endl;
  575. new pkgAcqIndex(Owner, RealURI, Description,Desc.ShortDesc,
  576. ExpectedHash);
  577. Finish();
  578. }
  579. /*}}}*/
  580. // Finish - helper that cleans the item out of the fetcher queue /*{{{*/
  581. void pkgAcqIndexDiffs::Finish(bool allDone)
  582. {
  583. // we restore the original name, this is required, otherwise
  584. // the file will be cleaned
  585. if(allDone)
  586. {
  587. DestFile = _config->FindDir("Dir::State::lists");
  588. DestFile += URItoFileName(RealURI);
  589. if(!ExpectedHash.empty() && !ExpectedHash.VerifyFile(DestFile))
  590. {
  591. RenameOnError(HashSumMismatch);
  592. Dequeue();
  593. return;
  594. }
  595. // this is for the "real" finish
  596. Complete = true;
  597. Status = StatDone;
  598. Dequeue();
  599. if(Debug)
  600. std::clog << "\n\nallDone: " << DestFile << "\n" << std::endl;
  601. return;
  602. }
  603. if(Debug)
  604. std::clog << "Finishing: " << Desc.URI << std::endl;
  605. Complete = false;
  606. Status = StatDone;
  607. Dequeue();
  608. return;
  609. }
  610. /*}}}*/
  611. bool pkgAcqIndexDiffs::QueueNextDiff() /*{{{*/
  612. {
  613. // calc sha1 of the just patched file
  614. string FinalFile = _config->FindDir("Dir::State::lists");
  615. FinalFile += URItoFileName(RealURI);
  616. FileFd fd(FinalFile, FileFd::ReadOnly);
  617. SHA1Summation SHA1;
  618. SHA1.AddFD(fd);
  619. string local_sha1 = string(SHA1.Result());
  620. if(Debug)
  621. std::clog << "QueueNextDiff: "
  622. << FinalFile << " (" << local_sha1 << ")"<<std::endl;
  623. // final file reached before all patches are applied
  624. if(local_sha1 == ServerSha1)
  625. {
  626. Finish(true);
  627. return true;
  628. }
  629. // remove all patches until the next matching patch is found
  630. // this requires the Index file to be ordered
  631. for(vector<DiffInfo>::iterator I=available_patches.begin();
  632. available_patches.empty() == false &&
  633. I != available_patches.end() &&
  634. I->sha1 != local_sha1;
  635. ++I)
  636. {
  637. available_patches.erase(I);
  638. }
  639. // error checking and falling back if no patch was found
  640. if(available_patches.empty() == true)
  641. {
  642. Failed("", NULL);
  643. return false;
  644. }
  645. // queue the right diff
  646. Desc.URI = RealURI + ".diff/" + available_patches[0].file + ".gz";
  647. Desc.Description = Description + " " + available_patches[0].file + string(".pdiff");
  648. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  649. DestFile += URItoFileName(RealURI + ".diff/" + available_patches[0].file);
  650. if(Debug)
  651. std::clog << "pkgAcqIndexDiffs::QueueNextDiff(): " << Desc.URI << std::endl;
  652. QueueURI(Desc);
  653. return true;
  654. }
  655. /*}}}*/
  656. void pkgAcqIndexDiffs::Done(string Message,unsigned long long Size,string Md5Hash, /*{{{*/
  657. pkgAcquire::MethodConfig *Cnf)
  658. {
  659. if(Debug)
  660. std::clog << "pkgAcqIndexDiffs::Done(): " << Desc.URI << std::endl;
  661. Item::Done(Message,Size,Md5Hash,Cnf);
  662. string FinalFile;
  663. FinalFile = _config->FindDir("Dir::State::lists")+URItoFileName(RealURI);
  664. // success in downloading a diff, enter ApplyDiff state
  665. if(State == StateFetchDiff)
  666. {
  667. // rred excepts the patch as $FinalFile.ed
  668. Rename(DestFile,FinalFile+".ed");
  669. if(Debug)
  670. std::clog << "Sending to rred method: " << FinalFile << std::endl;
  671. State = StateApplyDiff;
  672. Local = true;
  673. Desc.URI = "rred:" + FinalFile;
  674. QueueURI(Desc);
  675. Mode = "rred";
  676. return;
  677. }
  678. // success in download/apply a diff, queue next (if needed)
  679. if(State == StateApplyDiff)
  680. {
  681. // remove the just applied patch
  682. available_patches.erase(available_patches.begin());
  683. unlink((FinalFile + ".ed").c_str());
  684. // move into place
  685. if(Debug)
  686. {
  687. std::clog << "Moving patched file in place: " << std::endl
  688. << DestFile << " -> " << FinalFile << std::endl;
  689. }
  690. Rename(DestFile,FinalFile);
  691. chmod(FinalFile.c_str(),0644);
  692. // see if there is more to download
  693. if(available_patches.empty() == false) {
  694. new pkgAcqIndexDiffs(Owner, RealURI, Description, Desc.ShortDesc,
  695. ExpectedHash, ServerSha1, available_patches);
  696. return Finish();
  697. } else
  698. return Finish(true);
  699. }
  700. }
  701. /*}}}*/
  702. // AcqIndexMergeDiffs::AcqIndexMergeDiffs - Constructor /*{{{*/
  703. pkgAcqIndexMergeDiffs::pkgAcqIndexMergeDiffs(pkgAcquire *Owner,
  704. string const &URI, string const &URIDesc,
  705. string const &ShortDesc, HashString const &ExpectedHash,
  706. DiffInfo const &patch,
  707. std::vector<pkgAcqIndexMergeDiffs*> const * const allPatches)
  708. : Item(Owner), RealURI(URI), ExpectedHash(ExpectedHash),
  709. patch(patch),allPatches(allPatches), State(StateFetchDiff)
  710. {
  711. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  712. DestFile += URItoFileName(URI);
  713. Debug = _config->FindB("Debug::pkgAcquire::Diffs",false);
  714. Description = URIDesc;
  715. Desc.Owner = this;
  716. Desc.ShortDesc = ShortDesc;
  717. Desc.URI = RealURI + ".diff/" + patch.file + ".gz";
  718. Desc.Description = Description + " " + patch.file + string(".pdiff");
  719. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  720. DestFile += URItoFileName(RealURI + ".diff/" + patch.file);
  721. if(Debug)
  722. std::clog << "pkgAcqIndexMergeDiffs: " << Desc.URI << std::endl;
  723. QueueURI(Desc);
  724. }
  725. /*}}}*/
  726. void pkgAcqIndexMergeDiffs::Failed(string Message,pkgAcquire::MethodConfig * /*Cnf*/)/*{{{*/
  727. {
  728. if(Debug)
  729. std::clog << "pkgAcqIndexMergeDiffs failed: " << Desc.URI << " with " << Message << std::endl;
  730. Complete = false;
  731. Status = StatDone;
  732. Dequeue();
  733. // check if we are the first to fail, otherwise we are done here
  734. State = StateDoneDiff;
  735. for (std::vector<pkgAcqIndexMergeDiffs *>::const_iterator I = allPatches->begin();
  736. I != allPatches->end(); ++I)
  737. if ((*I)->State == StateErrorDiff)
  738. return;
  739. // first failure means we should fallback
  740. State = StateErrorDiff;
  741. std::clog << "Falling back to normal index file acquire" << std::endl;
  742. new pkgAcqIndex(Owner, RealURI, Description,Desc.ShortDesc,
  743. ExpectedHash);
  744. }
  745. /*}}}*/
  746. void pkgAcqIndexMergeDiffs::Done(string Message,unsigned long long Size,string Md5Hash, /*{{{*/
  747. pkgAcquire::MethodConfig *Cnf)
  748. {
  749. if(Debug)
  750. std::clog << "pkgAcqIndexMergeDiffs::Done(): " << Desc.URI << std::endl;
  751. Item::Done(Message,Size,Md5Hash,Cnf);
  752. string const FinalFile = _config->FindDir("Dir::State::lists") + URItoFileName(RealURI);
  753. if (State == StateFetchDiff)
  754. {
  755. // rred expects the patch as $FinalFile.ed.$patchname.gz
  756. Rename(DestFile, FinalFile + ".ed." + patch.file + ".gz");
  757. // check if this is the last completed diff
  758. State = StateDoneDiff;
  759. for (std::vector<pkgAcqIndexMergeDiffs *>::const_iterator I = allPatches->begin();
  760. I != allPatches->end(); ++I)
  761. if ((*I)->State != StateDoneDiff)
  762. {
  763. if(Debug)
  764. std::clog << "Not the last done diff in the batch: " << Desc.URI << std::endl;
  765. return;
  766. }
  767. // this is the last completed diff, so we are ready to apply now
  768. State = StateApplyDiff;
  769. if(Debug)
  770. std::clog << "Sending to rred method: " << FinalFile << std::endl;
  771. Local = true;
  772. Desc.URI = "rred:" + FinalFile;
  773. QueueURI(Desc);
  774. Mode = "rred";
  775. return;
  776. }
  777. // success in download/apply all diffs, clean up
  778. else if (State == StateApplyDiff)
  779. {
  780. // see if we really got the expected file
  781. if(!ExpectedHash.empty() && !ExpectedHash.VerifyFile(DestFile))
  782. {
  783. RenameOnError(HashSumMismatch);
  784. return;
  785. }
  786. // move the result into place
  787. if(Debug)
  788. std::clog << "Moving patched file in place: " << std::endl
  789. << DestFile << " -> " << FinalFile << std::endl;
  790. Rename(DestFile, FinalFile);
  791. chmod(FinalFile.c_str(), 0644);
  792. // otherwise lists cleanup will eat the file
  793. DestFile = FinalFile;
  794. // ensure the ed's are gone regardless of list-cleanup
  795. for (std::vector<pkgAcqIndexMergeDiffs *>::const_iterator I = allPatches->begin();
  796. I != allPatches->end(); ++I)
  797. {
  798. std::string patch = FinalFile + ".ed." + (*I)->patch.file + ".gz";
  799. unlink(patch.c_str());
  800. }
  801. // all set and done
  802. Complete = true;
  803. if(Debug)
  804. std::clog << "allDone: " << DestFile << "\n" << std::endl;
  805. }
  806. }
  807. /*}}}*/
  808. // AcqIndex::AcqIndex - Constructor /*{{{*/
  809. // ---------------------------------------------------------------------
  810. /* The package file is added to the queue and a second class is
  811. instantiated to fetch the revision file */
  812. pkgAcqIndex::pkgAcqIndex(pkgAcquire *Owner,
  813. string URI,string URIDesc,string ShortDesc,
  814. HashString ExpectedHash, string comprExt)
  815. : Item(Owner), RealURI(URI), ExpectedHash(ExpectedHash)
  816. {
  817. if(comprExt.empty() == true)
  818. {
  819. // autoselect the compression method
  820. std::vector<std::string> types = APT::Configuration::getCompressionTypes();
  821. for (std::vector<std::string>::const_iterator t = types.begin(); t != types.end(); ++t)
  822. comprExt.append(*t).append(" ");
  823. if (comprExt.empty() == false)
  824. comprExt.erase(comprExt.end()-1);
  825. }
  826. CompressionExtension = comprExt;
  827. Init(URI, URIDesc, ShortDesc);
  828. }
  829. pkgAcqIndex::pkgAcqIndex(pkgAcquire *Owner, IndexTarget const *Target,
  830. HashString const &ExpectedHash, indexRecords const *MetaIndexParser)
  831. : Item(Owner), RealURI(Target->URI), ExpectedHash(ExpectedHash)
  832. {
  833. // autoselect the compression method
  834. std::vector<std::string> types = APT::Configuration::getCompressionTypes();
  835. CompressionExtension = "";
  836. if (ExpectedHash.empty() == false)
  837. {
  838. for (std::vector<std::string>::const_iterator t = types.begin(); t != types.end(); ++t)
  839. if (*t == "uncompressed" || MetaIndexParser->Exists(string(Target->MetaKey).append(".").append(*t)) == true)
  840. CompressionExtension.append(*t).append(" ");
  841. }
  842. else
  843. {
  844. for (std::vector<std::string>::const_iterator t = types.begin(); t != types.end(); ++t)
  845. CompressionExtension.append(*t).append(" ");
  846. }
  847. if (CompressionExtension.empty() == false)
  848. CompressionExtension.erase(CompressionExtension.end()-1);
  849. Init(Target->URI, Target->Description, Target->ShortDesc);
  850. }
  851. /*}}}*/
  852. // AcqIndex::Init - defered Constructor /*{{{*/
  853. void pkgAcqIndex::Init(string const &URI, string const &URIDesc, string const &ShortDesc) {
  854. Decompression = false;
  855. Erase = false;
  856. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  857. DestFile += URItoFileName(URI);
  858. std::string const comprExt = CompressionExtension.substr(0, CompressionExtension.find(' '));
  859. if (comprExt == "uncompressed")
  860. Desc.URI = URI;
  861. else {
  862. Desc.URI = URI + '.' + comprExt;
  863. DestFile = DestFile + '.' + comprExt;
  864. }
  865. Desc.Description = URIDesc;
  866. Desc.Owner = this;
  867. Desc.ShortDesc = ShortDesc;
  868. QueueURI(Desc);
  869. }
  870. /*}}}*/
  871. // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
  872. // ---------------------------------------------------------------------
  873. /* The only header we use is the last-modified header. */
  874. string pkgAcqIndex::Custom600Headers()
  875. {
  876. std::string const compExt = CompressionExtension.substr(0, CompressionExtension.find(' '));
  877. string Final = _config->FindDir("Dir::State::lists");
  878. Final += URItoFileName(RealURI);
  879. if (_config->FindB("Acquire::GzipIndexes",false))
  880. Final += compExt;
  881. string msg = "\nIndex-File: true";
  882. // FIXME: this really should use "IndexTarget::IsOptional()" but that
  883. // seems to be difficult without breaking ABI
  884. if (ShortDesc().find("Translation") != 0)
  885. msg += "\nFail-Ignore: true";
  886. struct stat Buf;
  887. if (stat(Final.c_str(),&Buf) == 0)
  888. msg += "\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
  889. return msg;
  890. }
  891. /*}}}*/
  892. void pkgAcqIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf) /*{{{*/
  893. {
  894. size_t const nextExt = CompressionExtension.find(' ');
  895. if (nextExt != std::string::npos)
  896. {
  897. CompressionExtension = CompressionExtension.substr(nextExt+1);
  898. Init(RealURI, Desc.Description, Desc.ShortDesc);
  899. return;
  900. }
  901. // on decompression failure, remove bad versions in partial/
  902. if (Decompression && Erase) {
  903. string s = _config->FindDir("Dir::State::lists") + "partial/";
  904. s.append(URItoFileName(RealURI));
  905. unlink(s.c_str());
  906. }
  907. Item::Failed(Message,Cnf);
  908. }
  909. /*}}}*/
  910. // pkgAcqIndex::GetFinalFilename - Return the full final file path /*{{{*/
  911. std::string pkgAcqIndex::GetFinalFilename(std::string const &URI,
  912. std::string const &compExt)
  913. {
  914. std::string FinalFile = _config->FindDir("Dir::State::lists");
  915. FinalFile += URItoFileName(URI);
  916. if (_config->FindB("Acquire::GzipIndexes",false) == true)
  917. FinalFile += '.' + compExt;
  918. return FinalFile;
  919. }
  920. /*}}}*/
  921. // AcqIndex::ReverifyAfterIMS - Reverify index after an ims-hit /*{{{*/
  922. void pkgAcqIndex::ReverifyAfterIMS(std::string const &FileName)
  923. {
  924. std::string const compExt = CompressionExtension.substr(0, CompressionExtension.find(' '));
  925. if (_config->FindB("Acquire::GzipIndexes",false) == true)
  926. DestFile += compExt;
  927. string FinalFile = GetFinalFilename(RealURI, compExt);
  928. Rename(FinalFile, FileName);
  929. Decompression = true;
  930. Desc.URI = "copy:" + FileName;
  931. QueueURI(Desc);
  932. }
  933. /*}}}*/
  934. // AcqIndex::Done - Finished a fetch /*{{{*/
  935. // ---------------------------------------------------------------------
  936. /* This goes through a number of states.. On the initial fetch the
  937. method could possibly return an alternate filename which points
  938. to the uncompressed version of the file. If this is so the file
  939. is copied into the partial directory. In all other cases the file
  940. is decompressed with a gzip uri. */
  941. void pkgAcqIndex::Done(string Message,unsigned long long Size,string Hash,
  942. pkgAcquire::MethodConfig *Cfg)
  943. {
  944. Item::Done(Message,Size,Hash,Cfg);
  945. std::string const compExt = CompressionExtension.substr(0, CompressionExtension.find(' '));
  946. if (Decompression == true)
  947. {
  948. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  949. {
  950. std::cerr << std::endl << RealURI << ": Computed Hash: " << Hash;
  951. std::cerr << " Expected Hash: " << ExpectedHash.toStr() << std::endl;
  952. }
  953. if (!ExpectedHash.empty() && ExpectedHash.toStr() != Hash)
  954. {
  955. Desc.URI = RealURI;
  956. RenameOnError(HashSumMismatch);
  957. return;
  958. }
  959. // FIXME: this can go away once we only ever download stuff that
  960. // has a valid hash and we never do GET based probing
  961. //
  962. /* Always verify the index file for correctness (all indexes must
  963. * have a Package field) (LP: #346386) (Closes: #627642)
  964. */
  965. FileFd fd(DestFile, FileFd::ReadOnly, FileFd::Extension);
  966. // Only test for correctness if the file is not empty (empty is ok)
  967. if (fd.Size() > 0)
  968. {
  969. pkgTagSection sec;
  970. pkgTagFile tag(&fd);
  971. // all our current indexes have a field 'Package' in each section
  972. if (_error->PendingError() == true || tag.Step(sec) == false || sec.Exists("Package") == false)
  973. {
  974. RenameOnError(InvalidFormat);
  975. return;
  976. }
  977. }
  978. // Done, move it into position
  979. string FinalFile = GetFinalFilename(RealURI, compExt);
  980. Rename(DestFile,FinalFile);
  981. chmod(FinalFile.c_str(),0644);
  982. /* We restore the original name to DestFile so that the clean operation
  983. will work OK */
  984. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  985. DestFile += URItoFileName(RealURI);
  986. if (_config->FindB("Acquire::GzipIndexes",false))
  987. DestFile += '.' + compExt;
  988. // Remove the compressed version.
  989. if (Erase == true)
  990. unlink(DestFile.c_str());
  991. return;
  992. }
  993. Erase = false;
  994. Complete = true;
  995. // Handle the unzipd case
  996. string FileName = LookupTag(Message,"Alt-Filename");
  997. if (FileName.empty() == false)
  998. {
  999. Decompression = true;
  1000. Local = true;
  1001. DestFile += ".decomp";
  1002. Desc.URI = "copy:" + FileName;
  1003. QueueURI(Desc);
  1004. Mode = "copy";
  1005. return;
  1006. }
  1007. FileName = LookupTag(Message,"Filename");
  1008. if (FileName.empty() == true)
  1009. {
  1010. Status = StatError;
  1011. ErrorText = "Method gave a blank filename";
  1012. }
  1013. if (FileName == DestFile)
  1014. Erase = true;
  1015. else
  1016. Local = true;
  1017. // do not reverify cdrom sources as apt-cdrom may rewrite the Packages
  1018. // file when its doing the indexcopy
  1019. if (RealURI.substr(0,6) == "cdrom:" &&
  1020. StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
  1021. return;
  1022. // The files timestamp matches, for non-local URLs reverify the local
  1023. // file, for local file, uncompress again to ensure the hashsum is still
  1024. // matching the Release file
  1025. if (!Local && StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
  1026. {
  1027. // set destfile to the final destfile
  1028. if(_config->FindB("Acquire::GzipIndexes",false) == false)
  1029. {
  1030. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  1031. DestFile += URItoFileName(RealURI);
  1032. }
  1033. ReverifyAfterIMS(FileName);
  1034. return;
  1035. }
  1036. string decompProg;
  1037. // If we enable compressed indexes, queue for hash verification
  1038. if (_config->FindB("Acquire::GzipIndexes",false))
  1039. {
  1040. DestFile = _config->FindDir("Dir::State::lists");
  1041. DestFile += URItoFileName(RealURI) + '.' + compExt;
  1042. Decompression = true;
  1043. Desc.URI = "copy:" + FileName;
  1044. QueueURI(Desc);
  1045. return;
  1046. }
  1047. // get the binary name for your used compression type
  1048. decompProg = _config->Find(string("Acquire::CompressionTypes::").append(compExt),"");
  1049. if(decompProg.empty() == false);
  1050. else if(compExt == "uncompressed")
  1051. decompProg = "copy";
  1052. else {
  1053. _error->Error("Unsupported extension: %s", compExt.c_str());
  1054. return;
  1055. }
  1056. Decompression = true;
  1057. DestFile += ".decomp";
  1058. Desc.URI = decompProg + ":" + FileName;
  1059. QueueURI(Desc);
  1060. // FIXME: this points to a c++ string that goes out of scope
  1061. Mode = decompProg.c_str();
  1062. }
  1063. /*}}}*/
  1064. // AcqIndexTrans::pkgAcqIndexTrans - Constructor /*{{{*/
  1065. // ---------------------------------------------------------------------
  1066. /* The Translation file is added to the queue */
  1067. pkgAcqIndexTrans::pkgAcqIndexTrans(pkgAcquire *Owner,
  1068. string URI,string URIDesc,string ShortDesc)
  1069. : pkgAcqIndex(Owner, URI, URIDesc, ShortDesc, HashString(), "")
  1070. {
  1071. }
  1072. pkgAcqIndexTrans::pkgAcqIndexTrans(pkgAcquire *Owner, IndexTarget const *Target,
  1073. HashString const &ExpectedHash, indexRecords const *MetaIndexParser)
  1074. : pkgAcqIndex(Owner, Target, ExpectedHash, MetaIndexParser)
  1075. {
  1076. }
  1077. /*}}}*/
  1078. // AcqIndexTrans::Custom600Headers - Insert custom request headers /*{{{*/
  1079. // ---------------------------------------------------------------------
  1080. string pkgAcqIndexTrans::Custom600Headers()
  1081. {
  1082. std::string const compExt = CompressionExtension.substr(0, CompressionExtension.find(' '));
  1083. string Final = _config->FindDir("Dir::State::lists");
  1084. Final += URItoFileName(RealURI);
  1085. if (_config->FindB("Acquire::GzipIndexes",false))
  1086. Final += compExt;
  1087. struct stat Buf;
  1088. if (stat(Final.c_str(),&Buf) != 0)
  1089. return "\nFail-Ignore: true\nIndex-File: true";
  1090. return "\nFail-Ignore: true\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
  1091. }
  1092. /*}}}*/
  1093. // AcqIndexTrans::Failed - Silence failure messages for missing files /*{{{*/
  1094. // ---------------------------------------------------------------------
  1095. /* */
  1096. void pkgAcqIndexTrans::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  1097. {
  1098. size_t const nextExt = CompressionExtension.find(' ');
  1099. if (nextExt != std::string::npos)
  1100. {
  1101. CompressionExtension = CompressionExtension.substr(nextExt+1);
  1102. Init(RealURI, Desc.Description, Desc.ShortDesc);
  1103. Status = StatIdle;
  1104. return;
  1105. }
  1106. if (Cnf->LocalOnly == true ||
  1107. StringToBool(LookupTag(Message,"Transient-Failure"),false) == false)
  1108. {
  1109. // Ignore this
  1110. Status = StatDone;
  1111. Complete = false;
  1112. Dequeue();
  1113. return;
  1114. }
  1115. Item::Failed(Message,Cnf);
  1116. }
  1117. /*}}}*/
  1118. pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire *Owner, /*{{{*/
  1119. string URI,string URIDesc,string ShortDesc,
  1120. string MetaIndexURI, string MetaIndexURIDesc,
  1121. string MetaIndexShortDesc,
  1122. const vector<IndexTarget*>* IndexTargets,
  1123. indexRecords* MetaIndexParser) :
  1124. Item(Owner), RealURI(URI), MetaIndexURI(MetaIndexURI),
  1125. MetaIndexURIDesc(MetaIndexURIDesc), MetaIndexShortDesc(MetaIndexShortDesc),
  1126. MetaIndexParser(MetaIndexParser), IndexTargets(IndexTargets)
  1127. {
  1128. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  1129. DestFile += URItoFileName(URI);
  1130. // remove any partial downloaded sig-file in partial/.
  1131. // it may confuse proxies and is too small to warrant a
  1132. // partial download anyway
  1133. unlink(DestFile.c_str());
  1134. // Create the item
  1135. Desc.Description = URIDesc;
  1136. Desc.Owner = this;
  1137. Desc.ShortDesc = ShortDesc;
  1138. Desc.URI = URI;
  1139. string Final = _config->FindDir("Dir::State::lists");
  1140. Final += URItoFileName(RealURI);
  1141. if (RealFileExists(Final) == true)
  1142. {
  1143. // File was already in place. It needs to be re-downloaded/verified
  1144. // because Release might have changed, we do give it a different
  1145. // name than DestFile because otherwise the http method will
  1146. // send If-Range requests and there are too many broken servers
  1147. // out there that do not understand them
  1148. LastGoodSig = DestFile+".reverify";
  1149. Rename(Final,LastGoodSig);
  1150. }
  1151. QueueURI(Desc);
  1152. }
  1153. /*}}}*/
  1154. pkgAcqMetaSig::~pkgAcqMetaSig() /*{{{*/
  1155. {
  1156. // if the file was never queued undo file-changes done in the constructor
  1157. if (QueueCounter == 1 && Status == StatIdle && FileSize == 0 && Complete == false &&
  1158. LastGoodSig.empty() == false)
  1159. {
  1160. string const Final = _config->FindDir("Dir::State::lists") + URItoFileName(RealURI);
  1161. if (RealFileExists(Final) == false && RealFileExists(LastGoodSig) == true)
  1162. Rename(LastGoodSig, Final);
  1163. }
  1164. }
  1165. /*}}}*/
  1166. // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
  1167. // ---------------------------------------------------------------------
  1168. /* The only header we use is the last-modified header. */
  1169. string pkgAcqMetaSig::Custom600Headers()
  1170. {
  1171. struct stat Buf;
  1172. if (stat(LastGoodSig.c_str(),&Buf) != 0)
  1173. return "\nIndex-File: true";
  1174. return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
  1175. }
  1176. void pkgAcqMetaSig::Done(string Message,unsigned long long Size,string MD5,
  1177. pkgAcquire::MethodConfig *Cfg)
  1178. {
  1179. Item::Done(Message,Size,MD5,Cfg);
  1180. string FileName = LookupTag(Message,"Filename");
  1181. if (FileName.empty() == true)
  1182. {
  1183. Status = StatError;
  1184. ErrorText = "Method gave a blank filename";
  1185. return;
  1186. }
  1187. if (FileName != DestFile)
  1188. {
  1189. // We have to copy it into place
  1190. Local = true;
  1191. Desc.URI = "copy:" + FileName;
  1192. QueueURI(Desc);
  1193. return;
  1194. }
  1195. Complete = true;
  1196. // put the last known good file back on i-m-s hit (it will
  1197. // be re-verified again)
  1198. // Else do nothing, we have the new file in DestFile then
  1199. if(StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
  1200. Rename(LastGoodSig, DestFile);
  1201. // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
  1202. new pkgAcqMetaIndex(Owner, MetaIndexURI, MetaIndexURIDesc,
  1203. MetaIndexShortDesc, DestFile, IndexTargets,
  1204. MetaIndexParser);
  1205. }
  1206. /*}}}*/
  1207. void pkgAcqMetaSig::Failed(string Message,pkgAcquire::MethodConfig *Cnf)/*{{{*/
  1208. {
  1209. string Final = _config->FindDir("Dir::State::lists") + URItoFileName(RealURI);
  1210. // if we get a network error we fail gracefully
  1211. if(Status == StatTransientNetworkError)
  1212. {
  1213. Item::Failed(Message,Cnf);
  1214. // move the sigfile back on transient network failures
  1215. if(FileExists(LastGoodSig))
  1216. Rename(LastGoodSig,Final);
  1217. // set the status back to , Item::Failed likes to reset it
  1218. Status = pkgAcquire::Item::StatTransientNetworkError;
  1219. return;
  1220. }
  1221. // Delete any existing sigfile when the acquire failed
  1222. unlink(Final.c_str());
  1223. // queue a pkgAcqMetaIndex with no sigfile
  1224. new pkgAcqMetaIndex(Owner, MetaIndexURI, MetaIndexURIDesc, MetaIndexShortDesc,
  1225. "", IndexTargets, MetaIndexParser);
  1226. if (Cnf->LocalOnly == true ||
  1227. StringToBool(LookupTag(Message,"Transient-Failure"),false) == false)
  1228. {
  1229. // Ignore this
  1230. Status = StatDone;
  1231. Complete = false;
  1232. Dequeue();
  1233. return;
  1234. }
  1235. Item::Failed(Message,Cnf);
  1236. }
  1237. /*}}}*/
  1238. pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire *Owner, /*{{{*/
  1239. string URI,string URIDesc,string ShortDesc,
  1240. string SigFile,
  1241. const vector<struct IndexTarget*>* IndexTargets,
  1242. indexRecords* MetaIndexParser) :
  1243. Item(Owner), RealURI(URI), SigFile(SigFile), IndexTargets(IndexTargets),
  1244. MetaIndexParser(MetaIndexParser), AuthPass(false), IMSHit(false)
  1245. {
  1246. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  1247. DestFile += URItoFileName(URI);
  1248. // Create the item
  1249. Desc.Description = URIDesc;
  1250. Desc.Owner = this;
  1251. Desc.ShortDesc = ShortDesc;
  1252. Desc.URI = URI;
  1253. QueueURI(Desc);
  1254. }
  1255. /*}}}*/
  1256. // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
  1257. // ---------------------------------------------------------------------
  1258. /* The only header we use is the last-modified header. */
  1259. string pkgAcqMetaIndex::Custom600Headers()
  1260. {
  1261. string Final = _config->FindDir("Dir::State::lists");
  1262. Final += URItoFileName(RealURI);
  1263. struct stat Buf;
  1264. if (stat(Final.c_str(),&Buf) != 0)
  1265. return "\nIndex-File: true";
  1266. return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
  1267. }
  1268. /*}}}*/
  1269. void pkgAcqMetaIndex::Done(string Message,unsigned long long Size,string Hash, /*{{{*/
  1270. pkgAcquire::MethodConfig *Cfg)
  1271. {
  1272. Item::Done(Message,Size,Hash,Cfg);
  1273. // MetaIndexes are done in two passes: one to download the
  1274. // metaindex with an appropriate method, and a second to verify it
  1275. // with the gpgv method
  1276. if (AuthPass == true)
  1277. {
  1278. AuthDone(Message);
  1279. // all cool, move Release file into place
  1280. Complete = true;
  1281. }
  1282. else
  1283. {
  1284. RetrievalDone(Message);
  1285. if (!Complete)
  1286. // Still more retrieving to do
  1287. return;
  1288. if (SigFile == "")
  1289. {
  1290. // There was no signature file, so we are finished. Download
  1291. // the indexes and do only hashsum verification if possible
  1292. MetaIndexParser->Load(DestFile);
  1293. QueueIndexes(false);
  1294. }
  1295. else
  1296. {
  1297. // There was a signature file, so pass it to gpgv for
  1298. // verification
  1299. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  1300. std::cerr << "Metaindex acquired, queueing gpg verification ("
  1301. << SigFile << "," << DestFile << ")\n";
  1302. AuthPass = true;
  1303. Desc.URI = "gpgv:" + SigFile;
  1304. QueueURI(Desc);
  1305. Mode = "gpgv";
  1306. return;
  1307. }
  1308. }
  1309. if (Complete == true)
  1310. {
  1311. string FinalFile = _config->FindDir("Dir::State::lists");
  1312. FinalFile += URItoFileName(RealURI);
  1313. if (SigFile == DestFile)
  1314. SigFile = FinalFile;
  1315. Rename(DestFile,FinalFile);
  1316. chmod(FinalFile.c_str(),0644);
  1317. DestFile = FinalFile;
  1318. }
  1319. }
  1320. /*}}}*/
  1321. void pkgAcqMetaIndex::RetrievalDone(string Message) /*{{{*/
  1322. {
  1323. // We have just finished downloading a Release file (it is not
  1324. // verified yet)
  1325. string FileName = LookupTag(Message,"Filename");
  1326. if (FileName.empty() == true)
  1327. {
  1328. Status = StatError;
  1329. ErrorText = "Method gave a blank filename";
  1330. return;
  1331. }
  1332. if (FileName != DestFile)
  1333. {
  1334. Local = true;
  1335. Desc.URI = "copy:" + FileName;
  1336. QueueURI(Desc);
  1337. return;
  1338. }
  1339. // make sure to verify against the right file on I-M-S hit
  1340. IMSHit = StringToBool(LookupTag(Message,"IMS-Hit"),false);
  1341. if(IMSHit)
  1342. {
  1343. string FinalFile = _config->FindDir("Dir::State::lists");
  1344. FinalFile += URItoFileName(RealURI);
  1345. if (SigFile == DestFile)
  1346. {
  1347. SigFile = FinalFile;
  1348. // constructor of pkgAcqMetaClearSig moved it out of the way,
  1349. // now move it back in on IMS hit for the 'old' file
  1350. string const OldClearSig = DestFile + ".reverify";
  1351. if (RealFileExists(OldClearSig) == true)
  1352. Rename(OldClearSig, FinalFile);
  1353. }
  1354. DestFile = FinalFile;
  1355. }
  1356. Complete = true;
  1357. }
  1358. /*}}}*/
  1359. void pkgAcqMetaIndex::AuthDone(string Message) /*{{{*/
  1360. {
  1361. // At this point, the gpgv method has succeeded, so there is a
  1362. // valid signature from a key in the trusted keyring. We
  1363. // perform additional verification of its contents, and use them
  1364. // to verify the indexes we are about to download
  1365. if (!MetaIndexParser->Load(DestFile))
  1366. {
  1367. Status = StatAuthError;
  1368. ErrorText = MetaIndexParser->ErrorText;
  1369. return;
  1370. }
  1371. if (!VerifyVendor(Message))
  1372. {
  1373. return;
  1374. }
  1375. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  1376. std::cerr << "Signature verification succeeded: "
  1377. << DestFile << std::endl;
  1378. // do not trust any previously unverified content that we may have
  1379. string LastGoodSigFile = _config->FindDir("Dir::State::lists").append("partial/").append(URItoFileName(RealURI));
  1380. if (DestFile != SigFile)
  1381. LastGoodSigFile.append(".gpg");
  1382. LastGoodSigFile.append(".reverify");
  1383. if(IMSHit == false && RealFileExists(LastGoodSigFile) == false)
  1384. {
  1385. for (vector <struct IndexTarget*>::const_iterator Target = IndexTargets->begin();
  1386. Target != IndexTargets->end();
  1387. ++Target)
  1388. {
  1389. // remove old indexes
  1390. std::string index = _config->FindDir("Dir::State::lists") +
  1391. URItoFileName((*Target)->URI);
  1392. unlink(index.c_str());
  1393. // and also old gzipindexes
  1394. std::vector<std::string> types = APT::Configuration::getCompressionTypes();
  1395. for (std::vector<std::string>::const_iterator t = types.begin(); t != types.end(); ++t)
  1396. {
  1397. index += '.' + (*t);
  1398. unlink(index.c_str());
  1399. }
  1400. }
  1401. }
  1402. // Download further indexes with verification
  1403. QueueIndexes(true);
  1404. // is it a clearsigned MetaIndex file?
  1405. if (DestFile == SigFile)
  1406. return;
  1407. // Done, move signature file into position
  1408. string VerifiedSigFile = _config->FindDir("Dir::State::lists") +
  1409. URItoFileName(RealURI) + ".gpg";
  1410. Rename(SigFile,VerifiedSigFile);
  1411. chmod(VerifiedSigFile.c_str(),0644);
  1412. }
  1413. /*}}}*/
  1414. void pkgAcqMetaIndex::QueueIndexes(bool verify) /*{{{*/
  1415. {
  1416. #if 0
  1417. /* Reject invalid, existing Release files (LP: #346386) (Closes: #627642)
  1418. * FIXME: Disabled; it breaks unsigned repositories without hashes */
  1419. if (!verify && FileExists(DestFile) && !MetaIndexParser->Load(DestFile))
  1420. {
  1421. Status = StatError;
  1422. ErrorText = MetaIndexParser->ErrorText;
  1423. return;
  1424. }
  1425. #endif
  1426. bool transInRelease = false;
  1427. {
  1428. std::vector<std::string> const keys = MetaIndexParser->MetaKeys();
  1429. for (std::vector<std::string>::const_iterator k = keys.begin(); k != keys.end(); ++k)
  1430. // FIXME: Feels wrong to check for hardcoded string here, but what should we do else…
  1431. if (k->find("Translation-") != std::string::npos)
  1432. {
  1433. transInRelease = true;
  1434. break;
  1435. }
  1436. }
  1437. for (vector <struct IndexTarget*>::const_iterator Target = IndexTargets->begin();
  1438. Target != IndexTargets->end();
  1439. ++Target)
  1440. {
  1441. HashString ExpectedIndexHash;
  1442. const indexRecords::checkSum *Record = MetaIndexParser->Lookup((*Target)->MetaKey);
  1443. bool compressedAvailable = false;
  1444. if (Record == NULL)
  1445. {
  1446. if ((*Target)->IsOptional() == true)
  1447. {
  1448. std::vector<std::string> types = APT::Configuration::getCompressionTypes();
  1449. for (std::vector<std::string>::const_iterator t = types.begin(); t != types.end(); ++t)
  1450. if (MetaIndexParser->Exists((*Target)->MetaKey + "." + *t) == true)
  1451. {
  1452. compressedAvailable = true;
  1453. break;
  1454. }
  1455. }
  1456. else if (verify == true)
  1457. {
  1458. Status = StatAuthError;
  1459. strprintf(ErrorText, _("Unable to find expected entry '%s' in Release file (Wrong sources.list entry or malformed file)"), (*Target)->MetaKey.c_str());
  1460. return;
  1461. }
  1462. }
  1463. else
  1464. {
  1465. ExpectedIndexHash = Record->Hash;
  1466. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  1467. {
  1468. std::cerr << "Queueing: " << (*Target)->URI << std::endl;
  1469. std::cerr << "Expected Hash: " << ExpectedIndexHash.toStr() << std::endl;
  1470. std::cerr << "For: " << Record->MetaKeyFilename << std::endl;
  1471. }
  1472. if (verify == true && ExpectedIndexHash.empty() == true && (*Target)->IsOptional() == false)
  1473. {
  1474. Status = StatAuthError;
  1475. strprintf(ErrorText, _("Unable to find hash sum for '%s' in Release file"), (*Target)->MetaKey.c_str());
  1476. return;
  1477. }
  1478. }
  1479. if ((*Target)->IsOptional() == true)
  1480. {
  1481. if ((*Target)->IsSubIndex() == true)
  1482. new pkgAcqSubIndex(Owner, (*Target)->URI, (*Target)->Description,
  1483. (*Target)->ShortDesc, ExpectedIndexHash);
  1484. else if (transInRelease == false || Record != NULL || compressedAvailable == true)
  1485. {
  1486. if (_config->FindB("Acquire::PDiffs",true) == true && transInRelease == true &&
  1487. MetaIndexParser->Exists((*Target)->MetaKey + ".diff/Index") == true)
  1488. new pkgAcqDiffIndex(Owner, (*Target)->URI, (*Target)->Description,
  1489. (*Target)->ShortDesc, ExpectedIndexHash);
  1490. else
  1491. new pkgAcqIndexTrans(Owner, *Target, ExpectedIndexHash, MetaIndexParser);
  1492. }
  1493. continue;
  1494. }
  1495. /* Queue Packages file (either diff or full packages files, depending
  1496. on the users option) - we also check if the PDiff Index file is listed
  1497. in the Meta-Index file. Ideal would be if pkgAcqDiffIndex would test this
  1498. instead, but passing the required info to it is to much hassle */
  1499. if(_config->FindB("Acquire::PDiffs",true) == true && (verify == false ||
  1500. MetaIndexParser->Exists((*Target)->MetaKey + ".diff/Index") == true))
  1501. new pkgAcqDiffIndex(Owner, (*Target)->URI, (*Target)->Description,
  1502. (*Target)->ShortDesc, ExpectedIndexHash);
  1503. else
  1504. new pkgAcqIndex(Owner, *Target, ExpectedIndexHash, MetaIndexParser);
  1505. }
  1506. }
  1507. /*}}}*/
  1508. bool pkgAcqMetaIndex::VerifyVendor(string Message) /*{{{*/
  1509. {
  1510. string::size_type pos;
  1511. // check for missing sigs (that where not fatal because otherwise we had
  1512. // bombed earlier)
  1513. string missingkeys;
  1514. string msg = _("There is no public key available for the "
  1515. "following key IDs:\n");
  1516. pos = Message.find("NO_PUBKEY ");
  1517. if (pos != std::string::npos)
  1518. {
  1519. string::size_type start = pos+strlen("NO_PUBKEY ");
  1520. string Fingerprint = Message.substr(start, Message.find("\n")-start);
  1521. missingkeys += (Fingerprint);
  1522. }
  1523. if(!missingkeys.empty())
  1524. _error->Warning("%s", (msg + missingkeys).c_str());
  1525. string Transformed = MetaIndexParser->GetExpectedDist();
  1526. if (Transformed == "../project/experimental")
  1527. {
  1528. Transformed = "experimental";
  1529. }
  1530. pos = Transformed.rfind('/');
  1531. if (pos != string::npos)
  1532. {
  1533. Transformed = Transformed.substr(0, pos);
  1534. }
  1535. if (Transformed == ".")
  1536. {
  1537. Transformed = "";
  1538. }
  1539. if (_config->FindB("Acquire::Check-Valid-Until", true) == true &&
  1540. MetaIndexParser->GetValidUntil() > 0) {
  1541. time_t const invalid_since = time(NULL) - MetaIndexParser->GetValidUntil();
  1542. if (invalid_since > 0)
  1543. // TRANSLATOR: The first %s is the URL of the bad Release file, the second is
  1544. // the time since then the file is invalid - formated in the same way as in
  1545. // the download progress display (e.g. 7d 3h 42min 1s)
  1546. return _error->Error(
  1547. _("Release file for %s is expired (invalid since %s). "
  1548. "Updates for this repository will not be applied."),
  1549. RealURI.c_str(), TimeToStr(invalid_since).c_str());
  1550. }
  1551. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  1552. {
  1553. std::cerr << "Got Codename: " << MetaIndexParser->GetDist() << std::endl;
  1554. std::cerr << "Expecting Dist: " << MetaIndexParser->GetExpectedDist() << std::endl;
  1555. std::cerr << "Transformed Dist: " << Transformed << std::endl;
  1556. }
  1557. if (MetaIndexParser->CheckDist(Transformed) == false)
  1558. {
  1559. // This might become fatal one day
  1560. // Status = StatAuthError;
  1561. // ErrorText = "Conflicting distribution; expected "
  1562. // + MetaIndexParser->GetExpectedDist() + " but got "
  1563. // + MetaIndexParser->GetDist();
  1564. // return false;
  1565. if (!Transformed.empty())
  1566. {
  1567. _error->Warning(_("Conflicting distribution: %s (expected %s but got %s)"),
  1568. Desc.Description.c_str(),
  1569. Transformed.c_str(),
  1570. MetaIndexParser->GetDist().c_str());
  1571. }
  1572. }
  1573. return true;
  1574. }
  1575. /*}}}*/
  1576. // pkgAcqMetaIndex::Failed - no Release file present or no signature file present /*{{{*/
  1577. // ---------------------------------------------------------------------
  1578. /* */
  1579. void pkgAcqMetaIndex::Failed(string Message,pkgAcquire::MethodConfig * /*Cnf*/)
  1580. {
  1581. if (AuthPass == true)
  1582. {
  1583. // gpgv method failed, if we have a good signature
  1584. string LastGoodSigFile = _config->FindDir("Dir::State::lists").append("partial/").append(URItoFileName(RealURI));
  1585. if (DestFile != SigFile)
  1586. LastGoodSigFile.append(".gpg");
  1587. LastGoodSigFile.append(".reverify");
  1588. if(FileExists(LastGoodSigFile))
  1589. {
  1590. string VerifiedSigFile = _config->FindDir("Dir::State::lists") + URItoFileName(RealURI);
  1591. if (DestFile != SigFile)
  1592. VerifiedSigFile.append(".gpg");
  1593. Rename(LastGoodSigFile, VerifiedSigFile);
  1594. Status = StatTransientNetworkError;
  1595. _error->Warning(_("An error occurred during the signature "
  1596. "verification. The repository is not updated "
  1597. "and the previous index files will be used. "
  1598. "GPG error: %s: %s\n"),
  1599. Desc.Description.c_str(),
  1600. LookupTag(Message,"Message").c_str());
  1601. RunScripts("APT::Update::Auth-Failure");
  1602. return;
  1603. } else if (LookupTag(Message,"Message").find("NODATA") != string::npos) {
  1604. /* Invalid signature file, reject (LP: #346386) (Closes: #627642) */
  1605. _error->Error(_("GPG error: %s: %s"),
  1606. Desc.Description.c_str(),
  1607. LookupTag(Message,"Message").c_str());
  1608. return;
  1609. } else {
  1610. _error->Warning(_("GPG error: %s: %s"),
  1611. Desc.Description.c_str(),
  1612. LookupTag(Message,"Message").c_str());
  1613. }
  1614. // gpgv method failed
  1615. ReportMirrorFailure("GPGFailure");
  1616. }
  1617. /* Always move the meta index, even if gpgv failed. This ensures
  1618. * that PackageFile objects are correctly filled in */
  1619. if (FileExists(DestFile)) {
  1620. string FinalFile = _config->FindDir("Dir::State::lists");
  1621. FinalFile += URItoFileName(RealURI);
  1622. /* InRelease files become Release files, otherwise
  1623. * they would be considered as trusted later on */
  1624. if (SigFile == DestFile) {
  1625. RealURI = RealURI.replace(RealURI.rfind("InRelease"), 9,
  1626. "Release");
  1627. FinalFile = FinalFile.replace(FinalFile.rfind("InRelease"), 9,
  1628. "Release");
  1629. SigFile = FinalFile;
  1630. }
  1631. Rename(DestFile,FinalFile);
  1632. chmod(FinalFile.c_str(),0644);
  1633. DestFile = FinalFile;
  1634. }
  1635. // No Release file was present, or verification failed, so fall
  1636. // back to queueing Packages files without verification
  1637. QueueIndexes(false);
  1638. }
  1639. /*}}}*/
  1640. pkgAcqMetaClearSig::pkgAcqMetaClearSig(pkgAcquire *Owner, /*{{{*/
  1641. string const &URI, string const &URIDesc, string const &ShortDesc,
  1642. string const &MetaIndexURI, string const &MetaIndexURIDesc, string const &MetaIndexShortDesc,
  1643. string const &MetaSigURI, string const &MetaSigURIDesc, string const &MetaSigShortDesc,
  1644. const vector<struct IndexTarget*>* IndexTargets,
  1645. indexRecords* MetaIndexParser) :
  1646. pkgAcqMetaIndex(Owner, URI, URIDesc, ShortDesc, "", IndexTargets, MetaIndexParser),
  1647. MetaIndexURI(MetaIndexURI), MetaIndexURIDesc(MetaIndexURIDesc), MetaIndexShortDesc(MetaIndexShortDesc),
  1648. MetaSigURI(MetaSigURI), MetaSigURIDesc(MetaSigURIDesc), MetaSigShortDesc(MetaSigShortDesc)
  1649. {
  1650. SigFile = DestFile;
  1651. // keep the old InRelease around in case of transistent network errors
  1652. string const Final = _config->FindDir("Dir::State::lists") + URItoFileName(RealURI);
  1653. if (RealFileExists(Final) == true)
  1654. {
  1655. string const LastGoodSig = DestFile + ".reverify";
  1656. Rename(Final,LastGoodSig);
  1657. }
  1658. }
  1659. /*}}}*/
  1660. pkgAcqMetaClearSig::~pkgAcqMetaClearSig() /*{{{*/
  1661. {
  1662. // if the file was never queued undo file-changes done in the constructor
  1663. if (QueueCounter == 1 && Status == StatIdle && FileSize == 0 && Complete == false)
  1664. {
  1665. string const Final = _config->FindDir("Dir::State::lists") + URItoFileName(RealURI);
  1666. string const LastGoodSig = DestFile + ".reverify";
  1667. if (RealFileExists(Final) == false && RealFileExists(LastGoodSig) == true)
  1668. Rename(LastGoodSig, Final);
  1669. }
  1670. }
  1671. /*}}}*/
  1672. // pkgAcqMetaClearSig::Custom600Headers - Insert custom request headers /*{{{*/
  1673. // ---------------------------------------------------------------------
  1674. // FIXME: this can go away once the InRelease file is used widely
  1675. string pkgAcqMetaClearSig::Custom600Headers()
  1676. {
  1677. string Final = _config->FindDir("Dir::State::lists");
  1678. Final += URItoFileName(RealURI);
  1679. struct stat Buf;
  1680. if (stat(Final.c_str(),&Buf) != 0)
  1681. {
  1682. Final = DestFile + ".reverify";
  1683. if (stat(Final.c_str(),&Buf) != 0)
  1684. return "\nIndex-File: true\nFail-Ignore: true\n";
  1685. }
  1686. return "\nIndex-File: true\nFail-Ignore: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
  1687. }
  1688. /*}}}*/
  1689. void pkgAcqMetaClearSig::Failed(string Message,pkgAcquire::MethodConfig *Cnf) /*{{{*/
  1690. {
  1691. if (AuthPass == false)
  1692. {
  1693. // Remove the 'old' InRelease file if we try Release.gpg now as otherwise
  1694. // the file will stay around and gives a false-auth impression (CVE-2012-0214)
  1695. string FinalFile = _config->FindDir("Dir::State::lists");
  1696. FinalFile.append(URItoFileName(RealURI));
  1697. if (FileExists(FinalFile))
  1698. unlink(FinalFile.c_str());
  1699. new pkgAcqMetaSig(Owner,
  1700. MetaSigURI, MetaSigURIDesc, MetaSigShortDesc,
  1701. MetaIndexURI, MetaIndexURIDesc, MetaIndexShortDesc,
  1702. IndexTargets, MetaIndexParser);
  1703. if (Cnf->LocalOnly == true ||
  1704. StringToBool(LookupTag(Message, "Transient-Failure"), false) == false)
  1705. Dequeue();
  1706. }
  1707. else
  1708. pkgAcqMetaIndex::Failed(Message, Cnf);
  1709. }
  1710. /*}}}*/
  1711. // AcqArchive::AcqArchive - Constructor /*{{{*/
  1712. // ---------------------------------------------------------------------
  1713. /* This just sets up the initial fetch environment and queues the first
  1714. possibilitiy */
  1715. pkgAcqArchive::pkgAcqArchive(pkgAcquire *Owner,pkgSourceList *Sources,
  1716. pkgRecords *Recs,pkgCache::VerIterator const &Version,
  1717. string &StoreFilename) :
  1718. Item(Owner), Version(Version), Sources(Sources), Recs(Recs),
  1719. StoreFilename(StoreFilename), Vf(Version.FileList()),
  1720. Trusted(false)
  1721. {
  1722. Retries = _config->FindI("Acquire::Retries",0);
  1723. if (Version.Arch() == 0)
  1724. {
  1725. _error->Error(_("I wasn't able to locate a file for the %s package. "
  1726. "This might mean you need to manually fix this package. "
  1727. "(due to missing arch)"),
  1728. Version.ParentPkg().FullName().c_str());
  1729. return;
  1730. }
  1731. /* We need to find a filename to determine the extension. We make the
  1732. assumption here that all the available sources for this version share
  1733. the same extension.. */
  1734. // Skip not source sources, they do not have file fields.
  1735. for (; Vf.end() == false; ++Vf)
  1736. {
  1737. if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0)
  1738. continue;
  1739. break;
  1740. }
  1741. // Does not really matter here.. we are going to fail out below
  1742. if (Vf.end() != true)
  1743. {
  1744. // If this fails to get a file name we will bomb out below.
  1745. pkgRecords::Parser &Parse = Recs->Lookup(Vf);
  1746. if (_error->PendingError() == true)
  1747. return;
  1748. // Generate the final file name as: package_version_arch.foo
  1749. StoreFilename = QuoteString(Version.ParentPkg().Name(),"_:") + '_' +
  1750. QuoteString(Version.VerStr(),"_:") + '_' +
  1751. QuoteString(Version.Arch(),"_:.") +
  1752. "." + flExtension(Parse.FileName());
  1753. }
  1754. // check if we have one trusted source for the package. if so, switch
  1755. // to "TrustedOnly" mode - but only if not in AllowUnauthenticated mode
  1756. bool const allowUnauth = _config->FindB("APT::Get::AllowUnauthenticated", false);
  1757. bool const debugAuth = _config->FindB("Debug::pkgAcquire::Auth", false);
  1758. bool seenUntrusted = false;
  1759. for (pkgCache::VerFileIterator i = Version.FileList(); i.end() == false; ++i)
  1760. {
  1761. pkgIndexFile *Index;
  1762. if (Sources->FindIndex(i.File(),Index) == false)
  1763. continue;
  1764. if (debugAuth == true)
  1765. std::cerr << "Checking index: " << Index->Describe()
  1766. << "(Trusted=" << Index->IsTrusted() << ")" << std::endl;
  1767. if (Index->IsTrusted() == true)
  1768. {
  1769. Trusted = true;
  1770. if (allowUnauth == false)
  1771. break;
  1772. }
  1773. else
  1774. seenUntrusted = true;
  1775. }
  1776. // "allow-unauthenticated" restores apts old fetching behaviour
  1777. // that means that e.g. unauthenticated file:// uris are higher
  1778. // priority than authenticated http:// uris
  1779. if (allowUnauth == true && seenUntrusted == true)
  1780. Trusted = false;
  1781. // Select a source
  1782. if (QueueNext() == false && _error->PendingError() == false)
  1783. _error->Error(_("Can't find a source to download version '%s' of '%s'"),
  1784. Version.VerStr(), Version.ParentPkg().FullName(false).c_str());
  1785. }
  1786. /*}}}*/
  1787. // AcqArchive::QueueNext - Queue the next file source /*{{{*/
  1788. // ---------------------------------------------------------------------
  1789. /* This queues the next available file version for download. It checks if
  1790. the archive is already available in the cache and stashs the MD5 for
  1791. checking later. */
  1792. bool pkgAcqArchive::QueueNext()
  1793. {
  1794. string const ForceHash = _config->Find("Acquire::ForceHash");
  1795. for (; Vf.end() == false; ++Vf)
  1796. {
  1797. // Ignore not source sources
  1798. if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0)
  1799. continue;
  1800. // Try to cross match against the source list
  1801. pkgIndexFile *Index;
  1802. if (Sources->FindIndex(Vf.File(),Index) == false)
  1803. continue;
  1804. // only try to get a trusted package from another source if that source
  1805. // is also trusted
  1806. if(Trusted && !Index->IsTrusted())
  1807. continue;
  1808. // Grab the text package record
  1809. pkgRecords::Parser &Parse = Recs->Lookup(Vf);
  1810. if (_error->PendingError() == true)
  1811. return false;
  1812. string PkgFile = Parse.FileName();
  1813. if (ForceHash.empty() == false)
  1814. {
  1815. if(stringcasecmp(ForceHash, "sha512") == 0)
  1816. ExpectedHash = HashString("SHA512", Parse.SHA512Hash());
  1817. else if(stringcasecmp(ForceHash, "sha256") == 0)
  1818. ExpectedHash = HashString("SHA256", Parse.SHA256Hash());
  1819. else if (stringcasecmp(ForceHash, "sha1") == 0)
  1820. ExpectedHash = HashString("SHA1", Parse.SHA1Hash());
  1821. else
  1822. ExpectedHash = HashString("MD5Sum", Parse.MD5Hash());
  1823. }
  1824. else
  1825. {
  1826. string Hash;
  1827. if ((Hash = Parse.SHA512Hash()).empty() == false)
  1828. ExpectedHash = HashString("SHA512", Hash);
  1829. else if ((Hash = Parse.SHA256Hash()).empty() == false)
  1830. ExpectedHash = HashString("SHA256", Hash);
  1831. else if ((Hash = Parse.SHA1Hash()).empty() == false)
  1832. ExpectedHash = HashString("SHA1", Hash);
  1833. else
  1834. ExpectedHash = HashString("MD5Sum", Parse.MD5Hash());
  1835. }
  1836. if (PkgFile.empty() == true)
  1837. return _error->Error(_("The package index files are corrupted. No Filename: "
  1838. "field for package %s."),
  1839. Version.ParentPkg().Name());
  1840. Desc.URI = Index->ArchiveURI(PkgFile);
  1841. Desc.Description = Index->ArchiveInfo(Version);
  1842. Desc.Owner = this;
  1843. Desc.ShortDesc = Version.ParentPkg().FullName(true);
  1844. // See if we already have the file. (Legacy filenames)
  1845. FileSize = Version->Size;
  1846. string FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile);
  1847. struct stat Buf;
  1848. if (stat(FinalFile.c_str(),&Buf) == 0)
  1849. {
  1850. // Make sure the size matches
  1851. if ((unsigned long long)Buf.st_size == Version->Size)
  1852. {
  1853. Complete = true;
  1854. Local = true;
  1855. Status = StatDone;
  1856. StoreFilename = DestFile = FinalFile;
  1857. return true;
  1858. }
  1859. /* Hmm, we have a file and its size does not match, this means it is
  1860. an old style mismatched arch */
  1861. unlink(FinalFile.c_str());
  1862. }
  1863. // Check it again using the new style output filenames
  1864. FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename);
  1865. if (stat(FinalFile.c_str(),&Buf) == 0)
  1866. {
  1867. // Make sure the size matches
  1868. if ((unsigned long long)Buf.st_size == Version->Size)
  1869. {
  1870. Complete = true;
  1871. Local = true;
  1872. Status = StatDone;
  1873. StoreFilename = DestFile = FinalFile;
  1874. return true;
  1875. }
  1876. /* Hmm, we have a file and its size does not match, this shouldn't
  1877. happen.. */
  1878. unlink(FinalFile.c_str());
  1879. }
  1880. DestFile = _config->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename);
  1881. // Check the destination file
  1882. if (stat(DestFile.c_str(),&Buf) == 0)
  1883. {
  1884. // Hmm, the partial file is too big, erase it
  1885. if ((unsigned long long)Buf.st_size > Version->Size)
  1886. unlink(DestFile.c_str());
  1887. else
  1888. PartialSize = Buf.st_size;
  1889. }
  1890. // Disables download of archives - useful if no real installation follows,
  1891. // e.g. if we are just interested in proposed installation order
  1892. if (_config->FindB("Debug::pkgAcqArchive::NoQueue", false) == true)
  1893. {
  1894. Complete = true;
  1895. Local = true;
  1896. Status = StatDone;
  1897. StoreFilename = DestFile = FinalFile;
  1898. return true;
  1899. }
  1900. // Create the item
  1901. Local = false;
  1902. QueueURI(Desc);
  1903. ++Vf;
  1904. return true;
  1905. }
  1906. return false;
  1907. }
  1908. /*}}}*/
  1909. // AcqArchive::Done - Finished fetching /*{{{*/
  1910. // ---------------------------------------------------------------------
  1911. /* */
  1912. void pkgAcqArchive::Done(string Message,unsigned long long Size,string CalcHash,
  1913. pkgAcquire::MethodConfig *Cfg)
  1914. {
  1915. Item::Done(Message,Size,CalcHash,Cfg);
  1916. // Check the size
  1917. if (Size != Version->Size)
  1918. {
  1919. RenameOnError(SizeMismatch);
  1920. return;
  1921. }
  1922. // Check the hash
  1923. if(ExpectedHash.toStr() != CalcHash)
  1924. {
  1925. RenameOnError(HashSumMismatch);
  1926. return;
  1927. }
  1928. // Grab the output filename
  1929. string FileName = LookupTag(Message,"Filename");
  1930. if (FileName.empty() == true)
  1931. {
  1932. Status = StatError;
  1933. ErrorText = "Method gave a blank filename";
  1934. return;
  1935. }
  1936. Complete = true;
  1937. // Reference filename
  1938. if (FileName != DestFile)
  1939. {
  1940. StoreFilename = DestFile = FileName;
  1941. Local = true;
  1942. return;
  1943. }
  1944. // Done, move it into position
  1945. string FinalFile = _config->FindDir("Dir::Cache::Archives");
  1946. FinalFile += flNotDir(StoreFilename);
  1947. Rename(DestFile,FinalFile);
  1948. StoreFilename = DestFile = FinalFile;
  1949. Complete = true;
  1950. }
  1951. /*}}}*/
  1952. // AcqArchive::Failed - Failure handler /*{{{*/
  1953. // ---------------------------------------------------------------------
  1954. /* Here we try other sources */
  1955. void pkgAcqArchive::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  1956. {
  1957. ErrorText = LookupTag(Message,"Message");
  1958. /* We don't really want to retry on failed media swaps, this prevents
  1959. that. An interesting observation is that permanent failures are not
  1960. recorded. */
  1961. if (Cnf->Removable == true &&
  1962. StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
  1963. {
  1964. // Vf = Version.FileList();
  1965. while (Vf.end() == false) ++Vf;
  1966. StoreFilename = string();
  1967. Item::Failed(Message,Cnf);
  1968. return;
  1969. }
  1970. if (QueueNext() == false)
  1971. {
  1972. // This is the retry counter
  1973. if (Retries != 0 &&
  1974. Cnf->LocalOnly == false &&
  1975. StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
  1976. {
  1977. Retries--;
  1978. Vf = Version.FileList();
  1979. if (QueueNext() == true)
  1980. return;
  1981. }
  1982. StoreFilename = string();
  1983. Item::Failed(Message,Cnf);
  1984. }
  1985. }
  1986. /*}}}*/
  1987. // AcqArchive::IsTrusted - Determine whether this archive comes from a trusted source /*{{{*/
  1988. // ---------------------------------------------------------------------
  1989. APT_PURE bool pkgAcqArchive::IsTrusted()
  1990. {
  1991. return Trusted;
  1992. }
  1993. /*}}}*/
  1994. // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
  1995. // ---------------------------------------------------------------------
  1996. /* */
  1997. void pkgAcqArchive::Finished()
  1998. {
  1999. if (Status == pkgAcquire::Item::StatDone &&
  2000. Complete == true)
  2001. return;
  2002. StoreFilename = string();
  2003. }
  2004. /*}}}*/
  2005. // AcqFile::pkgAcqFile - Constructor /*{{{*/
  2006. // ---------------------------------------------------------------------
  2007. /* The file is added to the queue */
  2008. pkgAcqFile::pkgAcqFile(pkgAcquire *Owner,string URI,string Hash,
  2009. unsigned long long Size,string Dsc,string ShortDesc,
  2010. const string &DestDir, const string &DestFilename,
  2011. bool IsIndexFile) :
  2012. Item(Owner), ExpectedHash(Hash), IsIndexFile(IsIndexFile)
  2013. {
  2014. Retries = _config->FindI("Acquire::Retries",0);
  2015. if(!DestFilename.empty())
  2016. DestFile = DestFilename;
  2017. else if(!DestDir.empty())
  2018. DestFile = DestDir + "/" + flNotDir(URI);
  2019. else
  2020. DestFile = flNotDir(URI);
  2021. // Create the item
  2022. Desc.URI = URI;
  2023. Desc.Description = Dsc;
  2024. Desc.Owner = this;
  2025. // Set the short description to the archive component
  2026. Desc.ShortDesc = ShortDesc;
  2027. // Get the transfer sizes
  2028. FileSize = Size;
  2029. struct stat Buf;
  2030. if (stat(DestFile.c_str(),&Buf) == 0)
  2031. {
  2032. // Hmm, the partial file is too big, erase it
  2033. if ((Size > 0) && (unsigned long long)Buf.st_size > Size)
  2034. unlink(DestFile.c_str());
  2035. else
  2036. PartialSize = Buf.st_size;
  2037. }
  2038. QueueURI(Desc);
  2039. }
  2040. /*}}}*/
  2041. // AcqFile::Done - Item downloaded OK /*{{{*/
  2042. // ---------------------------------------------------------------------
  2043. /* */
  2044. void pkgAcqFile::Done(string Message,unsigned long long Size,string CalcHash,
  2045. pkgAcquire::MethodConfig *Cnf)
  2046. {
  2047. Item::Done(Message,Size,CalcHash,Cnf);
  2048. // Check the hash
  2049. if(!ExpectedHash.empty() && ExpectedHash.toStr() != CalcHash)
  2050. {
  2051. RenameOnError(HashSumMismatch);
  2052. return;
  2053. }
  2054. string FileName = LookupTag(Message,"Filename");
  2055. if (FileName.empty() == true)
  2056. {
  2057. Status = StatError;
  2058. ErrorText = "Method gave a blank filename";
  2059. return;
  2060. }
  2061. Complete = true;
  2062. // The files timestamp matches
  2063. if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
  2064. return;
  2065. // We have to copy it into place
  2066. if (FileName != DestFile)
  2067. {
  2068. Local = true;
  2069. if (_config->FindB("Acquire::Source-Symlinks",true) == false ||
  2070. Cnf->Removable == true)
  2071. {
  2072. Desc.URI = "copy:" + FileName;
  2073. QueueURI(Desc);
  2074. return;
  2075. }
  2076. // Erase the file if it is a symlink so we can overwrite it
  2077. struct stat St;
  2078. if (lstat(DestFile.c_str(),&St) == 0)
  2079. {
  2080. if (S_ISLNK(St.st_mode) != 0)
  2081. unlink(DestFile.c_str());
  2082. }
  2083. // Symlink the file
  2084. if (symlink(FileName.c_str(),DestFile.c_str()) != 0)
  2085. {
  2086. ErrorText = "Link to " + DestFile + " failure ";
  2087. Status = StatError;
  2088. Complete = false;
  2089. }
  2090. }
  2091. }
  2092. /*}}}*/
  2093. // AcqFile::Failed - Failure handler /*{{{*/
  2094. // ---------------------------------------------------------------------
  2095. /* Here we try other sources */
  2096. void pkgAcqFile::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  2097. {
  2098. ErrorText = LookupTag(Message,"Message");
  2099. // This is the retry counter
  2100. if (Retries != 0 &&
  2101. Cnf->LocalOnly == false &&
  2102. StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
  2103. {
  2104. Retries--;
  2105. QueueURI(Desc);
  2106. return;
  2107. }
  2108. Item::Failed(Message,Cnf);
  2109. }
  2110. /*}}}*/
  2111. // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
  2112. // ---------------------------------------------------------------------
  2113. /* The only header we use is the last-modified header. */
  2114. string pkgAcqFile::Custom600Headers()
  2115. {
  2116. if (IsIndexFile)
  2117. return "\nIndex-File: true";
  2118. return "";
  2119. }
  2120. /*}}}*/