summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/extent-map.c8
-rw-r--r--src/cow-extent-map.c13
2 files changed, 15 insertions, 6 deletions
diff --git a/lib/extent-map.c b/lib/extent-map.c
index ab9cfb0..42071c6 100644
--- a/lib/extent-map.c
+++ b/lib/extent-map.c
@@ -25,7 +25,13 @@ int fiemap(int fd, uint32_t flags, int (*handle_extent)(struct fiemap_extent)) {
if (ioctl(fd, FS_IOC_FIEMAP, fm) < 0)
return -errno;
- for (size_t i = 0; i < fm->fm_extent_count; i++) {
+ if (fm->fm_mapped_extents == 0) {
+ if (bytes_mapped != 0)
+ error(EXIT_FAILURE, 0, "FS_IOC_FIEMAP is misbehaving");
+ return EXIT_SUCCESS;
+ }
+
+ for (size_t i = 0; i < fm->fm_mapped_extents; i++) {
int r = handle_extent(fm->fm_extents[i]);
if (r) {
free(fm);
diff --git a/src/cow-extent-map.c b/src/cow-extent-map.c
index fe9e964..d636dad 100644
--- a/src/cow-extent-map.c
+++ b/src/cow-extent-map.c
@@ -9,6 +9,7 @@
#include <stdio.h> /* for printf(3p), fprintf(3p), stderr */
#include <stdlib.h> /* for free(3p), exit(3p), EXIT_SUCCESS, EXIT_FAILURE */
#include <string.h> /* for strcmp(3p) */
+#include <unistd.h> /* for close(2) */
#include "extent-map.h" /* for uint32_t, fiemap stuff */
@@ -101,11 +102,6 @@ int main(int argc, char *argv[]) {
int status = 0;
for (int i = optind; i < argc; i++) {
char *filename = argv[i];
- if (machine) {
- printf("%s", filename);
- putchar('\0');
- } else if (argc - optind > 1)
- printf("%s:\n", filename);
int fd = open(filename, O_RDONLY);
if (fd < 0) {
error(0, errno, "%s", filename);
@@ -113,6 +109,12 @@ int main(int argc, char *argv[]) {
continue;
}
+ if (machine) {
+ printf("%s", filename);
+ putchar('\0');
+ } else if (argc - optind > 1)
+ printf("%s:\n", filename);
+
int r = fiemap(fd, flags, print_extent);
if (r < 0) {
error(0, -r, "%s: FS_IOC_FIEMAP", filename);
@@ -122,6 +124,7 @@ int main(int argc, char *argv[]) {
}
if (machine)
putchar('\0');
+ close(fd);
}
return status;
}