Commit c8d08a80 authored by npzacs's avatar npzacs

Split function

parent 94016fd4
......@@ -109,12 +109,34 @@ int device_send_cmd(MMCDEV *dev, const uint8_t *cmd, uint8_t *buf, size_t tx, si
return 0;
}
static int _open_block_device(const char *path)
{
struct stat st;
int fd;
if (stat(path, &st)) {
BD_DEBUG(DBG_MMC | DBG_CRIT, "stat(%s) failed\n", path);
return -1;
}
if (!S_ISBLK(st.st_mode)) {
return -1;
}
BD_DEBUG(DBG_MMC, "Opening block device %s\n", path);
fd = open(path, O_RDONLY | O_NONBLOCK);
if (fd < 0) {
BD_DEBUG(DBG_MMC | DBG_CRIT, "Error opening block device %s\n", path);
}
return fd;
}
MMCDEV *device_open(const char *path)
{
char resolved_path[AACS_PATH_MAX];
size_t path_len;
struct stat st;
int fd = -1;
int fd;
/* resolve path */
if (!aacs_resolve_path(path, resolved_path)) {
......@@ -128,27 +150,9 @@ MMCDEV *device_open(const char *path)
resolved_path[path_len] = '\0';
}
if (stat(resolved_path, &st)) {
BD_DEBUG(DBG_MMC | DBG_CRIT, "stat(%s) failed\n", resolved_path);
return NULL;
}
if (S_ISBLK(st.st_mode)) {
/* opening device */
BD_DEBUG(DBG_MMC, "Opening block device %s\n", resolved_path);
fd = open(resolved_path, O_RDONLY | O_NONBLOCK);
if (fd < 0) {
BD_DEBUG(DBG_MMC | DBG_CRIT, "Error opening block device %s\n", resolved_path);
}
} else {
#if !defined(HAVE_MNTENT_H)
BD_DEBUG(DBG_MMC | DBG_CRIT, "Only block devices supported\n");
return NULL;
#endif
}
#if defined(HAVE_MNTENT_H)
fd = _open_block_device(resolved_path);
if (fd < 0) {
#if defined(HAVE_MNTENT_H)
/* resolve mount point to block device */
FILE *proc_mounts;
if ((proc_mounts = setmntent("/proc/mounts", "r"))) {
......@@ -178,8 +182,11 @@ MMCDEV *device_open(const char *path)
} else {
BD_DEBUG(DBG_MMC | DBG_CRIT, "Error opening /proc/mounts\n");
}
}
#else
BD_DEBUG(DBG_MMC | DBG_CRIT, "Only block devices supported\n");
return NULL;
#endif
}
if (fd >= 0) {
MMCDEV *dev = calloc(1, sizeof(MMCDEV));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment