acquire-item.cc 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543
  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. if (Complete == false && FileName == DestFile)
  99. {
  100. if (Owner->Log != 0)
  101. Owner->Log->Fetched(Size,atoi(LookupTag(Message,"Resume-Point","0").c_str()));
  102. }
  103. if (FileSize == 0)
  104. FileSize= Size;
  105. Status = StatDone;
  106. ErrorText = string();
  107. Owner->Dequeue(this);
  108. }
  109. /*}}}*/
  110. // Acquire::Item::Rename - Rename a file /*{{{*/
  111. // ---------------------------------------------------------------------
  112. /* This helper function is used by alot of item methods as thier final
  113. step */
  114. void pkgAcquire::Item::Rename(string From,string To)
  115. {
  116. if (rename(From.c_str(),To.c_str()) != 0)
  117. {
  118. char S[300];
  119. snprintf(S,sizeof(S),_("rename failed, %s (%s -> %s)."),strerror(errno),
  120. From.c_str(),To.c_str());
  121. Status = StatError;
  122. ErrorText = S;
  123. }
  124. }
  125. /*}}}*/
  126. // AcqDiffIndex::AcqDiffIndex - Constructor
  127. // ---------------------------------------------------------------------
  128. /* Get the DiffIndex file first and see if there are patches availabe
  129. * If so, create a pkgAcqIndexDiffs fetcher that will get and apply the
  130. * patches. If anything goes wrong in that process, it will fall back to
  131. * the original packages file
  132. */
  133. pkgAcqDiffIndex::pkgAcqDiffIndex(pkgAcquire *Owner,
  134. string URI,string URIDesc,string ShortDesc,
  135. string ExpectedMD5)
  136. : Item(Owner), RealURI(URI), ExpectedMD5(ExpectedMD5), Description(URIDesc)
  137. {
  138. Debug = _config->FindB("Debug::pkgAcquire::Diffs",false);
  139. Desc.Description = URIDesc + "/DiffIndex";
  140. Desc.Owner = this;
  141. Desc.ShortDesc = ShortDesc;
  142. Desc.URI = URI + ".diff/Index";
  143. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  144. DestFile += URItoFileName(URI) + string(".DiffIndex");
  145. if(Debug)
  146. std::clog << "pkgAcqDiffIndex: " << Desc.URI << std::endl;
  147. // look for the current package file
  148. CurrentPackagesFile = _config->FindDir("Dir::State::lists");
  149. CurrentPackagesFile += URItoFileName(RealURI);
  150. // FIXME: this file:/ check is a hack to prevent fetching
  151. // from local sources. this is really silly, and
  152. // should be fixed cleanly as soon as possible
  153. if(!FileExists(CurrentPackagesFile) ||
  154. Desc.URI.substr(0,strlen("file:/")) == "file:/" ||
  155. !_config->FindB("Acquire::Diffs",true))
  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. Desc.URI = "gzip:" + FileName;
  424. DestFile += ".decomp";
  425. QueueURI(Desc);
  426. Mode = "gzip";
  427. return;
  428. }
  429. // sucess in downloading a diff, enter ApplyDiff state
  430. if(State == StateUnzipDiff)
  431. {
  432. // rred excepts the patch as $FinalFile.ed
  433. Rename(DestFile,FinalFile+".ed");
  434. if(Debug)
  435. std::clog << "Sending to rred method: " << FinalFile << std::endl;
  436. State = StateApplyDiff;
  437. Desc.URI = "rred:" + FinalFile;
  438. QueueURI(Desc);
  439. Mode = "rred";
  440. return;
  441. }
  442. // success in download/apply a diff, queue next (if needed)
  443. if(State == StateApplyDiff)
  444. {
  445. // remove the just applied patch
  446. available_patches.erase(available_patches.begin());
  447. // move into place
  448. if(Debug)
  449. {
  450. std::clog << "Moving patched file in place: " << std::endl
  451. << DestFile << " -> " << FinalFile << std::endl;
  452. }
  453. Rename(DestFile,FinalFile);
  454. // see if there is more to download
  455. if(available_patches.size() > 0) {
  456. new pkgAcqIndexDiffs(Owner, RealURI, Description, Desc.ShortDesc,
  457. ExpectedMD5, available_patches);
  458. return Finish();
  459. } else
  460. return Finish(true);
  461. }
  462. }
  463. // AcqIndex::AcqIndex - Constructor /*{{{*/
  464. // ---------------------------------------------------------------------
  465. /* The package file is added to the queue and a second class is
  466. instantiated to fetch the revision file */
  467. pkgAcqIndex::pkgAcqIndex(pkgAcquire *Owner,
  468. string URI,string URIDesc,string ShortDesc,
  469. string ExpectedMD5, string comprExt)
  470. : Item(Owner), RealURI(URI), ExpectedMD5(ExpectedMD5)
  471. {
  472. Decompression = false;
  473. Erase = false;
  474. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  475. DestFile += URItoFileName(URI);
  476. if(comprExt.empty())
  477. {
  478. // autoselect the compression method
  479. if(FileExists("/usr/bin/bzip2"))
  480. CompressionExtension = ".bz2";
  481. else
  482. CompressionExtension = ".gz";
  483. } else {
  484. CompressionExtension = comprExt;
  485. }
  486. Desc.URI = URI + CompressionExtension;
  487. Desc.Description = URIDesc;
  488. Desc.Owner = this;
  489. Desc.ShortDesc = ShortDesc;
  490. QueueURI(Desc);
  491. }
  492. /*}}}*/
  493. // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
  494. // ---------------------------------------------------------------------
  495. /* The only header we use is the last-modified header. */
  496. string pkgAcqIndex::Custom600Headers()
  497. {
  498. string Final = _config->FindDir("Dir::State::lists");
  499. Final += URItoFileName(RealURI);
  500. struct stat Buf;
  501. if (stat(Final.c_str(),&Buf) != 0)
  502. return "\nIndex-File: true";
  503. return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
  504. }
  505. /*}}}*/
  506. void pkgAcqIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  507. {
  508. // no .bz2 found, retry with .gz
  509. if(Desc.URI.substr(Desc.URI.size()-3,Desc.URI.size()-1) == "bz2") {
  510. Desc.URI = Desc.URI.substr(0,Desc.URI.size()-3) + "gz";
  511. // retry with a gzip one
  512. new pkgAcqIndex(Owner, RealURI, Desc.Description,Desc.ShortDesc,
  513. ExpectedMD5, string(".gz"));
  514. Status = StatDone;
  515. Complete = false;
  516. Dequeue();
  517. return;
  518. }
  519. Item::Failed(Message,Cnf);
  520. }
  521. // AcqIndex::Done - Finished a fetch /*{{{*/
  522. // ---------------------------------------------------------------------
  523. /* This goes through a number of states.. On the initial fetch the
  524. method could possibly return an alternate filename which points
  525. to the uncompressed version of the file. If this is so the file
  526. is copied into the partial directory. In all other cases the file
  527. is decompressed with a gzip uri. */
  528. void pkgAcqIndex::Done(string Message,unsigned long Size,string MD5,
  529. pkgAcquire::MethodConfig *Cfg)
  530. {
  531. Item::Done(Message,Size,MD5,Cfg);
  532. if (Decompression == true)
  533. {
  534. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  535. {
  536. std::cerr << std::endl << RealURI << ": Computed MD5: " << MD5;
  537. std::cerr << " Expected MD5: " << ExpectedMD5 << std::endl;
  538. }
  539. if (MD5.empty())
  540. {
  541. MD5Summation sum;
  542. FileFd Fd(DestFile, FileFd::ReadOnly);
  543. sum.AddFD(Fd.Fd(), Fd.Size());
  544. Fd.Close();
  545. MD5 = (string)sum.Result();
  546. }
  547. if (!ExpectedMD5.empty() && MD5 != ExpectedMD5)
  548. {
  549. Status = StatAuthError;
  550. ErrorText = _("MD5Sum mismatch");
  551. Rename(DestFile,DestFile + ".FAILED");
  552. return;
  553. }
  554. // Done, move it into position
  555. string FinalFile = _config->FindDir("Dir::State::lists");
  556. FinalFile += URItoFileName(RealURI);
  557. Rename(DestFile,FinalFile);
  558. chmod(FinalFile.c_str(),0644);
  559. /* We restore the original name to DestFile so that the clean operation
  560. will work OK */
  561. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  562. DestFile += URItoFileName(RealURI);
  563. // Remove the compressed version.
  564. if (Erase == true)
  565. unlink(DestFile.c_str());
  566. return;
  567. }
  568. Erase = false;
  569. Complete = true;
  570. // Handle the unzipd case
  571. string FileName = LookupTag(Message,"Alt-Filename");
  572. if (FileName.empty() == false)
  573. {
  574. // The files timestamp matches
  575. if (StringToBool(LookupTag(Message,"Alt-IMS-Hit"),false) == true)
  576. return;
  577. Decompression = true;
  578. Local = true;
  579. DestFile += ".decomp";
  580. Desc.URI = "copy:" + FileName;
  581. QueueURI(Desc);
  582. Mode = "copy";
  583. return;
  584. }
  585. FileName = LookupTag(Message,"Filename");
  586. if (FileName.empty() == true)
  587. {
  588. Status = StatError;
  589. ErrorText = "Method gave a blank filename";
  590. }
  591. // The files timestamp matches
  592. if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
  593. return;
  594. if (FileName == DestFile)
  595. Erase = true;
  596. else
  597. Local = true;
  598. string compExt = Desc.URI.substr(Desc.URI.size()-3,Desc.URI.size()-1);
  599. char *decompProg;
  600. if(compExt == "bz2")
  601. decompProg = "bzip2";
  602. else if(compExt == ".gz")
  603. decompProg = "gzip";
  604. else {
  605. _error->Error("Unsupported extension: %s", compExt.c_str());
  606. return;
  607. }
  608. Decompression = true;
  609. DestFile += ".decomp";
  610. Desc.URI = string(decompProg) + ":" + FileName;
  611. QueueURI(Desc);
  612. Mode = decompProg;
  613. }
  614. pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire *Owner,
  615. string URI,string URIDesc,string ShortDesc,
  616. string MetaIndexURI, string MetaIndexURIDesc,
  617. string MetaIndexShortDesc,
  618. const vector<IndexTarget*>* IndexTargets,
  619. indexRecords* MetaIndexParser) :
  620. Item(Owner), RealURI(URI), MetaIndexURI(MetaIndexURI),
  621. MetaIndexURIDesc(MetaIndexURIDesc), MetaIndexShortDesc(MetaIndexShortDesc)
  622. {
  623. this->MetaIndexParser = MetaIndexParser;
  624. this->IndexTargets = IndexTargets;
  625. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  626. DestFile += URItoFileName(URI);
  627. // remove any partial downloaded sig-file. it may confuse proxies
  628. // and is too small to warrant a partial download anyway
  629. unlink(DestFile.c_str());
  630. // Create the item
  631. Desc.Description = URIDesc;
  632. Desc.Owner = this;
  633. Desc.ShortDesc = ShortDesc;
  634. Desc.URI = URI;
  635. string Final = _config->FindDir("Dir::State::lists");
  636. Final += URItoFileName(RealURI);
  637. struct stat Buf;
  638. if (stat(Final.c_str(),&Buf) == 0)
  639. {
  640. // File was already in place. It needs to be re-verified
  641. // because Release might have changed, so Move it into partial
  642. Rename(Final,DestFile);
  643. // unlink the file and do not try to use I-M-S and Last-Modified
  644. // if the users proxy is broken
  645. if(_config->FindB("Acquire::BrokenProxy", false) == true) {
  646. std::cerr << "forcing re-get of the signature file as requested" << std::endl;
  647. unlink(DestFile.c_str());
  648. }
  649. }
  650. QueueURI(Desc);
  651. }
  652. /*}}}*/
  653. // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
  654. // ---------------------------------------------------------------------
  655. /* The only header we use is the last-modified header. */
  656. string pkgAcqMetaSig::Custom600Headers()
  657. {
  658. struct stat Buf;
  659. if (stat(DestFile.c_str(),&Buf) != 0)
  660. return "\nIndex-File: true";
  661. return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
  662. }
  663. void pkgAcqMetaSig::Done(string Message,unsigned long Size,string MD5,
  664. pkgAcquire::MethodConfig *Cfg)
  665. {
  666. Item::Done(Message,Size,MD5,Cfg);
  667. string FileName = LookupTag(Message,"Filename");
  668. if (FileName.empty() == true)
  669. {
  670. Status = StatError;
  671. ErrorText = "Method gave a blank filename";
  672. return;
  673. }
  674. if (FileName != DestFile)
  675. {
  676. // We have to copy it into place
  677. Local = true;
  678. Desc.URI = "copy:" + FileName;
  679. QueueURI(Desc);
  680. return;
  681. }
  682. Complete = true;
  683. // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
  684. new pkgAcqMetaIndex(Owner, MetaIndexURI, MetaIndexURIDesc, MetaIndexShortDesc,
  685. DestFile, IndexTargets, MetaIndexParser);
  686. }
  687. /*}}}*/
  688. void pkgAcqMetaSig::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  689. {
  690. // Delete any existing sigfile, so that this source isn't
  691. // mistakenly trusted
  692. string Final = _config->FindDir("Dir::State::lists") + URItoFileName(RealURI);
  693. unlink(Final.c_str());
  694. // if we get a timeout if fail
  695. if(LookupTag(Message,"FailReason") == "Timeout" ||
  696. LookupTag(Message,"FailReason") == "TmpResolveFailure") {
  697. Item::Failed(Message,Cnf);
  698. return;
  699. }
  700. // queue a pkgAcqMetaIndex with no sigfile
  701. new pkgAcqMetaIndex(Owner, MetaIndexURI, MetaIndexURIDesc, MetaIndexShortDesc,
  702. "", IndexTargets, MetaIndexParser);
  703. if (Cnf->LocalOnly == true ||
  704. StringToBool(LookupTag(Message,"Transient-Failure"),false) == false)
  705. {
  706. // Ignore this
  707. Status = StatDone;
  708. Complete = false;
  709. Dequeue();
  710. return;
  711. }
  712. Item::Failed(Message,Cnf);
  713. }
  714. pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire *Owner,
  715. string URI,string URIDesc,string ShortDesc,
  716. string SigFile,
  717. const vector<struct IndexTarget*>* IndexTargets,
  718. indexRecords* MetaIndexParser) :
  719. Item(Owner), RealURI(URI), SigFile(SigFile)
  720. {
  721. this->AuthPass = false;
  722. this->MetaIndexParser = MetaIndexParser;
  723. this->IndexTargets = IndexTargets;
  724. DestFile = _config->FindDir("Dir::State::lists") + "partial/";
  725. DestFile += URItoFileName(URI);
  726. // Create the item
  727. Desc.Description = URIDesc;
  728. Desc.Owner = this;
  729. Desc.ShortDesc = ShortDesc;
  730. Desc.URI = URI;
  731. QueueURI(Desc);
  732. }
  733. /*}}}*/
  734. // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
  735. // ---------------------------------------------------------------------
  736. /* The only header we use is the last-modified header. */
  737. string pkgAcqMetaIndex::Custom600Headers()
  738. {
  739. string Final = _config->FindDir("Dir::State::lists");
  740. Final += URItoFileName(RealURI);
  741. struct stat Buf;
  742. if (stat(Final.c_str(),&Buf) != 0)
  743. return "\nIndex-File: true";
  744. return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
  745. }
  746. void pkgAcqMetaIndex::Done(string Message,unsigned long Size,string MD5,
  747. pkgAcquire::MethodConfig *Cfg)
  748. {
  749. Item::Done(Message,Size,MD5,Cfg);
  750. // MetaIndexes are done in two passes: one to download the
  751. // metaindex with an appropriate method, and a second to verify it
  752. // with the gpgv method
  753. if (AuthPass == true)
  754. {
  755. AuthDone(Message);
  756. }
  757. else
  758. {
  759. RetrievalDone(Message);
  760. if (!Complete)
  761. // Still more retrieving to do
  762. return;
  763. if (SigFile == "")
  764. {
  765. // There was no signature file, so we are finished. Download
  766. // the indexes without verification.
  767. QueueIndexes(false);
  768. }
  769. else
  770. {
  771. // There was a signature file, so pass it to gpgv for
  772. // verification
  773. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  774. std::cerr << "Metaindex acquired, queueing gpg verification ("
  775. << SigFile << "," << DestFile << ")\n";
  776. AuthPass = true;
  777. Desc.URI = "gpgv:" + SigFile;
  778. QueueURI(Desc);
  779. Mode = "gpgv";
  780. }
  781. }
  782. }
  783. void pkgAcqMetaIndex::RetrievalDone(string Message)
  784. {
  785. // We have just finished downloading a Release file (it is not
  786. // verified yet)
  787. string FileName = LookupTag(Message,"Filename");
  788. if (FileName.empty() == true)
  789. {
  790. Status = StatError;
  791. ErrorText = "Method gave a blank filename";
  792. return;
  793. }
  794. if (FileName != DestFile)
  795. {
  796. Local = true;
  797. Desc.URI = "copy:" + FileName;
  798. QueueURI(Desc);
  799. return;
  800. }
  801. Complete = true;
  802. string FinalFile = _config->FindDir("Dir::State::lists");
  803. FinalFile += URItoFileName(RealURI);
  804. // The files timestamp matches
  805. if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == false)
  806. {
  807. // Move it into position
  808. Rename(DestFile,FinalFile);
  809. }
  810. DestFile = FinalFile;
  811. }
  812. void pkgAcqMetaIndex::AuthDone(string Message)
  813. {
  814. // At this point, the gpgv method has succeeded, so there is a
  815. // valid signature from a key in the trusted keyring. We
  816. // perform additional verification of its contents, and use them
  817. // to verify the indexes we are about to download
  818. if (!MetaIndexParser->Load(DestFile))
  819. {
  820. Status = StatAuthError;
  821. ErrorText = MetaIndexParser->ErrorText;
  822. return;
  823. }
  824. if (!VerifyVendor())
  825. {
  826. return;
  827. }
  828. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  829. std::cerr << "Signature verification succeeded: "
  830. << DestFile << std::endl;
  831. // Download further indexes with verification
  832. QueueIndexes(true);
  833. // Done, move signature file into position
  834. string VerifiedSigFile = _config->FindDir("Dir::State::lists") +
  835. URItoFileName(RealURI) + ".gpg";
  836. Rename(SigFile,VerifiedSigFile);
  837. chmod(VerifiedSigFile.c_str(),0644);
  838. }
  839. void pkgAcqMetaIndex::QueueIndexes(bool verify)
  840. {
  841. for (vector <struct IndexTarget*>::const_iterator Target = IndexTargets->begin();
  842. Target != IndexTargets->end();
  843. Target++)
  844. {
  845. string ExpectedIndexMD5;
  846. if (verify)
  847. {
  848. const indexRecords::checkSum *Record = MetaIndexParser->Lookup((*Target)->MetaKey);
  849. if (!Record)
  850. {
  851. Status = StatAuthError;
  852. ErrorText = "Unable to find expected entry "
  853. + (*Target)->MetaKey + " in Meta-index file (malformed Release file?)";
  854. return;
  855. }
  856. ExpectedIndexMD5 = Record->MD5Hash;
  857. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  858. {
  859. std::cerr << "Queueing: " << (*Target)->URI << std::endl;
  860. std::cerr << "Expected MD5: " << ExpectedIndexMD5 << std::endl;
  861. }
  862. if (ExpectedIndexMD5.empty())
  863. {
  864. Status = StatAuthError;
  865. ErrorText = "Unable to find MD5 sum for "
  866. + (*Target)->MetaKey + " in Meta-index file";
  867. return;
  868. }
  869. }
  870. // Queue Packages file (either diff or full packages files, depending
  871. // on the users option)
  872. if(_config->FindB("Acquire::PDiffs",false) == false)
  873. new pkgAcqDiffIndex(Owner, (*Target)->URI, (*Target)->Description,
  874. (*Target)->ShortDesc, ExpectedIndexMD5);
  875. else
  876. new newPkgAcqIndex(Owner, (*Target)->URI, (*Target)->Description,
  877. (*Target)->ShortDesc, ExpectedIndexMD5);
  878. }
  879. }
  880. bool pkgAcqMetaIndex::VerifyVendor()
  881. {
  882. // // Maybe this should be made available from above so we don't have
  883. // // to read and parse it every time?
  884. // pkgVendorList List;
  885. // List.ReadMainList();
  886. // const Vendor* Vndr = NULL;
  887. // for (std::vector<string>::const_iterator I = GPGVOutput.begin(); I != GPGVOutput.end(); I++)
  888. // {
  889. // string::size_type pos = (*I).find("VALIDSIG ");
  890. // if (_config->FindB("Debug::Vendor", false))
  891. // std::cerr << "Looking for VALIDSIG in \"" << (*I) << "\": pos " << pos
  892. // << std::endl;
  893. // if (pos != std::string::npos)
  894. // {
  895. // string Fingerprint = (*I).substr(pos+sizeof("VALIDSIG"));
  896. // if (_config->FindB("Debug::Vendor", false))
  897. // std::cerr << "Looking for \"" << Fingerprint << "\" in vendor..." <<
  898. // std::endl;
  899. // Vndr = List.FindVendor(Fingerprint) != "";
  900. // if (Vndr != NULL);
  901. // break;
  902. // }
  903. // }
  904. string Transformed = MetaIndexParser->GetExpectedDist();
  905. if (Transformed == "../project/experimental")
  906. {
  907. Transformed = "experimental";
  908. }
  909. string::size_type pos = Transformed.rfind('/');
  910. if (pos != string::npos)
  911. {
  912. Transformed = Transformed.substr(0, pos);
  913. }
  914. if (Transformed == ".")
  915. {
  916. Transformed = "";
  917. }
  918. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  919. {
  920. std::cerr << "Got Codename: " << MetaIndexParser->GetDist() << std::endl;
  921. std::cerr << "Expecting Dist: " << MetaIndexParser->GetExpectedDist() << std::endl;
  922. std::cerr << "Transformed Dist: " << Transformed << std::endl;
  923. }
  924. if (MetaIndexParser->CheckDist(Transformed) == false)
  925. {
  926. // This might become fatal one day
  927. // Status = StatAuthError;
  928. // ErrorText = "Conflicting distribution; expected "
  929. // + MetaIndexParser->GetExpectedDist() + " but got "
  930. // + MetaIndexParser->GetDist();
  931. // return false;
  932. if (!Transformed.empty())
  933. {
  934. _error->Warning("Conflicting distribution: %s (expected %s but got %s)",
  935. Desc.Description.c_str(),
  936. Transformed.c_str(),
  937. MetaIndexParser->GetDist().c_str());
  938. }
  939. }
  940. return true;
  941. }
  942. /*}}}*/
  943. // pkgAcqMetaIndex::Failed - no Release file present or no signature
  944. // file present /*{{{*/
  945. // ---------------------------------------------------------------------
  946. /* */
  947. void pkgAcqMetaIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  948. {
  949. if (AuthPass == true)
  950. {
  951. // gpgv method failed
  952. _error->Warning("GPG error: %s: %s",
  953. Desc.Description.c_str(),
  954. LookupTag(Message,"Message").c_str());
  955. }
  956. // No Release file was present, or verification failed, so fall
  957. // back to queueing Packages files without verification
  958. QueueIndexes(false);
  959. }
  960. /*}}}*/
  961. // AcqArchive::AcqArchive - Constructor /*{{{*/
  962. // ---------------------------------------------------------------------
  963. /* This just sets up the initial fetch environment and queues the first
  964. possibilitiy */
  965. pkgAcqArchive::pkgAcqArchive(pkgAcquire *Owner,pkgSourceList *Sources,
  966. pkgRecords *Recs,pkgCache::VerIterator const &Version,
  967. string &StoreFilename) :
  968. Item(Owner), Version(Version), Sources(Sources), Recs(Recs),
  969. StoreFilename(StoreFilename), Vf(Version.FileList()),
  970. Trusted(false)
  971. {
  972. Retries = _config->FindI("Acquire::Retries",0);
  973. if (Version.Arch() == 0)
  974. {
  975. _error->Error(_("I wasn't able to locate a file for the %s package. "
  976. "This might mean you need to manually fix this package. "
  977. "(due to missing arch)"),
  978. Version.ParentPkg().Name());
  979. return;
  980. }
  981. /* We need to find a filename to determine the extension. We make the
  982. assumption here that all the available sources for this version share
  983. the same extension.. */
  984. // Skip not source sources, they do not have file fields.
  985. for (; Vf.end() == false; Vf++)
  986. {
  987. if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0)
  988. continue;
  989. break;
  990. }
  991. // Does not really matter here.. we are going to fail out below
  992. if (Vf.end() != true)
  993. {
  994. // If this fails to get a file name we will bomb out below.
  995. pkgRecords::Parser &Parse = Recs->Lookup(Vf);
  996. if (_error->PendingError() == true)
  997. return;
  998. // Generate the final file name as: package_version_arch.foo
  999. StoreFilename = QuoteString(Version.ParentPkg().Name(),"_:") + '_' +
  1000. QuoteString(Version.VerStr(),"_:") + '_' +
  1001. QuoteString(Version.Arch(),"_:.") +
  1002. "." + flExtension(Parse.FileName());
  1003. }
  1004. // check if we have one trusted source for the package. if so, switch
  1005. // to "TrustedOnly" mode
  1006. for (pkgCache::VerFileIterator i = Version.FileList(); i.end() == false; i++)
  1007. {
  1008. pkgIndexFile *Index;
  1009. if (Sources->FindIndex(i.File(),Index) == false)
  1010. continue;
  1011. if (_config->FindB("Debug::pkgAcquire::Auth", false))
  1012. {
  1013. std::cerr << "Checking index: " << Index->Describe()
  1014. << "(Trusted=" << Index->IsTrusted() << ")\n";
  1015. }
  1016. if (Index->IsTrusted()) {
  1017. Trusted = true;
  1018. break;
  1019. }
  1020. }
  1021. // Select a source
  1022. if (QueueNext() == false && _error->PendingError() == false)
  1023. _error->Error(_("I wasn't able to locate file for the %s package. "
  1024. "This might mean you need to manually fix this package."),
  1025. Version.ParentPkg().Name());
  1026. }
  1027. /*}}}*/
  1028. // AcqArchive::QueueNext - Queue the next file source /*{{{*/
  1029. // ---------------------------------------------------------------------
  1030. /* This queues the next available file version for download. It checks if
  1031. the archive is already available in the cache and stashs the MD5 for
  1032. checking later. */
  1033. bool pkgAcqArchive::QueueNext()
  1034. {
  1035. for (; Vf.end() == false; Vf++)
  1036. {
  1037. // Ignore not source sources
  1038. if ((Vf.File()->Flags & pkgCache::Flag::NotSource) != 0)
  1039. continue;
  1040. // Try to cross match against the source list
  1041. pkgIndexFile *Index;
  1042. if (Sources->FindIndex(Vf.File(),Index) == false)
  1043. continue;
  1044. // only try to get a trusted package from another source if that source
  1045. // is also trusted
  1046. if(Trusted && !Index->IsTrusted())
  1047. continue;
  1048. // Grab the text package record
  1049. pkgRecords::Parser &Parse = Recs->Lookup(Vf);
  1050. if (_error->PendingError() == true)
  1051. return false;
  1052. string PkgFile = Parse.FileName();
  1053. MD5 = Parse.MD5Hash();
  1054. if (PkgFile.empty() == true)
  1055. return _error->Error(_("The package index files are corrupted. No Filename: "
  1056. "field for package %s."),
  1057. Version.ParentPkg().Name());
  1058. Desc.URI = Index->ArchiveURI(PkgFile);
  1059. Desc.Description = Index->ArchiveInfo(Version);
  1060. Desc.Owner = this;
  1061. Desc.ShortDesc = Version.ParentPkg().Name();
  1062. // See if we already have the file. (Legacy filenames)
  1063. FileSize = Version->Size;
  1064. string FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile);
  1065. struct stat Buf;
  1066. if (stat(FinalFile.c_str(),&Buf) == 0)
  1067. {
  1068. // Make sure the size matches
  1069. if ((unsigned)Buf.st_size == Version->Size)
  1070. {
  1071. Complete = true;
  1072. Local = true;
  1073. Status = StatDone;
  1074. StoreFilename = DestFile = FinalFile;
  1075. return true;
  1076. }
  1077. /* Hmm, we have a file and its size does not match, this means it is
  1078. an old style mismatched arch */
  1079. unlink(FinalFile.c_str());
  1080. }
  1081. // Check it again using the new style output filenames
  1082. FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename);
  1083. if (stat(FinalFile.c_str(),&Buf) == 0)
  1084. {
  1085. // Make sure the size matches
  1086. if ((unsigned)Buf.st_size == Version->Size)
  1087. {
  1088. Complete = true;
  1089. Local = true;
  1090. Status = StatDone;
  1091. StoreFilename = DestFile = FinalFile;
  1092. return true;
  1093. }
  1094. /* Hmm, we have a file and its size does not match, this shouldnt
  1095. happen.. */
  1096. unlink(FinalFile.c_str());
  1097. }
  1098. DestFile = _config->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename);
  1099. // Check the destination file
  1100. if (stat(DestFile.c_str(),&Buf) == 0)
  1101. {
  1102. // Hmm, the partial file is too big, erase it
  1103. if ((unsigned)Buf.st_size > Version->Size)
  1104. unlink(DestFile.c_str());
  1105. else
  1106. PartialSize = Buf.st_size;
  1107. }
  1108. // Create the item
  1109. Local = false;
  1110. Desc.URI = Index->ArchiveURI(PkgFile);
  1111. Desc.Description = Index->ArchiveInfo(Version);
  1112. Desc.Owner = this;
  1113. Desc.ShortDesc = Version.ParentPkg().Name();
  1114. QueueURI(Desc);
  1115. Vf++;
  1116. return true;
  1117. }
  1118. return false;
  1119. }
  1120. /*}}}*/
  1121. // AcqArchive::Done - Finished fetching /*{{{*/
  1122. // ---------------------------------------------------------------------
  1123. /* */
  1124. void pkgAcqArchive::Done(string Message,unsigned long Size,string Md5Hash,
  1125. pkgAcquire::MethodConfig *Cfg)
  1126. {
  1127. Item::Done(Message,Size,Md5Hash,Cfg);
  1128. // Check the size
  1129. if (Size != Version->Size)
  1130. {
  1131. Status = StatError;
  1132. ErrorText = _("Size mismatch");
  1133. return;
  1134. }
  1135. // Check the md5
  1136. if (Md5Hash.empty() == false && MD5.empty() == false)
  1137. {
  1138. if (Md5Hash != MD5)
  1139. {
  1140. Status = StatError;
  1141. ErrorText = _("MD5Sum mismatch");
  1142. if(FileExists(DestFile))
  1143. Rename(DestFile,DestFile + ".FAILED");
  1144. return;
  1145. }
  1146. }
  1147. // Grab the output filename
  1148. string FileName = LookupTag(Message,"Filename");
  1149. if (FileName.empty() == true)
  1150. {
  1151. Status = StatError;
  1152. ErrorText = "Method gave a blank filename";
  1153. return;
  1154. }
  1155. Complete = true;
  1156. // Reference filename
  1157. if (FileName != DestFile)
  1158. {
  1159. StoreFilename = DestFile = FileName;
  1160. Local = true;
  1161. return;
  1162. }
  1163. // Done, move it into position
  1164. string FinalFile = _config->FindDir("Dir::Cache::Archives");
  1165. FinalFile += flNotDir(StoreFilename);
  1166. Rename(DestFile,FinalFile);
  1167. StoreFilename = DestFile = FinalFile;
  1168. Complete = true;
  1169. }
  1170. /*}}}*/
  1171. // AcqArchive::Failed - Failure handler /*{{{*/
  1172. // ---------------------------------------------------------------------
  1173. /* Here we try other sources */
  1174. void pkgAcqArchive::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  1175. {
  1176. ErrorText = LookupTag(Message,"Message");
  1177. /* We don't really want to retry on failed media swaps, this prevents
  1178. that. An interesting observation is that permanent failures are not
  1179. recorded. */
  1180. if (Cnf->Removable == true &&
  1181. StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
  1182. {
  1183. // Vf = Version.FileList();
  1184. while (Vf.end() == false) Vf++;
  1185. StoreFilename = string();
  1186. Item::Failed(Message,Cnf);
  1187. return;
  1188. }
  1189. if (QueueNext() == false)
  1190. {
  1191. // This is the retry counter
  1192. if (Retries != 0 &&
  1193. Cnf->LocalOnly == false &&
  1194. StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
  1195. {
  1196. Retries--;
  1197. Vf = Version.FileList();
  1198. if (QueueNext() == true)
  1199. return;
  1200. }
  1201. StoreFilename = string();
  1202. Item::Failed(Message,Cnf);
  1203. }
  1204. }
  1205. /*}}}*/
  1206. // AcqArchive::IsTrusted - Determine whether this archive comes from a
  1207. // trusted source /*{{{*/
  1208. // ---------------------------------------------------------------------
  1209. bool pkgAcqArchive::IsTrusted()
  1210. {
  1211. return Trusted;
  1212. }
  1213. // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
  1214. // ---------------------------------------------------------------------
  1215. /* */
  1216. void pkgAcqArchive::Finished()
  1217. {
  1218. if (Status == pkgAcquire::Item::StatDone &&
  1219. Complete == true)
  1220. return;
  1221. StoreFilename = string();
  1222. }
  1223. /*}}}*/
  1224. // AcqFile::pkgAcqFile - Constructor /*{{{*/
  1225. // ---------------------------------------------------------------------
  1226. /* The file is added to the queue */
  1227. pkgAcqFile::pkgAcqFile(pkgAcquire *Owner,string URI,string MD5,
  1228. unsigned long Size,string Dsc,string ShortDesc) :
  1229. Item(Owner), Md5Hash(MD5)
  1230. {
  1231. Retries = _config->FindI("Acquire::Retries",0);
  1232. DestFile = flNotDir(URI);
  1233. // Create the item
  1234. Desc.URI = URI;
  1235. Desc.Description = Dsc;
  1236. Desc.Owner = this;
  1237. // Set the short description to the archive component
  1238. Desc.ShortDesc = ShortDesc;
  1239. // Get the transfer sizes
  1240. FileSize = Size;
  1241. struct stat Buf;
  1242. if (stat(DestFile.c_str(),&Buf) == 0)
  1243. {
  1244. // Hmm, the partial file is too big, erase it
  1245. if ((unsigned)Buf.st_size > Size)
  1246. unlink(DestFile.c_str());
  1247. else
  1248. PartialSize = Buf.st_size;
  1249. }
  1250. QueueURI(Desc);
  1251. }
  1252. /*}}}*/
  1253. // AcqFile::Done - Item downloaded OK /*{{{*/
  1254. // ---------------------------------------------------------------------
  1255. /* */
  1256. void pkgAcqFile::Done(string Message,unsigned long Size,string MD5,
  1257. pkgAcquire::MethodConfig *Cnf)
  1258. {
  1259. // Check the md5
  1260. if (Md5Hash.empty() == false && MD5.empty() == false)
  1261. {
  1262. if (Md5Hash != MD5)
  1263. {
  1264. Status = StatError;
  1265. ErrorText = "MD5Sum mismatch";
  1266. Rename(DestFile,DestFile + ".FAILED");
  1267. return;
  1268. }
  1269. }
  1270. Item::Done(Message,Size,MD5,Cnf);
  1271. string FileName = LookupTag(Message,"Filename");
  1272. if (FileName.empty() == true)
  1273. {
  1274. Status = StatError;
  1275. ErrorText = "Method gave a blank filename";
  1276. return;
  1277. }
  1278. Complete = true;
  1279. // The files timestamp matches
  1280. if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
  1281. return;
  1282. // We have to copy it into place
  1283. if (FileName != DestFile)
  1284. {
  1285. Local = true;
  1286. if (_config->FindB("Acquire::Source-Symlinks",true) == false ||
  1287. Cnf->Removable == true)
  1288. {
  1289. Desc.URI = "copy:" + FileName;
  1290. QueueURI(Desc);
  1291. return;
  1292. }
  1293. // Erase the file if it is a symlink so we can overwrite it
  1294. struct stat St;
  1295. if (lstat(DestFile.c_str(),&St) == 0)
  1296. {
  1297. if (S_ISLNK(St.st_mode) != 0)
  1298. unlink(DestFile.c_str());
  1299. }
  1300. // Symlink the file
  1301. if (symlink(FileName.c_str(),DestFile.c_str()) != 0)
  1302. {
  1303. ErrorText = "Link to " + DestFile + " failure ";
  1304. Status = StatError;
  1305. Complete = false;
  1306. }
  1307. }
  1308. }
  1309. /*}}}*/
  1310. // AcqFile::Failed - Failure handler /*{{{*/
  1311. // ---------------------------------------------------------------------
  1312. /* Here we try other sources */
  1313. void pkgAcqFile::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
  1314. {
  1315. ErrorText = LookupTag(Message,"Message");
  1316. // This is the retry counter
  1317. if (Retries != 0 &&
  1318. Cnf->LocalOnly == false &&
  1319. StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
  1320. {
  1321. Retries--;
  1322. QueueURI(Desc);
  1323. return;
  1324. }
  1325. Item::Failed(Message,Cnf);
  1326. }
  1327. /*}}}*/