writer.cc 30 KB

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