acquire-item.cc 75 KB

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