summaryrefslogtreecommitdiffstats
path: root/src/libopkg/.svn/text-base/pkg_vec.c.svn-base
blob: 472962c9f88cd5237b909aa75d46545f37afbf7c (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
208
/* pkg_vec.c - the opkg package management system

   Steven M. Ayer

   Copyright (C) 2002 Compaq Computer Corporation

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.
*/

#include <stdio.h>
#include <fnmatch.h>

#include "pkg.h"
#include "opkg_message.h"
#include "libbb/libbb.h"

pkg_vec_t * pkg_vec_alloc(void)
{
    pkg_vec_t * vec = xcalloc(1, sizeof(pkg_vec_t));
    vec->pkgs = NULL;
    vec->len = 0;

    return vec;
}

void pkg_vec_free(pkg_vec_t *vec)
{
    if (!vec)
      return;

    if (vec->pkgs)
      free(vec->pkgs);

    free(vec);
}

/*
 * assumption: all names in a vector are identical
 * assumption: all version strings are trimmed,
 *             so identical versions have identical version strings,
 *             implying identical packages; let's marry these
 */
void pkg_vec_insert_merge(pkg_vec_t *vec, pkg_t *pkg, int set_status)
{
     int i;
     int found = 0;

     /* look for a duplicate pkg by name, version, and architecture */
     for (i = 0; i < vec->len; i++){
         opkg_msg(DEBUG2, "%s %s arch=%s vs. %s %s arch=%s.\n",
			pkg->name, pkg->version, pkg->architecture,
			vec->pkgs[i]->name, vec->pkgs[i]->version,
			vec->pkgs[i]->architecture);
	 /* if the name,ver,arch matches, or the name matches and the
	  * package is marked deinstall/hold  */
	  if ((!strcmp(pkg->name, vec->pkgs[i]->name))
	      && ((pkg->state_want == SW_DEINSTALL
		  && (pkg->state_flag & SF_HOLD))
	      || ((pkg_compare_versions(pkg, vec->pkgs[i]) == 0)
	      && (!strcmp(pkg->architecture, vec->pkgs[i]->architecture))))) {
	       found  = 1;
               opkg_msg(DEBUG2, "Duplicate for pkg=%s version=%s arch=%s.\n",
			pkg->name, pkg->version, pkg->architecture);
	       break;
	  }
     }

     /* we didn't find one, add it */
     if (!found){
          opkg_msg(DEBUG2, "Adding new pkg=%s version=%s arch=%s.\n",
			pkg->name, pkg->version, pkg->architecture);
          pkg_vec_insert(vec, pkg);
	  return;
     }

     /* update the one that we have */
     opkg_msg(DEBUG2, "Merging %s %s arch=%s, set_status=%d.\n",
			pkg->name, pkg->version, pkg->architecture, set_status);
     if (set_status) {
          /* This is from the status file,
	   * so need to merge with existing database */
          pkg_merge(pkg, vec->pkgs[i]);
     }

     /* overwrite the old one */
     pkg_deinit(vec->pkgs[i]);
     free(vec->pkgs[i]);
     vec->pkgs[i] = pkg;
}

void pkg_vec_insert(pkg_vec_t *vec, const pkg_t *pkg)
{
    vec->pkgs = xrealloc(vec->pkgs, (vec->len + 1) * sizeof(pkg_t *));
    vec->pkgs[vec->len] = (pkg_t *)pkg;
    vec->len++;
}

int pkg_vec_contains(pkg_vec_t *vec, pkg_t *apkg)
{
     int i;
     for (i = 0; i < vec->len; i++)
	  if (vec->pkgs[i] == apkg)
	       return 1;
     return 0;
}

void pkg_vec_sort(pkg_vec_t *vec, compare_fcn_t compar)
{
     qsort(vec->pkgs, vec->len, sizeof(pkg_t *), compar);
}

int pkg_vec_clear_marks(pkg_vec_t *vec)
{
     int npkgs = vec->len;
     int i;
     for (i = 0; i < npkgs; i++) {
	  pkg_t *pkg = vec->pkgs[i];
	  pkg->state_flag &= ~SF_MARKED;
     }
     return 0;
}

int pkg_vec_mark_if_matches(pkg_vec_t *vec, const char *pattern)
{
     int matching_count = 0;
     pkg_t **pkgs = vec->pkgs;
     int npkgs = vec->len;
     int i;
     for (i = 0; i < npkgs; i++) {
	  pkg_t *pkg = pkgs[i];
	  if (fnmatch(pattern, pkg->name, 0)==0) {
	       pkg->state_flag |= SF_MARKED;
	       matching_count++;
	  }
     }
     return matching_count;
}


abstract_pkg_vec_t * abstract_pkg_vec_alloc(void)
{
    abstract_pkg_vec_t * vec ;
    vec = xcalloc(1, sizeof(abstract_pkg_vec_t));
    vec->pkgs = NULL;
    vec->len = 0;

    return vec;
}

void abstract_pkg_vec_free(abstract_pkg_vec_t *vec)
{
    if (!vec)
      return;
    free(vec->pkgs);
    free(vec);
}

/*
 * assumption: all names in a vector are unique
 */
void abstract_pkg_vec_insert(abstract_pkg_vec_t *vec, abstract_pkg_t *pkg)
{
    vec->pkgs = xrealloc(vec->pkgs, (vec->len + 1) * sizeof(abstract_pkg_t *));
    vec->pkgs[vec->len] = pkg;
    vec->len++;
}

abstract_pkg_t * abstract_pkg_vec_get(abstract_pkg_vec_t *vec, int i)
{
    if (vec->len > i)
	return vec->pkgs[i];
    else
	return NULL;
}

int abstract_pkg_vec_contains(abstract_pkg_vec_t *vec, abstract_pkg_t *apkg)
{
     int i;
     for (i = 0; i < vec->len; i++)
	  if (vec->pkgs[i] == apkg)
	       return 1;
     return 0;
}

void abstract_pkg_vec_sort(pkg_vec_t *vec, compare_fcn_t compar)
{
     qsort(vec->pkgs, vec->len, sizeof(pkg_t *), compar);
}

int pkg_compare_names(const void *p1, const void *p2)
{
  const pkg_t *pkg1 = *(const pkg_t **)p1;
  const pkg_t *pkg2 = *(const pkg_t **)p2;
  if (pkg1->name == NULL)
    return 1;
  if (pkg2->name == NULL)
    return -1;
  return(strcmp(pkg1->name, pkg2->name));
}