indexcopy.cc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: indexcopy.cc,v 1.10 2002/03/26 07:38:58 jgg Exp $
  4. /* ######################################################################
  5. Index Copying - Aid for copying and verifying the index files
  6. This class helps apt-cache reconstruct a damaged index files.
  7. ##################################################################### */
  8. /*}}}*/
  9. // Include Files /*{{{*/
  10. #include "indexcopy.h"
  11. #include <apt-pkg/error.h>
  12. #include <apt-pkg/progress.h>
  13. #include <apt-pkg/strutl.h>
  14. #include <apt-pkg/fileutl.h>
  15. #include <apt-pkg/configuration.h>
  16. #include <apt-pkg/tagfile.h>
  17. #include <apt-pkg/indexrecords.h>
  18. #include <apt-pkg/md5.h>
  19. #include <apt-pkg/cdrom.h>
  20. #include <apti18n.h>
  21. #include <iostream>
  22. #include <sstream>
  23. #include <unistd.h>
  24. #include <sys/stat.h>
  25. #include <stdio.h>
  26. /*}}}*/
  27. using namespace std;
  28. // IndexCopy::CopyPackages - Copy the package files from the CD /*{{{*/
  29. // ---------------------------------------------------------------------
  30. /* */
  31. bool IndexCopy::CopyPackages(string CDROM,string Name,vector<string> &List,
  32. pkgCdromStatus *log)
  33. {
  34. OpProgress *Progress = NULL;
  35. if (List.size() == 0)
  36. return true;
  37. if(log)
  38. Progress = log->GetOpProgress();
  39. bool NoStat = _config->FindB("APT::CDROM::Fast",false);
  40. bool Debug = _config->FindB("Debug::aptcdrom",false);
  41. // Prepare the progress indicator
  42. unsigned long TotalSize = 0;
  43. for (vector<string>::iterator I = List.begin(); I != List.end(); I++)
  44. {
  45. struct stat Buf;
  46. if (stat(string(*I + GetFileName()).c_str(),&Buf) != 0 &&
  47. stat(string(*I + GetFileName() + ".gz").c_str(),&Buf) != 0)
  48. return _error->Errno("stat","Stat failed for %s",
  49. string(*I + GetFileName()).c_str());
  50. TotalSize += Buf.st_size;
  51. }
  52. unsigned long CurrentSize = 0;
  53. unsigned int NotFound = 0;
  54. unsigned int WrongSize = 0;
  55. unsigned int Packages = 0;
  56. for (vector<string>::iterator I = List.begin(); I != List.end(); I++)
  57. {
  58. string OrigPath = string(*I,CDROM.length());
  59. unsigned long FileSize = 0;
  60. // Open the package file
  61. FileFd Pkg;
  62. if (FileExists(*I + GetFileName()) == true)
  63. {
  64. Pkg.Open(*I + GetFileName(),FileFd::ReadOnly);
  65. FileSize = Pkg.Size();
  66. }
  67. else
  68. {
  69. FileFd From(*I + GetFileName() + ".gz",FileFd::ReadOnly);
  70. if (_error->PendingError() == true)
  71. return false;
  72. FileSize = From.Size();
  73. // Get a temp file
  74. FILE *tmp = tmpfile();
  75. if (tmp == 0)
  76. return _error->Errno("tmpfile","Unable to create a tmp file");
  77. Pkg.Fd(dup(fileno(tmp)));
  78. fclose(tmp);
  79. // Fork gzip
  80. pid_t Process = fork();
  81. if (Process < 0)
  82. return _error->Errno("fork","Couldn't fork gzip");
  83. // The child
  84. if (Process == 0)
  85. {
  86. dup2(From.Fd(),STDIN_FILENO);
  87. dup2(Pkg.Fd(),STDOUT_FILENO);
  88. SetCloseExec(STDIN_FILENO,false);
  89. SetCloseExec(STDOUT_FILENO,false);
  90. const char *Args[3];
  91. string Tmp = _config->Find("Dir::bin::gzip","gzip");
  92. Args[0] = Tmp.c_str();
  93. Args[1] = "-d";
  94. Args[2] = 0;
  95. execvp(Args[0],(char **)Args);
  96. exit(100);
  97. }
  98. // Wait for gzip to finish
  99. if (ExecWait(Process,_config->Find("Dir::bin::gzip","gzip").c_str(),false) == false)
  100. return _error->Error("gzip failed, perhaps the disk is full.");
  101. Pkg.Seek(0);
  102. }
  103. pkgTagFile Parser(&Pkg);
  104. if (_error->PendingError() == true)
  105. return false;
  106. // Open the output file
  107. char S[400];
  108. snprintf(S,sizeof(S),"cdrom:[%s]/%s%s",Name.c_str(),
  109. (*I).c_str() + CDROM.length(),GetFileName());
  110. string TargetF = _config->FindDir("Dir::State::lists") + "partial/";
  111. TargetF += URItoFileName(S);
  112. if (_config->FindB("APT::CDROM::NoAct",false) == true)
  113. TargetF = "/dev/null";
  114. FileFd Target(TargetF,FileFd::WriteEmpty);
  115. FILE *TargetFl = fdopen(dup(Target.Fd()),"w");
  116. if (_error->PendingError() == true)
  117. return false;
  118. if (TargetFl == 0)
  119. return _error->Errno("fdopen","Failed to reopen fd");
  120. // Setup the progress meter
  121. if(Progress)
  122. Progress->OverallProgress(CurrentSize,TotalSize,FileSize,
  123. string("Reading ") + Type() + " Indexes");
  124. // Parse
  125. if(Progress)
  126. Progress->SubProgress(Pkg.Size());
  127. pkgTagSection Section;
  128. this->Section = &Section;
  129. string Prefix;
  130. unsigned long Hits = 0;
  131. unsigned long Chop = 0;
  132. while (Parser.Step(Section) == true)
  133. {
  134. if(Progress)
  135. Progress->Progress(Parser.Offset());
  136. string File;
  137. unsigned long Size;
  138. if (GetFile(File,Size) == false)
  139. {
  140. fclose(TargetFl);
  141. return false;
  142. }
  143. if (Chop != 0)
  144. File = OrigPath + ChopDirs(File,Chop);
  145. // See if the file exists
  146. bool Mangled = false;
  147. if (NoStat == false || Hits < 10)
  148. {
  149. // Attempt to fix broken structure
  150. if (Hits == 0)
  151. {
  152. if (ReconstructPrefix(Prefix,OrigPath,CDROM,File) == false &&
  153. ReconstructChop(Chop,*I,File) == false)
  154. {
  155. if (Debug == true)
  156. clog << "Missed: " << File << endl;
  157. NotFound++;
  158. continue;
  159. }
  160. if (Chop != 0)
  161. File = OrigPath + ChopDirs(File,Chop);
  162. }
  163. // Get the size
  164. struct stat Buf;
  165. if (stat(string(CDROM + Prefix + File).c_str(),&Buf) != 0 ||
  166. Buf.st_size == 0)
  167. {
  168. // Attempt to fix busted symlink support for one instance
  169. string OrigFile = File;
  170. string::size_type Start = File.find("binary-");
  171. string::size_type End = File.find("/",Start+3);
  172. if (Start != string::npos && End != string::npos)
  173. {
  174. File.replace(Start,End-Start,"binary-all");
  175. Mangled = true;
  176. }
  177. if (Mangled == false ||
  178. stat(string(CDROM + Prefix + File).c_str(),&Buf) != 0)
  179. {
  180. if (Debug == true)
  181. clog << "Missed(2): " << OrigFile << endl;
  182. NotFound++;
  183. continue;
  184. }
  185. }
  186. // Size match
  187. if ((unsigned)Buf.st_size != Size)
  188. {
  189. if (Debug == true)
  190. clog << "Wrong Size: " << File << endl;
  191. WrongSize++;
  192. continue;
  193. }
  194. }
  195. Packages++;
  196. Hits++;
  197. if (RewriteEntry(TargetFl,File) == false)
  198. {
  199. fclose(TargetFl);
  200. return false;
  201. }
  202. }
  203. fclose(TargetFl);
  204. if (Debug == true)
  205. cout << " Processed by using Prefix '" << Prefix << "' and chop " << Chop << endl;
  206. if (_config->FindB("APT::CDROM::NoAct",false) == false)
  207. {
  208. // Move out of the partial directory
  209. Target.Close();
  210. string FinalF = _config->FindDir("Dir::State::lists");
  211. FinalF += URItoFileName(S);
  212. if (rename(TargetF.c_str(),FinalF.c_str()) != 0)
  213. return _error->Errno("rename","Failed to rename");
  214. }
  215. /* Mangle the source to be in the proper notation with
  216. prefix dist [component] */
  217. *I = string(*I,Prefix.length());
  218. ConvertToSourceList(CDROM,*I);
  219. *I = Prefix + ' ' + *I;
  220. CurrentSize += FileSize;
  221. }
  222. if(Progress)
  223. Progress->Done();
  224. // Some stats
  225. if(log) {
  226. stringstream msg;
  227. if(NotFound == 0 && WrongSize == 0)
  228. ioprintf(msg, _("Wrote %i records.\n"), Packages);
  229. else if (NotFound != 0 && WrongSize == 0)
  230. ioprintf(msg, _("Wrote %i records with %i missing files.\n"),
  231. Packages, NotFound);
  232. else if (NotFound == 0 && WrongSize != 0)
  233. ioprintf(msg, _("Wrote %i records with %i mismatched files\n"),
  234. Packages, WrongSize);
  235. if (NotFound != 0 && WrongSize != 0)
  236. ioprintf(msg, _("Wrote %i records with %i missing files and %i mismatched files\n"), Packages, NotFound, WrongSize);
  237. }
  238. if (Packages == 0)
  239. _error->Warning("No valid records were found.");
  240. if (NotFound + WrongSize > 10)
  241. _error->Warning("A lot of entries were discarded, something may be wrong.\n");
  242. return true;
  243. }
  244. /*}}}*/
  245. // IndexCopy::ChopDirs - Chop off the leading directory components /*{{{*/
  246. // ---------------------------------------------------------------------
  247. /* */
  248. string IndexCopy::ChopDirs(string Path,unsigned int Depth)
  249. {
  250. string::size_type I = 0;
  251. do
  252. {
  253. I = Path.find('/',I+1);
  254. Depth--;
  255. }
  256. while (I != string::npos && Depth != 0);
  257. if (I == string::npos)
  258. return string();
  259. return string(Path,I+1);
  260. }
  261. /*}}}*/
  262. // IndexCopy::ReconstructPrefix - Fix strange prefixing /*{{{*/
  263. // ---------------------------------------------------------------------
  264. /* This prepends dir components from the path to the package files to
  265. the path to the deb until it is found */
  266. bool IndexCopy::ReconstructPrefix(string &Prefix,string OrigPath,string CD,
  267. string File)
  268. {
  269. bool Debug = _config->FindB("Debug::aptcdrom",false);
  270. unsigned int Depth = 1;
  271. string MyPrefix = Prefix;
  272. while (1)
  273. {
  274. struct stat Buf;
  275. if (stat(string(CD + MyPrefix + File).c_str(),&Buf) != 0)
  276. {
  277. if (Debug == true)
  278. cout << "Failed, " << CD + MyPrefix + File << endl;
  279. if (GrabFirst(OrigPath,MyPrefix,Depth++) == true)
  280. continue;
  281. return false;
  282. }
  283. else
  284. {
  285. Prefix = MyPrefix;
  286. return true;
  287. }
  288. }
  289. return false;
  290. }
  291. /*}}}*/
  292. // IndexCopy::ReconstructChop - Fixes bad source paths /*{{{*/
  293. // ---------------------------------------------------------------------
  294. /* This removes path components from the filename and prepends the location
  295. of the package files until a file is found */
  296. bool IndexCopy::ReconstructChop(unsigned long &Chop,string Dir,string File)
  297. {
  298. // Attempt to reconstruct the filename
  299. unsigned long Depth = 0;
  300. while (1)
  301. {
  302. struct stat Buf;
  303. if (stat(string(Dir + File).c_str(),&Buf) != 0)
  304. {
  305. File = ChopDirs(File,1);
  306. Depth++;
  307. if (File.empty() == false)
  308. continue;
  309. return false;
  310. }
  311. else
  312. {
  313. Chop = Depth;
  314. return true;
  315. }
  316. }
  317. return false;
  318. }
  319. /*}}}*/
  320. // IndexCopy::ConvertToSourceList - Convert a Path to a sourcelist /*{{{*/
  321. // ---------------------------------------------------------------------
  322. /* We look for things in dists/ notation and convert them to
  323. <dist> <component> form otherwise it is left alone. This also strips
  324. the CD path.
  325. This implements a regex sort of like:
  326. (.*)/dists/([^/]*)/(.*)/binary-*
  327. ^ ^ ^- Component
  328. | |-------- Distribution
  329. |------------------- Path
  330. It was deciced to use only a single word for dist (rather than say
  331. unstable/non-us) to increase the chance that each CD gets a single
  332. line in sources.list.
  333. */
  334. void IndexCopy::ConvertToSourceList(string CD,string &Path)
  335. {
  336. char S[300];
  337. snprintf(S,sizeof(S),"binary-%s",_config->Find("Apt::Architecture").c_str());
  338. // Strip the cdrom base path
  339. Path = string(Path,CD.length());
  340. if (Path.empty() == true)
  341. Path = "/";
  342. // Too short to be a dists/ type
  343. if (Path.length() < strlen("dists/"))
  344. return;
  345. // Not a dists type.
  346. if (stringcmp(Path.c_str(),Path.c_str()+strlen("dists/"),"dists/") != 0)
  347. return;
  348. // Isolate the dist
  349. string::size_type Slash = strlen("dists/");
  350. string::size_type Slash2 = Path.find('/',Slash + 1);
  351. if (Slash2 == string::npos || Slash2 + 2 >= Path.length())
  352. return;
  353. string Dist = string(Path,Slash,Slash2 - Slash);
  354. // Isolate the component
  355. Slash = Slash2;
  356. for (unsigned I = 0; I != 10; I++)
  357. {
  358. Slash = Path.find('/',Slash+1);
  359. if (Slash == string::npos || Slash + 2 >= Path.length())
  360. return;
  361. string Comp = string(Path,Slash2+1,Slash - Slash2-1);
  362. // Verify the trailing binary- bit
  363. string::size_type BinSlash = Path.find('/',Slash + 1);
  364. if (Slash == string::npos)
  365. return;
  366. string Binary = string(Path,Slash+1,BinSlash - Slash-1);
  367. if (Binary != S && Binary != "source")
  368. continue;
  369. Path = Dist + ' ' + Comp;
  370. return;
  371. }
  372. }
  373. /*}}}*/
  374. // IndexCopy::GrabFirst - Return the first Depth path components /*{{{*/
  375. // ---------------------------------------------------------------------
  376. /* */
  377. bool IndexCopy::GrabFirst(string Path,string &To,unsigned int Depth)
  378. {
  379. string::size_type I = 0;
  380. do
  381. {
  382. I = Path.find('/',I+1);
  383. Depth--;
  384. }
  385. while (I != string::npos && Depth != 0);
  386. if (I == string::npos)
  387. return false;
  388. To = string(Path,0,I+1);
  389. return true;
  390. }
  391. /*}}}*/
  392. // PackageCopy::GetFile - Get the file information from the section /*{{{*/
  393. // ---------------------------------------------------------------------
  394. /* */
  395. bool PackageCopy::GetFile(string &File,unsigned long &Size)
  396. {
  397. File = Section->FindS("Filename");
  398. Size = Section->FindI("Size");
  399. if (File.empty() || Size == 0)
  400. return _error->Error("Cannot find filename or size tag");
  401. return true;
  402. }
  403. /*}}}*/
  404. // PackageCopy::RewriteEntry - Rewrite the entry with a new filename /*{{{*/
  405. // ---------------------------------------------------------------------
  406. /* */
  407. bool PackageCopy::RewriteEntry(FILE *Target,string File)
  408. {
  409. TFRewriteData Changes[] = {{"Filename",File.c_str()},
  410. {}};
  411. if (TFRewrite(Target,*Section,TFRewritePackageOrder,Changes) == false)
  412. return false;
  413. fputc('\n',Target);
  414. return true;
  415. }
  416. /*}}}*/
  417. // SourceCopy::GetFile - Get the file information from the section /*{{{*/
  418. // ---------------------------------------------------------------------
  419. /* */
  420. bool SourceCopy::GetFile(string &File,unsigned long &Size)
  421. {
  422. string Files = Section->FindS("Files");
  423. if (Files.empty() == true)
  424. return false;
  425. // Stash the / terminated directory prefix
  426. string Base = Section->FindS("Directory");
  427. if (Base.empty() == false && Base[Base.length()-1] != '/')
  428. Base += '/';
  429. // Read the first file triplet
  430. const char *C = Files.c_str();
  431. string sSize;
  432. string MD5Hash;
  433. // Parse each of the elements
  434. if (ParseQuoteWord(C,MD5Hash) == false ||
  435. ParseQuoteWord(C,sSize) == false ||
  436. ParseQuoteWord(C,File) == false)
  437. return _error->Error("Error parsing file record");
  438. // Parse the size and append the directory
  439. Size = atoi(sSize.c_str());
  440. File = Base + File;
  441. return true;
  442. }
  443. /*}}}*/
  444. // SourceCopy::RewriteEntry - Rewrite the entry with a new filename /*{{{*/
  445. // ---------------------------------------------------------------------
  446. /* */
  447. bool SourceCopy::RewriteEntry(FILE *Target,string File)
  448. {
  449. string Dir(File,0,File.rfind('/'));
  450. TFRewriteData Changes[] = {{"Directory",Dir.c_str()},
  451. {}};
  452. if (TFRewrite(Target,*Section,TFRewriteSourceOrder,Changes) == false)
  453. return false;
  454. fputc('\n',Target);
  455. return true;
  456. }
  457. /*}}}*/
  458. // SigVerify::Verify - Verify a files md5sum against its metaindex /*{{{*/
  459. // ---------------------------------------------------------------------
  460. /* */
  461. bool SigVerify::Verify(string prefix, string file, indexRecords *MetaIndex)
  462. {
  463. const indexRecords::checkSum *Record = MetaIndex->Lookup(file);
  464. // we skip non-existing files in the verifcation to support a cdrom
  465. // with no Packages file (just a Package.gz), see LP: #255545
  466. // (non-existing files are not considered a error)
  467. if(!FileExists(prefix+file))
  468. {
  469. _error->Warning(_("Skipping nonexistent file %s"), string(prefix+file).c_str());
  470. return true;
  471. }
  472. if (!Record)
  473. {
  474. _error->Warning(_("Can't find authentication record for: %s"), file.c_str());
  475. return false;
  476. }
  477. if (!Record->Hash.VerifyFile(prefix+file))
  478. {
  479. _error->Warning(_("Hash mismatch for: %s"),file.c_str());
  480. return false;
  481. }
  482. if(_config->FindB("Debug::aptcdrom",false))
  483. {
  484. cout << "File: " << prefix+file << endl;
  485. cout << "Expected Hash " << Record->Hash.toStr() << endl;
  486. }
  487. return true;
  488. }
  489. /*}}}*/
  490. bool SigVerify::CopyMetaIndex(string CDROM, string CDName, /*{{{*/
  491. string prefix, string file)
  492. {
  493. char S[400];
  494. snprintf(S,sizeof(S),"cdrom:[%s]/%s%s",CDName.c_str(),
  495. (prefix).c_str() + CDROM.length(),file.c_str());
  496. string TargetF = _config->FindDir("Dir::State::lists");
  497. TargetF += URItoFileName(S);
  498. FileFd Target;
  499. FileFd Rel;
  500. Target.Open(TargetF,FileFd::WriteEmpty);
  501. Rel.Open(prefix + file,FileFd::ReadOnly);
  502. if (_error->PendingError() == true)
  503. return false;
  504. if (CopyFile(Rel,Target) == false)
  505. return false;
  506. return true;
  507. }
  508. /*}}}*/
  509. bool SigVerify::CopyAndVerify(string CDROM,string Name,vector<string> &SigList, /*{{{*/
  510. vector<string> PkgList,vector<string> SrcList)
  511. {
  512. if (SigList.size() == 0)
  513. return true;
  514. bool Debug = _config->FindB("Debug::aptcdrom",false);
  515. // Read all Release files
  516. for (vector<string>::iterator I = SigList.begin(); I != SigList.end(); I++)
  517. {
  518. if(Debug)
  519. cout << "Signature verify for: " << *I << endl;
  520. indexRecords *MetaIndex = new indexRecords;
  521. string prefix = *I;
  522. string const releasegpg = *I+"Release.gpg";
  523. string const release = *I+"Release";
  524. // a Release.gpg without a Release should never happen
  525. if(FileExists(release) == false)
  526. {
  527. delete MetaIndex;
  528. continue;
  529. }
  530. pid_t pid = ExecFork();
  531. if(pid < 0) {
  532. _error->Error("Fork failed");
  533. return false;
  534. }
  535. if(pid == 0) {
  536. string const gpgvpath = _config->Find("Dir::Bin::gpg", "/usr/bin/gpgv");
  537. std::vector<const char*> Args = GetGPGVCommandLine();
  538. Args.push_back(releasegpg.c_str());
  539. Args.push_back(release.c_str());
  540. Args.push_back(NULL);
  541. execvp(gpgvpath.c_str(), (char**) &Args[0]);
  542. }
  543. if(!ExecWait(pid, "gpgv")) {
  544. _error->Warning("Signature verification failed for: %s",
  545. releasegpg.c_str());
  546. // something went wrong, don't copy the Release.gpg
  547. // FIXME: delete any existing gpg file?
  548. continue;
  549. }
  550. // Open the Release file and add it to the MetaIndex
  551. if(!MetaIndex->Load(release))
  552. {
  553. _error->Error("%s",MetaIndex->ErrorText.c_str());
  554. return false;
  555. }
  556. // go over the Indexfiles and see if they verify
  557. // if so, remove them from our copy of the lists
  558. vector<string> keys = MetaIndex->MetaKeys();
  559. for (vector<string>::iterator I = keys.begin(); I != keys.end(); I++)
  560. {
  561. if(!Verify(prefix,*I, MetaIndex)) {
  562. // something went wrong, don't copy the Release.gpg
  563. // FIXME: delete any existing gpg file?
  564. _error->Discard();
  565. continue;
  566. }
  567. }
  568. // we need a fresh one for the Release.gpg
  569. delete MetaIndex;
  570. // everything was fine, copy the Release and Release.gpg file
  571. CopyMetaIndex(CDROM, Name, prefix, "Release");
  572. CopyMetaIndex(CDROM, Name, prefix, "Release.gpg");
  573. }
  574. return true;
  575. }
  576. /*}}}*/
  577. // SigVerify::GetGPGVCommandLine - returns the command needed for verify/*{{{*/
  578. // ---------------------------------------------------------------------
  579. /* Generating the commandline for calling gpgv is somehow complicated as
  580. we need to add multiple keyrings and user supplied options. Also, as
  581. the cdrom code currently can not use the gpgv method we have two places
  582. these need to be done - so the place for this method is wrong but better
  583. than code duplication… */
  584. std::vector<const char *> SigVerify::GetGPGVCommandLine()
  585. {
  586. string const gpgvpath = _config->Find("Dir::Bin::gpg", "/usr/bin/gpgv");
  587. // FIXME: remove support for deprecated APT::GPGV setting
  588. string const trustedFile = _config->FindFile("Dir::Etc::Trusted",
  589. _config->Find("APT::GPGV::TrustedKeyring", "/etc/apt/trusted.gpg").c_str());
  590. string const trustedPath = _config->FindDir("Dir::Etc::TrustedParts", "/etc/apt/trusted.gpg.d");
  591. if (_config->FindB("Debug::Acquire::gpgv", false) == true)
  592. {
  593. std::clog << "gpgv path: " << gpgvpath << std::endl;
  594. std::clog << "Keyring file: " << trustedFile << std::endl;
  595. std::clog << "Keyring path: " << trustedPath << std::endl;
  596. }
  597. std::vector<string> keyrings = GetListOfFilesInDir(trustedPath, "gpg", false);
  598. if (FileExists(trustedFile) == true)
  599. keyrings.push_back(trustedFile);
  600. std::vector<const char *> Args;
  601. Args.reserve(30);
  602. if (keyrings.empty() == true)
  603. return Args;
  604. Args.push_back(gpgvpath.c_str());
  605. Args.push_back("--ignore-time-conflict");
  606. for (vector<string>::const_iterator K = keyrings.begin();
  607. K != keyrings.end(); ++K)
  608. {
  609. Args.push_back("--keyring");
  610. Args.push_back(K->c_str());
  611. }
  612. Configuration::Item const *Opts;
  613. Opts = _config->Tree("Acquire::gpgv::Options");
  614. if (Opts != 0)
  615. {
  616. Opts = Opts->Child;
  617. for (; Opts != 0; Opts = Opts->Next)
  618. {
  619. if (Opts->Value.empty() == true)
  620. continue;
  621. Args.push_back(Opts->Value.c_str());
  622. }
  623. }
  624. return Args;
  625. }
  626. /*}}}*/
  627. bool TranslationsCopy::CopyTranslations(string CDROM,string Name, /*{{{*/
  628. vector<string> &List, pkgCdromStatus *log)
  629. {
  630. OpProgress *Progress = NULL;
  631. if (List.size() == 0)
  632. return true;
  633. if(log)
  634. Progress = log->GetOpProgress();
  635. bool Debug = _config->FindB("Debug::aptcdrom",false);
  636. // Prepare the progress indicator
  637. unsigned long TotalSize = 0;
  638. for (vector<string>::iterator I = List.begin(); I != List.end(); I++)
  639. {
  640. struct stat Buf;
  641. if (stat(string(*I).c_str(),&Buf) != 0 &&
  642. stat(string(*I + ".gz").c_str(),&Buf) != 0)
  643. return _error->Errno("stat","Stat failed for %s",
  644. string(*I).c_str());
  645. TotalSize += Buf.st_size;
  646. }
  647. unsigned long CurrentSize = 0;
  648. unsigned int NotFound = 0;
  649. unsigned int WrongSize = 0;
  650. unsigned int Packages = 0;
  651. for (vector<string>::iterator I = List.begin(); I != List.end(); I++)
  652. {
  653. string OrigPath = string(*I,CDROM.length());
  654. unsigned long FileSize = 0;
  655. // Open the package file
  656. FileFd Pkg;
  657. if (FileExists(*I) == true)
  658. {
  659. Pkg.Open(*I,FileFd::ReadOnly);
  660. FileSize = Pkg.Size();
  661. }
  662. else
  663. {
  664. FileFd From(*I + ".gz",FileFd::ReadOnly);
  665. if (_error->PendingError() == true)
  666. return false;
  667. FileSize = From.Size();
  668. // Get a temp file
  669. FILE *tmp = tmpfile();
  670. if (tmp == 0)
  671. return _error->Errno("tmpfile","Unable to create a tmp file");
  672. Pkg.Fd(dup(fileno(tmp)));
  673. fclose(tmp);
  674. // Fork gzip
  675. pid_t Process = fork();
  676. if (Process < 0)
  677. return _error->Errno("fork","Couldn't fork gzip");
  678. // The child
  679. if (Process == 0)
  680. {
  681. dup2(From.Fd(),STDIN_FILENO);
  682. dup2(Pkg.Fd(),STDOUT_FILENO);
  683. SetCloseExec(STDIN_FILENO,false);
  684. SetCloseExec(STDOUT_FILENO,false);
  685. const char *Args[3];
  686. string Tmp = _config->Find("Dir::bin::gzip","gzip");
  687. Args[0] = Tmp.c_str();
  688. Args[1] = "-d";
  689. Args[2] = 0;
  690. execvp(Args[0],(char **)Args);
  691. exit(100);
  692. }
  693. // Wait for gzip to finish
  694. if (ExecWait(Process,_config->Find("Dir::bin::gzip","gzip").c_str(),false) == false)
  695. return _error->Error("gzip failed, perhaps the disk is full.");
  696. Pkg.Seek(0);
  697. }
  698. pkgTagFile Parser(&Pkg);
  699. if (_error->PendingError() == true)
  700. return false;
  701. // Open the output file
  702. char S[400];
  703. snprintf(S,sizeof(S),"cdrom:[%s]/%s",Name.c_str(),
  704. (*I).c_str() + CDROM.length());
  705. string TargetF = _config->FindDir("Dir::State::lists") + "partial/";
  706. TargetF += URItoFileName(S);
  707. if (_config->FindB("APT::CDROM::NoAct",false) == true)
  708. TargetF = "/dev/null";
  709. FileFd Target(TargetF,FileFd::WriteEmpty);
  710. FILE *TargetFl = fdopen(dup(Target.Fd()),"w");
  711. if (_error->PendingError() == true)
  712. return false;
  713. if (TargetFl == 0)
  714. return _error->Errno("fdopen","Failed to reopen fd");
  715. // Setup the progress meter
  716. if(Progress)
  717. Progress->OverallProgress(CurrentSize,TotalSize,FileSize,
  718. string("Reading Translation Indexes"));
  719. // Parse
  720. if(Progress)
  721. Progress->SubProgress(Pkg.Size());
  722. pkgTagSection Section;
  723. this->Section = &Section;
  724. string Prefix;
  725. unsigned long Hits = 0;
  726. unsigned long Chop = 0;
  727. while (Parser.Step(Section) == true)
  728. {
  729. if(Progress)
  730. Progress->Progress(Parser.Offset());
  731. const char *Start;
  732. const char *Stop;
  733. Section.GetSection(Start,Stop);
  734. fwrite(Start,Stop-Start, 1, TargetFl);
  735. fputc('\n',TargetFl);
  736. Packages++;
  737. Hits++;
  738. }
  739. fclose(TargetFl);
  740. if (Debug == true)
  741. cout << " Processed by using Prefix '" << Prefix << "' and chop " << Chop << endl;
  742. if (_config->FindB("APT::CDROM::NoAct",false) == false)
  743. {
  744. // Move out of the partial directory
  745. Target.Close();
  746. string FinalF = _config->FindDir("Dir::State::lists");
  747. FinalF += URItoFileName(S);
  748. if (rename(TargetF.c_str(),FinalF.c_str()) != 0)
  749. return _error->Errno("rename","Failed to rename");
  750. }
  751. CurrentSize += FileSize;
  752. }
  753. if(Progress)
  754. Progress->Done();
  755. // Some stats
  756. if(log) {
  757. stringstream msg;
  758. if(NotFound == 0 && WrongSize == 0)
  759. ioprintf(msg, _("Wrote %i records.\n"), Packages);
  760. else if (NotFound != 0 && WrongSize == 0)
  761. ioprintf(msg, _("Wrote %i records with %i missing files.\n"),
  762. Packages, NotFound);
  763. else if (NotFound == 0 && WrongSize != 0)
  764. ioprintf(msg, _("Wrote %i records with %i mismatched files\n"),
  765. Packages, WrongSize);
  766. if (NotFound != 0 && WrongSize != 0)
  767. ioprintf(msg, _("Wrote %i records with %i missing files and %i mismatched files\n"), Packages, NotFound, WrongSize);
  768. }
  769. if (Packages == 0)
  770. _error->Warning("No valid records were found.");
  771. if (NotFound + WrongSize > 10)
  772. _error->Warning("A lot of entries were discarded, something may be wrong.\n");
  773. return true;
  774. }
  775. /*}}}*/