]>
git.saurik.com Git - apt.git/blob - cmdline/apt-sortpkgs.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: apt-sortpkgs.cc,v 1.5 2003/01/11 07:18:44 jgg Exp $
4 /* ######################################################################
6 APT Sort Packages - Program to sort Package and Source files
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.
12 ##################################################################### */
14 // Include Files /*{{{*/
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>
23 #include <apt-pkg/fileutl.h>
24 #include <apt-pkg/pkgsystem.h>
26 #include <apt-private/private-cmndline.h>
27 #include <apt-private/private-main.h>
41 struct PkgName
/*{{{*/
49 inline int Compare3(const PkgName
&x
) const
51 int A
= stringcasecmp(Name
,x
.Name
);
54 A
= stringcasecmp(Ver
,x
.Ver
);
56 A
= stringcasecmp(Arch
,x
.Arch
);
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;};
66 // DoIt - Sort a single file /*{{{*/
67 // ---------------------------------------------------------------------
69 static bool DoIt(string InFile
)
71 FileFd
Fd(InFile
,FileFd::ReadOnly
);
73 if (_error
->PendingError() == true)
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)
86 /* Fetch the name, auto-detecting if this is a source file or a
88 Tmp
.Name
= Section
.FindS("Package");
89 Tmp
.Ver
= Section
.FindS("Version");
90 Tmp
.Arch
= Section
.FindS("Architecture");
92 if (Tmp
.Name
.empty() == true)
93 return _error
->Error(_("Unknown package record!"));
96 Tmp
.Length
= Section
.size();
97 if (Largest
< Tmp
.Length
)
102 Offset
= Tags
.Offset();
104 if (_error
->PendingError() == true)
108 sort(List
.begin(),List
.end());
110 const char **Order
= TFRewritePackageOrder
;
112 Order
= TFRewriteSourceOrder
;
116 stdoutfd
.OpenDescriptor(STDOUT_FILENO
, FileFd::WriteOnly
, false);
117 auto const Buffer
= std::unique_ptr
<unsigned char[]>(new unsigned char[Largest
+1]);
118 for (vector
<PkgName
>::iterator I
= List
.begin(); I
!= List
.end(); ++I
)
120 // Read in the Record.
121 if (Fd
.Seek(I
->Offset
) == false || Fd
.Read(Buffer
.get(),I
->Length
) == false)
124 Buffer
[I
->Length
] = '\n';
125 if (Section
.Scan((char *)Buffer
.get(),I
->Length
+1) == false)
126 return _error
->Error("Internal error, failed to scan buffer");
129 if (Section
.Write(stdoutfd
, Order
) == false || stdoutfd
.Write("\n", 1) == false)
130 return _error
->Error("Internal error, failed to sort fields");
135 static bool ShowHelp(CommandLine
&) /*{{{*/
138 _("Usage: apt-sortpkgs [options] file1 [file2 ...]\n"
140 "apt-sortpkgs is a simple tool to sort package information files.\n"
141 "By default it sorts by binary package information, but the -s option\n"
142 "can be used to switch to source package ordering instead.\n");
146 static std::vector
<aptDispatchWithHelp
> GetCommands() /*{{{*/
149 {nullptr, nullptr, nullptr}
153 int main(int argc
,const char *argv
[]) /*{{{*/
158 ParseCommandLine(CmdL
, APT_CMD::APT_SORTPKG
, &_config
, &_system
, argc
, argv
, &ShowHelp
, &GetCommands
);
160 // Match the operation
161 for (unsigned int I
= 0; I
!= CmdL
.FileSize(); I
++)
162 if (DoIt(CmdL
.FileList
[I
]) == false)
165 return DispatchCommandLine(CmdL
, {});