Change mediaCheckFile to return -2 when file open fails Add new symbolic result code for failing to find the file - ISOMD5SUM_FILE_NOT_FOUND. printMD5SUM now returns result codes instead of calling exit Add new function to handle rc value and print result string Add EXIT STATUS section to manpage --- checkisomd5.1 | 1 + checkisomd5.c | 64 +++++++++++++++++++++++++++++++++-------------------- libcheckisomd5.c | 16 +++++-------- libcheckisomd5.h | 3 +- 4 files changed, 49 insertions(+), 35 deletions(-) diff --git a/checkisomd5.1 b/checkisomd5.1 index ea45f66..78a3fa1 100644 --- a/checkisomd5.1 +++ b/checkisomd5.1 @@ -9,6 +9,7 @@ checkisomd5 \(em check an MD5 checksum implanted by \fBimplantisomd5\fR This manual page documents briefly the \fBcheckisomd5\fR command. \fBcheckisomd5\fR is a program that checks an embedded MD5 checksum in a ISO9660 image (.iso), or block device. The checksum is embedded by the corresponding \fBimplantisomd5\fR command. .PP The check can be aborted by pressing Esc key. +.SH "EXIT STATUS" .PP Program returns exit status 0 if the checksum is correct, or 1 if the checksum is incorrect, non-existent, or check was aborted. .SH "OPTIONS" diff --git a/checkisomd5.c b/checkisomd5.c index 67041a1..df06f5f 100644 --- a/checkisomd5.c +++ b/checkisomd5.c @@ -77,13 +77,45 @@ static void usage(void) { exit(1); } + +/* Process the result code and return the proper exit status value + */ +int processExitStatus(int rc) { + char * result; + + switch (rc) { + case ISOMD5SUM_CHECK_FAILED: + result = "FAIL.\n\nIt is not recommended to use this media."; + break; + case ISOMD5SUM_CHECK_ABORTED: + result = "UNKNOWN.\n\nThe media check was aborted."; + break; + case ISOMD5SUM_CHECK_NOT_FOUND: + result = "NA.\n\nNo checksum information available, unable to verify media."; + break; + case ISOMD5SUM_FILE_NOT_FOUND: + result = "NA.\n\nFile not found."; + break; + case ISOMD5SUM_CHECK_PASSED: + result = "PASS.\n\nIt is OK to use this media."; + break; + default: + result = "checkisomd5 ERROR - bad return value"; + break; + } + + fprintf(stderr, "\nThe media check is complete, the result is: %s\n", result); + + return(rc != ISOMD5SUM_CHECK_PASSED); +} + + int main(int argc, char **argv) { int rc; const char **args; int md5only; int help; struct progressCBData data; - char * result; poptContext optCon; memset(&data, 0, sizeof(struct progressCBData)); @@ -119,8 +151,12 @@ int main(int argc, char **argv) { if (!args || !args[0] || !args[0][0]) usage(); - if (md5only|data.verbose) - printMD5SUM((char *)args[0]); + if (md5only|data.verbose) { + rc = printMD5SUM((char *)args[0]); + if (rc < 0) { + exit(processExitStatus(rc)); + } + } if (md5only) exit(0); @@ -137,26 +173,6 @@ int main(int argc, char **argv) { if (data.verbose) printf("\n"); - switch (rc) { - case ISOMD5SUM_CHECK_FAILED: - result = "FAIL.\n\nIt is not recommended to use this media."; - break; - case ISOMD5SUM_CHECK_ABORTED: - result = "UNKNOWN.\n\nThe media check was aborted."; - break; - case ISOMD5SUM_CHECK_NOT_FOUND: - result = "NA.\n\nNo checksum information available, unable to verify media."; - break; - case ISOMD5SUM_CHECK_PASSED: - result = "PASS.\n\nIt is OK to use this media."; - break; - default: - result = "checkisomd5 ERROR - bad return value"; - break; - } - - fprintf(stderr, "\nThe media check is complete, the result is: %s\n", result); - - exit (rc != ISOMD5SUM_CHECK_PASSED); + exit(processExitStatus(rc)); } diff --git a/libcheckisomd5.c b/libcheckisomd5.c index 44dfc49..afcd198 100644 --- a/libcheckisomd5.c +++ b/libcheckisomd5.c @@ -321,7 +321,7 @@ int mediaCheckFile(char *file, checkCallback cb, void *cbdata) { isofd = open(file, O_RDONLY); if (isofd < 0) { - return ISOMD5SUM_CHECK_NOT_FOUND; + return ISOMD5SUM_FILE_NOT_FOUND; } rc = doMediaCheck(isofd, mediasum, computedsum, &isosize, &supported, cb, cbdata); @@ -334,7 +334,7 @@ int mediaCheckFile(char *file, checkCallback cb, void *cbdata) { return rc; } -void printMD5SUM(char *file) { +int printMD5SUM(char *file) { int isofd; char mediasum[64]; long long isosize; @@ -346,17 +346,11 @@ void printMD5SUM(char *file) { isofd = open(file, O_RDONLY); if (isofd < 0) { - fprintf(stderr, "%s: Unable to find install image.\n", file); - exit(1); + return ISOMD5SUM_FILE_NOT_FOUND; } if (parsepvd(isofd, mediasum, &skipsectors, &isosize, &supported, fragmentsums, &fragmentcount) < 0) { - fprintf(stderr, "%s: Could not get pvd data", file); - fprintf(stderr, "\nUnable to read the disc checksum from the " - "primary volume descriptor.\nThis probably " - "means the disc was created without adding the " - "checksum."); - exit(1); + return ISOMD5SUM_CHECK_NOT_FOUND; } close(isofd); @@ -366,4 +360,6 @@ void printMD5SUM(char *file) { printf("Fragment sums: %s\n", fragmentsums); printf("Fragment count: %lld\n", fragmentcount); } + + return 0; } diff --git a/libcheckisomd5.h b/libcheckisomd5.h index d1e113d..78213d1 100644 --- a/libcheckisomd5.h +++ b/libcheckisomd5.h @@ -5,11 +5,12 @@ #define ISOMD5SUM_CHECK_FAILED 0 #define ISOMD5SUM_CHECK_ABORTED 2 #define ISOMD5SUM_CHECK_NOT_FOUND -1 +#define ISOMD5SUM_FILE_NOT_FOUND -2 /* for non-zero return value, check is aborted */ typedef int (*checkCallback)(void *, long long offset, long long total); int mediaCheckFile(char *iso, checkCallback cb, void *cbdata); -void printMD5SUM(char *file); +int printMD5SUM(char *file); #endif -- 1.6.6.1 _______________________________________________ Anaconda-devel-list mailing list Anaconda-devel-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/anaconda-devel-list