-
-
Notifications
You must be signed in to change notification settings - Fork 339
/
update_package_requires.cpp
177 lines (140 loc) · 5.07 KB
/
update_package_requires.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de.
* Distributed under the terms of the MIT License.
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <list>
#include <map>
#include <package/PackageInfo.h>
#include <package/PackageResolvable.h>
#include <package/PackageResolvableExpression.h>
#include <package/RepositoryCache.h>
using namespace BPackageKit;
typedef std::list<BPackageResolvable*> ProvidesList;
static const char* sProgramName = "update_package_requires";
#define DIE(result, msg...) \
do { \
fprintf(stderr, "*** " msg); \
fprintf(stderr, " : %s\n", strerror(result)); \
exit(5); \
} while(0)
static void
print_usage_and_exit(bool error)
{
fprintf(error ? stderr : stdout,
"Usage: %s <package info> <repository>\n"
"Updates the versions in the \"requires\" list of the given package\n"
"info file using the available package information from the given\n"
"repository cache file <repository>.\n",
sProgramName);
exit(error ? 1 : 0);
}
static void
update_requires_expression(BPackageResolvableExpression& expression,
const ProvidesList& providesList)
{
// find the best-matching provides
BPackageResolvable* bestProvides = NULL;
for (ProvidesList::const_iterator it = providesList.begin();
it != providesList.end(); ++it) {
BPackageResolvable* provides = *it;
if (!expression.Matches(*provides))
continue;
if (bestProvides == NULL || bestProvides->Version().InitCheck() != B_OK
|| (provides->Version().InitCheck() == B_OK
&& provides->Version().Compare(bestProvides->Version()) > 0)) {
bestProvides = provides;
}
}
if (bestProvides == NULL || bestProvides->Version().InitCheck() != B_OK)
return;
// Update the expression. Enforce the minimum found version, if the requires
// has no version requirement or also a minimum. Otherwise enforce the exact
// version found.
BPackageResolvableOperator newOperator = B_PACKAGE_RESOLVABLE_OP_EQUAL;
switch (expression.Operator()) {
case B_PACKAGE_RESOLVABLE_OP_LESS:
case B_PACKAGE_RESOLVABLE_OP_LESS_EQUAL:
case B_PACKAGE_RESOLVABLE_OP_EQUAL:
case B_PACKAGE_RESOLVABLE_OP_NOT_EQUAL:
break;
case B_PACKAGE_RESOLVABLE_OP_GREATER_EQUAL:
case B_PACKAGE_RESOLVABLE_OP_GREATER:
case B_PACKAGE_RESOLVABLE_OP_ENUM_COUNT:
newOperator = B_PACKAGE_RESOLVABLE_OP_GREATER_EQUAL;
break;
}
expression.SetTo(expression.Name(), newOperator, bestProvides->Version());
}
int
main(int argc, const char* const* argv)
{
if (argc != 3)
print_usage_and_exit(true);
if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0)
print_usage_and_exit(false);
const char* const packageInfoPath = argv[1];
const char* const repositoryCachePath = argv[2];
// read the repository cache
BRepositoryCache repositoryCache;
status_t error = repositoryCache.SetTo(repositoryCachePath);
if (error != B_OK) {
DIE(error, "failed to read repository cache file \"%s\"",
repositoryCachePath);
}
// create a map for all provides (name -> resolvable list)
typedef std::map<BString, ProvidesList> ProvidesMap;
ProvidesMap providesMap;
for (BRepositoryCache::Iterator it = repositoryCache.GetIterator();
const BPackageInfo* info = it.Next();) {
const BObjectList<BPackageResolvable>& provides = info->ProvidesList();
int32 count = provides.CountItems();
for (int32 i = 0; i < count; i++) {
BPackageResolvable* resolvable = provides.ItemAt(i);
ProvidesList& providesList = providesMap[resolvable->Name()];
providesList.push_back(resolvable);
}
}
// load the package info
BPackageInfo packageInfo;
error = packageInfo.ReadFromConfigFile(packageInfoPath);
if (error != B_OK)
DIE(error, "failed to read package info file \"%s\"", packageInfoPath);
// clone the package info's requires list
typedef std::list<BPackageResolvableExpression> RequiresList;
RequiresList requiresList;
int32 requiresCount = packageInfo.RequiresList().CountItems();
for (int32 i = 0; i < requiresCount; i++)
requiresList.push_back(*packageInfo.RequiresList().ItemAt(i));
// rebuild the requires list with updated versions
packageInfo.ClearRequiresList();
for (RequiresList::iterator it = requiresList.begin();
it != requiresList.end(); ++it) {
BPackageResolvableExpression expression = *it;
ProvidesMap::iterator foundIt = providesMap.find(expression.Name());
if (foundIt != providesMap.end())
update_requires_expression(expression, foundIt->second);
error = packageInfo.AddRequires(expression);
if (error != B_OK)
DIE(error, "failed to add requires item to package info");
}
// write updated package info
BString configString;
error = packageInfo.GetConfigString(configString);
if (error != B_OK)
DIE(error, "failed to get updated package info string");
FILE* file = fopen(packageInfoPath, "w");
if (file == NULL) {
DIE(errno, "failed to open package info file \"%s\" for writing",
packageInfoPath);
}
if (fwrite(configString.String(), configString.Length(), 1, file) != 1) {
DIE(errno, "failed to write updated package info file \"%s\"",
packageInfoPath);
}
fclose(file);
return 0;
}