2007-10-19 13:17:51 -04:00
|
|
|
/*
|
|
|
|
* delta.c
|
|
|
|
*
|
2011-01-05 23:45:15 -05:00
|
|
|
* Copyright (c) 2006-2011 Pacman Development Team <pacman-dev@archlinux.org>
|
2009-07-01 03:08:33 -04:00
|
|
|
* Copyright (c) 2007-2006 by Judd Vinet <jvinet@zeroflux.org>
|
2007-11-16 21:18:45 -05:00
|
|
|
*
|
2007-10-19 13:17:51 -04:00
|
|
|
* 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 of the License, 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2007-12-10 23:55:22 -05:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2007-10-19 13:17:51 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2009-02-19 13:12:34 -05:00
|
|
|
#include <stdint.h> /* intmax_t */
|
2008-02-16 11:47:22 -05:00
|
|
|
#include <limits.h>
|
2008-11-30 18:17:00 -05:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <regex.h>
|
2007-10-19 13:17:51 -04:00
|
|
|
|
|
|
|
/* libalpm */
|
|
|
|
#include "delta.h"
|
2008-02-16 11:47:22 -05:00
|
|
|
#include "alpm_list.h"
|
2007-10-19 13:17:51 -04:00
|
|
|
#include "util.h"
|
|
|
|
#include "log.h"
|
2008-02-16 11:47:22 -05:00
|
|
|
#include "graph.h"
|
2007-10-19 13:17:51 -04:00
|
|
|
|
2009-09-18 15:59:52 -04:00
|
|
|
static alpm_list_t *graph_init(alpm_list_t *deltas, int reverse)
|
2007-10-19 13:17:51 -04:00
|
|
|
{
|
2008-02-16 11:47:22 -05:00
|
|
|
alpm_list_t *i, *j;
|
|
|
|
alpm_list_t *vertices = NULL;
|
|
|
|
/* create the vertices */
|
|
|
|
for(i = deltas; i; i = i->next) {
|
2011-06-28 00:54:19 -04:00
|
|
|
alpm_graph_t *v = _alpm_graph_new();
|
2011-06-07 17:06:16 -04:00
|
|
|
if(!v) {
|
|
|
|
alpm_list_free(vertices);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-06-28 00:38:38 -04:00
|
|
|
alpm_delta_t *vdelta = i->data;
|
2008-02-16 11:47:22 -05:00
|
|
|
vdelta->download_size = vdelta->delta_size;
|
2008-06-01 22:47:31 -04:00
|
|
|
v->weight = LONG_MAX;
|
2008-02-16 11:47:22 -05:00
|
|
|
v->data = vdelta;
|
|
|
|
vertices = alpm_list_add(vertices, v);
|
2007-10-19 13:17:51 -04:00
|
|
|
}
|
|
|
|
|
2008-02-16 11:47:22 -05:00
|
|
|
/* compute the edges */
|
|
|
|
for(i = vertices; i; i = i->next) {
|
2011-06-28 00:54:19 -04:00
|
|
|
alpm_graph_t *v_i = i->data;
|
2011-06-28 00:38:38 -04:00
|
|
|
alpm_delta_t *d_i = v_i->data;
|
2008-02-16 11:47:22 -05:00
|
|
|
/* loop a second time so we make all possible comparisons */
|
|
|
|
for(j = vertices; j; j = j->next) {
|
2011-06-28 00:54:19 -04:00
|
|
|
alpm_graph_t *v_j = j->data;
|
2011-06-28 00:38:38 -04:00
|
|
|
alpm_delta_t *d_j = v_j->data;
|
2008-02-16 11:47:22 -05:00
|
|
|
/* We want to create a delta tree like the following:
|
|
|
|
* 1_to_2
|
|
|
|
* |
|
|
|
|
* 1_to_3 2_to_3
|
|
|
|
* \ /
|
|
|
|
* 3_to_4
|
|
|
|
* If J 'from' is equal to I 'to', then J is a child of I.
|
|
|
|
* */
|
2009-09-18 15:59:52 -04:00
|
|
|
if((!reverse && strcmp(d_j->from, d_i->to) == 0) ||
|
|
|
|
(reverse && strcmp(d_j->to, d_i->from) == 0)) {
|
2008-02-16 11:47:22 -05:00
|
|
|
v_i->children = alpm_list_add(v_i->children, v_j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
v_i->childptr = v_i->children;
|
|
|
|
}
|
2011-03-20 20:45:57 -04:00
|
|
|
return vertices;
|
2007-10-19 13:17:51 -04:00
|
|
|
}
|
|
|
|
|
2011-06-28 00:04:00 -04:00
|
|
|
static void graph_init_size(alpm_handle_t *handle, alpm_list_t *vertices)
|
2009-09-18 10:45:04 -04:00
|
|
|
{
|
|
|
|
alpm_list_t *i;
|
|
|
|
|
|
|
|
for(i = vertices; i; i = i->next) {
|
|
|
|
char *fpath, *md5sum;
|
2011-06-28 00:54:19 -04:00
|
|
|
alpm_graph_t *v = i->data;
|
2011-06-28 00:38:38 -04:00
|
|
|
alpm_delta_t *vdelta = v->data;
|
2009-09-18 10:45:04 -04:00
|
|
|
|
|
|
|
/* determine whether the delta file already exists */
|
2011-06-07 14:15:43 -04:00
|
|
|
fpath = _alpm_filecache_find(handle, vdelta->delta);
|
2009-09-18 10:45:04 -04:00
|
|
|
md5sum = alpm_compute_md5sum(fpath);
|
|
|
|
if(fpath && md5sum && strcmp(md5sum, vdelta->delta_md5) == 0) {
|
|
|
|
vdelta->download_size = 0;
|
|
|
|
}
|
|
|
|
FREE(fpath);
|
|
|
|
FREE(md5sum);
|
|
|
|
|
|
|
|
/* determine whether a base 'from' file exists */
|
2011-06-07 14:15:43 -04:00
|
|
|
fpath = _alpm_filecache_find(handle, vdelta->from);
|
2009-09-18 10:45:04 -04:00
|
|
|
if(fpath) {
|
|
|
|
v->weight = vdelta->download_size;
|
|
|
|
}
|
|
|
|
FREE(fpath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void dijkstra(alpm_list_t *vertices)
|
|
|
|
{
|
2008-02-16 11:47:22 -05:00
|
|
|
alpm_list_t *i;
|
2011-06-28 00:54:19 -04:00
|
|
|
alpm_graph_t *v;
|
2008-02-16 11:47:22 -05:00
|
|
|
while(1) {
|
|
|
|
v = NULL;
|
|
|
|
/* find the smallest vertice not visited yet */
|
|
|
|
for(i = vertices; i; i = i->next) {
|
2011-06-28 00:54:19 -04:00
|
|
|
alpm_graph_t *v_i = i->data;
|
2008-02-16 11:47:22 -05:00
|
|
|
|
|
|
|
if(v_i->state == -1) {
|
|
|
|
continue;
|
|
|
|
}
|
2007-10-19 13:17:51 -04:00
|
|
|
|
2008-02-16 11:47:22 -05:00
|
|
|
if(v == NULL || v_i->weight < v->weight) {
|
|
|
|
v = v_i;
|
|
|
|
}
|
|
|
|
}
|
2008-06-01 22:47:31 -04:00
|
|
|
if(v == NULL || v->weight == LONG_MAX) {
|
2008-02-16 11:47:22 -05:00
|
|
|
break;
|
2007-10-19 13:17:51 -04:00
|
|
|
}
|
|
|
|
|
2008-02-16 11:47:22 -05:00
|
|
|
v->state = -1;
|
2007-10-19 13:17:51 -04:00
|
|
|
|
2008-02-16 11:47:22 -05:00
|
|
|
v->childptr = v->children;
|
|
|
|
while(v->childptr) {
|
2011-06-28 00:54:19 -04:00
|
|
|
alpm_graph_t *v_c = v->childptr->data;
|
2011-06-28 00:38:38 -04:00
|
|
|
alpm_delta_t *d_c = v_c->data;
|
2008-02-16 11:47:22 -05:00
|
|
|
if(v_c->weight > v->weight + d_c->download_size) {
|
|
|
|
v_c->weight = v->weight + d_c->download_size;
|
|
|
|
v_c->parent = v;
|
|
|
|
}
|
2007-10-19 13:17:51 -04:00
|
|
|
|
2008-02-16 11:47:22 -05:00
|
|
|
v->childptr = (v->childptr)->next;
|
2007-10-19 13:17:51 -04:00
|
|
|
|
2008-02-16 11:47:22 -05:00
|
|
|
}
|
2007-10-19 13:17:51 -04:00
|
|
|
}
|
2009-09-18 10:45:04 -04:00
|
|
|
}
|
2007-10-19 13:17:51 -04:00
|
|
|
|
2009-09-18 10:45:04 -04:00
|
|
|
static off_t shortest_path(alpm_list_t *vertices, const char *to, alpm_list_t **path)
|
|
|
|
{
|
|
|
|
alpm_list_t *i;
|
2011-06-28 00:54:19 -04:00
|
|
|
alpm_graph_t *v = NULL;
|
2008-06-01 22:47:31 -04:00
|
|
|
off_t bestsize = 0;
|
2009-09-18 10:45:04 -04:00
|
|
|
alpm_list_t *rpath = NULL;
|
2007-10-19 13:17:51 -04:00
|
|
|
|
2008-02-16 11:47:22 -05:00
|
|
|
for(i = vertices; i; i = i->next) {
|
2011-06-28 00:54:19 -04:00
|
|
|
alpm_graph_t *v_i = i->data;
|
2011-06-28 00:38:38 -04:00
|
|
|
alpm_delta_t *d_i = v_i->data;
|
2007-10-19 13:17:51 -04:00
|
|
|
|
2009-02-25 12:24:38 -05:00
|
|
|
if(strcmp(d_i->to, to) == 0) {
|
2008-02-16 11:47:22 -05:00
|
|
|
if(v == NULL || v_i->weight < v->weight) {
|
|
|
|
v = v_i;
|
|
|
|
bestsize = v->weight;
|
2007-10-19 13:17:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-16 11:47:22 -05:00
|
|
|
while(v != NULL) {
|
2011-06-28 00:38:38 -04:00
|
|
|
alpm_delta_t *vdelta = v->data;
|
2008-02-16 11:47:22 -05:00
|
|
|
rpath = alpm_list_add(rpath, vdelta);
|
|
|
|
v = v->parent;
|
|
|
|
}
|
|
|
|
*path = alpm_list_reverse(rpath);
|
|
|
|
alpm_list_free(rpath);
|
2007-12-07 16:56:16 -05:00
|
|
|
|
2011-03-20 20:45:57 -04:00
|
|
|
return bestsize;
|
2007-10-19 13:17:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Calculates the shortest path from one version to another.
|
|
|
|
* The shortest path is defined as the path with the smallest combined
|
|
|
|
* size, not the length of the path.
|
2011-06-07 14:15:43 -04:00
|
|
|
* @param handle the context handle
|
2011-06-28 00:38:38 -04:00
|
|
|
* @param deltas the list of alpm_delta_t * objects that a file has
|
2008-02-16 11:47:22 -05:00
|
|
|
* @param to the file to start the search at
|
2011-06-28 00:38:38 -04:00
|
|
|
* @param path the pointer to a list location where alpm_delta_t * objects that
|
2008-02-16 11:47:22 -05:00
|
|
|
* have the smallest size are placed. NULL is set if there is no path
|
|
|
|
* possible with the files available.
|
2008-06-01 22:47:31 -04:00
|
|
|
* @return the size of the path stored, or LONG_MAX if path is unfindable
|
2007-10-19 13:17:51 -04:00
|
|
|
*/
|
2011-06-28 00:04:00 -04:00
|
|
|
off_t _alpm_shortest_delta_path(alpm_handle_t *handle, alpm_list_t *deltas,
|
2009-02-25 12:24:38 -05:00
|
|
|
const char *to, alpm_list_t **path)
|
2007-10-19 13:17:51 -04:00
|
|
|
{
|
2008-02-16 11:47:22 -05:00
|
|
|
alpm_list_t *bestpath = NULL;
|
|
|
|
alpm_list_t *vertices;
|
2008-06-01 22:47:31 -04:00
|
|
|
off_t bestsize = LONG_MAX;
|
2008-02-16 11:47:22 -05:00
|
|
|
|
|
|
|
if(deltas == NULL) {
|
|
|
|
*path = NULL;
|
2011-03-20 20:45:57 -04:00
|
|
|
return bestsize;
|
2008-02-16 11:47:22 -05:00
|
|
|
}
|
|
|
|
|
2011-07-01 12:01:38 -04:00
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG, "started delta shortest-path search for '%s'\n", to);
|
2008-02-16 11:47:22 -05:00
|
|
|
|
2009-09-18 15:59:52 -04:00
|
|
|
vertices = graph_init(deltas, 0);
|
2011-06-07 14:15:43 -04:00
|
|
|
graph_init_size(handle, vertices);
|
2009-09-18 10:45:04 -04:00
|
|
|
dijkstra(vertices);
|
|
|
|
bestsize = shortest_path(vertices, to, &bestpath);
|
2008-02-16 11:47:22 -05:00
|
|
|
|
2011-07-01 12:01:38 -04:00
|
|
|
_alpm_log(handle, ALPM_LOG_DEBUG, "delta shortest-path search complete : '%jd'\n", (intmax_t)bestsize);
|
2007-10-19 13:17:51 -04:00
|
|
|
|
2008-02-16 11:47:22 -05:00
|
|
|
alpm_list_free_inner(vertices, _alpm_graph_free);
|
|
|
|
alpm_list_free(vertices);
|
2007-10-19 13:17:51 -04:00
|
|
|
|
2008-02-16 11:47:22 -05:00
|
|
|
*path = bestpath;
|
2011-03-20 20:45:57 -04:00
|
|
|
return bestsize;
|
2007-10-19 13:17:51 -04:00
|
|
|
}
|
|
|
|
|
2009-09-18 15:59:52 -04:00
|
|
|
static alpm_list_t *find_unused(alpm_list_t *deltas, const char *to, off_t quota)
|
|
|
|
{
|
|
|
|
alpm_list_t *unused = NULL;
|
|
|
|
alpm_list_t *vertices;
|
|
|
|
alpm_list_t *i;
|
|
|
|
vertices = graph_init(deltas, 1);
|
|
|
|
|
|
|
|
for(i = vertices; i; i = i->next) {
|
2011-06-28 00:54:19 -04:00
|
|
|
alpm_graph_t *v = i->data;
|
2011-06-28 00:38:38 -04:00
|
|
|
alpm_delta_t *vdelta = v->data;
|
2009-09-18 15:59:52 -04:00
|
|
|
if(strcmp(vdelta->to, to) == 0)
|
|
|
|
{
|
|
|
|
v->weight = vdelta->download_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dijkstra(vertices);
|
|
|
|
for(i = vertices; i; i = i->next) {
|
2011-06-28 00:54:19 -04:00
|
|
|
alpm_graph_t *v = i->data;
|
2011-06-28 00:38:38 -04:00
|
|
|
alpm_delta_t *vdelta = v->data;
|
2009-09-18 15:59:52 -04:00
|
|
|
if(v->weight > quota) {
|
|
|
|
unused = alpm_list_add(unused, vdelta->delta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
alpm_list_free_inner(vertices, _alpm_graph_free);
|
|
|
|
alpm_list_free(vertices);
|
2011-03-20 20:45:57 -04:00
|
|
|
return unused;
|
2009-09-18 15:59:52 -04:00
|
|
|
}
|
|
|
|
|
2011-06-16 12:49:34 -04:00
|
|
|
/** \addtogroup alpm_deltas Delta Functions
|
|
|
|
* @brief Functions to manipulate libalpm deltas
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2011-06-28 09:26:39 -04:00
|
|
|
alpm_list_t SYMEXPORT *alpm_pkg_unused_deltas(alpm_pkg_t *pkg)
|
2009-09-18 15:59:52 -04:00
|
|
|
{
|
|
|
|
off_t pkgsize = alpm_pkg_get_size(pkg);
|
|
|
|
alpm_list_t *unused = find_unused(
|
|
|
|
alpm_pkg_get_deltas(pkg),
|
|
|
|
alpm_pkg_get_filename(pkg),
|
|
|
|
pkgsize * MAX_DELTA_RATIO);
|
2011-03-20 20:45:57 -04:00
|
|
|
return unused;
|
2009-09-18 15:59:52 -04:00
|
|
|
}
|
|
|
|
|
2011-06-16 12:49:34 -04:00
|
|
|
/** @} */
|
2009-09-18 15:59:52 -04:00
|
|
|
|
2011-06-28 00:38:38 -04:00
|
|
|
/** Parses the string representation of a alpm_delta_t object.
|
2007-10-19 13:17:51 -04:00
|
|
|
* This function assumes that the string is in the correct format.
|
2008-02-15 21:17:57 -05:00
|
|
|
* This format is as follows:
|
2009-02-25 12:24:38 -05:00
|
|
|
* $deltafile $deltamd5 $deltasize $oldfile $newfile
|
2007-10-19 13:17:51 -04:00
|
|
|
* @param line the string to parse
|
2011-06-28 00:38:38 -04:00
|
|
|
* @return A pointer to the new alpm_delta_t object
|
2007-10-19 13:17:51 -04:00
|
|
|
*/
|
2008-02-15 21:17:57 -05:00
|
|
|
/* TODO this does not really belong here, but in a parsing lib */
|
2011-06-28 00:38:38 -04:00
|
|
|
alpm_delta_t *_alpm_delta_parse(char *line)
|
2007-10-19 13:17:51 -04:00
|
|
|
{
|
2011-06-28 00:38:38 -04:00
|
|
|
alpm_delta_t *delta;
|
2007-10-19 13:17:51 -04:00
|
|
|
char *tmp = line, *tmp2;
|
2008-11-30 18:17:00 -05:00
|
|
|
regex_t reg;
|
|
|
|
|
|
|
|
regcomp(®,
|
2009-02-25 12:24:38 -05:00
|
|
|
"^[^[:space:]]* [[:xdigit:]]{32} [[:digit:]]*"
|
|
|
|
" [^[:space:]]* [^[:space:]]*$",
|
2008-11-30 18:17:00 -05:00
|
|
|
REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
|
|
|
|
if(regexec(®, line, 0, 0, 0) != 0) {
|
|
|
|
/* delta line is invalid, return NULL */
|
|
|
|
regfree(®);
|
2011-03-20 20:45:57 -04:00
|
|
|
return NULL;
|
2008-11-30 18:17:00 -05:00
|
|
|
}
|
|
|
|
regfree(®);
|
2007-10-19 13:17:51 -04:00
|
|
|
|
2011-06-28 00:38:38 -04:00
|
|
|
CALLOC(delta, 1, sizeof(alpm_delta_t), return NULL);
|
2007-10-19 13:17:51 -04:00
|
|
|
|
|
|
|
tmp2 = tmp;
|
|
|
|
tmp = strchr(tmp, ' ');
|
|
|
|
*(tmp++) = '\0';
|
2011-06-07 17:06:16 -04:00
|
|
|
STRDUP(delta->delta, tmp2, return NULL);
|
2008-02-15 21:17:57 -05:00
|
|
|
|
2007-10-19 13:17:51 -04:00
|
|
|
tmp2 = tmp;
|
|
|
|
tmp = strchr(tmp, ' ');
|
|
|
|
*(tmp++) = '\0';
|
2011-06-07 17:06:16 -04:00
|
|
|
STRDUP(delta->delta_md5, tmp2, return NULL);
|
2007-10-19 13:17:51 -04:00
|
|
|
|
|
|
|
tmp2 = tmp;
|
|
|
|
tmp = strchr(tmp, ' ');
|
|
|
|
*(tmp++) = '\0';
|
2009-02-25 12:24:38 -05:00
|
|
|
delta->delta_size = atol(tmp2);
|
2008-02-15 21:17:57 -05:00
|
|
|
|
|
|
|
tmp2 = tmp;
|
|
|
|
tmp = strchr(tmp, ' ');
|
|
|
|
*(tmp++) = '\0';
|
2011-06-07 17:06:16 -04:00
|
|
|
STRDUP(delta->from, tmp2, return NULL);
|
2007-10-19 13:17:51 -04:00
|
|
|
|
|
|
|
tmp2 = tmp;
|
2011-06-07 17:06:16 -04:00
|
|
|
STRDUP(delta->to, tmp2, return NULL);
|
2007-10-19 13:17:51 -04:00
|
|
|
|
2011-03-20 20:45:57 -04:00
|
|
|
return delta;
|
2007-10-19 13:17:51 -04:00
|
|
|
}
|
|
|
|
|
2011-06-28 00:38:38 -04:00
|
|
|
void _alpm_delta_free(alpm_delta_t *delta)
|
2008-01-12 02:27:02 -05:00
|
|
|
{
|
2008-02-15 21:17:57 -05:00
|
|
|
FREE(delta->delta);
|
|
|
|
FREE(delta->delta_md5);
|
2011-06-16 14:03:33 -04:00
|
|
|
FREE(delta->from);
|
|
|
|
FREE(delta->to);
|
2008-01-12 02:27:02 -05:00
|
|
|
FREE(delta);
|
|
|
|
}
|
|
|
|
|
2011-06-28 00:38:38 -04:00
|
|
|
alpm_delta_t *_alpm_delta_dup(const alpm_delta_t *delta)
|
2011-06-16 14:03:33 -04:00
|
|
|
{
|
2011-06-28 00:38:38 -04:00
|
|
|
alpm_delta_t *newdelta;
|
|
|
|
CALLOC(newdelta, 1, sizeof(alpm_delta_t), return NULL);
|
2011-06-16 14:03:33 -04:00
|
|
|
STRDUP(newdelta->delta, delta->delta, return NULL);
|
|
|
|
STRDUP(newdelta->delta_md5, delta->delta_md5, return NULL);
|
|
|
|
STRDUP(newdelta->from, delta->from, return NULL);
|
|
|
|
STRDUP(newdelta->to, delta->to, return NULL);
|
|
|
|
newdelta->delta_size = delta->delta_size;
|
|
|
|
newdelta->download_size = delta->download_size;
|
|
|
|
|
|
|
|
return newdelta;
|
|
|
|
}
|
|
|
|
|
2007-10-19 13:17:51 -04:00
|
|
|
/* vim: set ts=2 sw=2 noet: */
|