2007-10-19 13:17:51 -04:00
|
|
|
/*
|
|
|
|
* delta.c
|
|
|
|
*
|
2008-01-12 02:27:02 -05:00
|
|
|
* Copyright (c) 2007-2008 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>
|
2008-02-16 11:47:22 -05:00
|
|
|
#include <limits.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
|
|
|
|
|
|
|
/** \addtogroup alpm_deltas Delta Functions
|
|
|
|
* @brief Functions to manipulate libalpm deltas
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2007-10-29 02:28:13 -04:00
|
|
|
const char SYMEXPORT *alpm_delta_get_from(pmdelta_t *delta)
|
2007-10-19 13:17:51 -04:00
|
|
|
{
|
|
|
|
ASSERT(delta != NULL, return(NULL));
|
|
|
|
return(delta->from);
|
|
|
|
}
|
|
|
|
|
2008-02-15 21:17:57 -05:00
|
|
|
const char SYMEXPORT *alpm_delta_get_from_md5sum(pmdelta_t *delta)
|
2007-10-19 13:17:51 -04:00
|
|
|
{
|
|
|
|
ASSERT(delta != NULL, return(NULL));
|
2008-02-15 21:17:57 -05:00
|
|
|
return(delta->from_md5);
|
|
|
|
}
|
2007-10-19 13:17:51 -04:00
|
|
|
|
2008-02-15 21:17:57 -05:00
|
|
|
const char SYMEXPORT *alpm_delta_get_to(pmdelta_t *delta)
|
|
|
|
{
|
|
|
|
ASSERT(delta != NULL, return(NULL));
|
2007-10-19 13:17:51 -04:00
|
|
|
return(delta->to);
|
|
|
|
}
|
|
|
|
|
2008-02-15 21:17:57 -05:00
|
|
|
const char SYMEXPORT *alpm_delta_get_to_md5sum(pmdelta_t *delta)
|
2007-10-19 13:17:51 -04:00
|
|
|
{
|
2008-02-15 21:17:57 -05:00
|
|
|
ASSERT(delta != NULL, return(NULL));
|
|
|
|
return(delta->to_md5);
|
2007-10-19 13:17:51 -04:00
|
|
|
}
|
|
|
|
|
2007-10-29 02:28:13 -04:00
|
|
|
const char SYMEXPORT *alpm_delta_get_filename(pmdelta_t *delta)
|
2007-10-19 13:17:51 -04:00
|
|
|
{
|
|
|
|
ASSERT(delta != NULL, return(NULL));
|
2008-02-15 21:17:57 -05:00
|
|
|
return(delta->delta);
|
2007-10-19 13:17:51 -04:00
|
|
|
}
|
|
|
|
|
2007-10-29 02:28:13 -04:00
|
|
|
const char SYMEXPORT *alpm_delta_get_md5sum(pmdelta_t *delta)
|
2007-10-19 13:17:51 -04:00
|
|
|
{
|
|
|
|
ASSERT(delta != NULL, return(NULL));
|
2008-02-15 21:17:57 -05:00
|
|
|
return(delta->delta_md5);
|
|
|
|
}
|
2007-10-19 13:17:51 -04:00
|
|
|
|
2008-06-01 22:47:31 -04:00
|
|
|
off_t SYMEXPORT alpm_delta_get_size(pmdelta_t *delta)
|
2008-02-15 21:17:57 -05:00
|
|
|
{
|
|
|
|
ASSERT(delta != NULL, return(-1));
|
|
|
|
return(delta->delta_size);
|
2007-10-19 13:17:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @} */
|
|
|
|
|
2008-02-16 11:47:22 -05:00
|
|
|
static alpm_list_t *delta_graph_init(alpm_list_t *deltas)
|
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) {
|
|
|
|
char *fpath, *md5sum;
|
|
|
|
pmgraph_t *v = _alpm_graph_new();
|
|
|
|
pmdelta_t *vdelta = i->data;
|
|
|
|
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
|
|
|
|
|
|
|
/* determine whether the delta file already exists */
|
|
|
|
fpath = _alpm_filecache_find(vdelta->delta);
|
|
|
|
md5sum = alpm_get_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 */
|
|
|
|
fpath = _alpm_filecache_find(vdelta->from);
|
|
|
|
md5sum = alpm_get_md5sum(fpath);
|
|
|
|
if(fpath && md5sum && strcmp(md5sum, vdelta->from_md5) == 0) {
|
|
|
|
v->weight = vdelta->download_size;
|
|
|
|
}
|
|
|
|
FREE(fpath);
|
|
|
|
FREE(md5sum);
|
2007-10-19 13:17:51 -04:00
|
|
|
|
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) {
|
|
|
|
pmgraph_t *v_i = i->data;
|
|
|
|
pmdelta_t *d_i = v_i->data;
|
|
|
|
/* loop a second time so we make all possible comparisons */
|
|
|
|
for(j = vertices; j; j = j->next) {
|
|
|
|
pmgraph_t *v_j = j->data;
|
|
|
|
pmdelta_t *d_j = v_j->data;
|
|
|
|
/* 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.
|
|
|
|
* */
|
|
|
|
if(strcmp(d_j->from, d_i->to) == 0
|
|
|
|
&& strcmp(d_j->from_md5, d_i->to_md5) == 0) {
|
|
|
|
v_i->children = alpm_list_add(v_i->children, v_j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
v_i->childptr = v_i->children;
|
|
|
|
}
|
|
|
|
return(vertices);
|
2007-10-19 13:17:51 -04:00
|
|
|
}
|
|
|
|
|
2008-06-01 22:47:31 -04:00
|
|
|
static off_t delta_vert(alpm_list_t *vertices,
|
2008-02-16 11:47:22 -05:00
|
|
|
const char *to, const char *to_md5, alpm_list_t **path) {
|
|
|
|
alpm_list_t *i;
|
|
|
|
pmgraph_t *v;
|
|
|
|
while(1) {
|
|
|
|
v = NULL;
|
|
|
|
/* find the smallest vertice not visited yet */
|
|
|
|
for(i = vertices; i; i = i->next) {
|
|
|
|
pmgraph_t *v_i = i->data;
|
|
|
|
|
|
|
|
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) {
|
|
|
|
pmgraph_t *v_c = v->childptr->data;
|
|
|
|
pmdelta_t *d_c = v_c->data;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-02-16 11:47:22 -05:00
|
|
|
v = NULL;
|
2008-06-01 22:47:31 -04:00
|
|
|
off_t bestsize = 0;
|
2007-10-19 13:17:51 -04:00
|
|
|
|
2008-02-16 11:47:22 -05:00
|
|
|
for(i = vertices; i; i = i->next) {
|
|
|
|
pmgraph_t *v_i = i->data;
|
|
|
|
pmdelta_t *d_i = v_i->data;
|
2007-10-19 13:17:51 -04:00
|
|
|
|
2008-02-16 11:47:22 -05:00
|
|
|
if(strcmp(d_i->to, to) == 0
|
|
|
|
|| strcmp(d_i->to_md5, to_md5) == 0) {
|
|
|
|
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
|
|
|
alpm_list_t *rpath = NULL;
|
|
|
|
while(v != NULL) {
|
|
|
|
pmdelta_t *vdelta = v->data;
|
|
|
|
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
|
|
|
|
2008-02-16 11:47:22 -05: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.
|
2008-02-16 11:47:22 -05:00
|
|
|
* @param deltas the list of pmdelta_t * objects that a file has
|
|
|
|
* @param to the file to start the search at
|
|
|
|
* @param to_md5 the md5sum of the above named file
|
|
|
|
* @param path the pointer to a list location where pmdelta_t * objects that
|
|
|
|
* 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
|
|
|
*/
|
2008-06-01 22:47:31 -04:00
|
|
|
off_t _alpm_shortest_delta_path(alpm_list_t *deltas,
|
2008-02-16 11:47:22 -05:00
|
|
|
const char *to, const char *to_md5, 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
|
|
|
|
|
|
|
ALPM_LOG_FUNC;
|
|
|
|
|
|
|
|
if(deltas == NULL) {
|
|
|
|
*path = NULL;
|
|
|
|
return(bestsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
_alpm_log(PM_LOG_DEBUG, "started delta shortest-path search\n");
|
|
|
|
|
|
|
|
vertices = delta_graph_init(deltas);
|
|
|
|
|
|
|
|
bestsize = delta_vert(vertices, to, to_md5, &bestpath);
|
|
|
|
|
|
|
|
_alpm_log(PM_LOG_DEBUG, "delta shortest-path search complete\n");
|
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;
|
|
|
|
return(bestsize);
|
2007-10-19 13:17:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Parses the string representation of a pmdelta_t object.
|
|
|
|
* This function assumes that the string is in the correct format.
|
2008-02-15 21:17:57 -05:00
|
|
|
* This format is as follows:
|
|
|
|
* $oldfile $oldmd5 $newfile $newmd5 $deltafile $deltamd5 $deltasize
|
2007-10-19 13:17:51 -04:00
|
|
|
* @param line the string to parse
|
|
|
|
* @return A pointer to the new pmdelta_t object
|
|
|
|
*/
|
2008-02-15 21:17:57 -05:00
|
|
|
/* TODO this does not really belong here, but in a parsing lib */
|
2007-10-19 13:17:51 -04:00
|
|
|
pmdelta_t *_alpm_delta_parse(char *line)
|
|
|
|
{
|
|
|
|
pmdelta_t *delta;
|
|
|
|
char *tmp = line, *tmp2;
|
|
|
|
|
2007-10-29 02:00:52 -04:00
|
|
|
CALLOC(delta, 1, sizeof(pmdelta_t), RET_ERR(PM_ERR_MEMORY, NULL));
|
2007-10-19 13:17:51 -04:00
|
|
|
|
|
|
|
tmp2 = tmp;
|
|
|
|
tmp = strchr(tmp, ' ');
|
|
|
|
*(tmp++) = '\0';
|
2008-01-12 02:27:02 -05:00
|
|
|
STRDUP(delta->from, tmp2, RET_ERR(PM_ERR_MEMORY, NULL));
|
2007-10-19 13:17:51 -04:00
|
|
|
|
2008-02-15 21:17:57 -05:00
|
|
|
tmp2 = tmp;
|
|
|
|
tmp = strchr(tmp, ' ');
|
|
|
|
*(tmp++) = '\0';
|
|
|
|
STRDUP(delta->from_md5, tmp2, RET_ERR(PM_ERR_MEMORY, NULL));
|
|
|
|
|
2007-10-19 13:17:51 -04:00
|
|
|
tmp2 = tmp;
|
|
|
|
tmp = strchr(tmp, ' ');
|
|
|
|
*(tmp++) = '\0';
|
2008-01-12 02:27:02 -05:00
|
|
|
STRDUP(delta->to, tmp2, RET_ERR(PM_ERR_MEMORY, NULL));
|
2007-10-19 13:17:51 -04:00
|
|
|
|
|
|
|
tmp2 = tmp;
|
|
|
|
tmp = strchr(tmp, ' ');
|
|
|
|
*(tmp++) = '\0';
|
2008-02-15 21:17:57 -05:00
|
|
|
STRDUP(delta->to_md5, tmp2, RET_ERR(PM_ERR_MEMORY, NULL));
|
|
|
|
|
|
|
|
tmp2 = tmp;
|
|
|
|
tmp = strchr(tmp, ' ');
|
|
|
|
*(tmp++) = '\0';
|
|
|
|
STRDUP(delta->delta, tmp2, RET_ERR(PM_ERR_MEMORY, NULL));
|
2007-10-19 13:17:51 -04:00
|
|
|
|
|
|
|
tmp2 = tmp;
|
|
|
|
tmp = strchr(tmp, ' ');
|
|
|
|
*(tmp++) = '\0';
|
2008-02-15 21:17:57 -05:00
|
|
|
STRDUP(delta->delta_md5, tmp2, RET_ERR(PM_ERR_MEMORY, NULL));
|
2007-10-19 13:17:51 -04:00
|
|
|
|
2008-02-15 21:17:57 -05:00
|
|
|
delta->delta_size = atol(tmp);
|
2007-10-19 13:17:51 -04:00
|
|
|
|
|
|
|
return(delta);
|
|
|
|
}
|
|
|
|
|
2008-01-12 02:27:02 -05:00
|
|
|
void _alpm_delta_free(pmdelta_t *delta)
|
|
|
|
{
|
|
|
|
FREE(delta->from);
|
2008-02-15 21:17:57 -05:00
|
|
|
FREE(delta->from_md5);
|
2008-01-12 02:27:02 -05:00
|
|
|
FREE(delta->to);
|
2008-02-15 21:17:57 -05:00
|
|
|
FREE(delta->to_md5);
|
|
|
|
FREE(delta->delta);
|
|
|
|
FREE(delta->delta_md5);
|
2008-01-12 02:27:02 -05:00
|
|
|
FREE(delta);
|
|
|
|
}
|
|
|
|
|
2007-10-19 13:17:51 -04:00
|
|
|
/* vim: set ts=2 sw=2 noet: */
|