lib/loopdev: use warn_unused_result forimportant functions

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2012-06-21 10:40:43 +02:00
parent 1aae31c048
commit defa0710b6
6 changed files with 74 additions and 36 deletions

View 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);

View 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) {

View 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;
}

View 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));

View 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)

View 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;