writer.cc 29 KB

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