-static int set_device_or_loop(const char *device_org, char **device, int *loop_fd)
-{
- int r, readonly = 0;
-
- if (!device_org)
- return 0;
-
- r = device_ready(NULL, device_org, O_RDONLY);
- if (r == -ENOTBLK) {
- *device = crypt_loop_get_device();
- log_dbg("Not a block device, %s%s.", *device ?
- "using free loop device " : "no free loop device found",
- *device ?: "");
- if (!*device) {
- log_err(NULL, _("Cannot find a free loopback device.\n"));
- return -ENOSYS;
- }
-
- /* Keep the loop open, dettached on last close. */
- *loop_fd = crypt_loop_attach(*device, device_org, 0, 1, &readonly);
- if (*loop_fd == -1) {
- log_err(NULL, _("Attaching loopback device failed "
- "(loop device with autoclear flag is required).\n"));
- return -EINVAL;
- }
-
- r = device_ready(NULL, *device, O_RDONLY);
- }
-
- if (r < 0)
- return -ENOTBLK;
-
- if (!*device && device_org && !(*device = strdup(device_org)))
- return -ENOMEM;
-
- return 0;
-}
-