Skip to content

Instantly share code, notes, and snippets.

@xkikeg
Last active July 13, 2022 08:36
Show Gist options
  • Save xkikeg/4645373 to your computer and use it in GitHub Desktop.
Save xkikeg/4645373 to your computer and use it in GitHub Desktop.
Find data positions of sparse file with SEEK_DATA & SEEK_HOLE, this does not work in Ubuntu 12.04.
#define _FILE_OFFSET_BITS 64
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#ifndef SEEK_DATA
#warning "SEEK_DATA is undeclared and manually defined."
#define SEEK_DATA 3 /* seek to the next data */
#endif
#ifndef SEEK_HOLE
#warning "SEEK_HOLE is undeclared and manually defined."
#define SEEK_HOLE 4 /* seek to the next hole */
#endif
int main(int argc, char ** argv)
{
if (argc != 2) {
fprintf(stderr, "Usage: %s file\n", argv[0]);
}
int fd = open(argv[1], O_RDONLY);
if(fd == -1) {
perror("failed to open the file.");
exit(EXIT_FAILURE);
}
off_t offset=0;
struct stat status;
fstat(fd, &status);
const off_t size=status.st_size;
do {
off_t beg=lseek(fd, offset, SEEK_DATA);
if(beg == -1) {
perror("SEEK_DATA failed\n");
exit(EXIT_FAILURE);
}
off_t end=lseek(fd, beg, SEEK_HOLE);
if(end == -1) {
perror("SEEK_HOLE failed\n");
exit(EXIT_FAILURE);
}
lseek(fd, beg, SEEK_SET);
fprintf(stderr, "0x%llx 0x%llx\n",
(unsigned long long)beg,
(unsigned long long)end);
offset = end;
} while (offset < size);
return 0;
}
@xkikeg
Copy link
Author

xkikeg commented Jul 13, 2022

Thanks for the comment with the code! I honestly got lost all the context around my snippet and even not sure how to test it though. At least I can say yours looks cleaner and better :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment