4 * Copyright (c) 2006-2012 Pacman Development Team <pacman-dev@archlinux.org>
5 * Copyright (c) 2007-2006 by Judd Vinet <jvinet@zeroflux.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <stdint.h> /* intmax_t */
25 #include <sys/types.h>
30 #include "alpm_list.h"
35 static alpm_list_t
*graph_init(alpm_list_t
*deltas
, int reverse
)
38 alpm_list_t
*vertices
= NULL
;
39 /* create the vertices */
40 for(i
= deltas
; i
; i
= i
->next
) {
41 alpm_graph_t
*v
= _alpm_graph_new();
43 alpm_list_free(vertices
);
46 alpm_delta_t
*vdelta
= i
->data
;
47 vdelta
->download_size
= vdelta
->delta_size
;
50 vertices
= alpm_list_add(vertices
, v
);
53 /* compute the edges */
54 for(i
= vertices
; i
; i
= i
->next
) {
55 alpm_graph_t
*v_i
= i
->data
;
56 alpm_delta_t
*d_i
= v_i
->data
;
57 /* loop a second time so we make all possible comparisons */
58 for(j
= vertices
; j
; j
= j
->next
) {
59 alpm_graph_t
*v_j
= j
->data
;
60 alpm_delta_t
*d_j
= v_j
->data
;
61 /* We want to create a delta tree like the following:
67 * If J 'from' is equal to I 'to', then J is a child of I.
69 if((!reverse
&& strcmp(d_j
->from
, d_i
->to
) == 0) ||
70 (reverse
&& strcmp(d_j
->to
, d_i
->from
) == 0)) {
71 v_i
->children
= alpm_list_add(v_i
->children
, v_j
);
74 v_i
->childptr
= v_i
->children
;
79 static void graph_init_size(alpm_handle_t
*handle
, alpm_list_t
*vertices
)
83 for(i
= vertices
; i
; i
= i
->next
) {
85 alpm_graph_t
*v
= i
->data
;
86 alpm_delta_t
*vdelta
= v
->data
;
88 /* determine whether the delta file already exists */
89 fpath
= _alpm_filecache_find(handle
, vdelta
->delta
);
91 md5sum
= alpm_compute_md5sum(fpath
);
92 if(md5sum
&& strcmp(md5sum
, vdelta
->delta_md5
) == 0) {
93 vdelta
->download_size
= 0;
99 CALLOC(fnamepart
, strlen(vdelta
->delta
) + 6, sizeof(char), return);
100 sprintf(fnamepart
, "%s.part", vdelta
->delta
);
101 fpath
= _alpm_filecache_find(handle
, fnamepart
);
104 if(stat(fpath
, &st
) == 0) {
105 vdelta
->download_size
= vdelta
->delta_size
- st
.st_size
;
106 vdelta
->download_size
= vdelta
->download_size
< 0 ? 0 : vdelta
->download_size
;
113 /* determine whether a base 'from' file exists */
114 fpath
= _alpm_filecache_find(handle
, vdelta
->from
);
116 v
->weight
= vdelta
->download_size
;
123 static void dijkstra(alpm_list_t
*vertices
)
129 /* find the smallest vertice not visited yet */
130 for(i
= vertices
; i
; i
= i
->next
) {
131 alpm_graph_t
*v_i
= i
->data
;
133 if(v_i
->state
== -1) {
137 if(v
== NULL
|| v_i
->weight
< v
->weight
) {
141 if(v
== NULL
|| v
->weight
== LONG_MAX
) {
147 v
->childptr
= v
->children
;
149 alpm_graph_t
*v_c
= v
->childptr
->data
;
150 alpm_delta_t
*d_c
= v_c
->data
;
151 if(v_c
->weight
> v
->weight
+ d_c
->download_size
) {
152 v_c
->weight
= v
->weight
+ d_c
->download_size
;
156 v
->childptr
= (v
->childptr
)->next
;
162 static off_t
shortest_path(alpm_list_t
*vertices
, const char *to
, alpm_list_t
**path
)
165 alpm_graph_t
*v
= NULL
;
167 alpm_list_t
*rpath
= NULL
;
169 for(i
= vertices
; i
; i
= i
->next
) {
170 alpm_graph_t
*v_i
= i
->data
;
171 alpm_delta_t
*d_i
= v_i
->data
;
173 if(strcmp(d_i
->to
, to
) == 0) {
174 if(v
== NULL
|| v_i
->weight
< v
->weight
) {
176 bestsize
= v
->weight
;
182 alpm_delta_t
*vdelta
= v
->data
;
183 rpath
= alpm_list_add(rpath
, vdelta
);
186 *path
= alpm_list_reverse(rpath
);
187 alpm_list_free(rpath
);
192 /** Calculates the shortest path from one version to another.
193 * The shortest path is defined as the path with the smallest combined
194 * size, not the length of the path.
195 * @param handle the context handle
196 * @param deltas the list of alpm_delta_t * objects that a file has
197 * @param to the file to start the search at
198 * @param path the pointer to a list location where alpm_delta_t * objects that
199 * have the smallest size are placed. NULL is set if there is no path
200 * possible with the files available.
201 * @return the size of the path stored, or LONG_MAX if path is unfindable
203 off_t
_alpm_shortest_delta_path(alpm_handle_t
*handle
, alpm_list_t
*deltas
,
204 const char *to
, alpm_list_t
**path
)
206 alpm_list_t
*bestpath
= NULL
;
207 alpm_list_t
*vertices
;
208 off_t bestsize
= LONG_MAX
;
215 _alpm_log(handle
, ALPM_LOG_DEBUG
, "started delta shortest-path search for '%s'\n", to
);
217 vertices
= graph_init(deltas
, 0);
218 graph_init_size(handle
, vertices
);
220 bestsize
= shortest_path(vertices
, to
, &bestpath
);
222 _alpm_log(handle
, ALPM_LOG_DEBUG
, "delta shortest-path search complete : '%jd'\n", (intmax_t)bestsize
);
224 alpm_list_free_inner(vertices
, _alpm_graph_free
);
225 alpm_list_free(vertices
);
231 static alpm_list_t
*find_unused(alpm_list_t
*deltas
, const char *to
, off_t quota
)
233 alpm_list_t
*unused
= NULL
;
234 alpm_list_t
*vertices
;
236 vertices
= graph_init(deltas
, 1);
238 for(i
= vertices
; i
; i
= i
->next
) {
239 alpm_graph_t
*v
= i
->data
;
240 alpm_delta_t
*vdelta
= v
->data
;
241 if(strcmp(vdelta
->to
, to
) == 0)
243 v
->weight
= vdelta
->download_size
;
247 for(i
= vertices
; i
; i
= i
->next
) {
248 alpm_graph_t
*v
= i
->data
;
249 alpm_delta_t
*vdelta
= v
->data
;
250 if(v
->weight
> quota
) {
251 unused
= alpm_list_add(unused
, vdelta
->delta
);
254 alpm_list_free_inner(vertices
, _alpm_graph_free
);
255 alpm_list_free(vertices
);
259 /** \addtogroup alpm_deltas Delta Functions
260 * @brief Functions to manipulate libalpm deltas
264 alpm_list_t SYMEXPORT
*alpm_pkg_unused_deltas(alpm_pkg_t
*pkg
)
266 ASSERT(pkg
!= NULL
, return NULL
);
267 return find_unused(pkg
->deltas
, pkg
->filename
,
268 pkg
->size
* pkg
->handle
->deltaratio
);
273 /** Parses the string representation of a alpm_delta_t object.
274 * This function assumes that the string is in the correct format.
275 * This format is as follows:
276 * $deltafile $deltamd5 $deltasize $oldfile $newfile
277 * @param handle the context handle
278 * @param line the string to parse
279 * @return A pointer to the new alpm_delta_t object
281 alpm_delta_t
*_alpm_delta_parse(alpm_handle_t
*handle
, const char *line
)
284 const int num_matches
= 6;
286 regmatch_t pmatch
[num_matches
];
289 /* this is so we only have to compile the pattern once */
290 if(!handle
->delta_regex_compiled
) {
291 /* $deltafile $deltamd5 $deltasize $oldfile $newfile*/
292 regcomp(&handle
->delta_regex
,
293 "^([^[:space:]]+) ([[:xdigit:]]{32}) ([[:digit:]]+)"
294 " ([^[:space:]]+) ([^[:space:]]+)$",
295 REG_EXTENDED
| REG_NEWLINE
);
296 handle
->delta_regex_compiled
= 1;
299 if(regexec(&handle
->delta_regex
, line
, num_matches
, pmatch
, 0) != 0) {
300 /* delta line is invalid, return NULL */
304 CALLOC(delta
, 1, sizeof(alpm_delta_t
), return NULL
);
306 /* start at index 1 -- match 0 is the entire match */
307 len
= pmatch
[1].rm_eo
- pmatch
[1].rm_so
;
308 STRNDUP(delta
->delta
, &line
[pmatch
[1].rm_so
], len
, return NULL
);
310 len
= pmatch
[2].rm_eo
- pmatch
[2].rm_so
;
311 STRNDUP(delta
->delta_md5
, &line
[pmatch
[2].rm_so
], len
, return NULL
);
313 len
= pmatch
[3].rm_eo
- pmatch
[3].rm_so
;
314 if(len
< sizeof(filesize
)) {
315 strncpy(filesize
, &line
[pmatch
[3].rm_so
], len
);
316 filesize
[len
] = '\0';
317 delta
->delta_size
= _alpm_strtoofft(filesize
);
320 len
= pmatch
[4].rm_eo
- pmatch
[4].rm_so
;
321 STRNDUP(delta
->from
, &line
[pmatch
[4].rm_so
], len
, return NULL
);
323 len
= pmatch
[5].rm_eo
- pmatch
[5].rm_so
;
324 STRNDUP(delta
->to
, &line
[pmatch
[5].rm_so
], len
, return NULL
);
329 void _alpm_delta_free(alpm_delta_t
*delta
)
332 FREE(delta
->delta_md5
);
338 alpm_delta_t
*_alpm_delta_dup(const alpm_delta_t
*delta
)
340 alpm_delta_t
*newdelta
;
341 CALLOC(newdelta
, 1, sizeof(alpm_delta_t
), return NULL
);
342 STRDUP(newdelta
->delta
, delta
->delta
, return NULL
);
343 STRDUP(newdelta
->delta_md5
, delta
->delta_md5
, return NULL
);
344 STRDUP(newdelta
->from
, delta
->from
, return NULL
);
345 STRDUP(newdelta
->to
, delta
->to
, return NULL
);
346 newdelta
->delta_size
= delta
->delta_size
;
347 newdelta
->download_size
= delta
->download_size
;
352 /* vim: set ts=2 sw=2 noet: */