From ff18daa68014b465f90586037c63ffe9a646e3ce Mon Sep 17 00:00:00 2001 From: Castulo Martinez Date: Tue, 22 Jan 2019 22:05:42 +0000 Subject: [PATCH] clean should return consistent exit codes Regardless of what codes are used internally, swupd should always exit with a code defined in swupd_exit_codes.h. This commit makes those changes for clean. Signed-off-by: Castulo Martinez --- docs/swupd.1 | 2 ++ docs/swupd.1.rst | 1 + src/clean.c | 23 ++++++++++++----------- src/swupd_exit_codes.h | 3 ++- test/functional/testlib.bash | 1 + 5 files changed, 18 insertions(+), 12 deletions(-) diff --git a/docs/swupd.1 b/docs/swupd.1 index 360e55f9..681e9dda 100644 --- a/docs/swupd.1 +++ b/docs/swupd.1 @@ -651,6 +651,8 @@ The non\-zero return codes for other operations are listed here: \fB30\fP: Unable to list the content of a directory .IP \(bu 2 \fB31\fP: An error ocurred computing the hash of a file +.IP \(bu 2 +\fB32\fP: Unable to get current system time .UNINDENT .UNINDENT .UNINDENT diff --git a/docs/swupd.1.rst b/docs/swupd.1.rst index 9b20e1d6..2d9f38b9 100644 --- a/docs/swupd.1.rst +++ b/docs/swupd.1.rst @@ -440,6 +440,7 @@ The non-zero return codes for other operations are listed here: - **29**: Unable to execute another program in a subprocess - **30**: Unable to list the content of a directory - **31**: An error ocurred computing the hash of a file + - **32**: Unable to get current system time SEE ALSO diff --git a/src/clean.c b/src/clean.c index c768ab24..349f64bd 100644 --- a/src/clean.c +++ b/src/clean.c @@ -86,21 +86,21 @@ typedef bool(remove_predicate_func)(const char *dir, const struct dirent *entry) /* Remove files from path for which pred returns true. * Currently it doesn't recursively remove directories. */ -static int remove_if(const char *path, bool dry_run, remove_predicate_func pred) +static swupd_code remove_if(const char *path, bool dry_run, remove_predicate_func pred) { - int ret = 0; + int ret = SWUPD_OK; DIR *dir; dir = opendir(path); if (!dir) { - return errno; + return SWUPD_COULDNT_LIST_DIR; } char *file = NULL; while (true) { free_string(&file); - ret = 0; + ret = SWUPD_OK; /* Reset errno to distinguish between a previous * failure and the end of stream. */ @@ -109,7 +109,7 @@ static int remove_if(const char *path, bool dry_run, remove_predicate_func pred) entry = readdir(dir); if (!entry) { if (errno) { - ret = errno; + ret = SWUPD_COULDNT_LIST_DIR; } break; } @@ -280,13 +280,13 @@ end: return contents; } -static int clean_staged_manifests(const char *path, bool dry_run, bool all) +static swupd_code clean_staged_manifests(const char *path, bool dry_run, bool all) { DIR *dir; dir = opendir(path); if (!dir) { - return errno; + return SWUPD_COULDNT_LIST_DIR; } /* NOTE: Currently Manifest files have their timestamp from generation @@ -304,7 +304,7 @@ static int clean_staged_manifests(const char *path, bool dry_run, bool all) } } - int ret = 0; + int ret = SWUPD_OK; while (true) { /* Reset errno to properly identify the end of stream. */ errno = 0; @@ -312,7 +312,7 @@ static int clean_staged_manifests(const char *path, bool dry_run, bool all) entry = readdir(dir); if (!entry) { if (errno) { - ret = errno; + ret = SWUPD_COULDNT_LIST_DIR; } break; } @@ -381,10 +381,10 @@ int clean_main(int argc, char **argv) { if (!parse_options(argc, argv)) { print_help(); - return EXIT_FAILURE; + return SWUPD_INVALID_OPTION; } - int ret = 0; + int ret = SWUPD_OK; ret = clean_init(); if (ret != 0) { fprintf(stderr, "Failed swupd initialization, exiting now.\n"); @@ -394,6 +394,7 @@ int clean_main(int argc, char **argv) if (!options.all) { ret = clock_gettime(CLOCK_REALTIME, &now); if (ret != 0) { + ret = SWUPD_COULDNT_GET_TIME; perror("couldn't read current time to decide what files to clean"); goto end; } diff --git a/src/swupd_exit_codes.h b/src/swupd_exit_codes.h index 8190444f..4fa3ef1b 100644 --- a/src/swupd_exit_codes.h +++ b/src/swupd_exit_codes.h @@ -41,7 +41,8 @@ typedef enum { SWUPD_COULDNT_RENAME_FILE, /* 28 couldn't rename a file */ SWUPD_SUBPROCESS_ERROR, /* 29 failure to execute another program in a subprocess */ SWUPD_COULDNT_LIST_DIR, /* 30 couldn't list the content of a directory */ - SWUPD_COMPUTE_HASH_ERROR /* 31 there was an error computing the hash of the specified file */ + SWUPD_COMPUTE_HASH_ERROR, /* 31 there was an error computing the hash of the specified file */ + SWUPD_COULDNT_GET_TIME /* 32 couldn't get current system time */ } swupd_code; diff --git a/test/functional/testlib.bash b/test/functional/testlib.bash index 7b8189aa..82221562 100644 --- a/test/functional/testlib.bash +++ b/test/functional/testlib.bash @@ -44,6 +44,7 @@ export SWUPD_COULDNT_RENAME_FILE=28 # couldn't rename a file export SWUPD_SUBPROCESS_ERROR=29 # failure to execute another program in a subprocess export SWUPD_COULDNT_LIST_DIR=30 # couldn't list the content of a directory export SWUPD_COMPUTE_HASH_ERROR=31 # there was an error computing the hash of the specified file +export SWUPD_COULDNT_GET_TIME=32 # couldn't get current system time # global constant export zero_hash="0000000000000000000000000000000000000000000000000000000000000000"