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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
static char buffer[65536];
static int
parse_int(const char *text, size_t *value)
{
char *ptr;
size_t val;
val = strtoul(text, &ptr, 0);
if (*ptr != 0) {
return 0;
}
*value = val;
return 1;
}
static int
fill_area(int fd, off_t offset, size_t size)
{
size_t len;
ssize_t res;
while (size > 0) {
len = sizeof(buffer);
if (len > size) {
len = size;
}
res = pwrite(fd, buffer, len, offset);
if (res < 0) {
fprintf(stderr,
"pwrite(%d, %p, %lu, %lu) failed: %d\n",
fd, buffer, size, offset, errno);
return 0;
}
if (res != len) {
fprintf(stderr,
"pwrite(%d, %p, %lu, %lu) didn't wrote all "
"data: %lu/%lu\n",
fd, buffer, size, offset, res, len);
return 0;
}
offset += len;
size -= len;
}
return 1;
}
static void
syntax(void)
{
fprintf(stderr, "Syntax: seek create <path> <offset> <size> [...]\n");
fprintf(stderr, " seek scan <path> data|hole <offset>\n");
}
static int
seek_create(const char *path, int argc, char *argv[])
{
size_t off, size;
int fd;
int ret = 1;
fd = open(path, O_CREAT | O_TRUNC | O_RDWR, 0644);
if (fd < 0) {
fprintf(stderr, "Failed to create the file\n");
goto out;
}
while (argc > 0) {
if (!parse_int(argv[0], &off) ||
!parse_int(argv[1], &size)) {
syntax();
goto out_close;
}
if (!fill_area(fd, off, size)) {
goto out_close;
}
argv += 2;
argc -= 2;
}
ret = 0;
out_close:
close(fd);
out:
return ret;
}
static int
seek_scan(const char *path, const char *type, const char *pos)
{
size_t off, res;
int fd, whence;
int ret = 1;
if (strcmp(type, "data") == 0) {
whence = SEEK_DATA;
} else if (strcmp(type, "hole") == 0) {
whence = SEEK_HOLE;
} else {
syntax();
goto out;
}
if (!parse_int(pos, &off)) {
syntax();
goto out;
}
fd = open(path, O_RDWR);
if (fd < 0) {
fprintf(stderr, "Failed to open the file\n");
goto out;
}
res = lseek(fd, off, whence);
if (res == (off_t)-1) {
if (errno != ENXIO) {
fprintf(stderr, "seek(%d, %lu, %d) failed: %d\n", fd,
off, whence, errno);
goto out_close;
}
fprintf(stdout, "ENXIO\n");
} else {
fprintf(stdout, "%lu\n", res);
}
ret = 0;
out_close:
close(fd);
out:
return ret;
}
int
main(int argc, char *argv[])
{
int ret = 1;
memset(buffer, 0x55, sizeof(buffer));
if (argc < 3) {
syntax();
goto out;
}
if (strcmp(argv[1], "create") == 0) {
if (((argc - 3) & 1) != 0) {
syntax();
goto out;
}
ret = seek_create(argv[2], argc - 3, argv + 3);
} else if (strcmp(argv[1], "scan") == 0) {
if (argc != 5) {
syntax();
goto out;
}
ret = seek_scan(argv[2], argv[3], argv[4]);
} else {
syntax();
goto out;
}
ret = 0;
out:
return ret;
}
|