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

945 lines
28 KiB

  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: writer.cc,v 1.14 2004/03/24 01:40:43 mdz Exp $
  4. /* ######################################################################
  5. Writer
  6. The file writer classes. These write various types of output, sources,
  7. packages and contents.
  8. ##################################################################### */
  9. /*}}}*/
  10. // Include Files /*{{{*/
  11. #include "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. if (Db.Loaded() == false)
  280. DoContents = false;
  281. // Read the override file
  282. if (Overrides.empty() == false && Over.ReadOverride(Overrides) == false)
  283. return;
  284. else
  285. NoOverride = true;
  286. if (ExtOverrides.empty() == false)
  287. Over.ReadExtraOverride(ExtOverrides);
  288. _error->DumpErrors();
  289. }
  290. /*}}}*/
  291. // FTWScanner::SetExts - Set extensions to support /*{{{*/
  292. // ---------------------------------------------------------------------
  293. /* */
  294. bool FTWScanner::SetExts(string Vals)
  295. {
  296. ClearPatterns();
  297. string::size_type Start = 0;
  298. while (Start <= Vals.length()-1)
  299. {
  300. string::size_type Space = Vals.find(' ',Start);
  301. string::size_type Length;
  302. if (Space == string::npos)
  303. {
  304. Length = Vals.length()-Start;
  305. }
  306. else
  307. {
  308. Length = Space-Start;
  309. }
  310. AddPattern(string("*") + Vals.substr(Start, Length));
  311. Start += Length + 1;
  312. }
  313. return true;
  314. }
  315. /*}}}*/
  316. // PackagesWriter::DoPackage - Process a single package /*{{{*/
  317. // ---------------------------------------------------------------------
  318. /* This method takes a package and gets its control information and
  319. MD5, SHA1 and SHA256 then writes out a control record with the proper fields
  320. rewritten and the path/size/hash appended. */
  321. bool PackagesWriter::DoPackage(string FileName)
  322. {
  323. // Pull all the data we need form the DB
  324. if (Db.GetFileInfo(FileName, true, DoContents, true, DoMD5, DoSHA1, DoSHA256)
  325. == false)
  326. {
  327. return false;
  328. }
  329. off_t FileSize = Db.GetFileSize();
  330. if (Delink(FileName,OriginalPath,Stats.DeLinkBytes,FileSize) == false)
  331. return false;
  332. // Lookup the overide information
  333. pkgTagSection &Tags = Db.Control.Section;
  334. string Package = Tags.FindS("Package");
  335. string Architecture;
  336. // if we generate a Packages file for a given arch, we use it to
  337. // look for overrides. if we run in "simple" mode without the
  338. // "Architecures" variable in the config we use the architecure value
  339. // from the deb file
  340. if(Arch != "")
  341. Architecture = Arch;
  342. else
  343. Architecture = Tags.FindS("Architecture");
  344. auto_ptr<Override::Item> OverItem(Over.GetItem(Package,Architecture));
  345. if (Package.empty() == true)
  346. return _error->Error(_("Archive had no package field"));
  347. // If we need to do any rewriting of the header do it now..
  348. if (OverItem.get() == 0)
  349. {
  350. if (NoOverride == false)
  351. {
  352. NewLine(1);
  353. ioprintf(c1out, _(" %s has no override entry\n"), Package.c_str());
  354. }
  355. OverItem = auto_ptr<Override::Item>(new Override::Item);
  356. OverItem->FieldOverride["Section"] = Tags.FindS("Section");
  357. OverItem->Priority = Tags.FindS("Priority");
  358. }
  359. char Size[40];
  360. sprintf(Size,"%lu", (unsigned long) FileSize);
  361. // Strip the DirStrip prefix from the FileName and add the PathPrefix
  362. string NewFileName;
  363. if (DirStrip.empty() == false &&
  364. FileName.length() > DirStrip.length() &&
  365. stringcmp(FileName.begin(),FileName.begin() + DirStrip.length(),
  366. DirStrip.begin(),DirStrip.end()) == 0)
  367. NewFileName = string(FileName.begin() + DirStrip.length(),FileName.end());
  368. else
  369. NewFileName = FileName;
  370. if (PathPrefix.empty() == false)
  371. NewFileName = flCombine(PathPrefix,NewFileName);
  372. // This lists all the changes to the fields we are going to make.
  373. // (7 hardcoded + maintainer + suggests + end marker)
  374. TFRewriteData Changes[6+2+OverItem->FieldOverride.size()+1];
  375. unsigned int End = 0;
  376. SetTFRewriteData(Changes[End++], "Size", Size);
  377. SetTFRewriteData(Changes[End++], "MD5sum", Db.MD5Res.c_str());
  378. SetTFRewriteData(Changes[End++], "SHA1", Db.SHA1Res.c_str());
  379. SetTFRewriteData(Changes[End++], "SHA256", Db.SHA256Res.c_str());
  380. SetTFRewriteData(Changes[End++], "Filename", NewFileName.c_str());
  381. SetTFRewriteData(Changes[End++], "Priority", OverItem->Priority.c_str());
  382. SetTFRewriteData(Changes[End++], "Status", 0);
  383. SetTFRewriteData(Changes[End++], "Optional", 0);
  384. // Rewrite the maintainer field if necessary
  385. bool MaintFailed;
  386. string NewMaint = OverItem->SwapMaint(Tags.FindS("Maintainer"),MaintFailed);
  387. if (MaintFailed == true)
  388. {
  389. if (NoOverride == false)
  390. {
  391. NewLine(1);
  392. ioprintf(c1out, _(" %s maintainer is %s not %s\n"),
  393. Package.c_str(), Tags.FindS("Maintainer").c_str(), OverItem->OldMaint.c_str());
  394. }
  395. }
  396. if (NewMaint.empty() == false)
  397. SetTFRewriteData(Changes[End++], "Maintainer", NewMaint.c_str());
  398. /* Get rid of the Optional tag. This is an ugly, ugly, ugly hack that
  399. dpkg-scanpackages does.. Well sort of. dpkg-scanpackages just does renaming
  400. but dpkg does this append bit. So we do the append bit, at least that way the
  401. status file and package file will remain similar. There are other transforms
  402. but optional is the only legacy one still in use for some lazy reason. */
  403. string OptionalStr = Tags.FindS("Optional");
  404. if (OptionalStr.empty() == false)
  405. {
  406. if (Tags.FindS("Suggests").empty() == false)
  407. OptionalStr = Tags.FindS("Suggests") + ", " + OptionalStr;
  408. SetTFRewriteData(Changes[End++], "Suggests", OptionalStr.c_str());
  409. }
  410. for (map<string,string>::iterator I = OverItem->FieldOverride.begin();
  411. I != OverItem->FieldOverride.end(); I++)
  412. SetTFRewriteData(Changes[End++],I->first.c_str(),I->second.c_str());
  413. SetTFRewriteData(Changes[End++], 0, 0);
  414. // Rewrite and store the fields.
  415. if (TFRewrite(Output,Tags,TFRewritePackageOrder,Changes) == false)
  416. return false;
  417. fprintf(Output,"\n");
  418. return Db.Finish();
  419. }
  420. /*}}}*/
  421. // SourcesWriter::SourcesWriter - Constructor /*{{{*/
  422. // ---------------------------------------------------------------------
  423. /* */
  424. SourcesWriter::SourcesWriter(string BOverrides,string SOverrides,
  425. string ExtOverrides)
  426. {
  427. Output = stdout;
  428. AddPattern("*.dsc");
  429. DeLinkLimit = 0;
  430. Buffer = 0;
  431. BufSize = 0;
  432. // Process the command line options
  433. NoOverride = _config->FindB("APT::FTPArchive::NoOverrideMsg",false);
  434. // Read the override file
  435. if (BOverrides.empty() == false && BOver.ReadOverride(BOverrides) == false)
  436. return;
  437. else
  438. NoOverride = true;
  439. // WTF?? The logic above: if we can't read binary overrides, don't even try
  440. // reading source overrides. if we can read binary overrides, then say there
  441. // are no overrides. THIS MAKES NO SENSE! -- ajt@d.o, 2006/02/28
  442. if (ExtOverrides.empty() == false)
  443. SOver.ReadExtraOverride(ExtOverrides);
  444. if (SOverrides.empty() == false && FileExists(SOverrides) == true)
  445. SOver.ReadOverride(SOverrides,true);
  446. }
  447. /*}}}*/
  448. // SourcesWriter::DoPackage - Process a single package /*{{{*/
  449. // ---------------------------------------------------------------------
  450. /* */
  451. bool SourcesWriter::DoPackage(string FileName)
  452. {
  453. // Open the archive
  454. FileFd F(FileName,FileFd::ReadOnly);
  455. if (_error->PendingError() == true)
  456. return false;
  457. // Stat the file for later
  458. struct stat St;
  459. if (fstat(F.Fd(),&St) != 0)
  460. return _error->Errno("fstat","Failed to stat %s",FileName.c_str());
  461. if (St.st_size > 128*1024)
  462. return _error->Error("DSC file '%s' is too large!",FileName.c_str());
  463. if (BufSize < (unsigned)St.st_size+1)
  464. {
  465. BufSize = St.st_size+1;
  466. Buffer = (char *)realloc(Buffer,St.st_size+1);
  467. }
  468. if (F.Read(Buffer,St.st_size) == false)
  469. return false;
  470. // Hash the file
  471. char *Start = Buffer;
  472. char *BlkEnd = Buffer + St.st_size;
  473. MD5Summation MD5;
  474. MD5.Add((unsigned char *)Start,BlkEnd - Start);
  475. // Add an extra \n to the end, just in case
  476. *BlkEnd++ = '\n';
  477. /* Remove the PGP trailer. Some .dsc's have this without a blank line
  478. before */
  479. const char *Key = "-----BEGIN PGP SIGNATURE-----";
  480. for (char *MsgEnd = Start; MsgEnd < BlkEnd - strlen(Key) -1; MsgEnd++)
  481. {
  482. if (*MsgEnd == '\n' && strncmp(MsgEnd+1,Key,strlen(Key)) == 0)
  483. {
  484. MsgEnd[1] = '\n';
  485. break;
  486. }
  487. }
  488. /* Read records until we locate the Source record. This neatly skips the
  489. GPG header (which is RFC822 formed) without any trouble. */
  490. pkgTagSection Tags;
  491. do
  492. {
  493. unsigned Pos;
  494. if (Tags.Scan(Start,BlkEnd - Start) == false)
  495. return _error->Error("Could not find a record in the DSC '%s'",FileName.c_str());
  496. if (Tags.Find("Source",Pos) == true)
  497. break;
  498. Start += Tags.size();
  499. }
  500. while (1);
  501. Tags.Trim();
  502. // Lookup the overide information, finding first the best priority.
  503. string BestPrio;
  504. string Bins = Tags.FindS("Binary");
  505. char Buffer[Bins.length() + 1];
  506. auto_ptr<Override::Item> OverItem(0);
  507. if (Bins.empty() == false)
  508. {
  509. strcpy(Buffer,Bins.c_str());
  510. // Ignore too-long errors.
  511. char *BinList[400];
  512. TokSplitString(',',Buffer,BinList,sizeof(BinList)/sizeof(BinList[0]));
  513. // Look at all the binaries
  514. unsigned char BestPrioV = pkgCache::State::Extra;
  515. for (unsigned I = 0; BinList[I] != 0; I++)
  516. {
  517. auto_ptr<Override::Item> Itm(BOver.GetItem(BinList[I]));
  518. if (Itm.get() == 0)
  519. continue;
  520. unsigned char NewPrioV = debListParser::GetPrio(Itm->Priority);
  521. if (NewPrioV < BestPrioV || BestPrio.empty() == true)
  522. {
  523. BestPrioV = NewPrioV;
  524. BestPrio = Itm->Priority;
  525. }
  526. if (OverItem.get() == 0)
  527. OverItem = Itm;
  528. }
  529. }
  530. // If we need to do any rewriting of the header do it now..
  531. if (OverItem.get() == 0)
  532. {
  533. if (NoOverride == false)
  534. {
  535. NewLine(1);
  536. ioprintf(c1out, _(" %s has no override entry\n"), Tags.FindS("Source").c_str());
  537. }
  538. OverItem = auto_ptr<Override::Item>(new Override::Item);
  539. }
  540. auto_ptr<Override::Item> SOverItem(SOver.GetItem(Tags.FindS("Source")));
  541. // const auto_ptr<Override::Item> autoSOverItem(SOverItem);
  542. if (SOverItem.get() == 0)
  543. {
  544. ioprintf(c1out, _(" %s has no source override entry\n"), Tags.FindS("Source").c_str());
  545. SOverItem = auto_ptr<Override::Item>(BOver.GetItem(Tags.FindS("Source")));
  546. if (SOverItem.get() == 0)
  547. {
  548. ioprintf(c1out, _(" %s has no binary override entry either\n"), Tags.FindS("Source").c_str());
  549. SOverItem = auto_ptr<Override::Item>(new Override::Item);
  550. *SOverItem = *OverItem;
  551. }
  552. }
  553. // Add the dsc to the files hash list
  554. char Files[1000];
  555. snprintf(Files,sizeof(Files),"\n %s %lu %s\n %s",
  556. string(MD5.Result()).c_str(),St.st_size,
  557. flNotDir(FileName).c_str(),
  558. Tags.FindS("Files").c_str());
  559. // Strip the DirStrip prefix from the FileName and add the PathPrefix
  560. string NewFileName;
  561. if (DirStrip.empty() == false &&
  562. FileName.length() > DirStrip.length() &&
  563. stringcmp(DirStrip,OriginalPath,OriginalPath + DirStrip.length()) == 0)
  564. NewFileName = string(OriginalPath + DirStrip.length());
  565. else
  566. NewFileName = OriginalPath;
  567. if (PathPrefix.empty() == false)
  568. NewFileName = flCombine(PathPrefix,NewFileName);
  569. string Directory = flNotFile(OriginalPath);
  570. string Package = Tags.FindS("Source");
  571. // Perform the delinking operation over all of the files
  572. string ParseJnk;
  573. const char *C = Files;
  574. for (;isspace(*C); C++);
  575. while (*C != 0)
  576. {
  577. // Parse each of the elements
  578. if (ParseQuoteWord(C,ParseJnk) == false ||
  579. ParseQuoteWord(C,ParseJnk) == false ||
  580. ParseQuoteWord(C,ParseJnk) == false)
  581. return _error->Error("Error parsing file record");
  582. char Jnk[2];
  583. string OriginalPath = Directory + ParseJnk;
  584. if (RealPath != 0 && readlink(OriginalPath.c_str(),Jnk,sizeof(Jnk)) != -1 &&
  585. realpath(OriginalPath.c_str(),RealPath) != 0)
  586. {
  587. string RP = RealPath;
  588. if (Delink(RP,OriginalPath.c_str(),Stats.DeLinkBytes,St.st_size) == false)
  589. return false;
  590. }
  591. }
  592. Directory = flNotFile(NewFileName);
  593. if (Directory.length() > 2)
  594. Directory.erase(Directory.end()-1);
  595. // This lists all the changes to the fields we are going to make.
  596. // (5 hardcoded + maintainer + end marker)
  597. TFRewriteData Changes[5+1+SOverItem->FieldOverride.size()+1];
  598. unsigned int End = 0;
  599. SetTFRewriteData(Changes[End++],"Source",Package.c_str(),"Package");
  600. SetTFRewriteData(Changes[End++],"Files",Files);
  601. if (Directory != "./")
  602. SetTFRewriteData(Changes[End++],"Directory",Directory.c_str());
  603. SetTFRewriteData(Changes[End++],"Priority",BestPrio.c_str());
  604. SetTFRewriteData(Changes[End++],"Status",0);
  605. // Rewrite the maintainer field if necessary
  606. bool MaintFailed;
  607. string NewMaint = OverItem->SwapMaint(Tags.FindS("Maintainer"),MaintFailed);
  608. if (MaintFailed == true)
  609. {
  610. if (NoOverride == false)
  611. {
  612. NewLine(1);
  613. ioprintf(c1out, _(" %s maintainer is %s not %s\n"), Package.c_str(),
  614. Tags.FindS("Maintainer").c_str(), OverItem->OldMaint.c_str());
  615. }
  616. }
  617. if (NewMaint.empty() == false)
  618. SetTFRewriteData(Changes[End++], "Maintainer", NewMaint.c_str());
  619. for (map<string,string>::iterator I = SOverItem->FieldOverride.begin();
  620. I != SOverItem->FieldOverride.end(); I++)
  621. SetTFRewriteData(Changes[End++],I->first.c_str(),I->second.c_str());
  622. SetTFRewriteData(Changes[End++], 0, 0);
  623. // Rewrite and store the fields.
  624. if (TFRewrite(Output,Tags,TFRewriteSourceOrder,Changes) == false)
  625. return false;
  626. fprintf(Output,"\n");
  627. Stats.Packages++;
  628. return true;
  629. }
  630. /*}}}*/
  631. // ContentsWriter::ContentsWriter - Constructor /*{{{*/
  632. // ---------------------------------------------------------------------
  633. /* */
  634. ContentsWriter::ContentsWriter(string DB) :
  635. Db(DB), Stats(Db.Stats)
  636. {
  637. AddPattern("*.deb");
  638. Output = stdout;
  639. }
  640. /*}}}*/
  641. // ContentsWriter::DoPackage - Process a single package /*{{{*/
  642. // ---------------------------------------------------------------------
  643. /* If Package is the empty string the control record will be parsed to
  644. determine what the package name is. */
  645. bool ContentsWriter::DoPackage(string FileName,string Package)
  646. {
  647. if (!Db.GetFileInfo(FileName, Package.empty(), true, false, false, false, false))
  648. {
  649. return false;
  650. }
  651. // Parse the package name
  652. if (Package.empty() == true)
  653. {
  654. Package = Db.Control.Section.FindS("Package");
  655. }
  656. Db.Contents.Add(Gen,Package);
  657. return Db.Finish();
  658. }
  659. /*}}}*/
  660. // ContentsWriter::ReadFromPkgs - Read from a packages file /*{{{*/
  661. // ---------------------------------------------------------------------
  662. /* */
  663. bool ContentsWriter::ReadFromPkgs(string PkgFile,string PkgCompress)
  664. {
  665. MultiCompress Pkgs(PkgFile,PkgCompress,0,false);
  666. if (_error->PendingError() == true)
  667. return false;
  668. // Open the package file
  669. int CompFd = -1;
  670. pid_t Proc = -1;
  671. if (Pkgs.OpenOld(CompFd,Proc) == false)
  672. return false;
  673. // No auto-close FD
  674. FileFd Fd(CompFd,false);
  675. pkgTagFile Tags(&Fd);
  676. if (_error->PendingError() == true)
  677. {
  678. Pkgs.CloseOld(CompFd,Proc);
  679. return false;
  680. }
  681. // Parse.
  682. pkgTagSection Section;
  683. while (Tags.Step(Section) == true)
  684. {
  685. string File = flCombine(Prefix,Section.FindS("FileName"));
  686. string Package = Section.FindS("Section");
  687. if (Package.empty() == false && Package.end()[-1] != '/')
  688. {
  689. Package += '/';
  690. Package += Section.FindS("Package");
  691. }
  692. else
  693. Package += Section.FindS("Package");
  694. DoPackage(File,Package);
  695. if (_error->empty() == false)
  696. {
  697. _error->Error("Errors apply to file '%s'",File.c_str());
  698. _error->DumpErrors();
  699. }
  700. }
  701. // Tidy the compressor
  702. if (Pkgs.CloseOld(CompFd,Proc) == false)
  703. return false;
  704. return true;
  705. }
  706. /*}}}*/
  707. // ReleaseWriter::ReleaseWriter - Constructor /*{{{*/
  708. // ---------------------------------------------------------------------
  709. /* */
  710. ReleaseWriter::ReleaseWriter(string DB)
  711. {
  712. AddPattern("Packages");
  713. AddPattern("Packages.gz");
  714. AddPattern("Packages.bz2");
  715. AddPattern("Sources");
  716. AddPattern("Sources.gz");
  717. AddPattern("Sources.bz2");
  718. AddPattern("Release");
  719. AddPattern("md5sum.txt");
  720. Output = stdout;
  721. time_t now = time(NULL);
  722. char datestr[128];
  723. if (strftime(datestr, sizeof(datestr), "%a, %d %b %Y %H:%M:%S UTC",
  724. gmtime(&now)) == 0)
  725. {
  726. datestr[0] = '\0';
  727. }
  728. map<string,string> Fields;
  729. Fields["Origin"] = "";
  730. Fields["Label"] = "";
  731. Fields["Suite"] = "";
  732. Fields["Version"] = "";
  733. Fields["Codename"] = "";
  734. Fields["Date"] = datestr;
  735. Fields["Architectures"] = "";
  736. Fields["Components"] = "";
  737. Fields["Description"] = "";
  738. for(map<string,string>::const_iterator I = Fields.begin();
  739. I != Fields.end();
  740. ++I)
  741. {
  742. string Config = string("APT::FTPArchive::Release::") + (*I).first;
  743. string Value = _config->Find(Config, (*I).second.c_str());
  744. if (Value == "")
  745. continue;
  746. fprintf(Output, "%s: %s\n", (*I).first.c_str(), Value.c_str());
  747. }
  748. }
  749. /*}}}*/
  750. // ReleaseWriter::DoPackage - Process a single package /*{{{*/
  751. // ---------------------------------------------------------------------
  752. bool ReleaseWriter::DoPackage(string FileName)
  753. {
  754. // Strip the DirStrip prefix from the FileName and add the PathPrefix
  755. string NewFileName;
  756. if (DirStrip.empty() == false &&
  757. FileName.length() > DirStrip.length() &&
  758. stringcmp(FileName.begin(),FileName.begin() + DirStrip.length(),
  759. DirStrip.begin(),DirStrip.end()) == 0)
  760. {
  761. NewFileName = string(FileName.begin() + DirStrip.length(),FileName.end());
  762. while (NewFileName[0] == '/')
  763. NewFileName = string(NewFileName.begin() + 1,NewFileName.end());
  764. }
  765. else
  766. NewFileName = FileName;
  767. if (PathPrefix.empty() == false)
  768. NewFileName = flCombine(PathPrefix,NewFileName);
  769. FileFd fd(FileName, FileFd::ReadOnly);
  770. if (!fd.IsOpen())
  771. {
  772. return false;
  773. }
  774. CheckSums[NewFileName].size = fd.Size();
  775. MD5Summation MD5;
  776. MD5.AddFD(fd.Fd(), fd.Size());
  777. CheckSums[NewFileName].MD5 = MD5.Result();
  778. fd.Seek(0);
  779. SHA1Summation SHA1;
  780. SHA1.AddFD(fd.Fd(), fd.Size());
  781. CheckSums[NewFileName].SHA1 = SHA1.Result();
  782. fd.Seek(0);
  783. SHA256Summation SHA256;
  784. SHA256.AddFD(fd.Fd(), fd.Size());
  785. CheckSums[NewFileName].SHA256 = SHA256.Result();
  786. fd.Close();
  787. return true;
  788. }
  789. /*}}}*/
  790. // ReleaseWriter::Finish - Output the checksums /*{{{*/
  791. // ---------------------------------------------------------------------
  792. void ReleaseWriter::Finish()
  793. {
  794. fprintf(Output, "MD5Sum:\n");
  795. for(map<string,struct CheckSum>::iterator I = CheckSums.begin();
  796. I != CheckSums.end();
  797. ++I)
  798. {
  799. fprintf(Output, " %s %16ld %s\n",
  800. (*I).second.MD5.c_str(),
  801. (*I).second.size,
  802. (*I).first.c_str());
  803. }
  804. fprintf(Output, "SHA1:\n");
  805. for(map<string,struct CheckSum>::iterator I = CheckSums.begin();
  806. I != CheckSums.end();
  807. ++I)
  808. {
  809. fprintf(Output, " %s %16ld %s\n",
  810. (*I).second.SHA1.c_str(),
  811. (*I).second.size,
  812. (*I).first.c_str());
  813. }
  814. fprintf(Output, "SHA256:\n");
  815. for(map<string,struct CheckSum>::iterator I = CheckSums.begin();
  816. I != CheckSums.end();
  817. ++I)
  818. {
  819. fprintf(Output, " %s %16ld %s\n",
  820. (*I).second.SHA256.c_str(),
  821. (*I).second.size,
  822. (*I).first.c_str());
  823. }
  824. }