mirror of
https://gitlab.com/cryptsetup/cryptsetup.git
synced 2025-12-13 03:40:05 +01:00
Prefer sysfs when reading backing file.
git-svn-id: https://cryptsetup.googlecode.com/svn/trunk@457 36d66b0a-2a48-0410-832c-cd162a569da5
This commit is contained in:
@@ -22,6 +22,7 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <limits.h>
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <linux/loop.h>
|
#include <linux/loop.h>
|
||||||
@@ -120,7 +121,7 @@ int crypt_loop_detach(const char *loop)
|
|||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *crypt_loop_backing_file(const char *loop)
|
static char *_ioctl_backing_file(const char *loop)
|
||||||
{
|
{
|
||||||
struct loop_info64 lo64 = {0};
|
struct loop_info64 lo64 = {0};
|
||||||
int loop_fd;
|
int loop_fd;
|
||||||
@@ -142,6 +143,38 @@ char *crypt_loop_backing_file(const char *loop)
|
|||||||
return strdup((char*)lo64.lo_file_name);
|
return strdup((char*)lo64.lo_file_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static char *_sysfs_backing_file(const char *loop)
|
||||||
|
{
|
||||||
|
struct stat st;
|
||||||
|
char buf[PATH_MAX];
|
||||||
|
size_t len;
|
||||||
|
int fd;
|
||||||
|
|
||||||
|
if (stat(loop, &st) || !S_ISBLK(st.st_mode))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
snprintf(buf, sizeof(buf), "/sys/dev/block/%d:%d/loop/backing_file",
|
||||||
|
major(st.st_rdev), minor(st.st_rdev));
|
||||||
|
|
||||||
|
fd = open(buf, O_RDONLY);
|
||||||
|
if (fd < 0)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
len = read(fd, buf, PATH_MAX);
|
||||||
|
close(fd);
|
||||||
|
if (len < 2)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
buf[len - 1] = '\0';
|
||||||
|
return strdup(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *crypt_loop_backing_file(const char *loop)
|
||||||
|
{
|
||||||
|
char *bf = _sysfs_backing_file(loop);
|
||||||
|
return bf ?: _ioctl_backing_file(loop);
|
||||||
|
}
|
||||||
|
|
||||||
int crypt_loop_device(const char *loop)
|
int crypt_loop_device(const char *loop)
|
||||||
{
|
{
|
||||||
struct stat st;
|
struct stat st;
|
||||||
|
|||||||
Reference in New Issue
Block a user