From 61864e1f6f51549c3b32c0d82b8c9deacea3ed73 Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Wed, 12 Jan 2011 00:05:22 -0600 Subject: Refactor backup file status check into separate function This will make it a lot easier to use this stuff elsewhere. Signed-off-by: Dan McGee --- src/pacman/package.c | 59 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/pacman/package.c b/src/pacman/package.c index 7af38435..34eab227 100644 --- a/src/pacman/package.c +++ b/src/pacman/package.c @@ -157,6 +157,37 @@ void dump_pkg_sync(pmpkg_t *pkg, const char *treename, int level) dump_pkg_full(pkg, -level); } +static const char *get_backup_file_status(const char *root, + const char *filename, const char *expected_md5) +{ + char path[PATH_MAX]; + char *ret; + + snprintf(path, PATH_MAX, "%s%s", root, filename); + + /* if we find the file, calculate checksums, otherwise it is missing */ + if(access(path, R_OK) == 0) { + char *md5sum = alpm_compute_md5sum(path); + + if(md5sum == NULL) { + pm_fprintf(stderr, PM_LOG_ERROR, + _("could not calculate checksums for %s\n"), path); + return(NULL); + } + + /* if checksums don't match, file has been modified */ + if (strcmp(md5sum, expected_md5) != 0) { + ret = _("MODIFIED"); + } else { + ret = _("Not Modified"); + } + free(md5sum); + } else { + ret = _("MISSING"); + } + return(ret); +} + /* Display list of backup files and their modification states */ void dump_pkg_backups(pmpkg_t *pkg) @@ -167,37 +198,17 @@ void dump_pkg_backups(pmpkg_t *pkg) if(alpm_pkg_get_backup(pkg)) { /* package has backup files, so print them */ for(i = alpm_pkg_get_backup(pkg); i; i = alpm_list_next(i)) { - char path[PATH_MAX]; + const char *value; char *str = strdup(alpm_list_getdata(i)); - char *ptr = index(str, '\t'); + char *ptr = strchr(str, '\t'); if(ptr == NULL) { free(str); continue; } *ptr = '\0'; ptr++; - snprintf(path, PATH_MAX-1, "%s%s", root, str); - /* if we find the file, calculate checksums, otherwise it is missing */ - if(access(path, R_OK) == 0) { - char *md5sum = alpm_compute_md5sum(path); - - if(md5sum == NULL) { - pm_fprintf(stderr, PM_LOG_ERROR, - _("could not calculate checksums for %s\n"), path); - free(str); - continue; - } - - /* if checksums don't match, file has been modified */ - if (strcmp(md5sum, ptr) != 0) { - printf(_("MODIFIED\t%s\n"), path); - } else { - printf(_("Not Modified\t%s\n"), path); - } - free(md5sum); - } else { - printf(_("MISSING\t\t%s\n"), path); - } + value = get_backup_file_status(root, str, ptr); + printf("%s\t%s%s\n", value, root, str); free(str); } } else { -- cgit v1.2.3-24-g4f1b From c91bd3dda9540bb80f9e73fed87eee69b4675977 Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Wed, 12 Jan 2011 00:06:39 -0600 Subject: Mark backup status strings as untranslated And also change "Not Modified" -> "UNMODIFIED" for consistency. This makes it a lot easier to machine-parse this and not worry about locale differences. Signed-off-by: Dan McGee --- src/pacman/package.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/pacman/package.c b/src/pacman/package.c index 34eab227..68b0693a 100644 --- a/src/pacman/package.c +++ b/src/pacman/package.c @@ -177,13 +177,13 @@ static const char *get_backup_file_status(const char *root, /* if checksums don't match, file has been modified */ if (strcmp(md5sum, expected_md5) != 0) { - ret = _("MODIFIED"); + ret = "MODIFIED"; } else { - ret = _("Not Modified"); + ret = "UNMODIFIED"; } free(md5sum); } else { - ret = _("MISSING"); + ret = "MISSING"; } return(ret); } -- cgit v1.2.3-24-g4f1b From cda7d7847f3434959ef077ec6eb78ea4b2078a5a Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Sat, 22 Jan 2011 11:38:34 -0600 Subject: Be smarter about failure to read backup file contents Instead of always printing MISSING, we can switch on the errno value set by access() and print a more useful string. In this case, handle files we can't read by printing UNREADABLE, print MISSING on ENOENT, and print UNKNOWN for anything else. Fixes FS#22546. Signed-off-by: Dan McGee --- src/pacman/package.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/pacman/package.c b/src/pacman/package.c index 68b0693a..77a5ee72 100644 --- a/src/pacman/package.c +++ b/src/pacman/package.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -183,7 +184,16 @@ static const char *get_backup_file_status(const char *root, } free(md5sum); } else { - ret = "MISSING"; + switch(errno) { + case EACCES: + ret = "UNREADABLE"; + break; + case ENOENT: + ret = "MISSING"; + break; + default: + ret = "UNKNOWN"; + } } return(ret); } -- cgit v1.2.3-24-g4f1b