1 /* pkg_vec.c - the itsy package management system
5 Copyright (C) 2002 Compaq Computer Corporation
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2, or (at
10 your option) any later version.
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
24 pkg_vec_t
* pkg_vec_alloc(void)
26 pkg_vec_t
* vec
= (pkg_vec_t
*)malloc(sizeof(pkg_vec_t
));
28 fprintf(stderr
, "%s: out of memory\n", __FUNCTION__
);
37 void pkg_vec_free(pkg_vec_t
*vec
)
44 * assumption: all names in a vector are identical
45 * assumption: all version strings are trimmed,
46 * so identical versions have identical version strings,
47 * implying identical packages; let's marry these
49 pkg_t
*pkg_vec_insert_merge(pkg_vec_t
*vec
, pkg_t
*pkg
, int set_status
,opkg_conf_t
*conf
)
54 /* look for a duplicate pkg by name, version, and architecture */
55 for (i
= 0; i
< vec
->len
; i
++){
56 opkg_message(conf
, OPKG_DEBUG2
, "Function: %s. Found pkg=%s version=%s arch=%s cmp=%s version=%s arch=%s \n",
57 __FUNCTION__
, pkg
->name
, pkg
->version
, pkg
->architecture
,
58 vec
->pkgs
[i
]->name
, vec
->pkgs
[i
]->version
,vec
->pkgs
[i
]->architecture
);
59 if ((strcmp(pkg
->name
, vec
->pkgs
[i
]->name
) == 0)
60 && (pkg_compare_versions(pkg
, vec
->pkgs
[i
]) == 0)
61 && (strcmp(pkg
->architecture
, vec
->pkgs
[i
]->architecture
) == 0)) {
63 opkg_message(conf
, OPKG_DEBUG2
, "Function: %s. Found duplicate for pkg=%s version=%s arch=%s\n",
64 __FUNCTION__
, pkg
->name
, pkg
->version
, pkg
->architecture
);
69 /* we didn't find one, add it */
71 opkg_message(conf
, OPKG_DEBUG2
, "Function: %s. Adding new pkg=%s version=%s arch=%s\n",
72 __FUNCTION__
, pkg
->name
, pkg
->version
, pkg
->architecture
);
74 vec
->pkgs
= (pkg_t
**)realloc(vec
->pkgs
, (vec
->len
+ 1) * sizeof(pkg_t
*));
75 vec
->pkgs
[vec
->len
] = pkg
;
79 /* update the one that we have */
81 opkg_message(conf
, OPKG_DEBUG2
, "Function: %s. calling pkg_merge for pkg=%s version=%s arch=%s",
82 __FUNCTION__
, pkg
->name
, pkg
->version
, pkg
->architecture
);
84 /* this is from the status file, so need to merge with existing database */
85 opkg_message(conf
, OPKG_DEBUG2
, " with set_status\n");
86 pkg_merge(vec
->pkgs
[i
], pkg
, set_status
);
87 /* XXX: CLEANUP: It's not so polite to free something here
88 that was passed in from above. */
92 opkg_message(conf
, OPKG_DEBUG2
, " WITHOUT set_status\n");
93 /* just overwrite the old one */
94 pkg_deinit(vec
->pkgs
[i
]);
102 void pkg_vec_insert(pkg_vec_t
*vec
, const pkg_t
*pkg
)
108 /* look for a duplicate pkg by name, version, and architecture */
109 for (i
= 0; i
< vec
->len
; i
++)
110 if ((strcmp(pkg
->name
, vec
->pkgs
[i
]->name
) == 0)
111 && (pkg_compare_versions(pkg
, vec
->pkgs
[i
]) == 0)
112 && (strcmp(pkg
->architecture
, vec
->pkgs
[i
]->name
) == 0)) {
118 /* we didn't find one, add it */
120 vec
->pkgs
= (pkg_t
**)realloc(vec
->pkgs
, (vec
->len
+ 1) * sizeof(pkg_t
*));
121 *(const pkg_t
**)&vec
->pkgs
[vec
->len
] = pkg
;
126 int pkg_vec_contains(pkg_vec_t
*vec
, pkg_t
*apkg
)
129 for (i
= 0; i
< vec
->len
; i
++)
130 if (vec
->pkgs
[i
] == apkg
)
135 typedef int (*compare_fcn_t
)(const void *, const void *);
136 void pkg_vec_sort(pkg_vec_t
*vec
, int (*compar
)(pkg_t
*, pkg_t
*))
138 qsort(vec
->pkgs
, vec
->len
, sizeof(pkg_t
*), (compare_fcn_t
)compar
);
141 int pkg_vec_clear_marks(pkg_vec_t
*vec
)
143 int npkgs
= vec
->len
;
145 for (i
= 0; i
< npkgs
; i
++) {
146 pkg_t
*pkg
= vec
->pkgs
[i
];
147 pkg
->state_flag
&= ~SF_MARKED
;
152 int pkg_vec_mark_if_matches(pkg_vec_t
*vec
, const char *pattern
)
154 int matching_count
= 0;
155 pkg_t
**pkgs
= vec
->pkgs
;
156 int npkgs
= vec
->len
;
158 for (i
= 0; i
< npkgs
; i
++) {
159 pkg_t
*pkg
= pkgs
[i
];
160 if (fnmatch(pattern
, pkg
->name
, 0)==0) {
161 pkg
->state_flag
|= SF_MARKED
;
165 return matching_count
;
169 abstract_pkg_vec_t
* abstract_pkg_vec_alloc(void)
171 abstract_pkg_vec_t
* vec
;
172 vec
= (abstract_pkg_vec_t
*)malloc(sizeof(abstract_pkg_vec_t
));
174 fprintf(stderr
, "%s: out of memory\n", __FUNCTION__
);
183 void abstract_pkg_vec_free(abstract_pkg_vec_t
*vec
)
190 * assumption: all names in a vector are unique
192 void abstract_pkg_vec_insert(abstract_pkg_vec_t
*vec
, abstract_pkg_t
*pkg
)
197 /* look for a duplicate pkg by name */
198 for(i
= 0; i
< vec
->len
; i
++)
199 if (strcmp(pkg
->name
, vec
->pkgs
[i
]->name
) == 0)
202 /* we didn't find one, add it */
207 realloc(vec
->pkgs
, (vec
->len
+ 1) * sizeof(abstract_pkg_t
*));
208 vec
->pkgs
[vec
->len
] = pkg
;
215 abstract_pkg_t
* abstract_pkg_vec_get(abstract_pkg_vec_t
*vec
, int i
)
223 int abstract_pkg_vec_contains(abstract_pkg_vec_t
*vec
, abstract_pkg_t
*apkg
)
226 for (i
= 0; i
< vec
->len
; i
++)
227 if (vec
->pkgs
[i
] == apkg
)
232 void abstract_pkg_vec_sort(pkg_vec_t
*vec
, int (*compar
)(abstract_pkg_t
*, abstract_pkg_t
*))
234 qsort(vec
->pkgs
, vec
->len
, sizeof(pkg_t
*), (compare_fcn_t
)compar
);