acquire-item.cc 45 KB

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