summaryrefslogtreecommitdiff
path: root/lib/rpmprob.c
blob: 266b9b7a8e43a04ba8776ba6b4ef99d521b5dafc (plain)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**
 * \file lib/rpmps.c
 */

#include "system.h"

#include <inttypes.h>
#include <stdlib.h>

#include <rpm/rpmstring.h>
#include <rpm/rpmprob.h>

#include "debug.h"

struct rpmProblem_s {
    char * pkgNEVR;
    char * altNEVR;
    fnpyKey key;
    rpmProblemType type;
    char * str1;
    uint64_t num1;
    int nrefs;
};

static rpmProblem rpmProblemUnlink(rpmProblem prob);

rpmProblem rpmProblemCreate(rpmProblemType type,
                            const char * pkgNEVR, fnpyKey key,
                            const char * altNEVR,
                            const char * str, uint64_t number)
{
    rpmProblem p = xcalloc(1, sizeof(*p));

    p->type = type;
    p->key = key;
    p->num1 = number;

    p->pkgNEVR = (pkgNEVR ? xstrdup(pkgNEVR) : NULL);
    p->altNEVR = (altNEVR ? xstrdup(altNEVR) : NULL);
    p->str1 = (str ? xstrdup(str) : NULL);

    return rpmProblemLink(p);
}

rpmProblem rpmProblemFree(rpmProblem prob)
{
    if (prob == NULL) return NULL;

    if (prob->nrefs > 1) {
	return rpmProblemUnlink(prob);
    }
    prob->pkgNEVR = _free(prob->pkgNEVR);
    prob->altNEVR = _free(prob->altNEVR);
    prob->str1 = _free(prob->str1);
    free(prob);
    return NULL;
}

rpmProblem rpmProblemLink(rpmProblem prob)
{
    if (prob) {
	prob->nrefs++;
    }
    return prob;
}

static rpmProblem rpmProblemUnlink(rpmProblem prob)
{
    if (prob) {
	prob->nrefs--;
    }
    return NULL;
}

const char * rpmProblemGetPkgNEVR(rpmProblem p)
{
    return (p->pkgNEVR);
}

const char * rpmProblemGetAltNEVR(rpmProblem p)
{
    return (p->altNEVR);
}

fnpyKey rpmProblemGetKey(rpmProblem p)
{
    return (p->key);
}

rpmProblemType rpmProblemGetType(rpmProblem p)
{
    return (p->type);
}

const char * rpmProblemGetStr(rpmProblem p)
{
    return (p->str1);
}

rpm_loff_t rpmProblemGetDiskNeed(rpmProblem p)
{
    return (p->num1);
}

char * rpmProblemString(rpmProblem prob)
{
    const char * pkgNEVR = (prob->pkgNEVR ? prob->pkgNEVR : "?pkgNEVR?");
    const char * altNEVR = (prob->altNEVR ? prob->altNEVR : "? ?altNEVR?");
    const char * str1 = (prob->str1 ? prob->str1 : N_("different"));
    char * buf = NULL;

    switch (prob->type) {
    case RPMPROB_BADARCH:
	rasprintf(&buf, _("package %s is intended for a %s architecture"),
		pkgNEVR, str1);
	break;
    case RPMPROB_BADOS:
	rasprintf(&buf, _("package %s is intended for a %s operating system"),
		pkgNEVR, str1);
	break;
    case RPMPROB_PKG_INSTALLED:
	rasprintf(&buf, _("package %s is already installed"), pkgNEVR);
	break;
    case RPMPROB_BADRELOCATE:
	rasprintf(&buf, _("path %s in package %s is not relocatable"),
		str1, pkgNEVR);
	break;
    case RPMPROB_NEW_FILE_CONFLICT:
	rasprintf(&buf, 
		_("file %s conflicts between attempted installs of %s and %s"),
		str1, pkgNEVR, altNEVR);
	break;
    case RPMPROB_FILE_CONFLICT:
	rasprintf(&buf,
	    _("file %s from install of %s conflicts with file from package %s"),
		str1, pkgNEVR, altNEVR);
	break;
    case RPMPROB_OLDPACKAGE:
	rasprintf(&buf,
		_("package %s (which is newer than %s) is already installed"),
		altNEVR, pkgNEVR);
	break;
    case RPMPROB_DISKSPACE:
	rasprintf(&buf,
	    _("installing package %s needs %" PRIu64 "%cB on the %s filesystem"),
		pkgNEVR,
		prob->num1 > (1024*1024)
		    ? (prob->num1 + 1024 * 1024 - 1) / (1024 * 1024)
		    : (prob->num1 + 1023) / 1024,
		prob->num1 > (1024*1024) ? 'M' : 'K',
		str1);
	break;
    case RPMPROB_DISKNODES:
	rasprintf(&buf,
	    _("installing package %s needs %" PRIu64 " inodes on the %s filesystem"),
		pkgNEVR, prob->num1, str1);
	break;
    case RPMPROB_REQUIRES:
	rasprintf(&buf, _("%s is needed by %s%s"),
		  prob->str1, (prob->num1 ? _("(installed) ") : ""), altNEVR);
	break;
    case RPMPROB_CONFLICT:
	rasprintf(&buf, _("%s conflicts with %s%s"),
		  prob->str1, (prob->num1 ? _("(installed) ") : ""), altNEVR);
	break;
    case RPMPROB_OBSOLETES:
	rasprintf(&buf, _("%s is obsoleted by %s%s"),
		  prob->str1, (prob->num1 ? _("(installed) ") : ""), altNEVR);
	break;
    default:
	rasprintf(&buf,
		_("unknown error %d encountered while manipulating package %s"),
		prob->type, pkgNEVR);
	break;
    }

    return buf;
}

static int cmpStr(const char *s1, const char *s2)
{
    if (s1 == s2) return 0;
    if (s1 && s2) return strcmp(s1, s2);
    return 1;
}

int rpmProblemCompare(rpmProblem ap, rpmProblem bp)
{
    if (ap == bp)
	return 0;
    if (ap == NULL || bp == NULL)
	return 1;
    if (ap->type != bp->type)
	return 1;
    if (ap->key != bp->key)
	return 1;
    if (ap->num1 != bp->num1)
	return 1;
    if (cmpStr(ap->pkgNEVR, bp->pkgNEVR))
	return 1;
    if (cmpStr(ap->altNEVR, bp->altNEVR))
	return 1;
    if (cmpStr(ap->str1, bp->str1))
	return 1;

    return 0;
}