You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1139 lines
36 KiB

  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: writer.cc,v 1.14 2004/03/24 01:40:43 mdz Exp $
  4. /* ######################################################################
  5. Writer
  6. The file writer classes. These write various types of output, sources,
  7. packages and contents.
  8. ##################################################################### */
  9. /*}}}*/
  10. // Include Files /*{{{*/
  11. #include <config.h>
  12. #include <apt-pkg/strutl.h>
  13. #include <apt-pkg/error.h>
  14. #include <apt-pkg/configuration.h>
  15. #include <apt-pkg/aptconfiguration.h>
  16. #include <apt-pkg/md5.h>
  17. #include <apt-pkg/hashes.h>
  18. #include <apt-pkg/deblistparser.h>
  19. #include <sys/types.h>
  20. #include <unistd.h>
  21. #include <ctime>
  22. #include <ftw.h>
  23. #include <fnmatch.h>
  24. #include <iostream>
  25. #include <sstream>
  26. #include <memory>
  27. #include "writer.h"
  28. #include "cachedb.h"
  29. #include "apt-ftparchive.h"
  30. #include "multicompress.h"
  31. #include <apti18n.h>
  32. /*}}}*/
  33. using namespace std;
  34. FTWScanner *FTWScanner::Owner;
  35. // SetTFRewriteData - Helper for setting rewrite lists /*{{{*/
  36. // ---------------------------------------------------------------------
  37. /* */
  38. inline void SetTFRewriteData(struct TFRewriteData &tfrd,
  39. const char *tag,
  40. const char *rewrite,
  41. const char *newtag = 0)
  42. {
  43. tfrd.Tag = tag;
  44. tfrd.Rewrite = rewrite;
  45. tfrd.NewTag = newtag;
  46. }
  47. /*}}}*/
  48. // FTWScanner::FTWScanner - Constructor /*{{{*/
  49. // ---------------------------------------------------------------------
  50. /* */
  51. FTWScanner::FTWScanner(string const &Arch): Arch(Arch)
  52. {
  53. ErrorPrinted = false;
  54. NoLinkAct = !_config->FindB("APT::FTPArchive::DeLinkAct",true);
  55. DoMD5 = _config->FindB("APT::FTPArchive::MD5",true);
  56. DoSHA1 = _config->FindB("APT::FTPArchive::SHA1",true);
  57. DoSHA256 = _config->FindB("APT::FTPArchive::SHA256",true);
  58. DoSHA512 = _config->FindB("APT::FTPArchive::SHA512",true);
  59. }
  60. /*}}}*/
  61. // FTWScanner::Scanner - FTW Scanner /*{{{*/
  62. // ---------------------------------------------------------------------
  63. /* This is the FTW scanner, it processes each directory element in the
  64. directory tree. */
  65. int FTWScanner::ScannerFTW(const char *File,const struct stat *sb,int Flag)
  66. {
  67. if (Flag == FTW_DNR)
  68. {
  69. Owner->NewLine(1);
  70. ioprintf(c1out, _("W: Unable to read directory %s\n"), File);
  71. }
  72. if (Flag == FTW_NS)
  73. {
  74. Owner->NewLine(1);
  75. ioprintf(c1out, _("W: Unable to stat %s\n"), File);
  76. }
  77. if (Flag != FTW_F)
  78. return 0;
  79. return ScannerFile(File, true);
  80. }
  81. /*}}}*/
  82. // FTWScanner::ScannerFile - File Scanner /*{{{*/
  83. // ---------------------------------------------------------------------
  84. /* */
  85. int FTWScanner::ScannerFile(const char *File, bool const &ReadLink)
  86. {
  87. const char *LastComponent = strrchr(File, '/');
  88. char *RealPath = NULL;
  89. if (LastComponent == NULL)
  90. LastComponent = File;
  91. else
  92. LastComponent++;
  93. vector<string>::const_iterator I;
  94. for(I = Owner->Patterns.begin(); I != Owner->Patterns.end(); ++I)
  95. {
  96. if (fnmatch((*I).c_str(), LastComponent, 0) == 0)
  97. break;
  98. }
  99. if (I == Owner->Patterns.end())
  100. return 0;
  101. /* Process it. If the file is a link then resolve it into an absolute
  102. name.. This works best if the directory components the scanner are
  103. given are not links themselves. */
  104. char Jnk[2];
  105. Owner->OriginalPath = File;
  106. if (ReadLink &&
  107. readlink(File,Jnk,sizeof(Jnk)) != -1 &&
  108. (RealPath = realpath(File,NULL)) != 0)
  109. {
  110. Owner->DoPackage(RealPath);
  111. free(RealPath);
  112. }
  113. else
  114. Owner->DoPackage(File);
  115. if (_error->empty() == false)
  116. {
  117. // Print any errors or warnings found
  118. string Err;
  119. bool SeenPath = false;
  120. while (_error->empty() == false)
  121. {
  122. Owner->NewLine(1);
  123. bool const Type = _error->PopMessage(Err);
  124. if (Type == true)
  125. cerr << _("E: ") << Err << endl;
  126. else
  127. cerr << _("W: ") << Err << endl;
  128. if (Err.find(File) != string::npos)
  129. SeenPath = true;
  130. }
  131. if (SeenPath == false)
  132. cerr << _("E: Errors apply to file ") << "'" << File << "'" << endl;
  133. return 0;
  134. }
  135. return 0;
  136. }
  137. /*}}}*/
  138. // FTWScanner::RecursiveScan - Just scan a directory tree /*{{{*/
  139. // ---------------------------------------------------------------------
  140. /* */
  141. bool FTWScanner::RecursiveScan(string const &Dir)
  142. {
  143. char *RealPath = NULL;
  144. /* If noprefix is set then jam the scan root in, so we don't generate
  145. link followed paths out of control */
  146. if (InternalPrefix.empty() == true)
  147. {
  148. if ((RealPath = realpath(Dir.c_str(),NULL)) == 0)
  149. return _error->Errno("realpath",_("Failed to resolve %s"),Dir.c_str());
  150. InternalPrefix = RealPath;
  151. free(RealPath);
  152. }
  153. // Do recursive directory searching
  154. Owner = this;
  155. int const Res = ftw(Dir.c_str(),ScannerFTW,30);
  156. // Error treewalking?
  157. if (Res != 0)
  158. {
  159. if (_error->PendingError() == false)
  160. _error->Errno("ftw",_("Tree walking failed"));
  161. return false;
  162. }
  163. return true;
  164. }
  165. /*}}}*/
  166. // FTWScanner::LoadFileList - Load the file list from a file /*{{{*/
  167. // ---------------------------------------------------------------------
  168. /* This is an alternative to using FTW to locate files, it reads the list
  169. of files from another file. */
  170. bool FTWScanner::LoadFileList(string const &Dir, string const &File)
  171. {
  172. char *RealPath = NULL;
  173. /* If noprefix is set then jam the scan root in, so we don't generate
  174. link followed paths out of control */
  175. if (InternalPrefix.empty() == true)
  176. {
  177. if ((RealPath = realpath(Dir.c_str(),NULL)) == 0)
  178. return _error->Errno("realpath",_("Failed to resolve %s"),Dir.c_str());
  179. InternalPrefix = RealPath;
  180. free(RealPath);
  181. }
  182. Owner = this;
  183. FILE *List = fopen(File.c_str(),"r");
  184. if (List == 0)
  185. return _error->Errno("fopen",_("Failed to open %s"),File.c_str());
  186. /* We are a tad tricky here.. We prefix the buffer with the directory
  187. name, that way if we need a full path with just use line.. Sneaky and
  188. fully evil. */
  189. char Line[1000];
  190. char *FileStart;
  191. if (Dir.empty() == true || Dir.end()[-1] != '/')
  192. FileStart = Line + snprintf(Line,sizeof(Line),"%s/",Dir.c_str());
  193. else
  194. FileStart = Line + snprintf(Line,sizeof(Line),"%s",Dir.c_str());
  195. while (fgets(FileStart,sizeof(Line) - (FileStart - Line),List) != 0)
  196. {
  197. char *FileName = _strstrip(FileStart);
  198. if (FileName[0] == 0)
  199. continue;
  200. if (FileName[0] != '/')
  201. {
  202. if (FileName != FileStart)
  203. memmove(FileStart,FileName,strlen(FileStart));
  204. FileName = Line;
  205. }
  206. #if 0
  207. struct stat St;
  208. int Flag = FTW_F;
  209. if (stat(FileName,&St) != 0)
  210. Flag = FTW_NS;
  211. #endif
  212. if (ScannerFile(FileName, false) != 0)
  213. break;
  214. }
  215. fclose(List);
  216. return true;
  217. }
  218. /*}}}*/
  219. // FTWScanner::Delink - Delink symlinks /*{{{*/
  220. // ---------------------------------------------------------------------
  221. /* */
  222. bool FTWScanner::Delink(string &FileName,const char *OriginalPath,
  223. unsigned long long &DeLinkBytes,
  224. unsigned long long const &FileSize)
  225. {
  226. // See if this isn't an internaly prefix'd file name.
  227. if (InternalPrefix.empty() == false &&
  228. InternalPrefix.length() < FileName.length() &&
  229. stringcmp(FileName.begin(),FileName.begin() + InternalPrefix.length(),
  230. InternalPrefix.begin(),InternalPrefix.end()) != 0)
  231. {
  232. if (DeLinkLimit != 0 && DeLinkBytes/1024 < DeLinkLimit)
  233. {
  234. // Tidy up the display
  235. if (DeLinkBytes == 0)
  236. cout << endl;
  237. NewLine(1);
  238. ioprintf(c1out, _(" DeLink %s [%s]\n"), (OriginalPath + InternalPrefix.length()),
  239. SizeToStr(FileSize).c_str());
  240. c1out << flush;
  241. if (NoLinkAct == false)
  242. {
  243. char OldLink[400];
  244. if (readlink(OriginalPath,OldLink,sizeof(OldLink)) == -1)
  245. _error->Errno("readlink",_("Failed to readlink %s"),OriginalPath);
  246. else
  247. {
  248. if (unlink(OriginalPath) != 0)
  249. _error->Errno("unlink",_("Failed to unlink %s"),OriginalPath);
  250. else
  251. {
  252. if (link(FileName.c_str(),OriginalPath) != 0)
  253. {
  254. // Panic! Restore the symlink
  255. symlink(OldLink,OriginalPath);
  256. return _error->Errno("link",_("*** Failed to link %s to %s"),
  257. FileName.c_str(),
  258. OriginalPath);
  259. }
  260. }
  261. }
  262. }
  263. DeLinkBytes += FileSize;
  264. if (DeLinkBytes/1024 >= DeLinkLimit)
  265. ioprintf(c1out, _(" DeLink limit of %sB hit.\n"), SizeToStr(DeLinkBytes).c_str());
  266. }
  267. FileName = OriginalPath;
  268. }
  269. return true;
  270. }
  271. /*}}}*/
  272. // PackagesWriter::PackagesWriter - Constructor /*{{{*/
  273. // ---------------------------------------------------------------------
  274. /* */
  275. PackagesWriter::PackagesWriter(string const &DB,string const &Overrides,string const &ExtOverrides,
  276. string const &Arch) :
  277. FTWScanner(Arch), Db(DB), Stats(Db.Stats), TransWriter(NULL)
  278. {
  279. Output = stdout;
  280. SetExts(".deb .udeb");
  281. DeLinkLimit = 0;
  282. // Process the command line options
  283. DoMD5 = _config->FindB("APT::FTPArchive::Packages::MD5",DoMD5);
  284. DoSHA1 = _config->FindB("APT::FTPArchive::Packages::SHA1",DoSHA1);
  285. DoSHA256 = _config->FindB("APT::FTPArchive::Packages::SHA256",DoSHA256);
  286. DoSHA512 = _config->FindB("APT::FTPArchive::Packages::SHA512",DoSHA512);
  287. DoAlwaysStat = _config->FindB("APT::FTPArchive::AlwaysStat", false);
  288. DoContents = _config->FindB("APT::FTPArchive::Contents",true);
  289. NoOverride = _config->FindB("APT::FTPArchive::NoOverrideMsg",false);
  290. LongDescription = _config->FindB("APT::FTPArchive::LongDescription",true);
  291. if (Db.Loaded() == false)
  292. DoContents = false;
  293. // Read the override file
  294. if (Overrides.empty() == false && Over.ReadOverride(Overrides) == false)
  295. return;
  296. else
  297. NoOverride = true;
  298. if (ExtOverrides.empty() == false)
  299. Over.ReadExtraOverride(ExtOverrides);
  300. _error->DumpErrors();
  301. }
  302. /*}}}*/
  303. // FTWScanner::SetExts - Set extensions to support /*{{{*/
  304. // ---------------------------------------------------------------------
  305. /* */
  306. bool FTWScanner::SetExts(string const &Vals)
  307. {
  308. ClearPatterns();
  309. string::size_type Start = 0;
  310. while (Start <= Vals.length()-1)
  311. {
  312. string::size_type const Space = Vals.find(' ',Start);
  313. string::size_type const Length = ((Space == string::npos) ? Vals.length() : Space) - Start;
  314. if ( Arch.empty() == false )
  315. {
  316. AddPattern(string("*_") + Arch + Vals.substr(Start, Length));
  317. AddPattern(string("*_all") + Vals.substr(Start, Length));
  318. }
  319. else
  320. AddPattern(string("*") + Vals.substr(Start, Length));
  321. Start += Length + 1;
  322. }
  323. return true;
  324. }
  325. /*}}}*/
  326. // PackagesWriter::DoPackage - Process a single package /*{{{*/
  327. // ---------------------------------------------------------------------
  328. /* This method takes a package and gets its control information and
  329. MD5, SHA1 and SHA256 then writes out a control record with the proper fields
  330. rewritten and the path/size/hash appended. */
  331. bool PackagesWriter::DoPackage(string FileName)
  332. {
  333. // Pull all the data we need form the DB
  334. if (Db.GetFileInfo(FileName, true, DoContents, true, DoMD5, DoSHA1, DoSHA256, DoSHA512, DoAlwaysStat)
  335. == false)
  336. {
  337. return false;
  338. }
  339. unsigned long long FileSize = Db.GetFileSize();
  340. if (Delink(FileName,OriginalPath,Stats.DeLinkBytes,FileSize) == false)
  341. return false;
  342. // Lookup the overide information
  343. pkgTagSection &Tags = Db.Control.Section;
  344. string Package = Tags.FindS("Package");
  345. string Architecture;
  346. // if we generate a Packages file for a given arch, we use it to
  347. // look for overrides. if we run in "simple" mode without the
  348. // "Architecures" variable in the config we use the architecure value
  349. // from the deb file
  350. if(Arch != "")
  351. Architecture = Arch;
  352. else
  353. Architecture = Tags.FindS("Architecture");
  354. auto_ptr<Override::Item> OverItem(Over.GetItem(Package,Architecture));
  355. if (Package.empty() == true)
  356. return _error->Error(_("Archive had no package field"));
  357. // If we need to do any rewriting of the header do it now..
  358. if (OverItem.get() == 0)
  359. {
  360. if (NoOverride == false)
  361. {
  362. NewLine(1);
  363. ioprintf(c1out, _(" %s has no override entry\n"), Package.c_str());
  364. }
  365. OverItem = auto_ptr<Override::Item>(new Override::Item);
  366. OverItem->FieldOverride["Section"] = Tags.FindS("Section");
  367. OverItem->Priority = Tags.FindS("Priority");
  368. }
  369. char Size[40];
  370. sprintf(Size,"%llu", (unsigned long long) FileSize);
  371. // Strip the DirStrip prefix from the FileName and add the PathPrefix
  372. string NewFileName;
  373. if (DirStrip.empty() == false &&
  374. FileName.length() > DirStrip.length() &&
  375. stringcmp(FileName.begin(),FileName.begin() + DirStrip.length(),
  376. DirStrip.begin(),DirStrip.end()) == 0)
  377. NewFileName = string(FileName.begin() + DirStrip.length(),FileName.end());
  378. else
  379. NewFileName = FileName;
  380. if (PathPrefix.empty() == false)
  381. NewFileName = flCombine(PathPrefix,NewFileName);
  382. /* Configuration says we don't want to include the long Description
  383. in the package file - instead we want to ship a separated file */
  384. string desc;
  385. if (LongDescription == false) {
  386. desc = Tags.FindS("Description").append("\n");
  387. OverItem->FieldOverride["Description"] = desc.substr(0, desc.find('\n')).c_str();
  388. }
  389. // This lists all the changes to the fields we are going to make.
  390. // (7 hardcoded + maintainer + suggests + end marker)
  391. TFRewriteData Changes[6+2+OverItem->FieldOverride.size()+1+1];
  392. unsigned int End = 0;
  393. SetTFRewriteData(Changes[End++], "Size", Size);
  394. if (DoMD5 == true)
  395. SetTFRewriteData(Changes[End++], "MD5sum", Db.MD5Res.c_str());
  396. if (DoSHA1 == true)
  397. SetTFRewriteData(Changes[End++], "SHA1", Db.SHA1Res.c_str());
  398. if (DoSHA256 == true)
  399. SetTFRewriteData(Changes[End++], "SHA256", Db.SHA256Res.c_str());
  400. if (DoSHA512 == true)
  401. SetTFRewriteData(Changes[End++], "SHA512", Db.SHA512Res.c_str());
  402. SetTFRewriteData(Changes[End++], "Filename", NewFileName.c_str());
  403. SetTFRewriteData(Changes[End++], "Priority", OverItem->Priority.c_str());
  404. SetTFRewriteData(Changes[End++], "Status", 0);
  405. SetTFRewriteData(Changes[End++], "Optional", 0);
  406. string DescriptionMd5;
  407. if (LongDescription == false) {
  408. MD5Summation descmd5;
  409. descmd5.Add(desc.c_str());
  410. DescriptionMd5 = descmd5.Result().Value();
  411. SetTFRewriteData(Changes[End++], "Description-md5", DescriptionMd5.c_str());
  412. if (TransWriter != NULL)
  413. TransWriter->DoPackage(Package, desc, DescriptionMd5);
  414. }
  415. // Rewrite the maintainer field if necessary
  416. bool MaintFailed;
  417. string NewMaint = OverItem->SwapMaint(Tags.FindS("Maintainer"),MaintFailed);
  418. if (MaintFailed == true)
  419. {
  420. if (NoOverride == false)
  421. {
  422. NewLine(1);
  423. ioprintf(c1out, _(" %s maintainer is %s not %s\n"),
  424. Package.c_str(), Tags.FindS("Maintainer").c_str(), OverItem->OldMaint.c_str());
  425. }
  426. }
  427. if (NewMaint.empty() == false)
  428. SetTFRewriteData(Changes[End++], "Maintainer", NewMaint.c_str());
  429. /* Get rid of the Optional tag. This is an ugly, ugly, ugly hack that
  430. dpkg-scanpackages does. Well sort of. dpkg-scanpackages just does renaming
  431. but dpkg does this append bit. So we do the append bit, at least that way the
  432. status file and package file will remain similar. There are other transforms
  433. but optional is the only legacy one still in use for some lazy reason. */
  434. string OptionalStr = Tags.FindS("Optional");
  435. if (OptionalStr.empty() == false)
  436. {
  437. if (Tags.FindS("Suggests").empty() == false)
  438. OptionalStr = Tags.FindS("Suggests") + ", " + OptionalStr;
  439. SetTFRewriteData(Changes[End++], "Suggests", OptionalStr.c_str());
  440. }
  441. for (map<string,string>::const_iterator I = OverItem->FieldOverride.begin();
  442. I != OverItem->FieldOverride.end(); ++I)
  443. SetTFRewriteData(Changes[End++],I->first.c_str(),I->second.c_str());
  444. SetTFRewriteData(Changes[End++], 0, 0);
  445. // Rewrite and store the fields.
  446. if (TFRewrite(Output,Tags,TFRewritePackageOrder,Changes) == false)
  447. return false;
  448. fprintf(Output,"\n");
  449. return Db.Finish();
  450. }
  451. /*}}}*/
  452. // TranslationWriter::TranslationWriter - Constructor /*{{{*/
  453. // ---------------------------------------------------------------------
  454. /* Create a Translation-Master file for this Packages file */
  455. TranslationWriter::TranslationWriter(string const &File, string const &TransCompress,
  456. mode_t const &Permissions) : Output(NULL),
  457. RefCounter(0)
  458. {
  459. if (File.empty() == true)
  460. return;
  461. Comp = new MultiCompress(File, TransCompress, Permissions);
  462. Output = Comp->Input;
  463. }
  464. /*}}}*/
  465. // TranslationWriter::DoPackage - Process a single package /*{{{*/
  466. // ---------------------------------------------------------------------
  467. /* Create a Translation-Master file for this Packages file */
  468. bool TranslationWriter::DoPackage(string const &Pkg, string const &Desc,
  469. string const &MD5)
  470. {
  471. if (Output == NULL)
  472. return true;
  473. // Different archs can include different versions and therefore
  474. // different descriptions - so we need to check for both name and md5.
  475. string const Record = Pkg + ":" + MD5;
  476. if (Included.find(Record) != Included.end())
  477. return true;
  478. fprintf(Output, "Package: %s\nDescription-md5: %s\nDescription-en: %s\n",
  479. Pkg.c_str(), MD5.c_str(), Desc.c_str());
  480. Included.insert(Record);
  481. return true;
  482. }
  483. /*}}}*/
  484. // TranslationWriter::~TranslationWriter - Destructor /*{{{*/
  485. // ---------------------------------------------------------------------
  486. /* */
  487. TranslationWriter::~TranslationWriter()
  488. {
  489. if (Comp == NULL)
  490. return;
  491. delete Comp;
  492. }
  493. /*}}}*/
  494. // SourcesWriter::SourcesWriter - Constructor /*{{{*/
  495. // ---------------------------------------------------------------------
  496. /* */
  497. SourcesWriter::SourcesWriter(string const &DB, string const &BOverrides,string const &SOverrides,
  498. string const &ExtOverrides) :
  499. Db(DB), Stats(Db.Stats)
  500. {
  501. Output = stdout;
  502. AddPattern("*.dsc");
  503. DeLinkLimit = 0;
  504. Buffer = 0;
  505. BufSize = 0;
  506. // Process the command line options
  507. DoMD5 = _config->FindB("APT::FTPArchive::Sources::MD5",DoMD5);
  508. DoSHA1 = _config->FindB("APT::FTPArchive::Sources::SHA1",DoSHA1);
  509. DoSHA256 = _config->FindB("APT::FTPArchive::Sources::SHA256",DoSHA256);
  510. DoSHA512 = _config->FindB("APT::FTPArchive::Sources::SHA512",DoSHA512);
  511. NoOverride = _config->FindB("APT::FTPArchive::NoOverrideMsg",false);
  512. DoAlwaysStat = _config->FindB("APT::FTPArchive::AlwaysStat", false);
  513. // Read the override file
  514. if (BOverrides.empty() == false && BOver.ReadOverride(BOverrides) == false)
  515. return;
  516. else
  517. NoOverride = true;
  518. // WTF?? The logic above: if we can't read binary overrides, don't even try
  519. // reading source overrides. if we can read binary overrides, then say there
  520. // are no overrides. THIS MAKES NO SENSE! -- ajt@d.o, 2006/02/28
  521. if (ExtOverrides.empty() == false)
  522. SOver.ReadExtraOverride(ExtOverrides);
  523. if (SOverrides.empty() == false && FileExists(SOverrides) == true)
  524. SOver.ReadOverride(SOverrides,true);
  525. }
  526. /*}}}*/
  527. // SourcesWriter::DoPackage - Process a single package /*{{{*/
  528. // ---------------------------------------------------------------------
  529. /* */
  530. bool SourcesWriter::DoPackage(string FileName)
  531. {
  532. // Open the archive
  533. FileFd F(FileName,FileFd::ReadOnly);
  534. if (_error->PendingError() == true)
  535. return false;
  536. // Stat the file for later
  537. struct stat St;
  538. if (fstat(F.Fd(),&St) != 0)
  539. return _error->Errno("fstat","Failed to stat %s",FileName.c_str());
  540. if (St.st_size > 128*1024)
  541. return _error->Error("DSC file '%s' is too large!",FileName.c_str());
  542. if (BufSize < (unsigned long long)St.st_size+1)
  543. {
  544. BufSize = St.st_size+1;
  545. Buffer = (char *)realloc(Buffer,St.st_size+1);
  546. }
  547. if (F.Read(Buffer,St.st_size) == false)
  548. return false;
  549. // Hash the file
  550. char *Start = Buffer;
  551. char *BlkEnd = Buffer + St.st_size;
  552. MD5Summation MD5;
  553. SHA1Summation SHA1;
  554. SHA256Summation SHA256;
  555. SHA256Summation SHA512;
  556. if (DoMD5 == true)
  557. MD5.Add((unsigned char *)Start,BlkEnd - Start);
  558. if (DoSHA1 == true)
  559. SHA1.Add((unsigned char *)Start,BlkEnd - Start);
  560. if (DoSHA256 == true)
  561. SHA256.Add((unsigned char *)Start,BlkEnd - Start);
  562. if (DoSHA512 == true)
  563. SHA512.Add((unsigned char *)Start,BlkEnd - Start);
  564. // Add an extra \n to the end, just in case
  565. *BlkEnd++ = '\n';
  566. /* Remove the PGP trailer. Some .dsc's have this without a blank line
  567. before */
  568. const char *Key = "-----BEGIN PGP SIGNATURE-----";
  569. for (char *MsgEnd = Start; MsgEnd < BlkEnd - strlen(Key) -1; MsgEnd++)
  570. {
  571. if (*MsgEnd == '\n' && strncmp(MsgEnd+1,Key,strlen(Key)) == 0)
  572. {
  573. MsgEnd[1] = '\n';
  574. break;
  575. }
  576. }
  577. /* Read records until we locate the Source record. This neatly skips the
  578. GPG header (which is RFC822 formed) without any trouble. */
  579. pkgTagSection Tags;
  580. do
  581. {
  582. unsigned Pos;
  583. if (Tags.Scan(Start,BlkEnd - Start) == false)
  584. return _error->Error("Could not find a record in the DSC '%s'",FileName.c_str());
  585. if (Tags.Find("Source",Pos) == true)
  586. break;
  587. Start += Tags.size();
  588. }
  589. while (1);
  590. Tags.Trim();
  591. // Lookup the overide information, finding first the best priority.
  592. string BestPrio;
  593. string Bins = Tags.FindS("Binary");
  594. char Buffer[Bins.length() + 1];
  595. auto_ptr<Override::Item> OverItem(0);
  596. if (Bins.empty() == false)
  597. {
  598. strcpy(Buffer,Bins.c_str());
  599. // Ignore too-long errors.
  600. char *BinList[400];
  601. TokSplitString(',',Buffer,BinList,sizeof(BinList)/sizeof(BinList[0]));
  602. // Look at all the binaries
  603. unsigned char BestPrioV = pkgCache::State::Extra;
  604. for (unsigned I = 0; BinList[I] != 0; I++)
  605. {
  606. auto_ptr<Override::Item> Itm(BOver.GetItem(BinList[I]));
  607. if (Itm.get() == 0)
  608. continue;
  609. unsigned char NewPrioV = debListParser::GetPrio(Itm->Priority);
  610. if (NewPrioV < BestPrioV || BestPrio.empty() == true)
  611. {
  612. BestPrioV = NewPrioV;
  613. BestPrio = Itm->Priority;
  614. }
  615. if (OverItem.get() == 0)
  616. OverItem = Itm;
  617. }
  618. }
  619. // If we need to do any rewriting of the header do it now..
  620. if (OverItem.get() == 0)
  621. {
  622. if (NoOverride == false)
  623. {
  624. NewLine(1);
  625. ioprintf(c1out, _(" %s has no override entry\n"), Tags.FindS("Source").c_str());
  626. }
  627. OverItem = auto_ptr<Override::Item>(new Override::Item);
  628. }
  629. auto_ptr<Override::Item> SOverItem(SOver.GetItem(Tags.FindS("Source")));
  630. // const auto_ptr<Override::Item> autoSOverItem(SOverItem);
  631. if (SOverItem.get() == 0)
  632. {
  633. ioprintf(c1out, _(" %s has no source override entry\n"), Tags.FindS("Source").c_str());
  634. SOverItem = auto_ptr<Override::Item>(BOver.GetItem(Tags.FindS("Source")));
  635. if (SOverItem.get() == 0)
  636. {
  637. ioprintf(c1out, _(" %s has no binary override entry either\n"), Tags.FindS("Source").c_str());
  638. SOverItem = auto_ptr<Override::Item>(new Override::Item);
  639. *SOverItem = *OverItem;
  640. }
  641. }
  642. // Add the dsc to the files hash list
  643. string const strippedName = flNotDir(FileName);
  644. std::ostringstream ostreamFiles;
  645. if (DoMD5 == true && Tags.Exists("Files"))
  646. ostreamFiles << "\n " << string(MD5.Result()) << " " << St.st_size << " "
  647. << strippedName << "\n " << Tags.FindS("Files");
  648. string const Files = ostreamFiles.str();
  649. std::ostringstream ostreamSha1;
  650. if (DoSHA1 == true && Tags.Exists("Checksums-Sha1"))
  651. ostreamSha1 << "\n " << string(SHA1.Result()) << " " << St.st_size << " "
  652. << strippedName << "\n " << Tags.FindS("Checksums-Sha1");
  653. std::ostringstream ostreamSha256;
  654. if (DoSHA256 == true && Tags.Exists("Checksums-Sha256"))
  655. ostreamSha256 << "\n " << string(SHA256.Result()) << " " << St.st_size << " "
  656. << strippedName << "\n " << Tags.FindS("Checksums-Sha256");
  657. std::ostringstream ostreamSha512;
  658. if (DoSHA512 == true && Tags.Exists("Checksums-Sha512"))
  659. ostreamSha512 << "\n " << string(SHA512.Result()) << " " << St.st_size << " "
  660. << strippedName << "\n " << Tags.FindS("Checksums-Sha512");
  661. // Strip the DirStrip prefix from the FileName and add the PathPrefix
  662. string NewFileName;
  663. if (DirStrip.empty() == false &&
  664. FileName.length() > DirStrip.length() &&
  665. stringcmp(DirStrip,OriginalPath,OriginalPath + DirStrip.length()) == 0)
  666. NewFileName = string(OriginalPath + DirStrip.length());
  667. else
  668. NewFileName = OriginalPath;
  669. if (PathPrefix.empty() == false)
  670. NewFileName = flCombine(PathPrefix,NewFileName);
  671. string Directory = flNotFile(OriginalPath);
  672. string Package = Tags.FindS("Source");
  673. // Perform operation over all of the files
  674. string ParseJnk;
  675. const char *C = Files.c_str();
  676. char *RealPath = NULL;
  677. for (;isspace(*C); C++);
  678. while (*C != 0)
  679. {
  680. // Parse each of the elements
  681. if (ParseQuoteWord(C,ParseJnk) == false ||
  682. ParseQuoteWord(C,ParseJnk) == false ||
  683. ParseQuoteWord(C,ParseJnk) == false)
  684. return _error->Error("Error parsing file record");
  685. string OriginalPath = Directory + ParseJnk;
  686. // Add missing hashes to source files
  687. if ((DoSHA1 == true && !Tags.Exists("Checksums-Sha1")) ||
  688. (DoSHA256 == true && !Tags.Exists("Checksums-Sha256")) ||
  689. (DoSHA512 == true && !Tags.Exists("Checksums-Sha512")))
  690. {
  691. if (Db.GetFileInfo(OriginalPath, false, false, false, DoMD5, DoSHA1, DoSHA256, DoSHA512, DoAlwaysStat)
  692. == false)
  693. {
  694. return _error->Error("Error getting file info");
  695. }
  696. if (DoSHA1 == true && !Tags.Exists("Checksums-Sha1"))
  697. ostreamSha1 << "\n " << string(Db.SHA1Res) << " "
  698. << Db.GetFileSize() << " " << ParseJnk;
  699. if (DoSHA256 == true && !Tags.Exists("Checksums-Sha256"))
  700. ostreamSha256 << "\n " << string(Db.SHA256Res) << " "
  701. << Db.GetFileSize() << " " << ParseJnk;
  702. if (DoSHA512 == true && !Tags.Exists("Checksums-Sha512"))
  703. ostreamSha512 << "\n " << string(Db.SHA512Res) << " "
  704. << Db.GetFileSize() << " " << ParseJnk;
  705. }
  706. // Perform the delinking operation
  707. char Jnk[2];
  708. if (readlink(OriginalPath.c_str(),Jnk,sizeof(Jnk)) != -1 &&
  709. (RealPath = realpath(OriginalPath.c_str(),NULL)) != 0)
  710. {
  711. string RP = RealPath;
  712. free(RealPath);
  713. if (Delink(RP,OriginalPath.c_str(),Stats.DeLinkBytes,St.st_size) == false)
  714. return false;
  715. }
  716. }
  717. Directory = flNotFile(NewFileName);
  718. if (Directory.length() > 2)
  719. Directory.erase(Directory.end()-1);
  720. string const ChecksumsSha1 = ostreamSha1.str();
  721. string const ChecksumsSha256 = ostreamSha256.str();
  722. string const ChecksumsSha512 = ostreamSha512.str();
  723. // This lists all the changes to the fields we are going to make.
  724. // (5 hardcoded + checksums + maintainer + end marker)
  725. TFRewriteData Changes[5+2+1+SOverItem->FieldOverride.size()+1];
  726. unsigned int End = 0;
  727. SetTFRewriteData(Changes[End++],"Source",Package.c_str(),"Package");
  728. if (Files.empty() == false)
  729. SetTFRewriteData(Changes[End++],"Files",Files.c_str());
  730. if (ChecksumsSha1.empty() == false)
  731. SetTFRewriteData(Changes[End++],"Checksums-Sha1",ChecksumsSha1.c_str());
  732. if (ChecksumsSha256.empty() == false)
  733. SetTFRewriteData(Changes[End++],"Checksums-Sha256",ChecksumsSha256.c_str());
  734. if (ChecksumsSha512.empty() == false)
  735. SetTFRewriteData(Changes[End++],"Checksums-Sha512",ChecksumsSha512.c_str());
  736. if (Directory != "./")
  737. SetTFRewriteData(Changes[End++],"Directory",Directory.c_str());
  738. SetTFRewriteData(Changes[End++],"Priority",BestPrio.c_str());
  739. SetTFRewriteData(Changes[End++],"Status",0);
  740. // Rewrite the maintainer field if necessary
  741. bool MaintFailed;
  742. string NewMaint = OverItem->SwapMaint(Tags.FindS("Maintainer"),MaintFailed);
  743. if (MaintFailed == true)
  744. {
  745. if (NoOverride == false)
  746. {
  747. NewLine(1);
  748. ioprintf(c1out, _(" %s maintainer is %s not %s\n"), Package.c_str(),
  749. Tags.FindS("Maintainer").c_str(), OverItem->OldMaint.c_str());
  750. }
  751. }
  752. if (NewMaint.empty() == false)
  753. SetTFRewriteData(Changes[End++], "Maintainer", NewMaint.c_str());
  754. for (map<string,string>::const_iterator I = SOverItem->FieldOverride.begin();
  755. I != SOverItem->FieldOverride.end(); ++I)
  756. SetTFRewriteData(Changes[End++],I->first.c_str(),I->second.c_str());
  757. SetTFRewriteData(Changes[End++], 0, 0);
  758. // Rewrite and store the fields.
  759. if (TFRewrite(Output,Tags,TFRewriteSourceOrder,Changes) == false)
  760. return false;
  761. fprintf(Output,"\n");
  762. Stats.Packages++;
  763. return Db.Finish();
  764. }
  765. /*}}}*/
  766. // ContentsWriter::ContentsWriter - Constructor /*{{{*/
  767. // ---------------------------------------------------------------------
  768. /* */
  769. ContentsWriter::ContentsWriter(string const &DB, string const &Arch) :
  770. FTWScanner(Arch), Db(DB), Stats(Db.Stats)
  771. {
  772. SetExts(".deb");
  773. Output = stdout;
  774. }
  775. /*}}}*/
  776. // ContentsWriter::DoPackage - Process a single package /*{{{*/
  777. // ---------------------------------------------------------------------
  778. /* If Package is the empty string the control record will be parsed to
  779. determine what the package name is. */
  780. bool ContentsWriter::DoPackage(string FileName, string Package)
  781. {
  782. if (!Db.GetFileInfo(FileName, Package.empty(), true, false, false, false, false, false))
  783. {
  784. return false;
  785. }
  786. // Parse the package name
  787. if (Package.empty() == true)
  788. {
  789. Package = Db.Control.Section.FindS("Package");
  790. }
  791. Db.Contents.Add(Gen,Package);
  792. return Db.Finish();
  793. }
  794. /*}}}*/
  795. // ContentsWriter::ReadFromPkgs - Read from a packages file /*{{{*/
  796. // ---------------------------------------------------------------------
  797. /* */
  798. bool ContentsWriter::ReadFromPkgs(string const &PkgFile,string const &PkgCompress)
  799. {
  800. MultiCompress Pkgs(PkgFile,PkgCompress,0,false);
  801. if (_error->PendingError() == true)
  802. return false;
  803. // Open the package file
  804. FileFd Fd;
  805. if (Pkgs.OpenOld(Fd) == false)
  806. return false;
  807. pkgTagFile Tags(&Fd);
  808. if (_error->PendingError() == true)
  809. return false;
  810. // Parse.
  811. pkgTagSection Section;
  812. while (Tags.Step(Section) == true)
  813. {
  814. string File = flCombine(Prefix,Section.FindS("FileName"));
  815. string Package = Section.FindS("Section");
  816. if (Package.empty() == false && Package.end()[-1] != '/')
  817. {
  818. Package += '/';
  819. Package += Section.FindS("Package");
  820. }
  821. else
  822. Package += Section.FindS("Package");
  823. DoPackage(File,Package);
  824. if (_error->empty() == false)
  825. {
  826. _error->Error("Errors apply to file '%s'",File.c_str());
  827. _error->DumpErrors();
  828. }
  829. }
  830. // Tidy the compressor
  831. Fd.Close();
  832. return true;
  833. }
  834. /*}}}*/
  835. // ReleaseWriter::ReleaseWriter - Constructor /*{{{*/
  836. // ---------------------------------------------------------------------
  837. /* */
  838. ReleaseWriter::ReleaseWriter(string const &DB)
  839. {
  840. if (_config->FindB("APT::FTPArchive::Release::Default-Patterns", true) == true)
  841. {
  842. AddPattern("Packages");
  843. AddPattern("Packages.gz");
  844. AddPattern("Packages.bz2");
  845. AddPattern("Packages.lzma");
  846. AddPattern("Packages.xz");
  847. AddPattern("Translation-*");
  848. AddPattern("Sources");
  849. AddPattern("Sources.gz");
  850. AddPattern("Sources.bz2");
  851. AddPattern("Sources.lzma");
  852. AddPattern("Sources.xz");
  853. AddPattern("Release");
  854. AddPattern("Contents-*");
  855. AddPattern("Index");
  856. AddPattern("md5sum.txt");
  857. }
  858. AddPatterns(_config->FindVector("APT::FTPArchive::Release::Patterns"));
  859. Output = stdout;
  860. time_t const now = time(NULL);
  861. setlocale(LC_TIME, "C");
  862. char datestr[128];
  863. if (strftime(datestr, sizeof(datestr), "%a, %d %b %Y %H:%M:%S UTC",
  864. gmtime(&now)) == 0)
  865. {
  866. datestr[0] = '\0';
  867. }
  868. time_t const validuntil = now + _config->FindI("APT::FTPArchive::Release::ValidTime", 0);
  869. char validstr[128];
  870. if (now == validuntil ||
  871. strftime(validstr, sizeof(validstr), "%a, %d %b %Y %H:%M:%S UTC",
  872. gmtime(&validuntil)) == 0)
  873. {
  874. validstr[0] = '\0';
  875. }
  876. setlocale(LC_TIME, "");
  877. map<string,string> Fields;
  878. Fields["Origin"] = "";
  879. Fields["Label"] = "";
  880. Fields["Suite"] = "";
  881. Fields["Version"] = "";
  882. Fields["Codename"] = "";
  883. Fields["Date"] = datestr;
  884. Fields["Valid-Until"] = validstr;
  885. Fields["Architectures"] = "";
  886. Fields["Components"] = "";
  887. Fields["Description"] = "";
  888. for(map<string,string>::const_iterator I = Fields.begin();
  889. I != Fields.end();
  890. ++I)
  891. {
  892. string Config = string("APT::FTPArchive::Release::") + (*I).first;
  893. string Value = _config->Find(Config, (*I).second.c_str());
  894. if (Value == "")
  895. continue;
  896. fprintf(Output, "%s: %s\n", (*I).first.c_str(), Value.c_str());
  897. }
  898. DoMD5 = _config->FindB("APT::FTPArchive::Release::MD5",DoMD5);
  899. DoSHA1 = _config->FindB("APT::FTPArchive::Release::SHA1",DoSHA1);
  900. DoSHA256 = _config->FindB("APT::FTPArchive::Release::SHA256",DoSHA256);
  901. }
  902. /*}}}*/
  903. // ReleaseWriter::DoPackage - Process a single package /*{{{*/
  904. // ---------------------------------------------------------------------
  905. bool ReleaseWriter::DoPackage(string FileName)
  906. {
  907. // Strip the DirStrip prefix from the FileName and add the PathPrefix
  908. string NewFileName;
  909. if (DirStrip.empty() == false &&
  910. FileName.length() > DirStrip.length() &&
  911. stringcmp(FileName.begin(),FileName.begin() + DirStrip.length(),
  912. DirStrip.begin(),DirStrip.end()) == 0)
  913. {
  914. NewFileName = string(FileName.begin() + DirStrip.length(),FileName.end());
  915. while (NewFileName[0] == '/')
  916. NewFileName = string(NewFileName.begin() + 1,NewFileName.end());
  917. }
  918. else
  919. NewFileName = FileName;
  920. if (PathPrefix.empty() == false)
  921. NewFileName = flCombine(PathPrefix,NewFileName);
  922. FileFd fd(FileName, FileFd::ReadOnly);
  923. if (!fd.IsOpen())
  924. {
  925. return false;
  926. }
  927. CheckSums[NewFileName].size = fd.Size();
  928. Hashes hs;
  929. hs.AddFD(fd, 0, DoMD5, DoSHA1, DoSHA256, DoSHA512);
  930. if (DoMD5 == true)
  931. CheckSums[NewFileName].MD5 = hs.MD5.Result();
  932. if (DoSHA1 == true)
  933. CheckSums[NewFileName].SHA1 = hs.SHA1.Result();
  934. if (DoSHA256 == true)
  935. CheckSums[NewFileName].SHA256 = hs.SHA256.Result();
  936. if (DoSHA512 == true)
  937. CheckSums[NewFileName].SHA512 = hs.SHA512.Result();
  938. fd.Close();
  939. return true;
  940. }
  941. /*}}}*/
  942. // ReleaseWriter::Finish - Output the checksums /*{{{*/
  943. // ---------------------------------------------------------------------
  944. void ReleaseWriter::Finish()
  945. {
  946. if (DoMD5 == true)
  947. {
  948. fprintf(Output, "MD5Sum:\n");
  949. for(map<string,struct CheckSum>::const_iterator I = CheckSums.begin();
  950. I != CheckSums.end(); ++I)
  951. {
  952. fprintf(Output, " %s %16llu %s\n",
  953. (*I).second.MD5.c_str(),
  954. (*I).second.size,
  955. (*I).first.c_str());
  956. }
  957. }
  958. if (DoSHA1 == true)
  959. {
  960. fprintf(Output, "SHA1:\n");
  961. for(map<string,struct CheckSum>::const_iterator I = CheckSums.begin();
  962. I != CheckSums.end(); ++I)
  963. {
  964. fprintf(Output, " %s %16llu %s\n",
  965. (*I).second.SHA1.c_str(),
  966. (*I).second.size,
  967. (*I).first.c_str());
  968. }
  969. }
  970. if (DoSHA256 == true)
  971. {
  972. fprintf(Output, "SHA256:\n");
  973. for(map<string,struct CheckSum>::const_iterator I = CheckSums.begin();
  974. I != CheckSums.end(); ++I)
  975. {
  976. fprintf(Output, " %s %16llu %s\n",
  977. (*I).second.SHA256.c_str(),
  978. (*I).second.size,
  979. (*I).first.c_str());
  980. }
  981. }
  982. fprintf(Output, "SHA512:\n");
  983. for(map<string,struct CheckSum>::const_iterator I = CheckSums.begin();
  984. I != CheckSums.end();
  985. ++I)
  986. {
  987. fprintf(Output, " %s %16llu %s\n",
  988. (*I).second.SHA512.c_str(),
  989. (*I).second.size,
  990. (*I).first.c_str());
  991. }
  992. }