From a0902fe51010645f779e4d02cd46f35e488b8bd2 Mon Sep 17 00:00:00 2001 From: Adam Spragg Date: Mon, 23 May 2022 15:29:11 +0100 Subject: Return non-zero exit status if "compare" finds differences This makes `metastore` more useful in scripts, just to check if it found anything, in the same way as `diff` and `grep`. Note that POSIX requires that EXIT_SUCCESS is 0, and EXIT_FAILURE is 1, so I've defined EXIT_DIFFERENCES as 2 which, while different from the values `diff` and `grep` use (1 for finding something, 2 for errors), maintains a certain amount of backwards compatibility with previous versions of metastore. --- src/metaentry.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'src/metaentry.c') diff --git a/src/metaentry.c b/src/metaentry.c index 7e90290..37069b0 100644 --- a/src/metaentry.c +++ b/src/metaentry.c @@ -875,7 +875,7 @@ mentry_compare(struct metaentry *left, struct metaentry *right, msettings *st) } /* Compares lists of real and stored metadata and calls pfunc for each */ -void +int mentries_compare(struct metahash *mhashreal, struct metahash *mhashstored, void (*pfunc) @@ -884,10 +884,11 @@ mentries_compare(struct metahash *mhashreal, { struct metaentry *real, *stored; int key; + int cmp, diffs = 0; if (!mhashreal || !mhashstored) { msg(MSG_ERROR, "%s called with empty list\n", __func__); - return; + return -1; } for (key = 0; key < HASH_INDEXES; key++) { @@ -895,18 +896,27 @@ mentries_compare(struct metahash *mhashreal, stored = mentry_find(real->path, mhashstored); if (!stored) - pfunc(real, NULL, DIFF_ADDED); + cmp = DIFF_ADDED; else - pfunc(real, stored, mentry_compare(real, stored, st)); + cmp = mentry_compare(real, stored, st); + + if (cmp > 0) + ++diffs; + + pfunc(real, stored, cmp); } for (stored = mhashstored->bucket[key]; stored; stored = stored->next) { real = mentry_find(stored->path, mhashreal); - if (!real) + if (!real) { + ++diffs; pfunc(NULL, stored, DIFF_DELE); + } } } + + return diffs; } /* Dumps given metadata */ -- cgit v1.2.1