Home | History | Annotate | Line # | Download | only in ckckp
      1  1.7    martin /*	$NetBSD: cleanalot_async.c,v 1.7 2008/04/28 20:23:06 martin Exp $	*/
      2  1.2  perseant 
      3  1.2  perseant /*-
      4  1.2  perseant  * Copyright (c) 2006 The NetBSD Foundation, Inc.
      5  1.2  perseant  * All rights reserved.
      6  1.2  perseant  *
      7  1.2  perseant  * This code is derived from software contributed to The NetBSD Foundation
      8  1.2  perseant  * by Konrad E. Schroder <perseant (at) hhhh.org>.
      9  1.2  perseant  *
     10  1.2  perseant  * Redistribution and use in source and binary forms, with or without
     11  1.2  perseant  * modification, are permitted provided that the following conditions
     12  1.2  perseant  * are met:
     13  1.2  perseant  * 1. Redistributions of source code must retain the above copyright
     14  1.2  perseant  *    notice, this list of conditions and the following disclaimer.
     15  1.2  perseant  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.2  perseant  *    notice, this list of conditions and the following disclaimer in the
     17  1.2  perseant  *    documentation and/or other materials provided with the distribution.
     18  1.2  perseant  *
     19  1.2  perseant  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.2  perseant  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.2  perseant  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.2  perseant  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.2  perseant  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.2  perseant  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.2  perseant  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.2  perseant  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.2  perseant  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.2  perseant  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.2  perseant  * POSSIBILITY OF SUCH DAMAGE.
     30  1.2  perseant  */
     31  1.2  perseant 
     32  1.1  perseant #include <stdio.h>
     33  1.1  perseant #include <stdlib.h>
     34  1.1  perseant #include <signal.h>
     35  1.6  perseant #include <string.h>
     36  1.1  perseant #include <unistd.h>
     37  1.1  perseant 
     38  1.1  perseant void dirname(int n, char *s)
     39  1.1  perseant {
     40  1.1  perseant 	if (n == 0) {
     41  1.1  perseant 		strcat(s, "/0");
     42  1.1  perseant 		mkdir(s);
     43  1.1  perseant 	}
     44  1.5  perseant 	while (n) {
     45  1.5  perseant 		sprintf(s + strlen(s), "/%x", n & 0xf);
     46  1.5  perseant 		n >>= 4;
     47  1.1  perseant 		mkdir(s);
     48  1.5  perseant 	}
     49  1.1  perseant }
     50  1.1  perseant 
     51  1.1  perseant /*
     52  1.1  perseant  * Write a file, creating the directory if necessary.
     53  1.1  perseant  */
     54  1.1  perseant int write_file(int gen, int n, int plex, char *buf, int size)
     55  1.1  perseant {
     56  1.5  perseant 	FILE *fp;
     57  1.5  perseant 	char s[1024], *t;
     58  1.5  perseant 	int r;
     59  1.1  perseant 
     60  1.5  perseant 	sprintf(s, "dir_%x_%x", plex, gen);
     61  1.5  perseant 	dirname(n, s);
     62  1.1  perseant 	strcat(s, ".file");
     63  1.1  perseant 
     64  1.6  perseant 	/* printf("write file %d.%d.%x: %s\n", gen, plex, n, s); */
     65  1.1  perseant 
     66  1.5  perseant 	fp = fopen(s, "wb");
     67  1.5  perseant 	if (fp == NULL)
     68  1.5  perseant 		return 0;
     69  1.4  perseant 	if (size)
     70  1.5  perseant 	r = fwrite(buf, size, 1, fp);
     71  1.4  perseant 	else
     72  1.4  perseant 		r = 1;
     73  1.5  perseant 	fclose(fp);
     74  1.1  perseant 
     75  1.5  perseant 	return r;
     76  1.1  perseant }
     77  1.1  perseant 
     78  1.1  perseant int write_dirs(int gen, int size, int plex)
     79  1.1  perseant {
     80  1.5  perseant 	int i, j, tot;
     81  1.5  perseant 	char s[1024];
     82  1.5  perseant 	char *buf;
     83  1.5  perseant 
     84  1.5  perseant 	/* Create all base dirs */
     85  1.5  perseant 	for (i = 0; i < plex; i++) {
     86  1.5  perseant 		sprintf(s, "dir_%x_%x", i, gen);
     87  1.5  perseant 		if (mkdir(s, 0700) != 0)
     88  1.5  perseant 			return 0;
     89  1.5  perseant 	}
     90  1.1  perseant 
     91  1.5  perseant 	/* Write files */
     92  1.4  perseant 	if (size) {
     93  1.5  perseant 	buf = malloc(size);
     94  1.5  perseant 	if (buf == NULL)
     95  1.5  perseant 		return 0;
     96  1.5  perseant 	}
     97  1.5  perseant 	tot = 0;
     98  1.5  perseant 	for (i = 0; ; i++) {
     99  1.5  perseant 		for (j = 0; j < plex; j++) {
    100  1.5  perseant 			if (write_file(gen, i, j, buf, size) == 0) {
    101  1.4  perseant 				if (size)
    102  1.5  perseant 				free(buf);
    103  1.5  perseant 				return tot;
    104  1.5  perseant 			}
    105  1.5  perseant 			++tot;
    106  1.5  perseant 		}
    107  1.5  perseant 	}
    108  1.5  perseant 	/* NOTREACHED */
    109  1.1  perseant }
    110  1.1  perseant 
    111  1.1  perseant int main(int argc, char **argv)
    112  1.1  perseant {
    113  1.5  perseant 	int c, i, j;
    114  1.5  perseant 	int bs = 0;
    115  1.5  perseant 	int count = 0;
    116  1.5  perseant 	int plex = 2;
    117  1.5  perseant 	char cmd[1024];
    118  1.1  perseant 
    119  1.4  perseant 	bs = -1;
    120  1.5  perseant 	while((c = getopt(argc, argv, "b:n:p:")) != -1) {
    121  1.5  perseant 		switch(c) {
    122  1.5  perseant 		    case 'b':
    123  1.5  perseant 			bs = atoi(optarg);
    124  1.5  perseant 			break;
    125  1.5  perseant 		    case 'n':
    126  1.5  perseant 			count = atoi(optarg);
    127  1.5  perseant 			break;
    128  1.5  perseant 		    case 'p':
    129  1.5  perseant 			plex = atoi(optarg);
    130  1.5  perseant 			break;
    131  1.1  perseant 		    default:
    132  1.5  perseant 			exit(1);
    133  1.5  perseant 		}
    134  1.5  perseant 	}
    135  1.5  perseant 
    136  1.5  perseant 	/*
    137  1.5  perseant 	 * Process old-style, non-flag parameters
    138  1.5  perseant 	 */
    139  1.5  perseant 	if (count == 0) {
    140  1.5  perseant 		if (argv[optind] != NULL)
    141  1.5  perseant 			count = atoi(argv[optind]);
    142  1.5  perseant 	}
    143  1.4  perseant 	if (bs < 0 && getenv("BS") != NULL)
    144  1.5  perseant 		bs = atoi(getenv("BS"));
    145  1.4  perseant 	if (bs < 0)
    146  1.5  perseant 		bs = 16384;
    147  1.5  perseant 	if (plex == 0)
    148  1.5  perseant 		plex = 2;
    149  1.5  perseant 
    150  1.5  perseant 	for (i = 0; count == 0 || i < count; i++) {
    151  1.5  perseant 		if (count)
    152  1.5  perseant 			printf("::: begin iteration %d/%d\n", i, count);
    153  1.5  perseant 		else
    154  1.5  perseant 			printf("::: begin iteration %d\n", i);
    155  1.5  perseant 
    156  1.5  perseant 		for (j = 0; ; j++) {
    157  1.5  perseant 			if ((c = write_dirs(j, bs, plex)) == 0)
    158  1.5  perseant 				break;
    159  1.5  perseant 			printf("%d: %d files of size %d\n", j, c, bs);
    160  1.5  perseant 			sprintf(cmd, "rm -rf dir_%x_%x", plex - 1, j);
    161  1.5  perseant 			system(cmd);
    162  1.1  perseant 			sync();
    163  1.5  perseant 		}
    164  1.1  perseant 		system("df -k .");
    165  1.3  perseant 		printf("remove files\n");
    166  1.3  perseant 		system("rm -rf dir_*");
    167  1.3  perseant 		system("df -k .");
    168  1.5  perseant 	}
    169  1.1  perseant }
    170