-
Notifications
You must be signed in to change notification settings - Fork 5
/
sparse-file.c
95 lines (76 loc) · 1.74 KB
/
sparse-file.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define SEQUENCE 10000
#define BLOCKSIZE 4096
#define HOLESIZE 4096
#define TOTALSIZE (4096*1024*1024UL)
char *output = "output";
void die(char *s)
{
perror(s);
exit(1);
}
void usage() {
printf("Usage :\n");
printf("\t-h : show help message\n");
printf("\t-s : block size, default %d\n", BLOCKSIZE);
printf("\t-l : hole size, default %d\n", HOLESIZE);
printf("\t-t : total size, default %ld\n", TOTALSIZE);
printf("\t-o : output file name, default %s\n", output);
exit(0);
}
int main(int argc, char *argv[])
{
char *buf = NULL;
long block_size = BLOCKSIZE;
long hole_size = HOLESIZE;
long total_size = TOTALSIZE;
int output_fd = 0;
int opt = 0;
while ((opt = getopt(argc, argv, "hs:l:t:o:")) != -1) {
switch (opt) {
case 's':
block_size = atoll(optarg);
break;
case 'l':
hole_size = atoll(optarg);
break;
case 't':
total_size = atoll(optarg);
break;
case 'o':
output = optarg;
break;
case 'h':
default :
usage();
}
}
if ((output_fd = open(output, O_CREAT | O_RDWR, 0644)) == -1)
die("open");
printf("open %s, prepare to write block %ld and hole %ld ...\n", output, block_size, hole_size);
buf = calloc(1, block_size);
if (buf == NULL)
die("malloc");
long blocks = 0;
long wrote = 0;
for (wrote = 0; wrote < total_size; ) {
size_t ret = 0;
ret = pwrite(output_fd, buf, block_size, wrote);
if (ret < 0)
die("write");
wrote = wrote + block_size + hole_size;
blocks++;
}
printf("write %s success, %ld blocks\n", output, blocks);
fsync(output_fd);
close(output_fd);
return 0;
}