From 40e55df46354536990e5f1e0f0a5026aa7b2b7de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Mon, 29 Jul 2013 23:01:12 +0200 Subject: [PATCH] Add sparsebundle_fatal_error function for unifying fatal errors The function is marked with the printf archetype, so that the compiler will not complain that we're passing non-string-literals to vfprintf. --- sparsebundlefs.cpp | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/sparsebundlefs.cpp b/sparsebundlefs.cpp index a600feb..168c2a3 100644 --- a/sparsebundlefs.cpp +++ b/sparsebundlefs.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -381,6 +382,23 @@ static int sparsebundle_release(const char * /* path */, struct fuse_file_info * return 0; } +__attribute__((noreturn, format(printf, 1, 2))) static void sparsebundle_fatal_error(const char *message, ...) +{ + fprintf(stderr, "sparsebundlefs: "); + + va_list args; + va_start(args, message); + vfprintf(stderr, message, args); + va_end(args); + + if (errno) + fprintf(stderr, ": %s", strerror(errno)); + + fprintf(stderr, "\n"); + + exit(EXIT_FAILURE); +} + static int sparsebundle_show_usage(char *program_name) { fprintf(stderr, "usage: %s [-o options] [-s] [-f] [-D] \n", program_name); @@ -416,10 +434,8 @@ static int sparsebundle_opt_proc(void *data, const char *arg, int key, struct fu static off_t read_size(const string &str) { uintmax_t value = strtoumax(str.c_str(), 0, 10); - if (errno == ERANGE || value > uintmax_t(numeric_limits::max())) { - fprintf(stderr, "Disk image too large to be mounted (%s bytes)\n", str.c_str()); - exit(EXIT_FAILURE); - } + if (errno == ERANGE || value > uintmax_t(numeric_limits::max())) + sparsebundle_fatal_error("disk image too large (%s bytes)", str.c_str()); return value; } @@ -453,10 +469,8 @@ int main(int argc, char **argv) sparsebundle.path = abs_path; char *plist_path; - if (asprintf(&plist_path, "%s/Info.plist", sparsebundle.path) == -1) { - perror("Failed to resolve Info.plist path"); - return EXIT_FAILURE; - } + if (asprintf(&plist_path, "%s/Info.plist", sparsebundle.path) == -1) + sparsebundle_fatal_error("could not resolve Info.plist path"); ifstream plist_file(plist_path); stringstream plist_data;