apt-sortpkgs.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 <apt-private/private-main.h>
  23. #include <vector>
  24. #include <algorithm>
  25. #include <stdio.h>
  26. #include <iostream>
  27. #include <string>
  28. #include <memory>
  29. #include <apti18n.h>
  30. /*}}}*/
  31. using namespace std;
  32. struct PkgName /*{{{*/
  33. {
  34. string Name;
  35. string Ver;
  36. string Arch;
  37. unsigned long Offset;
  38. unsigned long Length;
  39. inline int Compare3(const PkgName &x) const
  40. {
  41. int A = stringcasecmp(Name,x.Name);
  42. if (A == 0)
  43. {
  44. A = stringcasecmp(Ver,x.Ver);
  45. if (A == 0)
  46. A = stringcasecmp(Arch,x.Arch);
  47. }
  48. return A;
  49. }
  50. bool operator <(const PkgName &x) const {return Compare3(x) < 0;};
  51. bool operator >(const PkgName &x) const {return Compare3(x) > 0;};
  52. bool operator ==(const PkgName &x) const {return Compare3(x) == 0;};
  53. };
  54. /*}}}*/
  55. // DoIt - Sort a single file /*{{{*/
  56. // ---------------------------------------------------------------------
  57. /* */
  58. static bool DoIt(string InFile)
  59. {
  60. FileFd Fd(InFile,FileFd::ReadOnly);
  61. pkgTagFile Tags(&Fd);
  62. if (_error->PendingError() == true)
  63. return false;
  64. // Parse.
  65. vector<PkgName> List;
  66. pkgTagSection Section;
  67. unsigned long Largest = 0;
  68. unsigned long Offset = Tags.Offset();
  69. bool Source = _config->FindB("APT::SortPkgs::Source",false);
  70. while (Tags.Step(Section) == true)
  71. {
  72. PkgName Tmp;
  73. /* Fetch the name, auto-detecting if this is a source file or a
  74. package file */
  75. Tmp.Name = Section.FindS("Package");
  76. Tmp.Ver = Section.FindS("Version");
  77. Tmp.Arch = Section.FindS("Architecture");
  78. if (Tmp.Name.empty() == true)
  79. return _error->Error(_("Unknown package record!"));
  80. Tmp.Offset = Offset;
  81. Tmp.Length = Section.size();
  82. if (Largest < Tmp.Length)
  83. Largest = Tmp.Length;
  84. List.push_back(Tmp);
  85. Offset = Tags.Offset();
  86. }
  87. if (_error->PendingError() == true)
  88. return false;
  89. // Sort it
  90. sort(List.begin(),List.end());
  91. const char **Order = TFRewritePackageOrder;
  92. if (Source == true)
  93. Order = TFRewriteSourceOrder;
  94. // Emit
  95. FileFd stdoutfd;
  96. stdoutfd.OpenDescriptor(STDOUT_FILENO, FileFd::WriteOnly, false);
  97. auto const Buffer = std::unique_ptr<unsigned char[]>(new unsigned char[Largest+1]);
  98. for (vector<PkgName>::iterator I = List.begin(); I != List.end(); ++I)
  99. {
  100. // Read in the Record.
  101. if (Fd.Seek(I->Offset) == false || Fd.Read(Buffer.get(),I->Length) == false)
  102. return false;
  103. Buffer[I->Length] = '\n';
  104. if (Section.Scan((char *)Buffer.get(),I->Length+1) == false)
  105. return _error->Error("Internal error, failed to scan buffer");
  106. // Sort the section
  107. if (Section.Write(stdoutfd, Order) == false || stdoutfd.Write("\n", 1) == false)
  108. return _error->Error("Internal error, failed to sort fields");
  109. }
  110. return true;
  111. }
  112. /*}}}*/
  113. bool ShowHelp(CommandLine &, aptDispatchWithHelp const *) /*{{{*/
  114. {
  115. std::cout <<
  116. _("Usage: apt-sortpkgs [options] file1 [file2 ...]\n"
  117. "\n"
  118. "apt-sortpkgs is a simple tool to sort package files. The -s option is used\n"
  119. "to indicate what kind of file it is.\n"
  120. "\n"
  121. "Options:\n"
  122. " -h This help text\n"
  123. " -s Use source file sorting\n"
  124. " -c=? Read this configuration file\n"
  125. " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
  126. return true;
  127. }
  128. /*}}}*/
  129. std::vector<aptDispatchWithHelp> GetCommands() /*{{{*/
  130. {
  131. return {
  132. {nullptr, nullptr, nullptr}
  133. };
  134. }
  135. /*}}}*/
  136. int main(int argc,const char *argv[]) /*{{{*/
  137. {
  138. InitLocale();
  139. CommandLine CmdL;
  140. ParseCommandLine(CmdL, APT_CMD::APT_SORTPKG, &_config, &_system, argc, argv);
  141. // Match the operation
  142. for (unsigned int I = 0; I != CmdL.FileSize(); I++)
  143. if (DoIt(CmdL.FileList[I]) == false)
  144. break;
  145. return DispatchCommandLine(CmdL, {});
  146. }
  147. /*}}}*/