PERFORCE change 178895 for review
Ivan Voras
ivoras at FreeBSD.org
Thu May 27 23:38:45 UTC 2010
http://p4web.freebsd.org/@@178895?ac=10
Change 178895 by ivoras at 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 <sys/types.h>
#include <pthread.h>
+#include <string.h>
#include <assert.h>
+#include <err.h>
#include <md5.h>
#include <sha256.h>
@@ -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 <stdlib.h>
#include <stdio.h>
#include <unistd.h>
+#include <assert.h>
#include <err.h>
#include <pkg.h>
#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);
+}
More information about the p4-projects
mailing list