]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/loopdev: use warn_unused_result forimportant functions
authorKarel Zak <kzak@redhat.com>
Thu, 21 Jun 2012 08:40:43 +0000 (10:40 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 21 Jun 2012 08:40:43 +0000 (10:40 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
include/loopdev.h
lib/loopdev.c
libmount/src/cache.c
libmount/src/context_loopdev.c
partx/partx.c
sys-utils/losetup.c

index 143c0d30ff7f9abc806bb676f4d1a02ddbb9499f..5c458780314de81f1726fadd46f7234851b89784 100644 (file)
@@ -141,11 +141,13 @@ extern int loopdev_count_by_backing_file(const char *filename, char **loopdev);
 /*
  * Low-level
  */
-extern int loopcxt_init(struct loopdev_cxt *lc, int flags);
+extern int loopcxt_init(struct loopdev_cxt *lc, int flags)
+                               __attribute__ ((warn_unused_result));
 extern void loopcxt_deinit(struct loopdev_cxt *lc);
 extern void loopcxt_enable_debug(struct loopdev_cxt *lc, int enable);
 
-extern int loopcxt_set_device(struct loopdev_cxt *lc, const char *device);
+extern int loopcxt_set_device(struct loopdev_cxt *lc, const char *device)
+                               __attribute__ ((warn_unused_result));
 extern int loopcxt_has_device(struct loopdev_cxt *lc);
 extern char *loopcxt_strdup_device(struct loopdev_cxt *lc);
 extern const char *loopcxt_get_device(struct loopdev_cxt *lc);
index 8e57067358836345ea673c302301ee381acc011c..b759f0c28aee414f8435e466f5bbe880ee5a3802 100644 (file)
@@ -148,6 +148,7 @@ int loopcxt_has_device(struct loopdev_cxt *lc)
  */
 int loopcxt_init(struct loopdev_cxt *lc, int flags)
 {
+       int rc;
        struct stat st;
        struct loopdev_cxt dummy = UL_LOOPDEVCXT_EMPTY;
 
@@ -156,7 +157,10 @@ int loopcxt_init(struct loopdev_cxt *lc, int flags)
 
        memcpy(lc, &dummy, sizeof(dummy));
        lc->flags = flags;
-       loopcxt_set_device(lc, NULL);
+
+       rc = loopcxt_set_device(lc, NULL);
+       if (rc)
+               return rc;
 
        if (!(lc->flags & LOOPDEV_FL_NOSYSFS) &&
            get_linux_version() >= KERNEL_VERSION(2,6,37))
@@ -188,7 +192,7 @@ void loopcxt_deinit(struct loopdev_cxt *lc)
        free(lc->filename);
        lc->filename = NULL;
 
-       loopcxt_set_device(lc, NULL);
+       ignore_result( loopcxt_set_device(lc, NULL) );
        loopcxt_deinit_iterator(lc);
 
        errno = errsv;
@@ -378,8 +382,8 @@ static int loopiter_set_device(struct loopdev_cxt *lc, const char *device)
        if ((lc->iter.flags & LOOPITER_FL_FREE) && !used)
                return 0;
 
-       DBG(lc, loopdev_debug("iter: setting device"));
-       loopcxt_set_device(lc, NULL);
+       DBG(lc, loopdev_debug("iter: unset device"));
+       ignore_result( loopcxt_set_device(lc, NULL) );
        return 1;
 }
 
@@ -1274,27 +1278,29 @@ int loopdev_is_autoclear(const char *device)
        if (!device)
                return 0;
 
-       loopcxt_init(&lc, 0);
-       loopcxt_set_device(&lc, device);
-       rc = loopcxt_is_autoclear(&lc);
-       loopcxt_deinit(&lc);
+       rc = loopcxt_init(&lc, 0);
+       if (!rc)
+               rc = loopcxt_set_device(&lc, device);
+       if (!rc)
+               rc = loopcxt_is_autoclear(&lc);
 
+       loopcxt_deinit(&lc);
        return rc;
 }
 
 char *loopdev_get_backing_file(const char *device)
 {
        struct loopdev_cxt lc;
-       char *res;
+       char *res = NULL;
 
        if (!device)
                return NULL;
+       if (loopcxt_init(&lc, 0))
+               return NULL;
+       if (loopcxt_set_device(&lc, device) == 0)
+               res = loopcxt_get_backing_file(&lc);
 
-       loopcxt_init(&lc, 0);
-       loopcxt_set_device(&lc, device);
-       res = loopcxt_get_backing_file(&lc);
        loopcxt_deinit(&lc);
-
        return res;
 }
 
@@ -1311,8 +1317,11 @@ int loopdev_is_used(const char *device, const char *filename,
        if (!device || !filename)
                return 0;
 
-       loopcxt_init(&lc, 0);
-       loopcxt_set_device(&lc, device);
+       rc = loopcxt_init(&lc, 0);
+       if (!rc)
+               rc = loopcxt_set_device(&lc, device);
+       if (rc)
+               return rc;
 
        rc = !stat(filename, &st);
        rc = loopcxt_is_used(&lc, rc ? &st : NULL, filename, offset, flags);
@@ -1329,8 +1338,9 @@ int loopdev_delete(const char *device)
        if (!device)
                return -EINVAL;
 
-       loopcxt_init(&lc, 0);
-       rc = loopcxt_set_device(&lc, device);
+       rc = loopcxt_init(&lc, 0);
+       if (!rc)
+               rc = loopcxt_set_device(&lc, device);
        if (!rc)
                rc = loopcxt_delete_device(&lc);
        loopcxt_deinit(&lc);
@@ -1377,7 +1387,8 @@ char *loopdev_find_by_backing_file(const char *filename, uint64_t offset, int fl
        if (!filename)
                return NULL;
 
-       loopcxt_init(&lc, 0);
+       if (loopcxt_init(&lc, 0))
+               return NULL;
        if (loopcxt_find_by_backing_file(&lc, filename, offset, flags))
                res = loopcxt_strdup_device(&lc);
        loopcxt_deinit(&lc);
@@ -1393,12 +1404,14 @@ char *loopdev_find_by_backing_file(const char *filename, uint64_t offset, int fl
 int loopdev_count_by_backing_file(const char *filename, char **loopdev)
 {
        struct loopdev_cxt lc;
-       int count = 0;
+       int count = 0, rc;
 
        if (!filename)
                return -1;
 
-       loopcxt_init(&lc, 0);
+       rc = loopcxt_init(&lc, 0);
+       if (rc)
+               return rc;
        if (loopcxt_init_iterator(&lc, LOOPITER_FL_USED))
                return -1;
 
@@ -1436,7 +1449,8 @@ static void test_loop_info(const char *device, int flags, int debug)
        char *p;
        uint64_t u64;
 
-       loopcxt_init(&lc, flags);
+       if (loopcxt_init(&lc, flags))
+               return
        loopcxt_enable_debug(&lc, debug);
 
        if (loopcxt_set_device(&lc, device))
@@ -1462,7 +1476,8 @@ static void test_loop_scan(int flags, int debug)
        struct loopdev_cxt lc;
        int rc;
 
-       loopcxt_init(&lc, 0);
+       if (loopcxt_init(&lc, 0))
+               return;
        loopcxt_enable_debug(&lc, debug);
 
        if (loopcxt_init_iterator(&lc, flags))
@@ -1488,9 +1503,11 @@ static void test_loop_scan(int flags, int debug)
 static int test_loop_setup(const char *filename, const char *device, int debug)
 {
        struct loopdev_cxt lc;
-       int rc = 0;
+       int rc;
 
-       loopcxt_init(&lc, 0);
+       rc = loopcxt_init(&lc, 0);
+       if (rc)
+               return rc;
        loopcxt_enable_debug(&lc, debug);
 
        if (device) {
index fe9c821e17276a764a5c1d70c3867b9dac5542ef..220a1465ef06d15e00e23a82d4316a70faa48078 100644 (file)
@@ -487,8 +487,8 @@ char *mnt_pretty_path(const char *path, struct libmnt_cache *cache)
        if (strncmp(pretty, "/dev/loop", 9) == 0) {
                struct loopdev_cxt lc;
 
-               loopcxt_init(&lc, 0);
-               loopcxt_set_device(&lc, pretty);
+               if (loopcxt_init(&lc, 0) || loopcxt_set_device(&lc, pretty))
+                       goto done;
 
                if (loopcxt_is_autoclear(&lc)) {
                        char *tmp = loopcxt_get_backing_file(&lc);
@@ -502,6 +502,7 @@ char *mnt_pretty_path(const char *path, struct libmnt_cache *cache)
 
        }
 
+done:
        /* don't return pointer to the cache, allocate a new string */
        return cache ? strdup(pretty) : pretty;
 }
index 290e0d3d26eaceb1026b2523f948f17aac7825c5..da246e3bbce5ce694cbf67d7e87edb6e46c67f3c 100644 (file)
@@ -155,7 +155,9 @@ int mnt_context_setup_loopdev(struct libmnt_context *cxt)
                lo_flags |= LO_FLAGS_READ_ONLY;
        }
 
-       loopcxt_init(&lc, 0);
+       rc = loopcxt_init(&lc, 0);
+       if (rc)
+               return rc;
 
        ON_DBG(CXT, loopcxt_enable_debug(&lc, 1));
 
index fd8e4400cee5b9f9f770dd846baeb828ad1f7341..0c3f8f98086f7d48442d6c1847a4c2da232555ad 100644 (file)
@@ -102,7 +102,8 @@ static void assoc_loopdev(const char *fname)
 {
        int rc;
 
-       loopcxt_init(&lc, 0);
+       if (loopcxt_init(&lc, 0))
+               err(EXIT_FAILURE, _("failed to initialize loopcxt"));
 
        rc = loopcxt_find_unused(&lc);
        if (rc)
index bc9e6c68ae6ccd2f6419c8f6cdbf6f4b17fad232..48af8ebcac1e12f48e1349519df29f6783b4e5de 100644 (file)
@@ -254,7 +254,9 @@ int main(int argc, char **argv)
        textdomain(PACKAGE);
        atexit(close_stdout);
 
-       loopcxt_init(&lc, 0);
+       if (loopcxt_init(&lc, 0))
+               err(EXIT_FAILURE, _("failed to initialize loopcxt"));
+
        loopcxt_enable_debug(&lc, getenv("LOOPDEV_DEBUG") ? TRUE : FALSE);
 
        while ((c = getopt_long(argc, argv, "ac:d:De:E:fhj:o:p:PrvV",
@@ -267,7 +269,9 @@ int main(int argc, char **argv)
                case 'c':
                        exclusive_option(&excl_any, EXCL_SET_CAPACITY, EXCL_ERROR);
                        act = A_SET_CAPACITY;
-                       loopcxt_set_device(&lc, optarg);
+                       if (loopcxt_set_device(&lc, optarg))
+                               err(EXIT_FAILURE, _("%s: failed to use device"),
+                                               optarg);
                        break;
                case 'r':
                        lo_flags |= LO_FLAGS_READ_ONLY;
@@ -275,7 +279,9 @@ int main(int argc, char **argv)
                case 'd':
                        exclusive_option(&excl_any, EXCL_DETACH, EXCL_ERROR);
                        act = A_DELETE;
-                       loopcxt_set_device(&lc, optarg);
+                       if (loopcxt_set_device(&lc, optarg))
+                               err(EXIT_FAILURE, _("%s: failed to use device"),
+                                               optarg);
                        break;
                case 'D':
                        exclusive_option(&excl_any, EXCL_DETACH_ALL, EXCL_ERROR);
@@ -341,7 +347,10 @@ int main(int argc, char **argv)
                 * losetup <device>
                 */
                act = A_SHOW_ONE;
-               loopcxt_set_device(&lc, argv[optind++]);
+               if (loopcxt_set_device(&lc, argv[optind]))
+                       err(EXIT_FAILURE, _("%s: failed to use device"),
+                                       argv[optind]);
+               optind++;
        }
        if (!act) {
                /*
@@ -351,7 +360,10 @@ int main(int argc, char **argv)
 
                if (optind >= argc)
                        errx(EXIT_FAILURE, _("no loop device specified"));
-               loopcxt_set_device(&lc, argv[optind++]);
+               if (loopcxt_set_device(&lc, argv[optind]))
+                       err(EXIT_FAILURE, _("%s failed to use device"),
+                                       argv[optind]);
+               optind++;
 
                if (optind >= argc)
                        errx(EXIT_FAILURE, _("no file specified"));
@@ -425,7 +437,10 @@ int main(int argc, char **argv)
        case A_DELETE:
                res = delete_loop(&lc);
                while (optind < argc) {
-                       loopcxt_set_device(&lc, argv[optind++]);
+                       if (loopcxt_set_device(&lc, argv[optind]))
+                               warn(_("%s: failed to use device"),
+                                               argv[optind]);
+                       optind++;
                        res += delete_loop(&lc);
                }
                break;