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.
 
 
 
 
 
 

193 lines
5.4 KiB

  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: apt-sortpkgs.cc,v 1.5 2003/01/11 07:18:44 jgg Exp $
  4. /* ######################################################################
  5. APT Sort Packages - Program to sort Package and Source files
  6. This program is quite simple, it just sorts the package files by
  7. package and sorts the fields inside by the internal APT sort order.
  8. Input is taken from a named file and sent to stdout.
  9. ##################################################################### */
  10. /*}}}*/
  11. // Include Files /*{{{*/
  12. #include <config.h>
  13. #include <apt-pkg/tagfile.h>
  14. #include <apt-pkg/error.h>
  15. #include <apt-pkg/configuration.h>
  16. #include <apt-pkg/cmndline.h>
  17. #include <apt-pkg/init.h>
  18. #include <apt-pkg/strutl.h>
  19. #include <apt-pkg/fileutl.h>
  20. #include <apt-pkg/pkgsystem.h>
  21. #include <apt-private/private-cmndline.h>
  22. #include <vector>
  23. #include <algorithm>
  24. #include <stdio.h>
  25. #include <iostream>
  26. #include <string>
  27. #include <memory>
  28. #include <apti18n.h>
  29. /*}}}*/
  30. using namespace std;
  31. struct PkgName /*{{{*/
  32. {
  33. string Name;
  34. string Ver;
  35. string Arch;
  36. unsigned long Offset;
  37. unsigned long Length;
  38. inline int Compare3(const PkgName &x) const
  39. {
  40. int A = stringcasecmp(Name,x.Name);
  41. if (A == 0)
  42. {
  43. A = stringcasecmp(Ver,x.Ver);
  44. if (A == 0)
  45. A = stringcasecmp(Arch,x.Arch);
  46. }
  47. return A;
  48. }
  49. bool operator <(const PkgName &x) const {return Compare3(x) < 0;};
  50. bool operator >(const PkgName &x) const {return Compare3(x) > 0;};
  51. bool operator ==(const PkgName &x) const {return Compare3(x) == 0;};
  52. };
  53. /*}}}*/
  54. // DoIt - Sort a single file /*{{{*/
  55. // ---------------------------------------------------------------------
  56. /* */
  57. static bool DoIt(string InFile)
  58. {
  59. FileFd Fd(InFile,FileFd::ReadOnly);
  60. pkgTagFile Tags(&Fd);
  61. if (_error->PendingError() == true)
  62. return false;
  63. // Parse.
  64. vector<PkgName> List;
  65. pkgTagSection Section;
  66. unsigned long Largest = 0;
  67. unsigned long Offset = Tags.Offset();
  68. bool Source = _config->FindB("APT::SortPkgs::Source",false);
  69. while (Tags.Step(Section) == true)
  70. {
  71. PkgName Tmp;
  72. /* Fetch the name, auto-detecting if this is a source file or a
  73. package file */
  74. Tmp.Name = Section.FindS("Package");
  75. Tmp.Ver = Section.FindS("Version");
  76. Tmp.Arch = Section.FindS("Architecture");
  77. if (Tmp.Name.empty() == true)
  78. return _error->Error(_("Unknown package record!"));
  79. Tmp.Offset = Offset;
  80. Tmp.Length = Section.size();
  81. if (Largest < Tmp.Length)
  82. Largest = Tmp.Length;
  83. List.push_back(Tmp);
  84. Offset = Tags.Offset();
  85. }
  86. if (_error->PendingError() == true)
  87. return false;
  88. // Sort it
  89. sort(List.begin(),List.end());
  90. const char **Order = TFRewritePackageOrder;
  91. if (Source == true)
  92. Order = TFRewriteSourceOrder;
  93. // Emit
  94. FileFd stdoutfd;
  95. stdoutfd.OpenDescriptor(STDOUT_FILENO, FileFd::WriteOnly, false);
  96. auto const Buffer = std::unique_ptr<unsigned char[]>(new unsigned char[Largest+1]);
  97. for (vector<PkgName>::iterator I = List.begin(); I != List.end(); ++I)
  98. {
  99. // Read in the Record.
  100. if (Fd.Seek(I->Offset) == false || Fd.Read(Buffer.get(),I->Length) == false)
  101. return false;
  102. Buffer[I->Length] = '\n';
  103. if (Section.Scan((char *)Buffer.get(),I->Length+1) == false)
  104. return _error->Error("Internal error, failed to scan buffer");
  105. // Sort the section
  106. if (Section.Write(stdoutfd, Order) == false || stdoutfd.Write("\n", 1) == false)
  107. return _error->Error("Internal error, failed to sort fields");
  108. }
  109. return true;
  110. }
  111. /*}}}*/
  112. // ShowHelp - Show the help text /*{{{*/
  113. // ---------------------------------------------------------------------
  114. /* */
  115. static bool ShowHelp(CommandLine &)
  116. {
  117. ioprintf(std::cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
  118. if (_config->FindB("version") == true)
  119. return true;
  120. cout <<
  121. _("Usage: apt-sortpkgs [options] file1 [file2 ...]\n"
  122. "\n"
  123. "apt-sortpkgs is a simple tool to sort package files. The -s option is used\n"
  124. "to indicate what kind of file it is.\n"
  125. "\n"
  126. "Options:\n"
  127. " -h This help text\n"
  128. " -s Use source file sorting\n"
  129. " -c=? Read this configuration file\n"
  130. " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
  131. return true;
  132. }
  133. /*}}}*/
  134. int main(int argc,const char *argv[]) /*{{{*/
  135. {
  136. CommandLine::Args Args[] = {
  137. {'h',"help","help",0},
  138. {'v',"version","version",0},
  139. {'s',"source","APT::SortPkgs::Source",0},
  140. {'c',"config-file",0,CommandLine::ConfigFile},
  141. {'o',"option",0,CommandLine::ArbItem},
  142. {0,0,0,0}};
  143. // Set up gettext support
  144. setlocale(LC_ALL,"");
  145. textdomain(PACKAGE);
  146. // Parse the command line and initialize the package library
  147. CommandLine::Dispatch Cmds[] = {{NULL, NULL}};
  148. CommandLine CmdL;
  149. ParseCommandLine(CmdL, Cmds, Args, &_config, &_system, argc, argv, ShowHelp);
  150. // Match the operation
  151. for (unsigned int I = 0; I != CmdL.FileSize(); I++)
  152. if (DoIt(CmdL.FileList[I]) == false)
  153. break;
  154. // Print any errors or warnings found during parsing
  155. if (_error->empty() == false)
  156. {
  157. bool Errors = _error->PendingError();
  158. _error->DumpErrors();
  159. return Errors == true?100:0;
  160. }
  161. return 0;
  162. }
  163. /*}}}*/