first iteration that adds support for checksums-{sha512,sha256}
[ntk/apt.git] / apt-pkg / deb / debsrcrecords.cc
CommitLineData
11e7af84
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
c33b707e 3// $Id: debsrcrecords.cc,v 1.6 2004/03/17 05:58:54 mdz Exp $
11e7af84
AL
4/* ######################################################################
5
6 Debian Source Package Records - Parser implementation for Debian style
7 source indexes
8
9 ##################################################################### */
10 /*}}}*/
11// Include Files /*{{{*/
ea542140
DK
12#include <config.h>
13
b2e465d6 14#include <apt-pkg/deblistparser.h>
11e7af84
AL
15#include <apt-pkg/debsrcrecords.h>
16#include <apt-pkg/error.h>
36f610f1 17#include <apt-pkg/strutl.h>
b2e465d6 18#include <apt-pkg/configuration.h>
b0e1a43f 19#include <apt-pkg/aptconfiguration.h>
cb6a2b3e 20#include <apt-pkg/hashes.h>
4ab24e53
MV
21
22using std::max;
11e7af84
AL
23 /*}}}*/
24
8f3ba4e8
DK
25using std::string;
26
11e7af84
AL
27// SrcRecordParser::Binaries - Return the binaries field /*{{{*/
28// ---------------------------------------------------------------------
29/* This member parses the binaries field into a pair of class arrays and
30 returns a list of strings representing all of the components of the
31 binaries field. The returned array need not be freed and will be
b2e465d6
AL
32 reused by the next Binaries function call. This function is commonly
33 used during scanning to find the right package */
11e7af84
AL
34const char **debSrcRecordParser::Binaries()
35{
39fb1e24
DK
36 const char *Start, *End;
37 if (Sect.Find("Binary", Start, End) == false)
38 return NULL;
39 for (; isspace(*Start) != 0; ++Start);
40 if (Start >= End)
41 return NULL;
42
43 StaticBinList.clear();
44 free(Buffer);
45 Buffer = strndup(Start, End - Start);
c33b707e 46
39fb1e24
DK
47 char* bin = Buffer;
48 do {
49 char* binStartNext = strchrnul(bin, ',');
50 char* binEnd = binStartNext - 1;
51 for (; isspace(*binEnd) != 0; --binEnd)
52 binEnd = '\0';
53 StaticBinList.push_back(bin);
54 if (*binStartNext != ',')
55 break;
56 *binStartNext = '\0';
57 for (bin = binStartNext + 1; isspace(*bin) != 0; ++bin);
58 } while (*bin != '\0');
59 StaticBinList.push_back(NULL);
c33b707e 60
39fb1e24 61 return (const char **) &StaticBinList[0];
b2e465d6
AL
62}
63 /*}}}*/
64// SrcRecordParser::BuildDepends - Return the Build-Depends information /*{{{*/
65// ---------------------------------------------------------------------
66/* This member parses the build-depends information and returns a list of
67 package/version records representing the build dependency. The returned
68 array need not be freed and will be reused by the next call to this
69 function */
8f3ba4e8 70bool debSrcRecordParser::BuildDepends(std::vector<pkgSrcRecords::Parser::BuildDepRec> &BuildDeps,
41c81fd8 71 bool const &ArchOnly, bool const &StripMultiArch)
b2e465d6
AL
72{
73 unsigned int I;
74 const char *Start, *Stop;
75 BuildDepRec rec;
76 const char *fields[] = {"Build-Depends",
77 "Build-Depends-Indep",
78 "Build-Conflicts",
79 "Build-Conflicts-Indep"};
80
81 BuildDeps.clear();
11e7af84 82
b2e465d6 83 for (I = 0; I < 4; I++)
11e7af84 84 {
45430cbf
AL
85 if (ArchOnly && (I == 1 || I == 3))
86 continue;
87
b2e465d6
AL
88 if (Sect.Find(fields[I], Start, Stop) == false)
89 continue;
11e7af84 90
b2e465d6
AL
91 while (1)
92 {
93 Start = debListParser::ParseDepends(Start, Stop,
41c81fd8 94 rec.Package,rec.Version,rec.Op,true, StripMultiArch);
b2e465d6
AL
95
96 if (Start == 0)
97 return _error->Error("Problem parsing dependency: %s", fields[I]);
98 rec.Type = I;
99
100 if (rec.Package != "")
101 BuildDeps.push_back(rec);
102
103 if (Start == Stop)
104 break;
105 }
11e7af84
AL
106 }
107
b2e465d6 108 return true;
11e7af84
AL
109}
110 /*}}}*/
36f610f1
AL
111// SrcRecordParser::Files - Return a list of files for this source /*{{{*/
112// ---------------------------------------------------------------------
113/* This parses the list of files and returns it, each file is required to have
114 a complete source package */
8f3ba4e8 115bool debSrcRecordParser::Files(std::vector<pkgSrcRecords::File> &List)
36f610f1
AL
116{
117 List.erase(List.begin(),List.end());
cb6a2b3e
MV
118
119 const char *hash_field[] = { "Checksums-Sha512",
120 "Checksums-Sha256",
121 "Checksums-Sha1",
122 "Files", // historic name
123 NULL,
124 };
125 const char *hash_type[] = { "Sha512",
126 "Sha256",
127 "Sha1",
128 "md5sum",
129 NULL,
130 };
36f610f1 131
cb6a2b3e
MV
132 for (int i=0; hash_field[i] != NULL; i++)
133 {
134
135 string Files = Sect.FindS(hash_field[i]);
136 if (Files.empty() == true)
137 continue;
138
139 // Stash the / terminated directory prefix
140 string Base = Sect.FindS("Directory");
141 if (Base.empty() == false && Base[Base.length()-1] != '/')
142 Base += '/';
143
144 std::vector<std::string> const compExts = APT::Configuration::getCompressorExtensions();
145
146 // Iterate over the entire list grabbing each triplet
147 const char *C = Files.c_str();
148 while (*C != 0)
149 {
150 pkgSrcRecords::File F;
151 string Size;
152
153 // Parse each of the elements
154 std::string RawHash;
155 if (ParseQuoteWord(C, RawHash) == false ||
156 ParseQuoteWord(C, Size) == false ||
157 ParseQuoteWord(C, F.Path) == false)
158 return _error->Error("Error parsing '%s' record", hash_field[i]);
159 // assign full hash string
160 F.Hash = HashString(hash_type[i], RawHash).toStr();
161
162 // Parse the size and append the directory
163 F.Size = atoi(Size.c_str());
164 F.Path = Base + F.Path;
165
166 // Try to guess what sort of file it is we are getting.
167 string::size_type Pos = F.Path.length()-1;
168 while (1)
169 {
170 string::size_type Tmp = F.Path.rfind('.',Pos);
171 if (Tmp == string::npos)
172 break;
173 if (F.Type == "tar") {
174 // source v3 has extension 'debian.tar.*' instead of 'diff.*'
175 if (string(F.Path, Tmp+1, Pos-Tmp) == "debian")
176 F.Type = "diff";
177 break;
178 }
179 F.Type = string(F.Path,Tmp+1,Pos-Tmp);
180
181 if (std::find(compExts.begin(), compExts.end(), std::string(".").append(F.Type)) != compExts.end() ||
182 F.Type == "tar")
183 {
184 Pos = Tmp-1;
185 continue;
186 }
b2e465d6 187
cb6a2b3e
MV
188 break;
189 }
b2e465d6 190
cb6a2b3e
MV
191 List.push_back(F);
192 }
193 break;
36f610f1 194 }
cb6a2b3e 195 return (List.size() > 0);
36f610f1
AL
196}
197 /*}}}*/
7a9f09bd
MV
198// SrcRecordParser::~SrcRecordParser - Destructor /*{{{*/
199// ---------------------------------------------------------------------
200/* */
201debSrcRecordParser::~debSrcRecordParser()
202{
203 delete[] Buffer;
204}
205 /*}}}*/