Skip to content

Instantly share code, notes, and snippets.

Created July 7, 2016 10:57
Show Gist options
  • Save anonymous/992364ceca984d3f14099ec94aaacd9d to your computer and use it in GitHub Desktop.
Save anonymous/992364ceca984d3f14099ec94aaacd9d to your computer and use it in GitHub Desktop.
#define _GNU_SOURCE
#define _FILE_OFFSET_BITS 64
#include <unistd.h>
#include <linux/falloc.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <errno.h>
static void usage(int argc, char **argv)
{
(void)argc;
printf("Usage: %s <Path>\n", argv[0]);
exit(EXIT_FAILURE);
}
int main(int argc, char **argv)
{
int fd, ret;
off_t i, blk;
const int blk_size = 4096;
struct stat file_stat;
if (argc != 2)
usage(argc, argv);
fd = open(argv[1], O_RDWR);
if (fd < 0) {
perror("open");
return EXIT_FAILURE;
}
ret = fstat(fd, &file_stat);
if (ret < 0) {
perror("stat");
close(fd);
return EXIT_FAILURE;
}
blk = (file_stat.st_size + blk_size - 1) / blk_size;
for (i = 0; i < blk; i += 10) {
printf("i: %llu\n", i);
ret = fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, i * blk_size, blk_size * 6);
if (ret < 0) {
perror("fallcate");
close(fd);
return EXIT_FAILURE;
}
}
fsync(fd);
close(fd);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment