summaryrefslogtreecommitdiff
path: root/lib/libalpm/delta.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libalpm/delta.c')
-rw-r--r--lib/libalpm/delta.c166
1 files changed, 75 insertions, 91 deletions
diff --git a/lib/libalpm/delta.c b/lib/libalpm/delta.c
index 10c982f2..1b7e3eec 100644
--- a/lib/libalpm/delta.c
+++ b/lib/libalpm/delta.c
@@ -34,51 +34,18 @@
#include "log.h"
#include "graph.h"
-/** \addtogroup alpm_deltas Delta Functions
- * @brief Functions to manipulate libalpm deltas
- * @{
- */
-
-const char SYMEXPORT *alpm_delta_get_from(pmdelta_t *delta)
-{
- ASSERT(delta != NULL, return(NULL));
- return(delta->from);
-}
-
-const char SYMEXPORT *alpm_delta_get_to(pmdelta_t *delta)
-{
- ASSERT(delta != NULL, return(NULL));
- return(delta->to);
-}
-
-const char SYMEXPORT *alpm_delta_get_filename(pmdelta_t *delta)
-{
- ASSERT(delta != NULL, return(NULL));
- return(delta->delta);
-}
-
-const char SYMEXPORT *alpm_delta_get_md5sum(pmdelta_t *delta)
-{
- ASSERT(delta != NULL, return(NULL));
- return(delta->delta_md5);
-}
-
-off_t SYMEXPORT alpm_delta_get_size(pmdelta_t *delta)
-{
- ASSERT(delta != NULL, return(-1));
- return(delta->delta_size);
-}
-
-/** @} */
-
static alpm_list_t *graph_init(alpm_list_t *deltas, int reverse)
{
alpm_list_t *i, *j;
alpm_list_t *vertices = NULL;
/* create the vertices */
for(i = deltas; i; i = i->next) {
- pmgraph_t *v = _alpm_graph_new();
- pmdelta_t *vdelta = i->data;
+ alpm_graph_t *v = _alpm_graph_new();
+ if(!v) {
+ alpm_list_free(vertices);
+ return NULL;
+ }
+ alpm_delta_t *vdelta = i->data;
vdelta->download_size = vdelta->delta_size;
v->weight = LONG_MAX;
v->data = vdelta;
@@ -87,12 +54,12 @@ static alpm_list_t *graph_init(alpm_list_t *deltas, int reverse)
/* compute the edges */
for(i = vertices; i; i = i->next) {
- pmgraph_t *v_i = i->data;
- pmdelta_t *d_i = v_i->data;
+ alpm_graph_t *v_i = i->data;
+ alpm_delta_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;
+ alpm_graph_t *v_j = j->data;
+ alpm_delta_t *d_j = v_j->data;
/* We want to create a delta tree like the following:
* 1_to_2
* |
@@ -108,20 +75,20 @@ static alpm_list_t *graph_init(alpm_list_t *deltas, int reverse)
}
v_i->childptr = v_i->children;
}
- return(vertices);
+ return vertices;
}
-static void graph_init_size(alpm_list_t *vertices)
+static void graph_init_size(alpm_handle_t *handle, alpm_list_t *vertices)
{
alpm_list_t *i;
for(i = vertices; i; i = i->next) {
char *fpath, *md5sum;
- pmgraph_t *v = i->data;
- pmdelta_t *vdelta = v->data;
+ alpm_graph_t *v = i->data;
+ alpm_delta_t *vdelta = v->data;
/* determine whether the delta file already exists */
- fpath = _alpm_filecache_find(vdelta->delta);
+ fpath = _alpm_filecache_find(handle, vdelta->delta);
md5sum = alpm_compute_md5sum(fpath);
if(fpath && md5sum && strcmp(md5sum, vdelta->delta_md5) == 0) {
vdelta->download_size = 0;
@@ -130,7 +97,7 @@ static void graph_init_size(alpm_list_t *vertices)
FREE(md5sum);
/* determine whether a base 'from' file exists */
- fpath = _alpm_filecache_find(vdelta->from);
+ fpath = _alpm_filecache_find(handle, vdelta->from);
if(fpath) {
v->weight = vdelta->download_size;
}
@@ -142,12 +109,12 @@ static void graph_init_size(alpm_list_t *vertices)
static void dijkstra(alpm_list_t *vertices)
{
alpm_list_t *i;
- pmgraph_t *v;
+ alpm_graph_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;
+ alpm_graph_t *v_i = i->data;
if(v_i->state == -1) {
continue;
@@ -165,8 +132,8 @@ static void dijkstra(alpm_list_t *vertices)
v->childptr = v->children;
while(v->childptr) {
- pmgraph_t *v_c = v->childptr->data;
- pmdelta_t *d_c = v_c->data;
+ alpm_graph_t *v_c = v->childptr->data;
+ alpm_delta_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;
@@ -181,13 +148,13 @@ static void dijkstra(alpm_list_t *vertices)
static off_t shortest_path(alpm_list_t *vertices, const char *to, alpm_list_t **path)
{
alpm_list_t *i;
- pmgraph_t *v = NULL;
+ alpm_graph_t *v = NULL;
off_t bestsize = 0;
alpm_list_t *rpath = NULL;
for(i = vertices; i; i = i->next) {
- pmgraph_t *v_i = i->data;
- pmdelta_t *d_i = v_i->data;
+ alpm_graph_t *v_i = i->data;
+ alpm_delta_t *d_i = v_i->data;
if(strcmp(d_i->to, to) == 0) {
if(v == NULL || v_i->weight < v->weight) {
@@ -198,54 +165,53 @@ static off_t shortest_path(alpm_list_t *vertices, const char *to, alpm_list_t **
}
while(v != NULL) {
- pmdelta_t *vdelta = v->data;
+ alpm_delta_t *vdelta = v->data;
rpath = alpm_list_add(rpath, vdelta);
v = v->parent;
}
*path = alpm_list_reverse(rpath);
alpm_list_free(rpath);
- return(bestsize);
+ return bestsize;
}
/** 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.
- * @param deltas the list of pmdelta_t * objects that a file has
+ * @param handle the context handle
+ * @param deltas the list of alpm_delta_t * objects that a file has
* @param to the file to start the search at
- * @param path the pointer to a list location where pmdelta_t * objects that
+ * @param path the pointer to a list location where alpm_delta_t * objects that
* have the smallest size are placed. NULL is set if there is no path
* possible with the files available.
* @return the size of the path stored, or LONG_MAX if path is unfindable
*/
-off_t _alpm_shortest_delta_path(alpm_list_t *deltas,
+off_t _alpm_shortest_delta_path(alpm_handle_t *handle, alpm_list_t *deltas,
const char *to, alpm_list_t **path)
{
alpm_list_t *bestpath = NULL;
alpm_list_t *vertices;
off_t bestsize = LONG_MAX;
- ALPM_LOG_FUNC;
-
if(deltas == NULL) {
*path = NULL;
- return(bestsize);
+ return bestsize;
}
- _alpm_log(PM_LOG_DEBUG, "started delta shortest-path search for '%s'\n", to);
+ _alpm_log(handle, ALPM_LOG_DEBUG, "started delta shortest-path search for '%s'\n", to);
vertices = graph_init(deltas, 0);
- graph_init_size(vertices);
+ graph_init_size(handle, vertices);
dijkstra(vertices);
bestsize = shortest_path(vertices, to, &bestpath);
- _alpm_log(PM_LOG_DEBUG, "delta shortest-path search complete : '%jd'\n", (intmax_t)bestsize);
+ _alpm_log(handle, ALPM_LOG_DEBUG, "delta shortest-path search complete : '%jd'\n", (intmax_t)bestsize);
alpm_list_free_inner(vertices, _alpm_graph_free);
alpm_list_free(vertices);
*path = bestpath;
- return(bestsize);
+ return bestsize;
}
static alpm_list_t *find_unused(alpm_list_t *deltas, const char *to, off_t quota)
@@ -256,8 +222,8 @@ static alpm_list_t *find_unused(alpm_list_t *deltas, const char *to, off_t quota
vertices = graph_init(deltas, 1);
for(i = vertices; i; i = i->next) {
- pmgraph_t *v = i->data;
- pmdelta_t *vdelta = v->data;
+ alpm_graph_t *v = i->data;
+ alpm_delta_t *vdelta = v->data;
if(strcmp(vdelta->to, to) == 0)
{
v->weight = vdelta->download_size;
@@ -265,39 +231,45 @@ static alpm_list_t *find_unused(alpm_list_t *deltas, const char *to, off_t quota
}
dijkstra(vertices);
for(i = vertices; i; i = i->next) {
- pmgraph_t *v = i->data;
- pmdelta_t *vdelta = v->data;
+ alpm_graph_t *v = i->data;
+ alpm_delta_t *vdelta = v->data;
if(v->weight > quota) {
unused = alpm_list_add(unused, vdelta->delta);
}
}
alpm_list_free_inner(vertices, _alpm_graph_free);
alpm_list_free(vertices);
- return(unused);
+ return unused;
}
-alpm_list_t SYMEXPORT *alpm_pkg_unused_deltas(pmpkg_t *pkg)
+/** \addtogroup alpm_deltas Delta Functions
+ * @brief Functions to manipulate libalpm deltas
+ * @{
+ */
+
+alpm_list_t SYMEXPORT *alpm_pkg_unused_deltas(alpm_pkg_t *pkg)
{
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);
- return(unused);
+ return unused;
}
+/** @} */
-/** Parses the string representation of a pmdelta_t object.
+/** Parses the string representation of a alpm_delta_t object.
* This function assumes that the string is in the correct format.
* This format is as follows:
* $deltafile $deltamd5 $deltasize $oldfile $newfile
* @param line the string to parse
- * @return A pointer to the new pmdelta_t object
+ * @return A pointer to the new alpm_delta_t object
*/
/* TODO this does not really belong here, but in a parsing lib */
-pmdelta_t *_alpm_delta_parse(char *line)
+alpm_delta_t *_alpm_delta_parse(char *line)
{
- pmdelta_t *delta;
+ alpm_delta_t *delta;
char *tmp = line, *tmp2;
regex_t reg;
@@ -308,21 +280,21 @@ pmdelta_t *_alpm_delta_parse(char *line)
if(regexec(&reg, line, 0, 0, 0) != 0) {
/* delta line is invalid, return NULL */
regfree(&reg);
- return(NULL);
+ return NULL;
}
regfree(&reg);
- CALLOC(delta, 1, sizeof(pmdelta_t), RET_ERR(PM_ERR_MEMORY, NULL));
+ CALLOC(delta, 1, sizeof(alpm_delta_t), return NULL);
tmp2 = tmp;
tmp = strchr(tmp, ' ');
*(tmp++) = '\0';
- STRDUP(delta->delta, tmp2, RET_ERR(PM_ERR_MEMORY, NULL));
+ STRDUP(delta->delta, tmp2, return NULL);
tmp2 = tmp;
tmp = strchr(tmp, ' ');
*(tmp++) = '\0';
- STRDUP(delta->delta_md5, tmp2, RET_ERR(PM_ERR_MEMORY, NULL));
+ STRDUP(delta->delta_md5, tmp2, return NULL);
tmp2 = tmp;
tmp = strchr(tmp, ' ');
@@ -332,23 +304,35 @@ pmdelta_t *_alpm_delta_parse(char *line)
tmp2 = tmp;
tmp = strchr(tmp, ' ');
*(tmp++) = '\0';
- STRDUP(delta->from, tmp2, RET_ERR(PM_ERR_MEMORY, NULL));
+ STRDUP(delta->from, tmp2, return NULL);
tmp2 = tmp;
- STRDUP(delta->to, tmp2, RET_ERR(PM_ERR_MEMORY, NULL));
-
- _alpm_log(PM_LOG_DEBUG, "delta : %s %s '%jd'\n", delta->from, delta->to, (intmax_t)delta->delta_size);
+ STRDUP(delta->to, tmp2, return NULL);
- return(delta);
+ return delta;
}
-void _alpm_delta_free(pmdelta_t *delta)
+void _alpm_delta_free(alpm_delta_t *delta)
{
- FREE(delta->from);
- FREE(delta->to);
FREE(delta->delta);
FREE(delta->delta_md5);
+ FREE(delta->from);
+ FREE(delta->to);
FREE(delta);
}
+alpm_delta_t *_alpm_delta_dup(const alpm_delta_t *delta)
+{
+ alpm_delta_t *newdelta;
+ CALLOC(newdelta, 1, sizeof(alpm_delta_t), return NULL);
+ 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;
+}
+
/* vim: set ts=2 sw=2 noet: */