acquire-item.cc 50 KB

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