Last active
November 5, 2015 17:17
-
-
Save ralt/7b92937950f37a436977 to your computer and use it in GitHub Desktop.
Patch to fix mkfs.btrfs on partitioned loop devices.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/utils.c b/utils.c | |
index f10c178..04207b5 100644 | |
--- a/utils.c | |
+++ b/utils.c | |
@@ -937,22 +937,20 @@ static int is_loop_device (const char* device) { | |
static int resolve_loop_device(const char* loop_dev, char* loop_file, | |
int max_len) | |
{ | |
- int ret; | |
- FILE *f; | |
+ int fd; | |
char fmt[20]; | |
char p[PATH_MAX]; | |
char real_loop_dev[PATH_MAX]; | |
+ struct loop_info64 lo64; | |
- if (!realpath(loop_dev, real_loop_dev)) | |
+ if (!(fd = open(loop_dev, O_RDONLY))) | |
return -errno; | |
- snprintf(p, PATH_MAX, "/sys/block/%s/loop/backing_file", strrchr(real_loop_dev, '/')); | |
- if (!(f = fopen(p, "r"))) | |
+ if (ioctl(fd, LOOP_GET_STATUS64, &lo64) != 0) | |
return -errno; | |
- snprintf(fmt, 20, "%%%i[^\n]", max_len-1); | |
- ret = fscanf(f, fmt, loop_file); | |
- fclose(f); | |
- if (ret == EOF) | |
+ memcpy(loop_file, lo64.lo_file_name, strlen(lo64.lo_file_name) + 1); | |
+ | |
+ if (close(fd) != 0) | |
return -errno; | |
return 0; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment