diff options
author | Adam Spragg <adam@spra.gg> | 2022-05-23 15:29:11 +0100 |
---|---|---|
committer | Adam Spragg <adam@spra.gg> | 2022-05-24 10:12:33 +0100 |
commit | a0902fe51010645f779e4d02cd46f35e488b8bd2 (patch) | |
tree | c741aea22bae668fee418b8ac0938bdbc96675c6 | |
parent | 7c0520451b071e77b4c432ed89fc7559b8c58138 (diff) |
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.
-rw-r--r-- | man1/metastore.1 | 5 | ||||
-rw-r--r-- | metastore.txt | 5 | ||||
-rw-r--r-- | src/metaentry.c | 20 | ||||
-rw-r--r-- | src/metaentry.h | 2 | ||||
-rw-r--r-- | src/metastore.c | 3 | ||||
-rw-r--r-- | src/metastore.h | 3 |
6 files changed, 31 insertions, 7 deletions
diff --git a/man1/metastore.1 b/man1/metastore.1 index f9d1755..67926e1 100644 --- a/man1/metastore.1 +++ b/man1/metastore.1 @@ -81,6 +81,11 @@ Alternatively, one or more paths can be specified and they will each be examined. Later invocations should be made using the exact same paths to ensure that the stored metadata is interpreted correctly. .\" +.SH EXIT STATUS +metastore returns an exit status of 0 on success, or 1 if an error occurred. If +the \fBcompare\fR action is selected and differences are found, the exit status +is 2. +.\" .SH FORMATS .TP .B 0 diff --git a/metastore.txt b/metastore.txt index 351ccc4..691fea5 100644 --- a/metastore.txt +++ b/metastore.txt @@ -87,6 +87,11 @@ PATHS will each be examined. Later invocations should be made using the exact same paths to ensure that the stored metadata is interpreted correctly. +EXIT STATUS + metastore returns an exit status of 0 on success, or 1 if an error oc‐ + curred. If the compare action is selected and differences are found, + the exit status is 2. + FORMATS 0 The original and default format, it is a compact binary repre‐ sentation of the file metadata stored. 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 */ diff --git a/src/metaentry.h b/src/metaentry.h index fd28c17..497cbc2 100644 --- a/src/metaentry.h +++ b/src/metaentry.h @@ -87,7 +87,7 @@ int mentry_compare(struct metaentry *left, msettings *st); /* Compares lists of real and stored metadata and calls pfunc for each */ -void mentries_compare(struct metahash *mhashreal, +int mentries_compare(struct metahash *mhashreal, struct metahash *mhashstored, void (*pfunc)(struct metaentry *real, struct metaentry *stored, diff --git a/src/metastore.c b/src/metastore.c index 2f6d9bc..fb70834 100644 --- a/src/metastore.c +++ b/src/metastore.c @@ -572,7 +572,8 @@ main(int argc, char **argv) switch (action) { case ACTION_DIFF: - mentries_compare(real, stored, compare_print, &settings); + if (mentries_compare(real, stored, compare_print, &settings) > 0) + exit(EXIT_DIFFERENCES); break; case ACTION_SAVE: mentries_tofile(real, settings.metafile, settings.format); diff --git a/src/metastore.h b/src/metastore.h index de51292..b7d588e 100644 --- a/src/metastore.h +++ b/src/metastore.h @@ -46,6 +46,9 @@ #define ACTIONS_READING 0x07 #define ACTIONS_WRITING 0x70 +/* Exit statuses (statii?) other than SUCCESS (0) and FAILURE (1) */ +#define EXIT_DIFFERENCES 2 + /* Possible build defines */ #ifndef NO_XATTR # define NO_XATTR 0 |