Start to move the delta struct away from an assumed package name scheme and towards something that is package (or even filename) agnostic. This will allow us much greater flexibility in the usage of deltas (maybe even sync DBs some day) as well as allowing code outside of delta.h/delta.c to be much cleaner with less of a need for snprintf() calls. Signed-off-by: Dan McGee <dan@archlinux.org> --- Mainly an RFC here, although I feel like this is a good approach to take. Note both my comments above in the commit message, as well as taking a look at how not trying to even worry about the filename simplifies the code in sync.c a good amount. No promises that this works yet. :) -Dan lib/libalpm/alpm.h | 4 ++- lib/libalpm/delta.c | 88 ++++++++++++++++++++++++-------------------------- lib/libalpm/delta.h | 15 +++++++-- lib/libalpm/sync.c | 31 +++++++---------- 4 files changed, 70 insertions(+), 68 deletions(-) diff --git a/lib/libalpm/alpm.h b/lib/libalpm/alpm.h index b19e1dd..528bac9 100644 --- a/lib/libalpm/alpm.h +++ b/lib/libalpm/alpm.h @@ -231,10 +231,12 @@ unsigned long alpm_pkg_download_size(pmpkg_t *newpkg, pmdb_t *db_local); */ const char *alpm_delta_get_from(pmdelta_t *delta); +const char *alpm_delta_get_from_md5sum(pmdelta_t *delta); const char *alpm_delta_get_to(pmdelta_t *delta); -unsigned long alpm_delta_get_size(pmdelta_t *delta); +const char *alpm_delta_get_to_md5sum(pmdelta_t *delta); const char *alpm_delta_get_filename(pmdelta_t *delta); const char *alpm_delta_get_md5sum(pmdelta_t *delta); +unsigned long alpm_delta_get_size(pmdelta_t *delta); /* * Groups diff --git a/lib/libalpm/delta.c b/lib/libalpm/delta.c index 9eb6e82..923e05f 100644 --- a/lib/libalpm/delta.c +++ b/lib/libalpm/delta.c @@ -37,60 +37,50 @@ const char SYMEXPORT *alpm_delta_get_from(pmdelta_t *delta) { - ALPM_LOG_FUNC; - - /* Sanity checks */ ASSERT(delta != NULL, return(NULL)); - return(delta->from); } -const char SYMEXPORT *alpm_delta_get_to(pmdelta_t *delta) +const char SYMEXPORT *alpm_delta_get_from_md5sum(pmdelta_t *delta) { - ALPM_LOG_FUNC; - - /* Sanity checks */ ASSERT(delta != NULL, return(NULL)); + return(delta->from_md5); +} +const char SYMEXPORT *alpm_delta_get_to(pmdelta_t *delta) +{ + ASSERT(delta != NULL, return(NULL)); return(delta->to); } -unsigned long SYMEXPORT alpm_delta_get_size(pmdelta_t *delta) +const char SYMEXPORT *alpm_delta_get_to_md5sum(pmdelta_t *delta) { - ALPM_LOG_FUNC; - - /* Sanity checks */ - ASSERT(delta != NULL, return(-1)); - - return(delta->size); + ASSERT(delta != NULL, return(NULL)); + return(delta->to_md5); } const char SYMEXPORT *alpm_delta_get_filename(pmdelta_t *delta) { - ALPM_LOG_FUNC; - - /* Sanity checks */ ASSERT(delta != NULL, return(NULL)); - - return(delta->filename); + return(delta->delta); } const char SYMEXPORT *alpm_delta_get_md5sum(pmdelta_t *delta) { - ALPM_LOG_FUNC; - - /* Sanity checks */ ASSERT(delta != NULL, return(NULL)); + return(delta->delta_md5); +} - return(delta->md5sum); +unsigned long SYMEXPORT alpm_delta_get_size(pmdelta_t *delta) +{ + ASSERT(delta != NULL, return(-1)); + return(delta->delta_size); } /** @} */ /** Calculates the combined size of a list of delta files. - * * @param deltas the list of pmdelta_t * objects - * * @return the combined size */ unsigned long _alpm_delta_path_size(alpm_list_t *deltas) @@ -100,7 +90,7 @@ unsigned long _alpm_delta_path_size(alpm_list_t *deltas) while(dlts) { pmdelta_t *d = alpm_list_getdata(dlts); - sum += d->size; + sum += d->delta_size; dlts = alpm_list_next(dlts); } @@ -110,9 +100,7 @@ unsigned long _alpm_delta_path_size(alpm_list_t *deltas) /** Calculates the combined size of a list of delta files that are not * in the cache. - * * @param deltas the list of pmdelta_t * objects - * * @return the combined size */ unsigned long _alpm_delta_path_size_uncached(alpm_list_t *deltas) @@ -122,10 +110,10 @@ unsigned long _alpm_delta_path_size_uncached(alpm_list_t *deltas) while(dlts) { pmdelta_t *d = alpm_list_getdata(dlts); - char *fname = _alpm_filecache_find(d->filename); + char *fname = _alpm_filecache_find(d->delta); if(!fname) { - sum += d->size; + sum += d->delta_size; } FREE(fname); @@ -137,17 +125,13 @@ unsigned long _alpm_delta_path_size_uncached(alpm_list_t *deltas) } /** 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. - * * The algorithm is based on Dijkstra's shortest path algorithm. - * * @param deltas the list of pmdelta_t * objects that a package has * @param from the version to start from * @param to the version to end at * @param path the current path - * * @return the list of pmdelta_t * objects that has the smallest size. * NULL (the empty list) is returned if there is no path between the * versions. @@ -201,14 +185,11 @@ static alpm_list_t *shortest_delta_path(alpm_list_t *deltas, } /** 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 package has * @param from the version to start from * @param to the version to end at - * * @return the list of pmdelta_t * objects that has the smallest size. * NULL (the empty list) is returned if there is no path between the * versions. @@ -224,13 +205,13 @@ alpm_list_t *_alpm_shortest_delta_path(alpm_list_t *deltas, const char *from, } /** Parses the string representation of a pmdelta_t object. - * * This function assumes that the string is in the correct format. - * + * This format is as follows: + * $oldfile $oldmd5 $newfile $newmd5 $deltafile $deltamd5 $deltasize * @param line the string to parse - * * @return A pointer to the new pmdelta_t object */ +/* TODO this does not really belong here, but in a parsing lib */ pmdelta_t *_alpm_delta_parse(char *line) { pmdelta_t *delta; @@ -246,19 +227,32 @@ pmdelta_t *_alpm_delta_parse(char *line) tmp2 = tmp; tmp = strchr(tmp, ' '); *(tmp++) = '\0'; + STRDUP(delta->from_md5, tmp2, RET_ERR(PM_ERR_MEMORY, NULL)); + + tmp2 = tmp; + tmp = strchr(tmp, ' '); + *(tmp++) = '\0'; STRDUP(delta->to, tmp2, RET_ERR(PM_ERR_MEMORY, NULL)); tmp2 = tmp; tmp = strchr(tmp, ' '); *(tmp++) = '\0'; - delta->size = atol(tmp2); + STRDUP(delta->to_md5, tmp2, RET_ERR(PM_ERR_MEMORY, NULL)); tmp2 = tmp; tmp = strchr(tmp, ' '); *(tmp++) = '\0'; - STRDUP(delta->filename, tmp2, RET_ERR(PM_ERR_MEMORY, NULL)); + STRDUP(delta->delta, tmp2, RET_ERR(PM_ERR_MEMORY, NULL)); - STRDUP(delta->md5sum, tmp, RET_ERR(PM_ERR_MEMORY, NULL)); + tmp2 = tmp; + tmp = strchr(tmp, ' '); + *(tmp++) = '\0'; + STRDUP(delta->delta_md5, tmp2, RET_ERR(PM_ERR_MEMORY, NULL)); + + tmp2 = tmp; + tmp = strchr(tmp, ' '); + *(tmp++) = '\0'; + delta->delta_size = atol(tmp2); return(delta); } @@ -266,9 +260,11 @@ pmdelta_t *_alpm_delta_parse(char *line) void _alpm_delta_free(pmdelta_t *delta) { FREE(delta->from); + FREE(delta->from_md5); FREE(delta->to); - FREE(delta->filename); - FREE(delta->md5sum); + FREE(delta->to_md5); + FREE(delta->delta); + FREE(delta->delta_md5); FREE(delta); } diff --git a/lib/libalpm/delta.h b/lib/libalpm/delta.h index 007e5d4..a2ac5f0 100644 --- a/lib/libalpm/delta.h +++ b/lib/libalpm/delta.h @@ -22,11 +22,20 @@ #include "alpm.h" struct __pmdelta_t { + /** filename of the 'before' file */ char *from; + /** md5sum of the 'before' file */ + char *from_md5; + /** filename of the 'after' file */ char *to; - unsigned long size; - char *filename; - char *md5sum; + /** md5sum of the 'after' file */ + char *to_md5; + /** filename of the delta patch */ + char *delta; + /** md5sum of the delta file */ + char *delta_md5; + /** filesize of the delta file */ + unsigned long delta_size; }; unsigned long _alpm_delta_path_size(alpm_list_t *deltas); diff --git a/lib/libalpm/sync.c b/lib/libalpm/sync.c index 4101158..3e343ff 100644 --- a/lib/libalpm/sync.c +++ b/lib/libalpm/sync.c @@ -720,7 +720,6 @@ static int apply_deltas(pmtrans_t *trans, alpm_list_t *patches) pmpkg_t *pkg; pmdelta_t *d; char command[PATH_MAX], fname[PATH_MAX]; - char pkgfilename[PATH_MAX]; pkg = alpm_list_getdata(p); p = alpm_list_next(p); @@ -745,26 +744,23 @@ static int apply_deltas(pmtrans_t *trans, alpm_list_t *patches) /* build the patch command */ snprintf(command, PATH_MAX, - "xdelta patch" /* the command */ - " %s/%s" /* the delta */ - " %s/%s-%s-%s" PKGEXT /* the 'from' package */ - " %s/%s-%s-%s" PKGEXT, /* the 'to' package */ - cachedir, d->filename, - cachedir, pkg->name, d->from, pkg->arch, - cachedir, pkg->name, d->to, pkg->arch); + "xdelta patch" /* the command */ + " %s/%s" /* the delta */ + " %s/%s" /* the 'from' package */ + " %s/%s", /* the 'to' package */ + cachedir, d->delta, + cachedir, d->from, + cachedir, d->to); _alpm_log(PM_LOG_DEBUG, _("command: %s\n"), command); - snprintf(pkgfilename, PATH_MAX, "%s-%s-%s" PKGEXT, - pkg->name, d->to, pkg->arch); - - EVENT(trans, PM_TRANS_EVT_DELTA_PATCH_START, pkgfilename, d->filename); + EVENT(trans, PM_TRANS_EVT_DELTA_PATCH_START, d->to, d->delta); if(system(command) == 0) { EVENT(trans, PM_TRANS_EVT_DELTA_PATCH_DONE, NULL, NULL); /* delete the delta file */ - snprintf(fname, PATH_MAX, "%s/%s", cachedir, d->filename); + snprintf(fname, PATH_MAX, "%s/%s", cachedir, d->delta); unlink(fname); /* Delete the 'from' package but only if it is an intermediate @@ -772,8 +768,7 @@ static int apply_deltas(pmtrans_t *trans, alpm_list_t *patches) * as if deltas were not used. Delete the package file if the * previous iteration of the loop used the same package. */ if(pkg == lastpkg) { - snprintf(fname, PATH_MAX, "%s/%s-%s-%s" PKGEXT, - cachedir, pkg->name, d->from, pkg->arch); + snprintf(fname, PATH_MAX, "%s/%s", cachedir, d->from); unlink(fname); } else { lastpkg = pkg; @@ -953,12 +948,12 @@ int _alpm_sync_commit(pmtrans_t *trans, pmdb_t *db_local, alpm_list_t **data) for(dlts = delta_path; dlts; dlts = alpm_list_next(dlts)) { pmdelta_t *d = (pmdelta_t *)alpm_list_getdata(dlts); - char *fpath2 = _alpm_filecache_find(d->filename); + char *fpath2 = _alpm_filecache_find(d->delta); if(!fpath2) { /* add the delta filename to the download list if - * it's not in the cache*/ - files = alpm_list_add(files, strdup(d->filename)); + * it's not in the cache */ + files = alpm_list_add(files, strdup(d->delta)); } /* save the package and delta so that the xdelta patch -- 1.5.4.1