From owner-p4-projects@FreeBSD.ORG Thu May 27 23:38:45 2010 Return-Path: Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id 7020F1065674; Thu, 27 May 2010 23:38:45 +0000 (UTC) Delivered-To: perforce@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 1C27A1065670 for ; Thu, 27 May 2010 23:38:45 +0000 (UTC) (envelope-from ivoras@FreeBSD.org) Received: from repoman.freebsd.org (unknown [IPv6:2001:4f8:fff6::29]) by mx1.freebsd.org (Postfix) with ESMTP id 098D48FC12 for ; Thu, 27 May 2010 23:38:45 +0000 (UTC) Received: from repoman.freebsd.org (localhost [127.0.0.1]) by repoman.freebsd.org (8.14.3/8.14.3) with ESMTP id o4RNcis2030583 for ; Thu, 27 May 2010 23:38:44 GMT (envelope-from ivoras@FreeBSD.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.14.3/8.14.3/Submit) id o4RNcid0030581 for perforce@freebsd.org; Thu, 27 May 2010 23:38:44 GMT (envelope-from ivoras@FreeBSD.org) Date: Thu, 27 May 2010 23:38:44 GMT Message-Id: <201005272338.o4RNcid0030581@repoman.freebsd.org> X-Authentication-Warning: repoman.freebsd.org: perforce set sender to ivoras@FreeBSD.org using -f From: Ivan Voras To: Perforce Change Reviews Precedence: bulk Cc: Subject: PERFORCE change 178895 for review X-BeenThere: p4-projects@freebsd.org X-Mailman-Version: 2.1.5 List-Id: p4 projects tree changes List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 27 May 2010 23:38:45 -0000 http://p4web.freebsd.org/@@178895?ac=10 Change 178895 by ivoras@betelgeuse on 2010/05/27 23:38:34 Step 3: detect changed files Affected files ... .. //depot/projects/soc2010/pkg_patch/src/patch/Makefile#6 edit .. //depot/projects/soc2010/pkg_patch/src/patch/hashjob.c#5 edit .. //depot/projects/soc2010/pkg_patch/src/patch/hashjob.h#5 edit .. //depot/projects/soc2010/pkg_patch/src/patch/main.c#6 edit .. //depot/projects/soc2010/pkg_patch/src/patch/mkpatch.c#4 edit .. //depot/projects/soc2010/pkg_patch/src/patch/mkpatch.h#4 edit .. //depot/projects/soc2010/pkg_patch/src/patch/pkg_patch.h#4 edit .. //depot/projects/soc2010/pkg_patch/src/patch/support.c#3 edit Differences ... ==== //depot/projects/soc2010/pkg_patch/src/patch/Makefile#6 (text+ko) ==== ==== //depot/projects/soc2010/pkg_patch/src/patch/hashjob.c#5 (text+ko) ==== @@ -15,7 +15,9 @@ #include #include +#include #include +#include #include #include @@ -28,20 +30,23 @@ struct hashjob *job = arg; assert(job->filename != NULL); - MD5File(job->filename, job->hash); - job->hash_len = (128/8); + job->hash_len = 32; + memset(job->hash, 0, HASH_MAX_LEN); + if (MD5File(job->filename, job->hash) == NULL) + err(1, "MD5File failed on: %s", job->filename); return (job); } - static void * +static void * hashjob_sha256(void *arg) { struct hashjob *job = arg; assert(job->filename != NULL); + job->hash_len = 64; + memset(job->hash, 0, HASH_MAX_LEN); SHA256_File(job->filename, job->hash); - job->hash_len = (256/8); return (job); } @@ -49,8 +54,7 @@ int hashjob_start(struct hashjob *job, char *filename, enum HASH_TYPE type) { - if (job->filename == NULL) - job->filename = filename; + job->filename = filename; job->finished = 0; if (type == HASH_MD5) return (pthread_create(&job->thread, NULL, hashjob_md5, job)); ==== //depot/projects/soc2010/pkg_patch/src/patch/hashjob.h#5 (text+ko) ==== @@ -22,7 +22,7 @@ HASH_SHA256 }; -#define HASH_MAX_LEN (256/8) +#define HASH_MAX_LEN 65 struct hashjob { char *filename; ==== //depot/projects/soc2010/pkg_patch/src/patch/main.c#6 (text+ko) ==== @@ -38,7 +38,7 @@ char **argv; enum PP_OP patch_op = PP_NONE; char *my_tmp = NULL; -int verbose = 1; +int verbose = 0; static void usage_short(void); ==== //depot/projects/soc2010/pkg_patch/src/patch/mkpatch.c#4 (text+ko) ==== @@ -25,11 +25,13 @@ #include #include #include +#include #include #include #include "pkg_patch.h" #include "mkpatch.h" +#include "hashjob.h" void @@ -39,7 +41,8 @@ char dold[PATH_MAX], dnew[PATH_MAX]; struct pkgxjob xold, xnew; struct filelist_head flold, flnew; - struct filelist_head fldiff_old_new, fldiff_new_old; + struct filelist_head fldiff_old_new, fldiff_new_old, flintersect; + struct filelist_head flchanged; struct filelist *fl; if (argc < 3) @@ -81,18 +84,70 @@ filelist_gather(dold, &flold); SLIST_INIT(&flnew); filelist_gather(dnew, &flnew); + if (verbose) + printf("Processing %d files in old package and %d in new.\n", + filelist_count(&flold), filelist_count(&flnew)); SLIST_INIT(&fldiff_old_new); filelist_diff(&flold, &flnew, &fldiff_old_new); SLIST_INIT(&fldiff_new_old); filelist_diff(&flnew, &flold, &fldiff_new_old); + SLIST_INIT(&flintersect); + filelist_intersect(&flnew, &flold, &flintersect); + if (verbose) + printf("Found %d files to add and %d files to delete.\n", + filelist_count(&fldiff_new_old), + filelist_count(&fldiff_old_new)); - if (verbose > 1) { + if (verbose > 2) { SLIST_FOREACH(fl, &fldiff_new_old, linkage) printf("++ %s\n", fl->filename); SLIST_FOREACH(fl, &fldiff_old_new, linkage) printf("-- %s\n", fl->filename); + if (verbose > 3) + SLIST_FOREACH(fl, &flintersect, linkage) + printf("?? %s\n", fl->filename); } - + SLIST_INIT(&flchanged); + SLIST_FOREACH(fl, &flintersect, linkage) { + char fcold[PATH_MAX], fcnew[PATH_MAX]; + struct hashjob hjold_md5, hjold_sha256, hjnew_md5, hjnew_sha256; + struct filelist *fl2; + + /* TODO: Handle when a file is replaced by a directory and + * vice-versa */ + if (S_ISDIR(fl->st.st_mode)) + continue; + + snprintf(fcold, PATH_MAX, "%s/%s", dold, fl->filename); + assert(access(fcold, R_OK) == 0); + snprintf(fcnew, PATH_MAX, "%s/%s", dnew, fl->filename); + assert(access(fcnew, R_OK) == 0); + + hashjob_start(&hjold_md5, fcold, HASH_MD5); + hashjob_start(&hjold_sha256, fcold, HASH_SHA256); + hashjob_start(&hjnew_md5, fcnew, HASH_MD5); + hashjob_start(&hjnew_sha256, fcnew, HASH_SHA256); + hashjob_finish(&hjold_md5); + hashjob_finish(&hjold_sha256); + hashjob_finish(&hjnew_md5); + hashjob_finish(&hjnew_sha256); + + assert(hjold_md5.hash_len == hjnew_md5.hash_len); + assert(hjnew_sha256.hash_len == hjnew_sha256.hash_len); + + if (memcmp(hjold_md5.hash, hjnew_md5.hash, + hjold_md5.hash_len) != 0 || memcmp(hjold_sha256.hash, + hjnew_sha256.hash, hjold_sha256.hash_len) != 0) { + /* Assume changed files */ + if (verbose > 3) + printf("~~ %s\n", fl->filename); + fl2 = malloc(sizeof(*fl2)); + memcpy(fl2, fl, sizeof(*fl2)); + SLIST_INSERT_HEAD(&flchanged, fl2, linkage); + } + } + if (verbose) + printf("Found %d changed files.\n", filelist_count(&flchanged)); } ==== //depot/projects/soc2010/pkg_patch/src/patch/mkpatch.h#4 (text+ko) ==== ==== //depot/projects/soc2010/pkg_patch/src/patch/pkg_patch.h#4 (text+ko) ==== @@ -54,5 +54,8 @@ int filelist_gather(char *dir, struct filelist_head *head); int filelist_diff(struct filelist_head *fl1, struct filelist_head *fl2, struct filelist_head *diff); +int filelist_intersect(struct filelist_head *fl1, struct filelist_head *fl2, + struct filelist_head *flisect); +unsigned int filelist_count(struct filelist_head *flist); #endif ==== //depot/projects/soc2010/pkg_patch/src/patch/support.c#3 (text+ko) ==== @@ -53,7 +53,7 @@ /* libarchive not threadsafe for extract; call external tar */ job->filename = filename; sprintf(cmd, "%s -x -C %s -f %s", _PATH_TAR, dir, filename); - if (verbose) + if (verbose > 1) printf("pkgxjob: %s\n", cmd); job->fp = popen(cmd, "r+"); if (job->fp == NULL) @@ -92,7 +92,7 @@ fl = malloc(sizeof(*fl)); strncpy(fl->filename, fe->fts_path + dir_len + 1, PATH_MAX); - memcpy(&fl->st, fe->fts_statp, sizeof(fl->st)); + memcpy(&fl->st, fe->fts_statp, sizeof(struct stat)); SLIST_INSERT_HEAD(head, fl, linkage); } } @@ -126,3 +126,40 @@ } return (0); } + + +int +filelist_intersect(struct filelist_head *flist1, struct filelist_head *flist2, + struct filelist_head *flintersect) +{ + struct filelist *fl1, *fl2; + int found; + + SLIST_FOREACH(fl1, flist1, linkage) { + found = 0; + SLIST_FOREACH(fl2, flist2, linkage) { + if (strncmp(fl1->filename, fl2->filename, PATH_MAX) == 0) { + found = 1; + break; + } + } + if (found) { + fl2 = malloc(sizeof(*fl2)); + memcpy(fl2, fl1, sizeof(*fl2)); + SLIST_INSERT_HEAD(flintersect, fl2, linkage); + } + } + return (0); +} + + +unsigned int +filelist_count(struct filelist_head *flist) +{ + unsigned int count = 0; + struct filelist *fl; + + SLIST_FOREACH(fl, flist, linkage) + count++; + return (count); +}