Update displayed copyright to 2023
[cygwin-setup.git] / IniDBBuilderPackage.cc
blob6305ac80ff1d11bfd100319d4d11394ce33d5ea8
1 /*
2 * Copyright (c) 2002, Robert Collins.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * A copy of the GNU General Public License can be found at
10 * http://www.gnu.org/
12 * Written by Robert Collins <rbtcollins@hotmail.com>
16 #include "IniDBBuilderPackage.h"
18 #include "csu_util/version_compare.h"
20 #include "setup_version.h"
22 #include "IniParseFeedback.h"
23 #include "package_db.h"
24 #include "package_meta.h"
25 #include "ini.h"
26 // for strtoul
27 #include <string.h>
28 #include "LogSingleton.h"
29 #include "PackageSpecification.h"
30 #include <algorithm>
32 IniDBBuilderPackage::IniDBBuilderPackage (IniParseFeedback const &aFeedback) :
33 currentSpec (0), _feedback (aFeedback), minimum_version_checked(FALSE) {}
35 IniDBBuilderPackage::~IniDBBuilderPackage()
37 process();
38 packagedb db;
39 db.solver.internalize();
42 void
43 IniDBBuilderPackage::buildTimestamp (const std::string& time)
45 timestamp = strtoul (time.c_str(), 0, 0);
48 void
49 IniDBBuilderPackage::buildVersion (const std::string& aVersion)
51 version = aVersion;
53 /* We shouldn't need to warn about potential setup.ini parse problems if we
54 exceed the version in setup-minimum-version: (we will still advise about a
55 possible setup upgrade in do_ini_thread()). If we don't exceed
56 setup-minimum-version:, we've already encountered a fatal error, so no need
57 to warn as well. */
58 if (minimum_version_checked)
59 return;
61 if (version.size())
63 if (version_compare(setup_version, version) < 0)
65 char old_vers[256];
66 snprintf (old_vers, sizeof old_vers,
67 "The current ini file is from a newer version of setup-%s.exe. "
68 "If you have any trouble installing, please download a fresh "
69 "version from https://cygwin.com/setup-%s.exe",
70 is_64bit ? "x86_64" : "x86",
71 is_64bit ? "x86_64" : "x86");
72 _feedback.warning(old_vers);
77 const std::string
78 IniDBBuilderPackage::buildMinimumVersion (const std::string& minimum)
80 minimum_version_checked = TRUE;
81 if (version_compare(setup_version, minimum) < 0)
83 char min_vers[256];
84 snprintf (min_vers, sizeof(min_vers),
85 "The current ini file requires at least version %s of setup.\n"
86 "Please download a newer version from https://cygwin.com/setup-%s.exe",
87 minimum.c_str(),
88 is_64bit ? "x86_64" : "x86");
89 return min_vers;
91 return "";
94 void
95 IniDBBuilderPackage::buildPackage (const std::string& _name)
97 process();
99 /* Reset for next package */
100 name = _name;
101 message_id = "";
102 message_string = "";
103 categories.clear();
104 replace_versions.clear();
106 cbpv.reponame = release;
107 cbpv.version = "";
108 cbpv.vendor = release;
109 cbpv.sdesc = "";
110 cbpv.ldesc = "";
111 cbpv.stability = TRUST_CURR;
112 cbpv.type = package_binary;
113 cbpv.spkg = PackageSpecification();
114 cbpv.spkg_id = packageversion();
115 cbpv.requires = NULL;
116 cbpv.obsoletes = NULL;
117 cbpv.provides = NULL;
118 cbpv.conflicts = NULL;
119 cbpv.archive = packagesource();
121 currentSpec = NULL;
122 currentNodeList = NULL;
123 dependsNodeList = PackageDepends();
124 obsoletesNodeList = PackageDepends();
125 providesNodeList = PackageDepends();
126 conflictsNodeList = PackageDepends();
127 #if DEBUG
128 Log (LOG_BABBLE) << "Created package " << name << endLog;
129 #endif
132 void
133 IniDBBuilderPackage::buildPackageVersion (const std::string& version)
135 cbpv.version = version;
138 void
139 IniDBBuilderPackage::buildPackageSDesc (const std::string& theDesc)
141 cbpv.sdesc = theDesc;
144 void
145 IniDBBuilderPackage::buildPackageLDesc (const std::string& theDesc)
147 cbpv.ldesc = theDesc;
150 void
151 IniDBBuilderPackage::buildPackageInstall (const std::string& path,
152 const std::string& size,
153 char *hash,
154 hashType type)
156 // set archive path, size, mirror, hash
157 cbpv.archive.set_canonical(path.c_str());
158 cbpv.archive.size = atoi(size.c_str());
159 cbpv.archive.sites.push_back(site(parse_mirror));
161 switch (type) {
162 case hashType::sha512:
163 if (hash && !cbpv.archive.sha512_isSet)
165 memcpy (cbpv.archive.sha512sum, hash, sizeof(cbpv.archive.sha512sum));
166 cbpv.archive.sha512_isSet = true;
168 break;
170 case hashType::md5:
171 if (hash && !cbpv.archive.md5.isSet())
172 cbpv.archive.md5.set((unsigned char *)hash);
173 break;
175 case hashType::none:
176 break;
180 void
181 IniDBBuilderPackage::buildPackageSource (const std::string& path,
182 const std::string& size,
183 char *hash,
184 hashType type)
186 /* When there is a source: line, we invent a package to contain the source,
187 and make it the source package for this package. */
189 /* create a source package version */
190 SolverPool::addPackageData cspv = cbpv;
191 cspv.type = package_source;
192 cspv.requires = NULL;
193 cspv.obsoletes = NULL;
194 cspv.provides = NULL;
195 cspv.conflicts = NULL;
197 /* set archive path, size, mirror, hash */
198 cspv.archive = packagesource();
199 cspv.archive.set_canonical(path.c_str());
200 cspv.archive.size = atoi(size.c_str());
201 cspv.archive.sites.push_back(site(parse_mirror));
203 switch (type) {
204 case hashType::sha512:
205 if (hash && !cspv.archive.sha512_isSet)
207 memcpy (cspv.archive.sha512sum, hash, sizeof(cspv.archive.sha512sum));
208 cspv.archive.sha512_isSet = true;
210 break;
212 case hashType::md5:
213 if (hash && !cspv.archive.md5.isSet())
214 cspv.archive.md5.set((unsigned char *)hash);
215 break;
217 case hashType::none:
218 break;
221 packagedb db;
222 packageversion spkg_id = db.addSource (name + "-src", cspv);
224 /* create relationship between binary and source packageversions */
225 cbpv.spkg = PackageSpecification(name + "-src");
226 cbpv.spkg_id = spkg_id;
229 void
230 IniDBBuilderPackage::buildPackageTrust (trusts newtrust)
232 process();
233 cbpv.stability = newtrust;
236 void
237 IniDBBuilderPackage::buildPackageCategory (const std::string& name)
239 categories.insert(name);
242 void
243 IniDBBuilderPackage::buildBeginDepends ()
245 #if DEBUG
246 Log (LOG_BABBLE) << "Beginning of a depends statement " << endLog;
247 #endif
248 currentSpec = NULL;
249 dependsNodeList = PackageDepends();
250 currentNodeList = &dependsNodeList;
251 cbpv.requires = &dependsNodeList;
254 void
255 IniDBBuilderPackage::buildBeginBuildDepends ()
257 #if DEBUG
258 Log (LOG_BABBLE) << "Beginning of a Build-Depends statement" << endLog;
259 #endif
260 currentSpec = NULL;
261 currentNodeList = NULL;
262 /* there is currently nowhere to store Build-Depends information */
265 void
266 IniDBBuilderPackage::buildBeginObsoletes ()
268 #if DEBUG
269 Log (LOG_BABBLE) << "Beginning of an obsoletes statement" << endLog;
270 #endif
271 currentSpec = NULL;
272 obsoletesNodeList = PackageDepends();
273 currentNodeList = &obsoletesNodeList;
274 cbpv.obsoletes = &obsoletesNodeList;
277 void
278 IniDBBuilderPackage::buildBeginProvides ()
280 #if DEBUG
281 Log (LOG_BABBLE) << "Beginning of a provides statement" << endLog;
282 #endif
283 currentSpec = NULL;
284 providesNodeList = PackageDepends();
285 currentNodeList = &providesNodeList;
286 cbpv.provides = &providesNodeList;
289 void
290 IniDBBuilderPackage::buildBeginConflicts ()
292 #if DEBUG
293 Log (LOG_BABBLE) << "Beginning of a conflicts statement" << endLog;
294 #endif
295 currentSpec = NULL;
296 conflictsNodeList = PackageDepends();
297 currentNodeList = &conflictsNodeList;
298 cbpv.conflicts = &conflictsNodeList;
301 void
302 IniDBBuilderPackage::buildSourceName (const std::string& _name)
304 // When there is a Source: line, that names a real source package
305 packagedb db;
306 cbpv.spkg = PackageSpecification(_name);
307 cbpv.spkg_id = db.findSourceVersion (PackageSpecification(_name, cbpv.version));
308 #if DEBUG
309 Log (LOG_BABBLE) << "\"" << _name << "\" is the source package for " << name << "." << endLog;
310 #endif
313 void
314 IniDBBuilderPackage::buildSourceNameVersion (const std::string& version)
316 // XXX: should be stored as sourceevr
319 void
320 IniDBBuilderPackage::buildPackageListNode (const std::string & name)
322 #if DEBUG
323 Log (LOG_BABBLE) << "New node '" << name << "' for package list" << endLog;
324 #endif
326 std::string actual_name = name;
327 if (name == "_self-destruct")
328 actual_name = "libsolv-self-destruct-pkg()";
330 currentSpec = new PackageSpecification (actual_name);
331 if (currentNodeList)
332 currentNodeList->push_back (currentSpec);
335 void
336 IniDBBuilderPackage::buildPackageListOperator (PackageSpecification::_operators const &_operator)
338 if (currentSpec)
340 currentSpec->setOperator (_operator);
341 #if DEBUG
342 Log (LOG_BABBLE) << "Current specification is " << *currentSpec << "." <<
343 endLog;
344 #endif
348 void
349 IniDBBuilderPackage::buildPackageListOperatorVersion (const std::string& aVersion)
351 if (currentSpec)
353 currentSpec->setVersion (aVersion);
354 #if DEBUG
355 Log (LOG_BABBLE) << "Current specification is " << *currentSpec << "." <<
356 endLog;
357 #endif
361 void
362 IniDBBuilderPackage::buildMessage (const std::string& _message_id, const std::string& _message_string)
364 message_id = _message_id;
365 message_string = _message_string;
368 void
369 IniDBBuilderPackage::buildPackageReplaceVersionsList (const std::string& version)
371 replace_versions.insert(version);
374 /* privates */
375 void
376 IniDBBuilderPackage::process ()
378 if (!name.size())
379 return;
381 if (cbpv.version.empty())
382 return;
384 // no install: line, no package
385 if (!cbpv.archive.Canonical())
386 return;
388 #if DEBUG
389 Log (LOG_BABBLE) << "Finished with package " << name << endLog;
390 Log (LOG_BABBLE) << "Version " << cbpv.version << endLog;
391 #endif
393 /* Transfer the accumulated package information to packagedb */
394 packagedb db;
395 packagemeta *pkg = db.addBinary (name, cbpv);
397 // For no good historical reason, some data lives in packagemeta rather than
398 // the packageversion
399 for (auto i = categories.begin(); i != categories.end(); i++)
401 pkg->add_category(*i);
403 pkg->set_message(message_id, message_string);
404 pkg->set_version_blacklist(replace_versions);
406 // Reset for next version
407 cbpv.version = "";
408 cbpv.type = package_binary;
409 cbpv.spkg = PackageSpecification();
410 cbpv.spkg_id = packageversion();
411 cbpv.archive = packagesource();
412 obsoletesNodeList = PackageDepends();
413 providesNodeList = PackageDepends();
414 conflictsNodeList = PackageDepends();