summaryrefslogtreecommitdiff
path: root/src/fill2.c
blob: 4cc1c3d79a98b9286ba1dd1d8750349e3d88548d (plain)
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2000-2003 Silicon Graphics, Inc.
 * All Rights Reserved.
 */


/*
 * fill2:
 *   Derived from fill.c, adds command line options, block boundary marking
 *   and allows me to tweak to suit fill2fs without breaking other qa tests.
 *   This version marks block boundaries (512, 1k, 4k and 16k) within the file
 *   using characters guaranteed not to appear anywhere else, this may allow
 *   simple checks in future which can inspect these offsets and ensure one of
 *   of the four characters is present. Also fixes off-by-one error in fill.c
 *   and is more careful about checking when write operations fail - this is 
 *   needed by fill2fs to ensure that the number of bytes written is accurately
 *   determined.
 */

#include "global.h"

#define constpp char * const *

#define N(x) (sizeof(x)/sizeof(x[0]))

/* function prototypes */
static void illegal(char *, char *);
static void reqval(char, char * [], int);
static void respec(char, char * [], int);
static void unknown(char, char *);
static void usage(void);
char *progname;

char *dopts[] =		{ "nbytes", "linelength", "seed", "file", NULL };
enum			{ D_NBYTES, D_LINELENGTH, D_SEED, D_FILE, D_ISSET, D_NUM };
int dflags[D_NUM] =	{ 0 };
char *bopts[] =		{ "512", "1k", "4k", "16k" };
enum			{ B_512, B_1K, B_4K, B_16K, B_ISSET, B_NUM };
int bflags[B_NUM] =	{ 0 };


/*
 * block boundaries
 *
 */

/* block boundaries which should be flagged in the output file */
/* flag is the character that should be used to indicate each type */
/* of block boundary */


struct s_block_type {
    int mark;
    int size;
    char flag;
};

static struct s_block_type btypes[] = {
    { 0, 512, '!' },	/* 512 */
    { 0, 1024, '"' },	/* 1k */
    { 0, 4096, '#' },	/* 4k */
    { 0, 16384, '$' },	/* 16k */
};

/*
 * main
 *
 */

int
main(int argc, char **argv)
{
    int			status = 0;	/* return status */
    int			c;		/* current option character */
    char		*p;		/* for getsubopt calls */
    long		nbytes;		/* total number of bytes to write */
    int			dlen;		/* length of normal output data line */
    const char		*dseed = NULL;	/* input string for seeding rand */
    unsigned int	seed;		/* seed for output data */    
    char		*dfile = NULL;	/* where to write output */

    FILE		*f;		/* output file */
    char		*dbuf;		/* output line buffer */
    char		bbuf[50];	/* block boundary string */
    char		*active = NULL;	/* active buffer to copy out of */
    size_t		hlen;		/* header length (bytes+key) in output */
					/* lines */
    char		*hptr;		/* pointer to end of header */
    char		*ptr;		/* current position to copy from */
    int			blktype = 0;	/* current block boundary type */
    int			boundary;	/* set if current output char lies on */
					/* block boundary */
    long		i;
    int			j;
    int			l = 0;


    /* defaults */

    progname = basename(argv[0]);
    for (p = progname; *p; p++) {
	    if (*p == '/') {
		    progname = p + 1;
	    }
    }
    nbytes = 1024 * 1024;
    dlen = 73;	/* includes the trailing newline */

    while ((c = getopt(argc, argv, "d:b:")) != EOF) {
	switch (c) {
	case 'd':
	    if (dflags[D_ISSET])
		respec('d', NULL, 0);
	    dflags[D_ISSET] = 1;

	    p = optarg;
	    while (*p != '\0') {
		char *value;
		switch (getsubopt(&p, (constpp)dopts, &value)) {
		case D_NBYTES:
		    if (! value) reqval('d', dopts, D_NBYTES);
		    if (dflags[D_NBYTES]) respec('d', dopts, D_NBYTES);
		    dflags[D_NBYTES] = 1;
		    nbytes = strtol(value, &ptr, 10);
		    if (ptr == value) illegal(value, "d nbytes");
		    break;

		case D_LINELENGTH:
		    if (! value) reqval('d', dopts, D_LINELENGTH);
		    if (dflags[D_LINELENGTH]) respec('d', dopts, D_LINELENGTH);
		    dflags[D_LINELENGTH] = 1;
		    dlen = (int) strtol(value, &ptr, 10) + 1;
		    if (ptr == value) illegal(value, "d linelength");
		    break;

		case D_SEED:
		    if (! value) reqval('d', dopts, D_SEED);
		    if (dflags[D_SEED]) respec('D', dopts, D_SEED);
		    dflags[D_SEED] = 1;
		    dseed = value;
		    break;

		case D_FILE:
		    if (! value) reqval('d', dopts, D_FILE);
		    if (dflags[D_FILE]) respec('d', dopts, D_FILE);
		    dflags[D_FILE] = 1;
		    dfile = value;
		    break;
		    
		default:
		    unknown('d', value);
		}
	    }
	    break;

	case 'b':
	    if (bflags[B_ISSET])
		respec('b', NULL, 0);
	    bflags[B_ISSET] = 1;

	    p = optarg;
	    while (*p != '\0') {
		char *value;
		switch (getsubopt(&p, (constpp)bopts, &value)) {
		case B_512:
		    if (value) illegal(value, "b 512");
		    if (bflags[B_512]) respec('b', bopts, B_512);
		    bflags[B_512] = 1;
		    btypes[0].mark = 1;
		    break;

		case B_1K:
		    if (value) illegal(value, "b 1k");
		    if (bflags[B_1K]) respec('b', bopts, B_1K);
		    bflags[B_1K] = 1;
		    btypes[1].mark = 1;
		    break;

		case B_4K:
		    if (value) illegal(value, "b 4k");
		    if (bflags[B_4K]) respec('b', bopts, B_4K);
		    bflags[B_4K] = 1;
		    btypes[2].mark = 1;
		    break;

		case B_16K:
		    if (value) illegal(value, "b 16k");
		    if (bflags[B_16K]) respec('b', bopts, B_16K);
		    bflags[B_16K] = 1;
		    btypes[3].mark = 1;
		    break;

		default:
		    unknown('b', value);
		    break;
		}
	    }
	    break;

	case '?':
	    usage();
	}
    }

    if (dflags[D_FILE] && optind != argc)
	usage();

    if (! dflags[D_FILE] && argc - optind != 1)
	usage();

    if (! dflags[D_FILE])
	dfile = argv[optind];

    if ((f = fopen(dfile, "w")) == NULL) {
	fprintf(stderr, "fill2: cannot create \"%s\": %s\n",
		dfile, strerror(errno));
	status = 1;
	goto cleanup;
    }

    if (! dflags[D_SEED]) 
	dseed = dfile;

    /* seed is an ascii string */
    seed = 0;
    i = 0;
    while (dseed[i]) {
	seed <<= 8;
	seed |= dseed[i];
	i++;
    }
    srand(seed);


    /* normal data line format: XXXXXXXXXXXX CCCC...CCC CCCCCCCCCCCC...CCC */
    /* block boundary format: CXXXXXXX */

    dbuf = (char *) malloc(dlen + 1);
    hlen = 12+1+strlen(dseed)+1;
    assert(hlen < dlen);
    hptr = dbuf + hlen;

    for (i = 0; i < nbytes; i++) {

	
	/* is this a block boundary? check largest to smallest */
	boundary = 0;
	for (j = N(btypes) - 1; j >= 0; j--) {
	    if (btypes[j].mark) {
		/* first time through or just before a block boundary? */
		if (i == 0 || i % btypes[j].size == btypes[j].size - 1) {
		    boundary = 1;
		    blktype = j;
		    break;
		}
	    }
	}


	/* are there block boundaries to check? */
	/* is this the first time through? */
	/* block boundaries take priority over other output */
	if (bflags[B_ISSET] && (i == 0 || boundary)) {
	    sprintf(bbuf, "%s%c%07ld\n",
		    i ? "\n" : "",
		    btypes[blktype].flag,
		    /* remember i is one less than block boundary */
		    i ? (i+1) / btypes[blktype].size : 0);
	    active = bbuf;
	    ptr = bbuf;
	}
	/* are we at the start of a new line? */
	else if (i == 0
		 || (active == bbuf && *ptr == '\0')
		 || (active == dbuf && l == dlen))
	{
	    sprintf(dbuf, "%012ld %s ", i, dseed);
	    assert(*hptr == '\0');
	    for (ptr = hptr; ptr != dbuf + dlen - 1; ptr++) {
		/* characters upto 126 '~' are used */
		/* do not use !"#$ - leave free for use as block markers */
		*ptr = 37 + (rand() % (127 - 37)); 
	    }
	    *ptr++ = '\n';
	    *ptr = '\0';
	    assert(ptr == dbuf + dlen);
	    active = dbuf;
	    ptr = dbuf;
	    l = 0;
	}
	else {
	    /* continue copying from the active buffer */
	}

	/* output one new character from current buffer */
	if (fputc(*ptr++, f) == EOF) {
	    fprintf(stderr, "fill2: could not write character to \"%s\": %s\n",
		    dfile, strerror(errno));
	    status = 1;
	    goto cleanup;
	}
	if (active == dbuf) l++;

    }

 cleanup:

    /* close file and flush buffers - check if this fails */
    if (fclose(f) != 0) {
	fprintf(stderr, "fill2: fclose() on \"%s\" failed: %s\n",
		dfile, strerror(errno));
	status = 1;
    }
    return status;
}



/*
 * suboption checking functions
 *
 */

static void
illegal(char *value, char *opt)
{
    fprintf(stderr, "%s: Error: Illegal value \"%s\" for -%s option\n",
	    progname, value, opt);
    usage();
}
static void
reqval(char opt, char *tab[], int idx)
{
    fprintf(stderr, "%s: Error: -%c %s option requires a value\n",
	    progname, opt, tab[idx]);
    usage();
}
static void
respec(char opt, char *tab[], int idx)
{
    fprintf(stderr, "%s: Error: ", progname);
    fprintf(stderr, "-%c ", opt);
    if (tab) fprintf(stderr, "%s ", tab[idx]);
    fprintf(stderr, "option respecified\n");
    usage();
}
static void
unknown(char opt, char *s)
{
    fprintf(stderr, "%s: Error: Unknown option -%c %s\n", progname, opt, s);
    usage();
}
static void
usage(void)
{
    fprintf(stderr, "Usage: %s [options] filename\n"
"Options:\n"
"  -d [nbytes=num,linelength=num,   output data properties\n"
"      seed=string,file=name]\n"
"  -b [512,1k,4k,16k]               where to mark block boundaries\n", progname);
    exit(2);
}