indexcopy.cc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  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("Alot 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. if (!Record)
  465. {
  466. _error->Warning("Can't find authentication record for: %s",file.c_str());
  467. return false;
  468. }
  469. MD5Summation sum;
  470. FileFd Fd(prefix+file, FileFd::ReadOnly);
  471. sum.AddFD(Fd.Fd(), Fd.Size());
  472. Fd.Close();
  473. string MD5 = (string)sum.Result();
  474. if (Record->MD5Hash != MD5)
  475. {
  476. _error->Warning("MD5 mismatch for: %s",file.c_str());
  477. return false;
  478. }
  479. if(_config->FindB("Debug::aptcdrom",false))
  480. {
  481. cout << "File: " << prefix+file << endl;
  482. cout << "Expected MD5sum: " << Record->MD5Hash << endl;
  483. cout << "got: " << MD5 << endl << endl;
  484. }
  485. return true;
  486. }
  487. bool SigVerify::CopyMetaIndex(string CDROM, string CDName,
  488. string prefix, string file)
  489. {
  490. char S[400];
  491. snprintf(S,sizeof(S),"cdrom:[%s]/%s%s",CDName.c_str(),
  492. (prefix).c_str() + CDROM.length(),file.c_str());
  493. string TargetF = _config->FindDir("Dir::State::lists");
  494. TargetF += URItoFileName(S);
  495. FileFd Target;
  496. FileFd Rel;
  497. Target.Open(TargetF,FileFd::WriteEmpty);
  498. Rel.Open(prefix + file,FileFd::ReadOnly);
  499. if (_error->PendingError() == true)
  500. return false;
  501. if (CopyFile(Rel,Target) == false)
  502. return false;
  503. return true;
  504. }
  505. bool SigVerify::CopyAndVerify(string CDROM,string Name,vector<string> &SigList,
  506. vector<string> PkgList,vector<string> SrcList)
  507. {
  508. if (SigList.size() == 0)
  509. return true;
  510. bool Debug = _config->FindB("Debug::aptcdrom",false);
  511. // Read all Release files
  512. for (vector<string>::iterator I = SigList.begin(); I != SigList.end(); I++)
  513. {
  514. if(Debug)
  515. cout << "Signature verify for: " << *I << endl;
  516. indexRecords *MetaIndex = new indexRecords;
  517. string prefix = *I;
  518. // a Release.gpg without a Release should never happen
  519. if(!FileExists(*I+"Release"))
  520. continue;
  521. // verify the gpg signature of "Release"
  522. // gpg --verify "*I+Release.gpg", "*I+Release"
  523. const char *Args[400];
  524. unsigned int i = 0;
  525. string gpgvpath = _config->Find("Dir::Bin::gpg", "/usr/bin/gpgv");
  526. string pubringpath = _config->Find("Apt::GPGV::TrustedKeyring", "/etc/apt/trusted.gpg");
  527. string releasegpg = *I+"Release.gpg";
  528. string release = *I+"Release";
  529. Args[i++] = gpgvpath.c_str();
  530. Args[i++] = "--keyring";
  531. Args[i++] = pubringpath.c_str();
  532. Configuration::Item const *Opts;
  533. Opts = _config->Tree("Acquire::gpgv::Options");
  534. if (Opts != 0)
  535. {
  536. Opts = Opts->Child;
  537. for (; Opts != 0; Opts = Opts->Next)
  538. {
  539. if (Opts->Value.empty() == true)
  540. continue;
  541. Args[i++] = Opts->Value.c_str();
  542. if(i >= 390) {
  543. _error->Error("Argument list from Acquire::gpgv::Options too long. Exiting.");
  544. return false;
  545. }
  546. }
  547. }
  548. Args[i++] = releasegpg.c_str();
  549. Args[i++] = release.c_str();
  550. Args[i++] = NULL;
  551. pid_t pid = ExecFork();
  552. if(pid < 0) {
  553. _error->Error("Fork failed");
  554. return false;
  555. }
  556. if(pid == 0) {
  557. execvp(gpgvpath.c_str(), (char**)Args);
  558. }
  559. if(!ExecWait(pid, "gpgv")) {
  560. _error->Warning("Signature verification failed for: %s",
  561. string(*I+"Release.gpg").c_str());
  562. // something went wrong, don't copy the Release.gpg
  563. // FIXME: delete any existing gpg file?
  564. continue;
  565. }
  566. // Open the Release file and add it to the MetaIndex
  567. if(!MetaIndex->Load(*I+"Release"))
  568. {
  569. _error->Error(MetaIndex->ErrorText.c_str());
  570. return false;
  571. }
  572. // go over the Indexfiles and see if they verify
  573. // if so, remove them from our copy of the lists
  574. vector<string> keys = MetaIndex->MetaKeys();
  575. for (vector<string>::iterator I = keys.begin(); I != keys.end(); I++)
  576. {
  577. if(!Verify(prefix,*I, MetaIndex)) {
  578. // something went wrong, don't copy the Release.gpg
  579. // FIXME: delete any existing gpg file?
  580. continue;
  581. }
  582. }
  583. // we need a fresh one for the Release.gpg
  584. delete MetaIndex;
  585. // everything was fine, copy the Release and Release.gpg file
  586. CopyMetaIndex(CDROM, Name, prefix, "Release");
  587. CopyMetaIndex(CDROM, Name, prefix, "Release.gpg");
  588. }
  589. return true;
  590. }
  591. bool TranslationsCopy::CopyTranslations(string CDROM,string Name,vector<string> &List,
  592. pkgCdromStatus *log)
  593. {
  594. OpProgress *Progress = NULL;
  595. if (List.size() == 0)
  596. return true;
  597. if(log)
  598. Progress = log->GetOpProgress();
  599. bool Debug = _config->FindB("Debug::aptcdrom",false);
  600. // Prepare the progress indicator
  601. unsigned long TotalSize = 0;
  602. for (vector<string>::iterator I = List.begin(); I != List.end(); I++)
  603. {
  604. struct stat Buf;
  605. if (stat(string(*I).c_str(),&Buf) != 0 &&
  606. stat(string(*I + ".gz").c_str(),&Buf) != 0)
  607. return _error->Errno("stat","Stat failed for %s",
  608. string(*I).c_str());
  609. TotalSize += Buf.st_size;
  610. }
  611. unsigned long CurrentSize = 0;
  612. unsigned int NotFound = 0;
  613. unsigned int WrongSize = 0;
  614. unsigned int Packages = 0;
  615. for (vector<string>::iterator I = List.begin(); I != List.end(); I++)
  616. {
  617. string OrigPath = string(*I,CDROM.length());
  618. unsigned long FileSize = 0;
  619. // Open the package file
  620. FileFd Pkg;
  621. if (FileExists(*I) == true)
  622. {
  623. Pkg.Open(*I,FileFd::ReadOnly);
  624. FileSize = Pkg.Size();
  625. }
  626. else
  627. {
  628. FileFd From(*I + ".gz",FileFd::ReadOnly);
  629. if (_error->PendingError() == true)
  630. return false;
  631. FileSize = From.Size();
  632. // Get a temp file
  633. FILE *tmp = tmpfile();
  634. if (tmp == 0)
  635. return _error->Errno("tmpfile","Unable to create a tmp file");
  636. Pkg.Fd(dup(fileno(tmp)));
  637. fclose(tmp);
  638. // Fork gzip
  639. pid_t Process = fork();
  640. if (Process < 0)
  641. return _error->Errno("fork","Couldn't fork gzip");
  642. // The child
  643. if (Process == 0)
  644. {
  645. dup2(From.Fd(),STDIN_FILENO);
  646. dup2(Pkg.Fd(),STDOUT_FILENO);
  647. SetCloseExec(STDIN_FILENO,false);
  648. SetCloseExec(STDOUT_FILENO,false);
  649. const char *Args[3];
  650. string Tmp = _config->Find("Dir::bin::gzip","gzip");
  651. Args[0] = Tmp.c_str();
  652. Args[1] = "-d";
  653. Args[2] = 0;
  654. execvp(Args[0],(char **)Args);
  655. exit(100);
  656. }
  657. // Wait for gzip to finish
  658. if (ExecWait(Process,_config->Find("Dir::bin::gzip","gzip").c_str(),false) == false)
  659. return _error->Error("gzip failed, perhaps the disk is full.");
  660. Pkg.Seek(0);
  661. }
  662. pkgTagFile Parser(&Pkg);
  663. if (_error->PendingError() == true)
  664. return false;
  665. // Open the output file
  666. char S[400];
  667. snprintf(S,sizeof(S),"cdrom:[%s]/%s",Name.c_str(),
  668. (*I).c_str() + CDROM.length());
  669. string TargetF = _config->FindDir("Dir::State::lists") + "partial/";
  670. TargetF += URItoFileName(S);
  671. if (_config->FindB("APT::CDROM::NoAct",false) == true)
  672. TargetF = "/dev/null";
  673. FileFd Target(TargetF,FileFd::WriteEmpty);
  674. FILE *TargetFl = fdopen(dup(Target.Fd()),"w");
  675. if (_error->PendingError() == true)
  676. return false;
  677. if (TargetFl == 0)
  678. return _error->Errno("fdopen","Failed to reopen fd");
  679. // Setup the progress meter
  680. if(Progress)
  681. Progress->OverallProgress(CurrentSize,TotalSize,FileSize,
  682. string("Reading Translation Indexes"));
  683. // Parse
  684. if(Progress)
  685. Progress->SubProgress(Pkg.Size());
  686. pkgTagSection Section;
  687. this->Section = &Section;
  688. string Prefix;
  689. unsigned long Hits = 0;
  690. unsigned long Chop = 0;
  691. while (Parser.Step(Section) == true)
  692. {
  693. if(Progress)
  694. Progress->Progress(Parser.Offset());
  695. const char *Start;
  696. const char *Stop;
  697. Section.GetSection(Start,Stop);
  698. fwrite(Start,Stop-Start, 1, TargetFl);
  699. fputc('\n',TargetFl);
  700. Packages++;
  701. Hits++;
  702. }
  703. fclose(TargetFl);
  704. if (Debug == true)
  705. cout << " Processed by using Prefix '" << Prefix << "' and chop " << Chop << endl;
  706. if (_config->FindB("APT::CDROM::NoAct",false) == false)
  707. {
  708. // Move out of the partial directory
  709. Target.Close();
  710. string FinalF = _config->FindDir("Dir::State::lists");
  711. FinalF += URItoFileName(S);
  712. if (rename(TargetF.c_str(),FinalF.c_str()) != 0)
  713. return _error->Errno("rename","Failed to rename");
  714. }
  715. CurrentSize += FileSize;
  716. }
  717. if(Progress)
  718. Progress->Done();
  719. // Some stats
  720. if(log) {
  721. stringstream msg;
  722. if(NotFound == 0 && WrongSize == 0)
  723. ioprintf(msg, _("Wrote %i records.\n"), Packages);
  724. else if (NotFound != 0 && WrongSize == 0)
  725. ioprintf(msg, _("Wrote %i records with %i missing files.\n"),
  726. Packages, NotFound);
  727. else if (NotFound == 0 && WrongSize != 0)
  728. ioprintf(msg, _("Wrote %i records with %i mismatched files\n"),
  729. Packages, WrongSize);
  730. if (NotFound != 0 && WrongSize != 0)
  731. ioprintf(msg, _("Wrote %i records with %i missing files and %i mismatched files\n"), Packages, NotFound, WrongSize);
  732. }
  733. if (Packages == 0)
  734. _error->Warning("No valid records were found.");
  735. if (NotFound + WrongSize > 10)
  736. _error->Warning("Alot of entries were discarded, something may be wrong.\n");
  737. return true;
  738. }