writer.cc 33 KB

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