]> git.saurik.com Git - apt.git/blob - apt-pkg/deb/debsrcrecords.cc
imbue datetime parsing with C.UTF-8 locale
[apt.git] / apt-pkg / deb / debsrcrecords.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: debsrcrecords.cc,v 1.6 2004/03/17 05:58:54 mdz Exp $
4 /* ######################################################################
5
6 Debian Source Package Records - Parser implementation for Debian style
7 source indexes
8
9 ##################################################################### */
10 /*}}}*/
11 // Include Files /*{{{*/
12 #include <config.h>
13
14 #include <apt-pkg/deblistparser.h>
15 #include <apt-pkg/debsrcrecords.h>
16 #include <apt-pkg/error.h>
17 #include <apt-pkg/strutl.h>
18 #include <apt-pkg/aptconfiguration.h>
19 #include <apt-pkg/srcrecords.h>
20 #include <apt-pkg/tagfile.h>
21 #include <apt-pkg/hashes.h>
22 #include <apt-pkg/gpgv.h>
23
24 #include <ctype.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <algorithm>
28 #include <string>
29 #include <vector>
30 /*}}}*/
31
32 using std::max;
33 using std::string;
34
35 debSrcRecordParser::debSrcRecordParser(std::string const &File,pkgIndexFile const *Index)
36 : Parser(Index), d(NULL), Tags(&Fd), iOffset(0), Buffer(NULL)
37 {
38 if (File.empty() == false)
39 {
40 if (Fd.Open(File, FileFd::ReadOnly, FileFd::Extension))
41 Tags.Init(&Fd, 102400);
42 }
43 }
44
45 // SrcRecordParser::Binaries - Return the binaries field /*{{{*/
46 // ---------------------------------------------------------------------
47 /* This member parses the binaries field into a pair of class arrays and
48 returns a list of strings representing all of the components of the
49 binaries field. The returned array need not be freed and will be
50 reused by the next Binaries function call. This function is commonly
51 used during scanning to find the right package */
52 const char **debSrcRecordParser::Binaries()
53 {
54 const char *Start, *End;
55 if (Sect.Find("Binary", Start, End) == false)
56 return NULL;
57 for (; isspace_ascii(*Start) != 0; ++Start);
58 if (Start >= End)
59 return NULL;
60
61 StaticBinList.clear();
62 free(Buffer);
63 Buffer = strndup(Start, End - Start);
64
65 char* bin = Buffer;
66 do {
67 char* binStartNext = strchrnul(bin, ',');
68 // Found a comma, clean up any space before it
69 if (binStartNext > Buffer) {
70 char* binEnd = binStartNext - 1;
71 for (; binEnd > Buffer && isspace_ascii(*binEnd) != 0; --binEnd)
72 *binEnd = 0;
73 }
74 StaticBinList.push_back(bin);
75 if (*binStartNext != ',')
76 break;
77 *binStartNext = '\0';
78 for (bin = binStartNext + 1; isspace_ascii(*bin) != 0; ++bin)
79 ;
80 } while (*bin != '\0');
81 StaticBinList.push_back(NULL);
82
83 return &StaticBinList[0];
84 }
85 /*}}}*/
86 // SrcRecordParser::BuildDepends - Return the Build-Depends information /*{{{*/
87 // ---------------------------------------------------------------------
88 /* This member parses the build-depends information and returns a list of
89 package/version records representing the build dependency. The returned
90 array need not be freed and will be reused by the next call to this
91 function */
92 bool debSrcRecordParser::BuildDepends(std::vector<pkgSrcRecords::Parser::BuildDepRec> &BuildDeps,
93 bool const &ArchOnly, bool const &StripMultiArch)
94 {
95 unsigned int I;
96 const char *Start, *Stop;
97 BuildDepRec rec;
98 const char *fields[] = {"Build-Depends",
99 "Build-Depends-Indep",
100 "Build-Conflicts",
101 "Build-Conflicts-Indep"};
102
103 BuildDeps.clear();
104
105 for (I = 0; I < 4; I++)
106 {
107 if (ArchOnly && (I == 1 || I == 3))
108 continue;
109
110 if (Sect.Find(fields[I], Start, Stop) == false)
111 continue;
112
113 while (1)
114 {
115 Start = debListParser::ParseDepends(Start, Stop,
116 rec.Package,rec.Version,rec.Op,true,StripMultiArch,true);
117
118 if (Start == 0)
119 return _error->Error("Problem parsing dependency: %s", fields[I]);
120 rec.Type = I;
121
122 if (rec.Package != "")
123 BuildDeps.push_back(rec);
124
125 if (Start == Stop)
126 break;
127 }
128 }
129
130 return true;
131 }
132 /*}}}*/
133 // SrcRecordParser::Files - Return a list of files for this source /*{{{*/
134 // ---------------------------------------------------------------------
135 /* This parses the list of files and returns it, each file is required to have
136 a complete source package */
137 bool debSrcRecordParser::Files(std::vector<pkgSrcRecords::File> &F)
138 {
139 std::vector<pkgSrcRecords::File2> F2;
140 if (Files2(F2) == false)
141 return false;
142 for (std::vector<pkgSrcRecords::File2>::const_iterator f2 = F2.begin(); f2 != F2.end(); ++f2)
143 {
144 pkgSrcRecords::File2 f;
145 #if __GNUC__ >= 4
146 #pragma GCC diagnostic push
147 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
148 #endif
149 f.MD5Hash = f2->MD5Hash;
150 f.Size = f2->Size;
151 #if __GNUC__ >= 4
152 #pragma GCC diagnostic pop
153 #endif
154 f.Path = f2->Path;
155 f.Type = f2->Type;
156 F.push_back(f);
157 }
158 return true;
159 }
160 bool debSrcRecordParser::Files2(std::vector<pkgSrcRecords::File2> &List)
161 {
162 List.clear();
163
164 // Stash the / terminated directory prefix
165 string Base = Sect.FindS("Directory");
166 if (Base.empty() == false && Base[Base.length()-1] != '/')
167 Base += '/';
168
169 std::vector<std::string> const compExts = APT::Configuration::getCompressorExtensions();
170
171 for (char const * const * type = HashString::SupportedHashes(); *type != NULL; ++type)
172 {
173 // derive field from checksum type
174 std::string checksumField("Checksums-");
175 if (strcmp(*type, "MD5Sum") == 0)
176 checksumField = "Files"; // historic name for MD5 checksums
177 else
178 checksumField.append(*type);
179
180 string const Files = Sect.FindS(checksumField.c_str());
181 if (Files.empty() == true)
182 continue;
183
184 // Iterate over the entire list grabbing each triplet
185 const char *C = Files.c_str();
186 while (*C != 0)
187 {
188 string hash, size, path;
189
190 // Parse each of the elements
191 if (ParseQuoteWord(C, hash) == false ||
192 ParseQuoteWord(C, size) == false ||
193 ParseQuoteWord(C, path) == false)
194 return _error->Error("Error parsing file record in %s of source package %s", checksumField.c_str(), Package().c_str());
195
196 HashString const hashString(*type, hash);
197 if (Base.empty() == false)
198 path = Base + path;
199
200 // look if we have a record for this file already
201 std::vector<pkgSrcRecords::File2>::iterator file = List.begin();
202 for (; file != List.end(); ++file)
203 if (file->Path == path)
204 break;
205
206 // we have it already, store the new hash and be done
207 if (file != List.end())
208 {
209 if (checksumField == "Files")
210 APT_IGNORE_DEPRECATED(file->MD5Hash = hash;)
211 // an error here indicates that we have two different hashes for the same file
212 if (file->Hashes.push_back(hashString) == false)
213 return _error->Error("Error parsing checksum in %s of source package %s", checksumField.c_str(), Package().c_str());
214 continue;
215 }
216
217 // we haven't seen this file yet
218 pkgSrcRecords::File2 F;
219 F.Path = path;
220 F.FileSize = strtoull(size.c_str(), NULL, 10);
221 F.Hashes.push_back(hashString);
222 F.Hashes.FileSize(F.FileSize);
223
224 APT_IGNORE_DEPRECATED_PUSH
225 F.Size = F.FileSize;
226 if (checksumField == "Files")
227 F.MD5Hash = hash;
228 APT_IGNORE_DEPRECATED_POP
229
230 // Try to guess what sort of file it is we are getting.
231 string::size_type Pos = F.Path.length()-1;
232 while (1)
233 {
234 string::size_type Tmp = F.Path.rfind('.',Pos);
235 if (Tmp == string::npos)
236 break;
237 if (F.Type == "tar") {
238 // source v3 has extension 'debian.tar.*' instead of 'diff.*'
239 if (string(F.Path, Tmp+1, Pos-Tmp) == "debian")
240 F.Type = "diff";
241 break;
242 }
243 F.Type = string(F.Path,Tmp+1,Pos-Tmp);
244
245 if (std::find(compExts.begin(), compExts.end(), std::string(".").append(F.Type)) != compExts.end() ||
246 F.Type == "tar")
247 {
248 Pos = Tmp-1;
249 continue;
250 }
251
252 break;
253 }
254 List.push_back(F);
255 }
256 }
257
258 return true;
259 }
260 /*}}}*/
261 // SrcRecordParser::~SrcRecordParser - Destructor /*{{{*/
262 // ---------------------------------------------------------------------
263 /* */
264 debSrcRecordParser::~debSrcRecordParser()
265 {
266 // was allocated via strndup()
267 free(Buffer);
268 }
269 /*}}}*/
270
271
272 debDscRecordParser::debDscRecordParser(std::string const &DscFile, pkgIndexFile const *Index)
273 : debSrcRecordParser("", Index)
274 {
275 // support clear signed files
276 if (OpenMaybeClearSignedFile(DscFile, Fd) == false)
277 {
278 _error->Error("Failed to open %s", DscFile.c_str());
279 return;
280 }
281
282 // re-init to ensure the updated Fd is used
283 Tags.Init(&Fd, pkgTagFile::SUPPORT_COMMENTS);
284 // read the first (and only) record
285 Step();
286
287 }