cleanalot_async.c revision 1.5 1 1.5 perseant /* $NetBSD: cleanalot_async.c,v 1.5 2006/05/05 19:42:07 perseant 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 * 3. All advertising materials mentioning features or use of this software
19 1.2 perseant * must display the following acknowledgement:
20 1.2 perseant * This product includes software developed by the NetBSD
21 1.2 perseant * Foundation, Inc. and its contributors.
22 1.2 perseant * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.2 perseant * contributors may be used to endorse or promote products derived
24 1.2 perseant * from this software without specific prior written permission.
25 1.2 perseant *
26 1.2 perseant * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.2 perseant * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.2 perseant * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.2 perseant * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.2 perseant * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.2 perseant * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.2 perseant * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.2 perseant * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.2 perseant * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.2 perseant * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.2 perseant * POSSIBILITY OF SUCH DAMAGE.
37 1.2 perseant */
38 1.2 perseant
39 1.1 perseant #include <stdio.h>
40 1.1 perseant #include <stdlib.h>
41 1.1 perseant #include <signal.h>
42 1.1 perseant #include <unistd.h>
43 1.1 perseant
44 1.1 perseant void dirname(int n, char *s)
45 1.1 perseant {
46 1.1 perseant if (n == 0) {
47 1.1 perseant strcat(s, "/0");
48 1.1 perseant mkdir(s);
49 1.1 perseant }
50 1.5 perseant while (n) {
51 1.5 perseant sprintf(s + strlen(s), "/%x", n & 0xf);
52 1.5 perseant n >>= 4;
53 1.1 perseant mkdir(s);
54 1.5 perseant }
55 1.1 perseant }
56 1.1 perseant
57 1.1 perseant /*
58 1.1 perseant * Write a file, creating the directory if necessary.
59 1.1 perseant */
60 1.1 perseant int write_file(int gen, int n, int plex, char *buf, int size)
61 1.1 perseant {
62 1.5 perseant FILE *fp;
63 1.5 perseant char s[1024], *t;
64 1.5 perseant int r;
65 1.1 perseant
66 1.5 perseant sprintf(s, "dir_%x_%x", plex, gen);
67 1.5 perseant dirname(n, s);
68 1.1 perseant strcat(s, ".file");
69 1.1 perseant
70 1.5 perseant // printf("write file %d.%d.%x: %s\n", gen, plex, n, s);
71 1.1 perseant
72 1.5 perseant fp = fopen(s, "wb");
73 1.5 perseant if (fp == NULL)
74 1.5 perseant return 0;
75 1.4 perseant if (size)
76 1.5 perseant r = fwrite(buf, size, 1, fp);
77 1.4 perseant else
78 1.4 perseant r = 1;
79 1.5 perseant fclose(fp);
80 1.1 perseant
81 1.5 perseant return r;
82 1.1 perseant }
83 1.1 perseant
84 1.1 perseant int write_dirs(int gen, int size, int plex)
85 1.1 perseant {
86 1.5 perseant int i, j, tot;
87 1.5 perseant char s[1024];
88 1.5 perseant char *buf;
89 1.5 perseant
90 1.5 perseant /* Create all base dirs */
91 1.5 perseant for (i = 0; i < plex; i++) {
92 1.5 perseant sprintf(s, "dir_%x_%x", i, gen);
93 1.5 perseant if (mkdir(s, 0700) != 0)
94 1.5 perseant return 0;
95 1.5 perseant }
96 1.1 perseant
97 1.5 perseant /* Write files */
98 1.4 perseant if (size) {
99 1.5 perseant buf = malloc(size);
100 1.5 perseant if (buf == NULL)
101 1.5 perseant return 0;
102 1.5 perseant }
103 1.5 perseant tot = 0;
104 1.5 perseant for (i = 0; ; i++) {
105 1.5 perseant for (j = 0; j < plex; j++) {
106 1.5 perseant if (write_file(gen, i, j, buf, size) == 0) {
107 1.4 perseant if (size)
108 1.5 perseant free(buf);
109 1.5 perseant return tot;
110 1.5 perseant }
111 1.5 perseant ++tot;
112 1.5 perseant }
113 1.5 perseant }
114 1.5 perseant /* NOTREACHED */
115 1.1 perseant }
116 1.1 perseant
117 1.1 perseant int main(int argc, char **argv)
118 1.1 perseant {
119 1.5 perseant int c, i, j;
120 1.5 perseant int bs = 0;
121 1.5 perseant int count = 0;
122 1.5 perseant int plex = 2;
123 1.5 perseant char cmd[1024];
124 1.1 perseant
125 1.4 perseant bs = -1;
126 1.5 perseant while((c = getopt(argc, argv, "b:n:p:")) != -1) {
127 1.5 perseant switch(c) {
128 1.5 perseant case 'b':
129 1.5 perseant bs = atoi(optarg);
130 1.5 perseant break;
131 1.5 perseant case 'n':
132 1.5 perseant count = atoi(optarg);
133 1.5 perseant break;
134 1.5 perseant case 'p':
135 1.5 perseant plex = atoi(optarg);
136 1.5 perseant break;
137 1.1 perseant default:
138 1.5 perseant exit(1);
139 1.5 perseant }
140 1.5 perseant }
141 1.5 perseant
142 1.5 perseant /*
143 1.5 perseant * Process old-style, non-flag parameters
144 1.5 perseant */
145 1.5 perseant if (count == 0) {
146 1.5 perseant if (argv[optind] != NULL)
147 1.5 perseant count = atoi(argv[optind]);
148 1.5 perseant }
149 1.4 perseant if (bs < 0 && getenv("BS") != NULL)
150 1.5 perseant bs = atoi(getenv("BS"));
151 1.4 perseant if (bs < 0)
152 1.5 perseant bs = 16384;
153 1.5 perseant if (plex == 0)
154 1.5 perseant plex = 2;
155 1.5 perseant
156 1.5 perseant for (i = 0; count == 0 || i < count; i++) {
157 1.5 perseant if (count)
158 1.5 perseant printf("::: begin iteration %d/%d\n", i, count);
159 1.5 perseant else
160 1.5 perseant printf("::: begin iteration %d\n", i);
161 1.5 perseant
162 1.5 perseant for (j = 0; ; j++) {
163 1.5 perseant if ((c = write_dirs(j, bs, plex)) == 0)
164 1.5 perseant break;
165 1.5 perseant printf("%d: %d files of size %d\n", j, c, bs);
166 1.5 perseant sprintf(cmd, "rm -rf dir_%x_%x", plex - 1, j);
167 1.5 perseant system(cmd);
168 1.1 perseant sync();
169 1.5 perseant }
170 1.1 perseant system("df -k .");
171 1.3 perseant printf("remove files\n");
172 1.3 perseant system("rm -rf dir_*");
173 1.3 perseant system("df -k .");
174 1.5 perseant }
175 1.1 perseant }
176