Test out compatibility modes attempting to retrieve an
[xfstests-dev.git] / src / permname.c
1 /*
2  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
3  * 
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  * 
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * 
12  * Further, this software is distributed without any warranty that it is
13  * free of the rightful claim of any third person regarding infringement
14  * or the like.  Any license provided herein, whether implied or
15  * otherwise, applies only to this software file.  Patent licenses, if
16  * any, provided herein do not apply to combinations of this program with
17  * other software, or any other product whatsoever.
18  * 
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write the Free Software Foundation, Inc., 59
21  * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22  * 
23  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24  * Mountain View, CA  94043, or:
25  * 
26  * http://www.sgi.com 
27  * 
28  * For further information regarding this notice, see: 
29  * 
30  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31  */
32  
33 #include "global.h"
34
35 char    *alpha;
36 int     asize = 1;
37 int     asplit;
38 char    *buf;
39 int     dflag;
40 int     len = 1;
41 int     nproc = 1;
42 int     nflag;
43 int     vflag;
44 int     pid;
45
46 void    mkf(int idx, int p);
47
48 int
49 main(int argc, char **argv)
50 {
51         int             a;
52         int             stat;
53         long long       tot;
54         int             usage=0;
55         char *          argv0=argv[0];
56
57         argc--;
58         argv++;
59         pid=getpid();
60         
61         if (!argc) usage++;
62         while (argc) {
63                 if (strcmp(*argv, "-c") == 0) {
64                         argc--;
65                         argv++;
66                         asize = atoi(*argv);
67                         if (asize > 64 || asize < 1) {
68                                 fprintf(stderr, "bad alpha size %s\n", *argv);
69                                 return 1;
70                         }
71                 } else if (strcmp(*argv, "-d") == 0) {
72                         dflag = 1;
73                 } else if (strcmp(*argv, "-l") == 0) {
74                         argc--;
75                         argv++;
76                         len = atoi(*argv);
77                         if (len < 1) {
78                                 fprintf(stderr, "bad name length %s\n", *argv);
79                                 return 1;
80                         }
81                 } else if (strcmp(*argv, "-p") == 0) {
82                         argc--;
83                         argv++;
84                         nproc = atoi(*argv);
85                         if (nproc < 1) {
86                                 fprintf(stderr, "bad process count %s\n",
87                                         *argv);
88                                 return 1;
89                         }
90                 } else if (strcmp(*argv, "-n") == 0) {
91                         nflag = 1; 
92                 } else if (strcmp(*argv, "-v") == 0) {
93                         vflag = 1;
94                 } else if (strcmp(*argv, "-h") == 0) {
95                         usage++;
96                 } else {
97                         fprintf(stderr,"unknown switch \"%s\"\n", *argv);
98                         usage++;
99                 }
100                 argc--;
101                 argv++;
102         }
103         
104         if (usage) {
105                 fprintf(stderr,"permname: usage %s [-c alpha size] [-l name length] "
106                                "[-p proc count] [-n] [-v] [-d] [-h]\n", argv0);
107                 fprintf(stderr,"          -n : don't actually perform action\n");
108                 fprintf(stderr,"          -v : be verbose\n");
109                 fprintf(stderr,"          -d : create directories, not files\n");
110                 exit(1);
111         }
112         
113         if (asize % nproc) {
114                 fprintf(stderr,
115                         "alphabet size must be multiple of process count\n");
116                 return 1;
117         }
118         asplit = asize / nproc;
119         alpha = malloc(asize + 1);
120         buf = malloc(len + 1);
121         for (a = 0, tot = 1; a < len; a++)
122                 tot *= asize;
123         if (vflag) fprintf(stderr,"[%d] ",pid);
124         fprintf(stderr,
125                 "alpha size = %d, name length = %d, total files = %lld, nproc=%d\n",
126                 asize, len, tot, nproc);
127         fflush(stderr);
128         for (a = 0; a < asize; a++) {
129                 if (a < 26)
130                         alpha[a] = 'a' + a;
131                 else if (a < 52)
132                         alpha[a] = 'A' + a - 26;
133                 else if (a < 62)
134                         alpha[a] = '0' + a - 52;
135                 else if (a == 62)
136                         alpha[62] = '_';
137                 else if (a == 63)
138                         alpha[63] = '@';
139         }
140         for (a = 0; a < nproc; a++) {
141                 int r=fork();
142                 if (r<0) {
143                         perror("fork");
144                         exit(1);
145                 }
146                 if (!r) {
147                         pid=getpid();
148                         mkf(0, a);
149                         return 0;
150                 }
151         }
152         while (1) {
153                 int r=wait(&stat);
154                 if (r<0) {
155                         if (errno==ECHILD) break;
156                         perror("wait");
157                         exit(1);
158                 }
159                 if (!r) break;
160         }
161         
162         return 0;
163 }
164
165 void
166 mkf(int idx, int p)
167 {
168         int     i;
169         int     last;
170
171         last = (idx == len - 1);
172         if (last) {
173                 buf[len] = '\0';
174                 for (i = p * asplit; i < (p + 1) * asplit; i++) {
175                         buf[idx] = alpha[i];
176                         
177                         if (dflag) {
178                                 if (vflag) printf("[%d] mkdir %s\n", pid, buf);
179                                 if (!nflag) {
180                                     if (mkdir(buf, 0777)<0) {
181                                         perror("mkdir");
182                                         exit(1);
183                                     }
184                                 }
185                         } else {
186                                 if (vflag) printf("[%d] touch %s\n", pid, buf);
187                                 if (!nflag) {
188                                     int f=creat(buf, 0666);
189                                     if (f<0) {
190                                         perror("creat");
191                                         exit(1);
192                                     }
193                                     if (close(f)) {
194                                         perror("close");
195                                         exit(1);
196                                     }
197                                 }
198                         }
199                 }
200         } else {
201                 for (i = 0; i < asize; i++) {
202                         buf[idx] = alpha[i];
203                         mkf(idx + 1, p);
204                 }
205         }
206 }