writer.cc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: writer.cc,v 1.6 2002/11/11 04:27:51 doogie 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. #ifdef __GNUG__
  12. #pragma implementation "writer.h"
  13. #endif
  14. #include "writer.h"
  15. #include <apt-pkg/strutl.h>
  16. #include <apt-pkg/error.h>
  17. #include <apt-pkg/configuration.h>
  18. #include <apt-pkg/md5.h>
  19. #include <apt-pkg/deblistparser.h>
  20. #include <sys/types.h>
  21. #include <unistd.h>
  22. #include <ftw.h>
  23. #include <iostream>
  24. #include "cachedb.h"
  25. #include "apt-ftparchive.h"
  26. #include "multicompress.h"
  27. /*}}}*/
  28. using namespace std;
  29. FTWScanner *FTWScanner::Owner;
  30. // SetTFRewriteData - Helper for setting rewrite lists /*{{{*/
  31. // ---------------------------------------------------------------------
  32. /* */
  33. inline void SetTFRewriteData(struct TFRewriteData &tfrd,
  34. const char *tag,
  35. const char *rewrite,
  36. const char *newtag = 0)
  37. {
  38. tfrd.Tag = tag;
  39. tfrd.Rewrite = rewrite;
  40. tfrd.NewTag = newtag;
  41. }
  42. /*}}}*/
  43. // FTWScanner::FTWScanner - Constructor /*{{{*/
  44. // ---------------------------------------------------------------------
  45. /* */
  46. FTWScanner::FTWScanner()
  47. {
  48. ErrorPrinted = false;
  49. NoLinkAct = !_config->FindB("APT::FTPArchive::DeLinkAct",true);
  50. TmpExt = 0;
  51. Ext[0] = 0;
  52. RealPath = 0;
  53. long PMax = pathconf(".",_PC_PATH_MAX);
  54. if (PMax > 0)
  55. RealPath = new char[PMax];
  56. }
  57. /*}}}*/
  58. // FTWScanner::Scanner - FTW Scanner /*{{{*/
  59. // ---------------------------------------------------------------------
  60. /* This is the FTW scanner, it processes each directory element in the
  61. directory tree. */
  62. int FTWScanner::Scanner(const char *File,const struct stat *sb,int Flag)
  63. {
  64. if (Flag == FTW_DNR)
  65. {
  66. Owner->NewLine(1);
  67. c1out << "W: Unable to read directory " << File << endl;
  68. }
  69. if (Flag == FTW_NS)
  70. {
  71. Owner->NewLine(1);
  72. c1out << "W: Unable to stat " << File << endl;
  73. }
  74. if (Flag != FTW_F)
  75. return 0;
  76. // See if it is a .deb
  77. if (strlen(File) < 4)
  78. return 0;
  79. unsigned CurExt = 0;
  80. for (; Owner->Ext[CurExt] != 0; CurExt++)
  81. if (strcmp(File+strlen(File)-strlen(Owner->Ext[CurExt]),
  82. Owner->Ext[CurExt]) == 0)
  83. break;
  84. if (Owner->Ext[CurExt] == 0)
  85. return 0;
  86. /* Process it. If the file is a link then resolve it into an absolute
  87. name.. This works best if the directory components the scanner are
  88. given are not links themselves. */
  89. char Jnk[2];
  90. Owner->OriginalPath = File;
  91. if (Owner->RealPath != 0 && readlink(File,Jnk,sizeof(Jnk)) != -1 &&
  92. realpath(File,Owner->RealPath) != 0)
  93. Owner->DoPackage(Owner->RealPath);
  94. else
  95. Owner->DoPackage(File);
  96. if (_error->empty() == false)
  97. {
  98. // Print any errors or warnings found
  99. string Err;
  100. bool SeenPath = false;
  101. while (_error->empty() == false)
  102. {
  103. Owner->NewLine(1);
  104. bool Type = _error->PopMessage(Err);
  105. if (Type == true)
  106. cerr << "E: " << Err << endl;
  107. else
  108. cerr << "W: " << Err << endl;
  109. if (Err.find(File) != string::npos)
  110. SeenPath = true;
  111. }
  112. if (SeenPath == false)
  113. cerr << "E: Errors apply to file '" << File << "'" << endl;
  114. return 0;
  115. }
  116. return 0;
  117. }
  118. /*}}}*/
  119. // FTWScanner::RecursiveScan - Just scan a directory tree /*{{{*/
  120. // ---------------------------------------------------------------------
  121. /* */
  122. bool FTWScanner::RecursiveScan(string Dir)
  123. {
  124. /* If noprefix is set then jam the scan root in, so we don't generate
  125. link followed paths out of control */
  126. if (InternalPrefix.empty() == true)
  127. {
  128. if (realpath(Dir.c_str(),RealPath) == 0)
  129. return _error->Errno("realpath","Failed to resolve %s",Dir.c_str());
  130. InternalPrefix = RealPath;
  131. }
  132. // Do recursive directory searching
  133. Owner = this;
  134. int Res = ftw(Dir.c_str(),Scanner,30);
  135. // Error treewalking?
  136. if (Res != 0)
  137. {
  138. if (_error->PendingError() == false)
  139. _error->Errno("ftw","Tree walking failed");
  140. return false;
  141. }
  142. return true;
  143. }
  144. /*}}}*/
  145. // FTWScanner::LoadFileList - Load the file list from a file /*{{{*/
  146. // ---------------------------------------------------------------------
  147. /* This is an alternative to using FTW to locate files, it reads the list
  148. of files from another file. */
  149. bool FTWScanner::LoadFileList(string Dir,string File)
  150. {
  151. /* If noprefix is set then jam the scan root in, so we don't generate
  152. link followed paths out of control */
  153. if (InternalPrefix.empty() == true)
  154. {
  155. if (realpath(Dir.c_str(),RealPath) == 0)
  156. return _error->Errno("realpath","Failed to resolve %s",Dir.c_str());
  157. InternalPrefix = RealPath;
  158. }
  159. Owner = this;
  160. FILE *List = fopen(File.c_str(),"r");
  161. if (List == 0)
  162. return _error->Errno("fopen","Failed to open %s",File.c_str());
  163. /* We are a tad tricky here.. We prefix the buffer with the directory
  164. name, that way if we need a full path with just use line.. Sneaky and
  165. fully evil. */
  166. char Line[1000];
  167. char *FileStart;
  168. if (Dir.empty() == true || Dir.end()[-1] != '/')
  169. FileStart = Line + snprintf(Line,sizeof(Line),"%s/",Dir.c_str());
  170. else
  171. FileStart = Line + snprintf(Line,sizeof(Line),"%s",Dir.c_str());
  172. while (fgets(FileStart,sizeof(Line) - (FileStart - Line),List) != 0)
  173. {
  174. char *FileName = _strstrip(FileStart);
  175. if (FileName[0] == 0)
  176. continue;
  177. if (FileName[0] != '/')
  178. {
  179. if (FileName != FileStart)
  180. memmove(FileStart,FileName,strlen(FileStart));
  181. FileName = Line;
  182. }
  183. struct stat St;
  184. int Flag = FTW_F;
  185. if (stat(FileName,&St) != 0)
  186. Flag = FTW_NS;
  187. if (Scanner(FileName,&St,Flag) != 0)
  188. break;
  189. }
  190. fclose(List);
  191. return true;
  192. }
  193. /*}}}*/
  194. // FTWScanner::Delink - Delink symlinks /*{{{*/
  195. // ---------------------------------------------------------------------
  196. /* */
  197. bool FTWScanner::Delink(string &FileName,const char *OriginalPath,
  198. unsigned long &DeLinkBytes,
  199. struct stat &St)
  200. {
  201. // See if this isn't an internaly prefix'd file name.
  202. if (InternalPrefix.empty() == false &&
  203. InternalPrefix.length() < FileName.length() &&
  204. stringcmp(FileName.begin(),FileName.begin() + InternalPrefix.length(),
  205. InternalPrefix.begin(),InternalPrefix.end()) != 0)
  206. {
  207. if (DeLinkLimit != 0 && DeLinkBytes/1024 < DeLinkLimit)
  208. {
  209. // Tidy up the display
  210. if (DeLinkBytes == 0)
  211. cout << endl;
  212. NewLine(1);
  213. c1out << " DeLink " << (OriginalPath + InternalPrefix.length())
  214. << " [" << SizeToStr(St.st_size) << "B]" << endl << flush;
  215. if (NoLinkAct == false)
  216. {
  217. char OldLink[400];
  218. if (readlink(OriginalPath,OldLink,sizeof(OldLink)) == -1)
  219. _error->Errno("readlink","Failed to readlink %s",OriginalPath);
  220. else
  221. {
  222. if (unlink(OriginalPath) != 0)
  223. _error->Errno("unlink","Failed to unlink %s",OriginalPath);
  224. else
  225. {
  226. if (link(FileName.c_str(),OriginalPath) != 0)
  227. {
  228. // Panic! Restore the symlink
  229. symlink(OldLink,OriginalPath);
  230. return _error->Errno("link","*** Failed to link %s to %s",
  231. FileName.c_str(),
  232. OriginalPath);
  233. }
  234. }
  235. }
  236. }
  237. DeLinkBytes += St.st_size;
  238. if (DeLinkBytes/1024 >= DeLinkLimit)
  239. c1out << " DeLink limit of " << SizeToStr(DeLinkBytes) << "B hit." << endl;
  240. }
  241. FileName = OriginalPath;
  242. }
  243. return true;
  244. }
  245. /*}}}*/
  246. // FTWScanner::SetExts - Set extensions to support /*{{{*/
  247. // ---------------------------------------------------------------------
  248. /* */
  249. bool FTWScanner::SetExts(string Vals)
  250. {
  251. delete [] TmpExt;
  252. TmpExt = new char[Vals.length()+1];
  253. strcpy(TmpExt,Vals.c_str());
  254. return TokSplitString(' ',TmpExt,(char **)Ext,sizeof(Ext)/sizeof(Ext[0]));
  255. }
  256. /*}}}*/
  257. // PackagesWriter::PackagesWriter - Constructor /*{{{*/
  258. // ---------------------------------------------------------------------
  259. /* */
  260. PackagesWriter::PackagesWriter(string DB,string Overrides,string ExtOverrides) :
  261. Db(DB),Stats(Db.Stats)
  262. {
  263. Output = stdout;
  264. Ext[0] = ".deb";
  265. Ext[1] = 0;
  266. DeLinkLimit = 0;
  267. // Process the command line options
  268. DoMD5 = _config->FindB("APT::FTPArchive::MD5",true);
  269. DoContents = _config->FindB("APT::FTPArchive::Contents",true);
  270. NoOverride = _config->FindB("APT::FTPArchive::NoOverrideMsg",false);
  271. if (Db.Loaded() == false)
  272. DoContents = false;
  273. // Read the override file
  274. if (Overrides.empty() == false && Over.ReadOverride(Overrides) == false)
  275. return;
  276. else
  277. NoOverride = true;
  278. if (ExtOverrides.empty() == false)
  279. Over.ReadExtraOverride(ExtOverrides);
  280. _error->DumpErrors();
  281. }
  282. /*}}}*/
  283. // PackagesWriter::DoPackage - Process a single package /*{{{*/
  284. // ---------------------------------------------------------------------
  285. /* This method takes a package and gets its control information and
  286. MD5 then writes out a control record with the proper fields rewritten
  287. and the path/size/hash appended. */
  288. bool PackagesWriter::DoPackage(string FileName)
  289. {
  290. // Open the archive
  291. FileFd F(FileName,FileFd::ReadOnly);
  292. if (_error->PendingError() == true)
  293. return false;
  294. // Stat the file for later
  295. struct stat St;
  296. if (fstat(F.Fd(),&St) != 0)
  297. return _error->Errno("fstat","Failed to stat %s",FileName.c_str());
  298. // Pull all the data we need form the DB
  299. string MD5Res;
  300. if (Db.SetFile(FileName,St,&F) == false ||
  301. Db.LoadControl() == false ||
  302. (DoContents == true && Db.LoadContents(true) == false) ||
  303. (DoMD5 == true && Db.GetMD5(MD5Res,false) == false))
  304. return false;
  305. if (Delink(FileName,OriginalPath,Stats.DeLinkBytes,St) == false)
  306. return false;
  307. // Lookup the overide information
  308. pkgTagSection &Tags = Db.Control.Section;
  309. string Package = Tags.FindS("Package");
  310. Override::Item Tmp;
  311. Override::Item *OverItem = Over.GetItem(Package);
  312. if (Package.empty() == true)
  313. return _error->Error("Archive had no package field");
  314. // If we need to do any rewriting of the header do it now..
  315. if (OverItem == 0)
  316. {
  317. if (NoOverride == false)
  318. {
  319. NewLine(1);
  320. c1out << " " << Package << " has no override entry" << endl;
  321. }
  322. OverItem = &Tmp;
  323. Tmp.FieldOverride["Section"] = Tags.FindS("Section");
  324. Tmp.Priority = Tags.FindS("Priority");
  325. }
  326. char Size[40];
  327. sprintf(Size,"%lu",St.st_size);
  328. // Strip the DirStrip prefix from the FileName and add the PathPrefix
  329. string NewFileName;
  330. if (DirStrip.empty() == false &&
  331. FileName.length() > DirStrip.length() &&
  332. stringcmp(FileName.begin(),FileName.begin() + DirStrip.length(),
  333. DirStrip.begin(),DirStrip.end()) == 0)
  334. NewFileName = string(FileName.begin() + DirStrip.length(),FileName.end());
  335. else
  336. NewFileName = FileName;
  337. if (PathPrefix.empty() == false)
  338. NewFileName = flCombine(PathPrefix,NewFileName);
  339. // This lists all the changes to the fields we are going to make.
  340. // (7 hardcoded + maintainer + suggests + end marker)
  341. TFRewriteData Changes[6+2+OverItem->FieldOverride.size()+1];
  342. unsigned int End = 0;
  343. SetTFRewriteData(Changes[End++], "Size", Size);
  344. SetTFRewriteData(Changes[End++], "MD5sum", MD5Res.c_str());
  345. SetTFRewriteData(Changes[End++], "Filename", NewFileName.c_str());
  346. SetTFRewriteData(Changes[End++], "Priority", OverItem->Priority.c_str());
  347. SetTFRewriteData(Changes[End++], "Status", 0);
  348. SetTFRewriteData(Changes[End++], "Optional", 0);
  349. // Rewrite the maintainer field if necessary
  350. bool MaintFailed;
  351. string NewMaint = OverItem->SwapMaint(Tags.FindS("Maintainer"),MaintFailed);
  352. if (MaintFailed == true)
  353. {
  354. if (NoOverride == false)
  355. {
  356. NewLine(1);
  357. c1out << " " << Package << " maintainer is " <<
  358. Tags.FindS("Maintainer") << " not " <<
  359. OverItem->OldMaint << endl;
  360. }
  361. }
  362. if (NewMaint.empty() == false)
  363. SetTFRewriteData(Changes[End++], "Maintainer", NewMaint.c_str());
  364. /* Get rid of the Optional tag. This is an ugly, ugly, ugly hack that
  365. dpkg-scanpackages does.. Well sort of. dpkg-scanpackages just does renaming
  366. but dpkg does this append bit. So we do the append bit, at least that way the
  367. status file and package file will remain similar. There are other transforms
  368. but optional is the only legacy one still in use for some lazy reason. */
  369. string OptionalStr = Tags.FindS("Optional");
  370. if (OptionalStr.empty() == false)
  371. {
  372. if (Tags.FindS("Suggests").empty() == false)
  373. OptionalStr = Tags.FindS("Suggests") + ", " + OptionalStr;
  374. SetTFRewriteData(Changes[End++], "Suggests", OptionalStr.c_str());
  375. }
  376. for (map<string,string>::iterator I = OverItem->FieldOverride.begin();
  377. I != OverItem->FieldOverride.end(); I++)
  378. SetTFRewriteData(Changes[End++],I->first.c_str(),I->second.c_str());
  379. SetTFRewriteData(Changes[End++], 0, 0);
  380. // Rewrite and store the fields.
  381. if (TFRewrite(Output,Tags,TFRewritePackageOrder,Changes) == false)
  382. return false;
  383. fprintf(Output,"\n");
  384. return Db.Finish();
  385. }
  386. /*}}}*/
  387. // SourcesWriter::SourcesWriter - Constructor /*{{{*/
  388. // ---------------------------------------------------------------------
  389. /* */
  390. SourcesWriter::SourcesWriter(string BOverrides,string SOverrides,
  391. string ExtOverrides)
  392. {
  393. Output = stdout;
  394. Ext[0] = ".dsc";
  395. Ext[1] = 0;
  396. DeLinkLimit = 0;
  397. Buffer = 0;
  398. BufSize = 0;
  399. // Process the command line options
  400. NoOverride = _config->FindB("APT::FTPArchive::NoOverrideMsg",false);
  401. // Read the override file
  402. if (BOverrides.empty() == false && BOver.ReadOverride(BOverrides) == false)
  403. return;
  404. else
  405. NoOverride = true;
  406. if (ExtOverrides.empty() == false)
  407. SOver.ReadExtraOverride(ExtOverrides);
  408. if (SOverrides.empty() == false && FileExists(SOverrides) == true)
  409. SOver.ReadOverride(SOverrides,true);
  410. }
  411. /*}}}*/
  412. // SourcesWriter::DoPackage - Process a single package /*{{{*/
  413. // ---------------------------------------------------------------------
  414. /* */
  415. bool SourcesWriter::DoPackage(string FileName)
  416. {
  417. // Open the archive
  418. FileFd F(FileName,FileFd::ReadOnly);
  419. if (_error->PendingError() == true)
  420. return false;
  421. // Stat the file for later
  422. struct stat St;
  423. if (fstat(F.Fd(),&St) != 0)
  424. return _error->Errno("fstat","Failed to stat %s",FileName.c_str());
  425. if (St.st_size > 128*1024)
  426. return _error->Error("DSC file '%s' is too large!",FileName.c_str());
  427. if (BufSize < (unsigned)St.st_size+1)
  428. {
  429. BufSize = St.st_size+1;
  430. Buffer = (char *)realloc(Buffer,St.st_size+1);
  431. }
  432. if (F.Read(Buffer,St.st_size) == false)
  433. return false;
  434. // Hash the file
  435. char *Start = Buffer;
  436. char *BlkEnd = Buffer + St.st_size;
  437. MD5Summation MD5;
  438. MD5.Add((unsigned char *)Start,BlkEnd - Start);
  439. // Add an extra \n to the end, just in case
  440. *BlkEnd++ = '\n';
  441. /* Remove the PGP trailer. Some .dsc's have this without a blank line
  442. before */
  443. const char *Key = "-----BEGIN PGP SIGNATURE-----";
  444. for (char *MsgEnd = Start; MsgEnd < BlkEnd - strlen(Key) -1; MsgEnd++)
  445. {
  446. if (*MsgEnd == '\n' && strncmp(MsgEnd+1,Key,strlen(Key)) == 0)
  447. {
  448. MsgEnd[1] = '\n';
  449. break;
  450. }
  451. }
  452. /* Read records until we locate the Source record. This neatly skips the
  453. GPG header (which is RFC822 formed) without any trouble. */
  454. pkgTagSection Tags;
  455. do
  456. {
  457. unsigned Pos;
  458. if (Tags.Scan(Start,BlkEnd - Start) == false)
  459. return _error->Error("Could not find a record in the DSC '%s'",FileName.c_str());
  460. if (Tags.Find("Source",Pos) == true)
  461. break;
  462. Start += Tags.size();
  463. }
  464. while (1);
  465. Tags.Trim();
  466. // Lookup the overide information, finding first the best priority.
  467. string BestPrio;
  468. char Buffer[1000];
  469. string Bins = Tags.FindS("Binary");
  470. Override::Item *OverItem = 0;
  471. if (Bins.empty() == false && Bins.length() < sizeof(Buffer))
  472. {
  473. strcpy(Buffer,Bins.c_str());
  474. // Ignore too-long errors.
  475. char *BinList[400];
  476. TokSplitString(',',Buffer,BinList,sizeof(BinList)/sizeof(BinList[0]));
  477. // Look at all the binaries
  478. unsigned char BestPrioV = pkgCache::State::Extra;
  479. for (unsigned I = 0; BinList[I] != 0; I++)
  480. {
  481. Override::Item *Itm = BOver.GetItem(BinList[I]);
  482. if (Itm == 0)
  483. continue;
  484. if (OverItem == 0)
  485. OverItem = Itm;
  486. unsigned char NewPrioV = debListParser::GetPrio(Itm->Priority);
  487. if (NewPrioV < BestPrioV || BestPrio.empty() == true)
  488. {
  489. BestPrioV = NewPrioV;
  490. BestPrio = Itm->Priority;
  491. }
  492. }
  493. }
  494. // If we need to do any rewriting of the header do it now..
  495. Override::Item Tmp;
  496. if (OverItem == 0)
  497. {
  498. if (NoOverride == false)
  499. {
  500. NewLine(1);
  501. c1out << " " << Tags.FindS("Source") << " has no override entry" << endl;
  502. }
  503. OverItem = &Tmp;
  504. }
  505. Override::Item *SOverItem = SOver.GetItem(Tags.FindS("Source"));
  506. if (SOverItem == 0)
  507. {
  508. SOverItem = BOver.GetItem(Tags.FindS("Source"));
  509. if (SOverItem == 0)
  510. SOverItem = OverItem;
  511. }
  512. // Add the dsc to the files hash list
  513. char Files[1000];
  514. snprintf(Files,sizeof(Files),"\n %s %lu %s\n %s",
  515. string(MD5.Result()).c_str(),St.st_size,
  516. flNotDir(FileName).c_str(),
  517. Tags.FindS("Files").c_str());
  518. // Strip the DirStrip prefix from the FileName and add the PathPrefix
  519. string NewFileName;
  520. if (DirStrip.empty() == false &&
  521. FileName.length() > DirStrip.length() &&
  522. stringcmp(DirStrip,OriginalPath,OriginalPath + DirStrip.length()) == 0)
  523. NewFileName = string(OriginalPath + DirStrip.length());
  524. else
  525. NewFileName = OriginalPath;
  526. if (PathPrefix.empty() == false)
  527. NewFileName = flCombine(PathPrefix,NewFileName);
  528. string Directory = flNotFile(OriginalPath);
  529. string Package = Tags.FindS("Source");
  530. // Perform the delinking operation over all of the files
  531. string ParseJnk;
  532. const char *C = Files;
  533. for (;isspace(*C); C++);
  534. while (*C != 0)
  535. {
  536. // Parse each of the elements
  537. if (ParseQuoteWord(C,ParseJnk) == false ||
  538. ParseQuoteWord(C,ParseJnk) == false ||
  539. ParseQuoteWord(C,ParseJnk) == false)
  540. return _error->Error("Error parsing file record");
  541. char Jnk[2];
  542. string OriginalPath = Directory + ParseJnk;
  543. if (RealPath != 0 && readlink(OriginalPath.c_str(),Jnk,sizeof(Jnk)) != -1 &&
  544. realpath(OriginalPath.c_str(),RealPath) != 0)
  545. {
  546. string RP = RealPath;
  547. if (Delink(RP,OriginalPath.c_str(),Stats.DeLinkBytes,St) == false)
  548. return false;
  549. }
  550. }
  551. Directory = flNotFile(NewFileName);
  552. if (Directory.length() > 2)
  553. Directory.erase(Directory.end()-1);
  554. // This lists all the changes to the fields we are going to make.
  555. // (5 hardcoded + maintainer + end marker)
  556. TFRewriteData Changes[5+1+SOverItem->FieldOverride.size()+1];
  557. unsigned int End = 0;
  558. SetTFRewriteData(Changes[End++],"Source",Package.c_str(),"Package");
  559. SetTFRewriteData(Changes[End++],"Files",Files);
  560. if (Directory != "./")
  561. SetTFRewriteData(Changes[End++],"Directory",Directory.c_str());
  562. SetTFRewriteData(Changes[End++],"Priority",BestPrio.c_str());
  563. SetTFRewriteData(Changes[End++],"Status",0);
  564. // Rewrite the maintainer field if necessary
  565. bool MaintFailed;
  566. string NewMaint = OverItem->SwapMaint(Tags.FindS("Maintainer"),MaintFailed);
  567. if (MaintFailed == true)
  568. {
  569. if (NoOverride == false)
  570. {
  571. NewLine(1);
  572. c1out << " " << Package << " maintainer is " <<
  573. Tags.FindS("Maintainer") << " not " <<
  574. OverItem->OldMaint << endl;
  575. }
  576. }
  577. if (NewMaint.empty() == false)
  578. SetTFRewriteData(Changes[End++], "Maintainer", NewMaint.c_str());
  579. for (map<string,string>::iterator I = SOverItem->FieldOverride.begin();
  580. I != SOverItem->FieldOverride.end(); I++)
  581. SetTFRewriteData(Changes[End++],I->first.c_str(),I->second.c_str());
  582. SetTFRewriteData(Changes[End++], 0, 0);
  583. // Rewrite and store the fields.
  584. if (TFRewrite(Output,Tags,TFRewriteSourceOrder,Changes) == false)
  585. return false;
  586. fprintf(Output,"\n");
  587. Stats.Packages++;
  588. return true;
  589. }
  590. /*}}}*/
  591. // ContentsWriter::ContentsWriter - Constructor /*{{{*/
  592. // ---------------------------------------------------------------------
  593. /* */
  594. ContentsWriter::ContentsWriter(string DB) :
  595. Db(DB), Stats(Db.Stats)
  596. {
  597. Ext[0] = ".deb";
  598. Ext[1] = 0;
  599. Output = stdout;
  600. }
  601. /*}}}*/
  602. // ContentsWriter::DoPackage - Process a single package /*{{{*/
  603. // ---------------------------------------------------------------------
  604. /* If Package is the empty string the control record will be parsed to
  605. determine what the package name is. */
  606. bool ContentsWriter::DoPackage(string FileName,string Package)
  607. {
  608. // Open the archive
  609. FileFd F(FileName,FileFd::ReadOnly);
  610. if (_error->PendingError() == true)
  611. return false;
  612. // Stat the file for later
  613. struct stat St;
  614. if (fstat(F.Fd(),&St) != 0)
  615. return _error->Errno("fstat","Failed too stat %s",FileName.c_str());
  616. // Ready the DB
  617. if (Db.SetFile(FileName,St,&F) == false ||
  618. Db.LoadContents(false) == false)
  619. return false;
  620. // Parse the package name
  621. if (Package.empty() == true)
  622. {
  623. if (Db.LoadControl() == false)
  624. return false;
  625. Package = Db.Control.Section.FindS("Package");
  626. }
  627. Db.Contents.Add(Gen,Package);
  628. return Db.Finish();
  629. }
  630. /*}}}*/
  631. // ContentsWriter::ReadFromPkgs - Read from a packages file /*{{{*/
  632. // ---------------------------------------------------------------------
  633. /* */
  634. bool ContentsWriter::ReadFromPkgs(string PkgFile,string PkgCompress)
  635. {
  636. MultiCompress Pkgs(PkgFile,PkgCompress,0,false);
  637. if (_error->PendingError() == true)
  638. return false;
  639. // Open the package file
  640. int CompFd = -1;
  641. int Proc = -1;
  642. if (Pkgs.OpenOld(CompFd,Proc) == false)
  643. return false;
  644. // No auto-close FD
  645. FileFd Fd(CompFd,false);
  646. pkgTagFile Tags(&Fd);
  647. if (_error->PendingError() == true)
  648. {
  649. Pkgs.CloseOld(CompFd,Proc);
  650. return false;
  651. }
  652. // Parse.
  653. pkgTagSection Section;
  654. while (Tags.Step(Section) == true)
  655. {
  656. string File = flCombine(Prefix,Section.FindS("FileName"));
  657. string Package = Section.FindS("Section");
  658. if (Package.empty() == false && Package.end()[-1] != '/')
  659. {
  660. Package += '/';
  661. Package += Section.FindS("Package");
  662. }
  663. else
  664. Package += Section.FindS("Package");
  665. DoPackage(File,Package);
  666. if (_error->empty() == false)
  667. {
  668. _error->Error("Errors apply to file '%s'",File.c_str());
  669. _error->DumpErrors();
  670. }
  671. }
  672. // Tidy the compressor
  673. if (Pkgs.CloseOld(CompFd,Proc) == false)
  674. return false;
  675. return true;
  676. }
  677. /*}}}*/