summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2004-07-18 18:47:55 +0000
committerLennart Poettering <lennart@poettering.net>2004-07-18 18:47:55 +0000
commitebf3b3ad1795c877c4164b46aab1397237210417 (patch)
tree28104ccf5c0352a804ef6de3b16bb199d547e1af
parent90c80089c6d6e2d7fe9680e567d8241806d7de9e (diff)
add support for file size accounting in --diff
git-svn-id: file:///home/lennart/svn/public/syrep/trunk@57 07ea20a6-d2c5-0310-9e02-9ef735347d72
-rw-r--r--src/diff.c43
-rw-r--r--src/diff.h2
-rw-r--r--src/syrep.c3
-rw-r--r--src/syrep.ggo2
-rw-r--r--src/util.c27
-rw-r--r--src/util.h2
6 files changed, 71 insertions, 8 deletions
diff --git a/src/diff.c b/src/diff.c
index a589504..b2d33a7 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -270,9 +270,10 @@ finish:
return NULL;
}
-
struct cb_info {
struct syrep_db_context *c1, *c2;
+ const char *p1, *p2;
+ off_t sum1, sum2;
};
static int list_cb(DB *ddb, struct syrep_name *name, struct diff_entry *de, void *p) {
@@ -305,7 +306,9 @@ static int list_cb(DB *ddb, struct syrep_name *name, struct diff_entry *de, void
switch (de->action) {
case DIFF_COPY: {
char d[33];
+ char sizet[100] = " ", *psizet = "";
char dst;
+ const char *root = NULL;
int mf;
if ((mf = get_current_nrecno_by_md(de->repository == cb_info->c1 ? cb_info->c2 : cb_info->c1,
@@ -313,16 +316,37 @@ static int list_cb(DB *ddb, struct syrep_name *name, struct diff_entry *de, void
return -1;
if (de->repository == cb_info->c1) {
+ root = cb_info->p1;
dst = 'B';
fhex_md5(md1.digest, d);
} else {
+ root = cb_info->p2;
dst = 'A';
fhex_md5(md2.digest, d);
}
d[32] = 0;
- printf("COPY <%s|%s> TO %c%s\n", d, name->path, dst, mf ? " (LINK POSSIBLE)" : "");
+ if (args.sizes_flag && root && !mf) {
+ off_t fsize;
+ char cpath[PATH_MAX];
+ snprintf(cpath, sizeof(cpath), "%s/%s", root, name->path);
+
+ if ((fsize = filesize2(cpath)) != (off_t) -1) {
+ strcpy(sizet, " (");
+ snprint_off(sizet+2, sizeof(sizet)-3, fsize);
+ strcat(sizet, ")");
+ psizet = sizet;
+
+
+ if (de->repository == cb_info->c1)
+ cb_info->sum1 += fsize;
+ else
+ cb_info->sum2 += fsize;
+ }
+ }
+
+ printf("COPY <%s|%s> TO %c%s%s\n", d, name->path, dst, mf ? " (LINK POSSIBLE)" : "", psizet);
break;
}
@@ -361,12 +385,23 @@ static int list_cb(DB *ddb, struct syrep_name *name, struct diff_entry *de, void
return 0;
}
-int list_diff(struct syrep_db_context *c1, struct syrep_db_context *c2, DB *ddb) {
+int list_diff(struct syrep_db_context *c1, struct syrep_db_context *c2, DB *ddb, const char *p1, const char *p2) {
struct cb_info cb_info;
+ char sumt[100];
cb_info.c1 = c1;
cb_info.c2 = c2;
+ cb_info.p1 = p1;
+ cb_info.p2 = p2;
+ cb_info.sum1 = cb_info.sum2 = 0;
- return diff_foreach(ddb, list_cb, &cb_info);
+ if (diff_foreach(ddb, list_cb, &cb_info) < 0)
+ return -1;
+
+ if (cb_info.sum1)
+ printf("TOTAL SUM FROM A: %s\n", snprint_off(sumt, sizeof(sumt), cb_info.sum1));
+ if (cb_info.sum2)
+ printf("TOTAL SUM FROM B: %s\n", snprint_off(sumt, sizeof(sumt), cb_info.sum2));
+ return 0;
}
int diff_foreach(DB *ddb, int (*cb)(DB *ddb, struct syrep_name *name, struct diff_entry *de, void *p), void *p) {
diff --git a/src/diff.h b/src/diff.h
index 2c1c134..1b2c711 100644
--- a/src/diff.h
+++ b/src/diff.h
@@ -38,6 +38,6 @@ struct diff_entry {
DB* make_diff(struct syrep_db_context *c1, struct syrep_db_context *c2);
int diff_foreach(DB *ddb, int (*cb)(DB *db, struct syrep_name *name, struct diff_entry *de, void *p), void *p);
-int list_diff(struct syrep_db_context *c1, struct syrep_db_context *c2, DB *ddb);
+int list_diff(struct syrep_db_context *c1, struct syrep_db_context *c2, DB *ddb, const char *p1, const char *p2);
#endif
diff --git a/src/syrep.c b/src/syrep.c
index 17e6427..266082f 100644
--- a/src/syrep.c
+++ b/src/syrep.c
@@ -100,7 +100,7 @@ static int do_diff(void) {
if (!(ddb = make_diff(c1, c2)))
goto finish;
- if (list_diff(c1, c2, ddb) < 0)
+ if (list_diff(c1, c2, ddb, isdirectory(args.inputs[0]) > 0 ? args.inputs[0] : NULL, isdirectory(args.inputs[1]) > 0 ? args.inputs[1] : NULL) < 0)
goto finish;
r = 0;
@@ -230,7 +230,6 @@ static int do_makepatch(void) {
if (makepatch(c1, c2, args.inputs[0]) < 0)
goto finish;
-
if (!args.output_file_given && isatty(fileno(stdout)))
fprintf(stderr, "Sorry, I am not going to write the patch data to a tty.\n");
else if (db_context_save(c1, args.output_file_given ? args.output_file_arg : NULL) < 0)
diff --git a/src/syrep.ggo b/src/syrep.ggo
index 2342371..d2a7bb1 100644
--- a/src/syrep.ggo
+++ b/src/syrep.ggo
@@ -43,6 +43,8 @@ option "update" - "Update a repository snapshot" flag off
option "progress" p "update: Show progress" flag off
option "diff" - "Show difference between two repositories or snapshots" flag off
+ option "sizes" s "diff: show file sizes to copy (works online on repositories)" flag off
+ option "human-readable" H "diff: show sizes human readable" flag off
option "merge" - "Merge a snapshot or a repository into a repository" flag off
option "question" q "merge: Ask a question before each action" flag off
diff --git a/src/util.c b/src/util.c
index 97e470f..2d010a0 100644
--- a/src/util.c
+++ b/src/util.c
@@ -200,7 +200,7 @@ off_t filesize(int fd) {
struct stat st;
if (fstat(fd, &st) < 0) {
- fprintf(stderr, "stat(): %s\n", strerror(errno));
+/* fprintf(stderr, "stat(): %s\n", strerror(errno));*/
return (off_t) -1;
}
@@ -765,3 +765,28 @@ ssize_t loop_write(int fd, void *d, size_t l) {
return p-d;
}
+
+char *snprint_off(char *s, size_t l, off_t off) {
+ assert(s && l);
+
+ if (args.human_readable_flag && off >= 1024*1024*1024)
+ snprintf(s, l, "%0.1f GB", (double) off/1024/1024/1024);
+ else if (args.human_readable_flag && off >= 1024*1024)
+ snprintf(s, l, "%0.1f MB", (double) off/1024/1024);
+ else if (args.human_readable_flag && off >= 1024)
+ snprintf(s, l, "%0.1f KB", (double) off/1024);
+ else
+ snprintf(s, l, "%llu", (uint64_t) off);
+
+ return s;
+}
+
+off_t filesize2(const char *p) {
+ struct stat st;
+
+ if (stat(p, &st) < 0)
+ return (off_t) -1;
+
+ return st.st_size;
+
+}
diff --git a/src/util.h b/src/util.h
index 50e3a95..01fe31d 100644
--- a/src/util.h
+++ b/src/util.h
@@ -59,5 +59,7 @@ ssize_t loop_write(int fd, void *d, size_t l);
int expand_file(int fd, off_t l);
off_t filesize(int fd);
+off_t filesize2(const char *p);
+char *snprint_off(char *s, size_t l, off_t off);
#endif