indexcopy.cc 23 KB

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