]> git.saurik.com Git - apt.git/blame - cmdline/apt-sortpkgs.cc
do not sent Last-Modified if we expect a changed file
[apt.git] / cmdline / apt-sortpkgs.cc
CommitLineData
b2e465d6
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
8f312f45 3// $Id: apt-sortpkgs.cc,v 1.5 2003/01/11 07:18:44 jgg Exp $
b2e465d6
AL
4/* ######################################################################
5
6 APT Sort Packages - Program to sort Package and Source files
7
8 This program is quite simple, it just sorts the package files by
9 package and sorts the fields inside by the internal APT sort order.
10 Input is taken from a named file and sent to stdout.
11
12 ##################################################################### */
13 /*}}}*/
14// Include Files /*{{{*/
ea542140
DK
15#include <config.h>
16
b2e465d6
AL
17#include <apt-pkg/tagfile.h>
18#include <apt-pkg/error.h>
19#include <apt-pkg/configuration.h>
20#include <apt-pkg/cmndline.h>
21#include <apt-pkg/init.h>
22#include <apt-pkg/strutl.h>
472ff00e
DK
23#include <apt-pkg/fileutl.h>
24#include <apt-pkg/pkgsystem.h>
b2e465d6 25
ad7e0941 26#include <apt-private/private-cmndline.h>
e7e10e47 27#include <apt-private/private-main.h>
ad7e0941 28
b2e465d6
AL
29#include <vector>
30#include <algorithm>
453b82a3
DK
31#include <stdio.h>
32#include <iostream>
33#include <string>
7414af7f 34#include <memory>
ea542140
DK
35
36#include <apti18n.h>
b2e465d6
AL
37 /*}}}*/
38
8f312f45
AL
39using namespace std;
40
92fcbfc1 41struct PkgName /*{{{*/
b2e465d6
AL
42{
43 string Name;
44 string Ver;
45 string Arch;
46 unsigned long Offset;
47 unsigned long Length;
48
49 inline int Compare3(const PkgName &x) const
50 {
51 int A = stringcasecmp(Name,x.Name);
52 if (A == 0)
53 {
54 A = stringcasecmp(Ver,x.Ver);
55 if (A == 0)
56 A = stringcasecmp(Arch,x.Arch);
57 }
58 return A;
59 }
60
61 bool operator <(const PkgName &x) const {return Compare3(x) < 0;};
62 bool operator >(const PkgName &x) const {return Compare3(x) > 0;};
63 bool operator ==(const PkgName &x) const {return Compare3(x) == 0;};
64};
92fcbfc1 65 /*}}}*/
b2e465d6
AL
66// DoIt - Sort a single file /*{{{*/
67// ---------------------------------------------------------------------
68/* */
c3ccac92 69static bool DoIt(string InFile)
b2e465d6
AL
70{
71 FileFd Fd(InFile,FileFd::ReadOnly);
72 pkgTagFile Tags(&Fd);
73 if (_error->PendingError() == true)
74 return false;
75
76 // Parse.
77 vector<PkgName> List;
78 pkgTagSection Section;
79 unsigned long Largest = 0;
80 unsigned long Offset = Tags.Offset();
81 bool Source = _config->FindB("APT::SortPkgs::Source",false);
82 while (Tags.Step(Section) == true)
83 {
84 PkgName Tmp;
85
86 /* Fetch the name, auto-detecting if this is a source file or a
87 package file */
88 Tmp.Name = Section.FindS("Package");
89 Tmp.Ver = Section.FindS("Version");
90 Tmp.Arch = Section.FindS("Architecture");
91
92 if (Tmp.Name.empty() == true)
93 return _error->Error(_("Unknown package record!"));
94
95 Tmp.Offset = Offset;
96 Tmp.Length = Section.size();
97 if (Largest < Tmp.Length)
98 Largest = Tmp.Length;
99
100 List.push_back(Tmp);
101
102 Offset = Tags.Offset();
103 }
104 if (_error->PendingError() == true)
105 return false;
106
107 // Sort it
108 sort(List.begin(),List.end());
109
110 const char **Order = TFRewritePackageOrder;
111 if (Source == true)
112 Order = TFRewriteSourceOrder;
88593886 113
b2e465d6 114 // Emit
88593886
DK
115 FileFd stdoutfd;
116 stdoutfd.OpenDescriptor(STDOUT_FILENO, FileFd::WriteOnly, false);
7414af7f 117 auto const Buffer = std::unique_ptr<unsigned char[]>(new unsigned char[Largest+1]);
91c03d37 118 for (vector<PkgName>::iterator I = List.begin(); I != List.end(); ++I)
b2e465d6
AL
119 {
120 // Read in the Record.
7414af7f 121 if (Fd.Seek(I->Offset) == false || Fd.Read(Buffer.get(),I->Length) == false)
b2e465d6 122 return false;
88593886
DK
123
124 Buffer[I->Length] = '\n';
7414af7f 125 if (Section.Scan((char *)Buffer.get(),I->Length+1) == false)
b2e465d6 126 return _error->Error("Internal error, failed to scan buffer");
b2e465d6
AL
127
128 // Sort the section
88593886 129 if (Section.Write(stdoutfd, Order) == false || stdoutfd.Write("\n", 1) == false)
b2e465d6 130 return _error->Error("Internal error, failed to sort fields");
b2e465d6 131 }
b2e465d6
AL
132 return true;
133}
134 /*}}}*/
8561c2fe 135bool ShowHelp(CommandLine &) /*{{{*/
b2e465d6 136{
41d39345 137 std::cout <<
b2e465d6
AL
138 _("Usage: apt-sortpkgs [options] file1 [file2 ...]\n"
139 "\n"
8561c2fe
DK
140 "apt-sortpkgs is a simple tool to sort package information files.\n"
141 "It sorts by default by binary package information, but the -s option\n"
142 "can be used to switch to source package ordering instead.\n");
ad7e0941 143 return true;
b2e465d6
AL
144}
145 /*}}}*/
6079b276 146std::vector<aptDispatchWithHelp> GetCommands() /*{{{*/
011188e3
DK
147{
148 return {
149 {nullptr, nullptr, nullptr}
150 };
151}
152 /*}}}*/
92fcbfc1 153int main(int argc,const char *argv[]) /*{{{*/
b2e465d6 154{
e7e10e47 155 InitLocale();
67111687 156
ad7e0941 157 CommandLine CmdL;
011188e3 158 ParseCommandLine(CmdL, APT_CMD::APT_SORTPKG, &_config, &_system, argc, argv);
b2e465d6
AL
159
160 // Match the operation
161 for (unsigned int I = 0; I != CmdL.FileSize(); I++)
162 if (DoIt(CmdL.FileList[I]) == false)
163 break;
e7e10e47 164
011188e3 165 return DispatchCommandLine(CmdL, {});
b2e465d6 166}
92fcbfc1 167 /*}}}*/