1 // -*- mode: cpp; mode: fold -*-
3 // $Id: debsrcrecords.cc,v 1.6 2004/03/17 05:58:54 mdz Exp $
4 /* ######################################################################
6 Debian Source Package Records - Parser implementation for Debian style
9 ##################################################################### */
11 // Include Files /*{{{*/
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>
35 debSrcRecordParser::debSrcRecordParser(std::string
const &File
,pkgIndexFile
const *Index
)
36 : Parser(Index
), d(NULL
), Tags(&Fd
), iOffset(0), Buffer(NULL
)
38 if (File
.empty() == false)
40 if (Fd
.Open(File
, FileFd::ReadOnly
, FileFd::Extension
))
41 Tags
.Init(&Fd
, 102400);
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()
54 const char *Start
, *End
;
55 if (Sect
.Find("Binary", Start
, End
) == false)
57 for (; isspace_ascii(*Start
) != 0; ++Start
);
61 StaticBinList
.clear();
63 Buffer
= strndup(Start
, End
- Start
);
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
)
74 StaticBinList
.push_back(bin
);
75 if (*binStartNext
!= ',')
78 for (bin
= binStartNext
+ 1; isspace_ascii(*bin
) != 0; ++bin
)
80 } while (*bin
!= '\0');
81 StaticBinList
.push_back(NULL
);
83 return &StaticBinList
[0];
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
92 bool debSrcRecordParser::BuildDepends(std::vector
<pkgSrcRecords::Parser::BuildDepRec
> &BuildDeps
,
93 bool const &ArchOnly
, bool const &StripMultiArch
)
96 const char *Start
, *Stop
;
98 const char *fields
[] = {"Build-Depends",
99 "Build-Depends-Indep",
101 "Build-Conflicts-Indep"};
105 for (I
= 0; I
< 4; I
++)
107 if (ArchOnly
&& (I
== 1 || I
== 3))
110 if (Sect
.Find(fields
[I
], Start
, Stop
) == false)
115 Start
= debListParser::ParseDepends(Start
, Stop
,
116 rec
.Package
,rec
.Version
,rec
.Op
,true,StripMultiArch
,true);
119 return _error
->Error("Problem parsing dependency: %s", fields
[I
]);
122 if (rec
.Package
!= "")
123 BuildDeps
.push_back(rec
);
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
)
139 std::vector
<pkgSrcRecords::File2
> F2
;
140 if (Files2(F2
) == false)
142 for (std::vector
<pkgSrcRecords::File2
>::const_iterator f2
= F2
.begin(); f2
!= F2
.end(); ++f2
)
144 pkgSrcRecords::File2 f
;
146 #pragma GCC diagnostic push
147 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
149 f
.MD5Hash
= f2
->MD5Hash
;
152 #pragma GCC diagnostic pop
160 bool debSrcRecordParser::Files2(std::vector
<pkgSrcRecords::File2
> &List
)
164 // Stash the / terminated directory prefix
165 string Base
= Sect
.FindS("Directory");
166 if (Base
.empty() == false && Base
[Base
.length()-1] != '/')
169 std::vector
<std::string
> const compExts
= APT::Configuration::getCompressorExtensions();
171 for (char const * const * type
= HashString::SupportedHashes(); *type
!= NULL
; ++type
)
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
178 checksumField
.append(*type
);
180 string
const Files
= Sect
.FindS(checksumField
.c_str());
181 if (Files
.empty() == true)
184 // Iterate over the entire list grabbing each triplet
185 const char *C
= Files
.c_str();
188 string hash
, size
, path
;
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());
196 HashString
const hashString(*type
, hash
);
197 if (Base
.empty() == false)
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
)
206 // we have it already, store the new hash and be done
207 if (file
!= List
.end())
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());
217 // we haven't seen this file yet
218 pkgSrcRecords::File2 F
;
220 F
.FileSize
= strtoull(size
.c_str(), NULL
, 10);
221 F
.Hashes
.push_back(hashString
);
222 F
.Hashes
.FileSize(F
.FileSize
);
224 APT_IGNORE_DEPRECATED_PUSH
226 if (checksumField
== "Files")
228 APT_IGNORE_DEPRECATED_POP
230 // Try to guess what sort of file it is we are getting.
231 string::size_type Pos
= F
.Path
.length()-1;
234 string::size_type Tmp
= F
.Path
.rfind('.',Pos
);
235 if (Tmp
== string::npos
)
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")
243 F
.Type
= string(F
.Path
,Tmp
+1,Pos
-Tmp
);
245 if (std::find(compExts
.begin(), compExts
.end(), std::string(".").append(F
.Type
)) != compExts
.end() ||
261 // SrcRecordParser::~SrcRecordParser - Destructor /*{{{*/
262 // ---------------------------------------------------------------------
264 debSrcRecordParser::~debSrcRecordParser()
266 // was allocated via strndup()
272 debDscRecordParser::debDscRecordParser(std::string
const &DscFile
, pkgIndexFile
const *Index
)
273 : debSrcRecordParser("", Index
)
275 // support clear signed files
276 if (OpenMaybeClearSignedFile(DscFile
, Fd
) == false)
278 _error
->Error("Failed to open %s", DscFile
.c_str());
282 // re-init to ensure the updated Fd is used
283 Tags
.Init(&Fd
, pkgTagFile::SUPPORT_COMMENTS
);
284 // read the first (and only) record