#define _GNU_SOURCE #include #include #include #include #include #include #include #include #define FILE_SIZE 1024000 #define HOLE_START 409600 #define HOLE_LEN 204800 #define HOLE_SHIFET 102400 int main(int argc, char *argv[]) { int fd; int ret; void* data_foo; int fd_bar; char bar_path[256]; if (argc != 2) { printf("usage: a.out [file Path]\n"); exit(1); } sprintf(bar_path,"%s_bar",argv[1]); printf("file path: %s \n",argv[1]); printf("file_bar path: %s \n",bar_path); fd = open(argv[1], O_CREAT | O_RDWR , 0755); if (fd < 0) { printf("open err! \n"); exit(1); } data_foo = malloc(FILE_SIZE); if (!data_foo) { printf("malloc err! \n"); exit(1); } int offset_foo = 0; int to_write_foo = FILE_SIZE ; const char *text_foo = "ddddddddddklmnopqrstuvwxyz123456"; while (offset_foo < FILE_SIZE){ if (to_write_foo < 32){ memcpy((char *)data_foo+ offset_foo, text_foo, to_write_foo); offset_foo += to_write_foo; } else { memcpy((char *)data_foo+ offset_foo,text_foo, 32); offset_foo += 32; } } ret = pwrite(fd, data_foo, FILE_SIZE, 0); if (ret != FILE_SIZE) { printf("write err! [%d] \n",ret); exit(1); } ret = fallocate( fd , FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE , HOLE_START , HOLE_LEN); if ( ret < 0){ printf("fallocate err! [%d] \n",ret); exit(1); } fd_bar = open(bar_path, O_CREAT | O_RDWR , 0755); if (fd_bar < 0) { printf("open fd_bar err! \n"); exit(1); } fsync(fd_bar); close(fd_bar); ret = fallocate( fd , FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE , HOLE_START-HOLE_SHIFET , HOLE_LEN); if ( ret < 0){ printf("fallocate err! [%d] \n",ret); exit(1); } fsync(fd); close(fd); exit(0); }