• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************/
2 /*                                                                            */
3 /* Copyright (c) 2009 FUJITSU LIMITED                                         */
4 /*                                                                            */
5 /* This program is free software;  you can redistribute it and/or modify      */
6 /* it under the terms of the GNU General Public License as published by       */
7 /* the Free Software Foundation; either version 2 of the License, or          */
8 /* (at your option) any later version.                                        */
9 /*                                                                            */
10 /* This program is distributed in the hope that it will be useful,            */
11 /* but WITHOUT ANY WARRANTY;  without even the implied warranty of            */
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See                  */
13 /* the GNU General Public License for more details.                           */
14 /*                                                                            */
15 /* You should have received a copy of the GNU General Public License          */
16 /* along with this program;  if not, write to the Free Software               */
17 /* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA    */
18 /*                                                                            */
19 /* Author: Li Zefan <lizf@cn.fujitsu.com>                                     */
20 /*                                                                            */
21 /******************************************************************************/
22 
23 #define _ATFILE_SOURCE
24 #include <unistd.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include "config.h"
32 
33 /* valid characters for a directory name */
34 char chars[] = "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
35 
36 /* to store the generated directory name */
37 char name[10];
38 int names;
39 int parent_fd;
40 
41 /* NCHARS = 10 + 26 + 26 = 62 */
42 #define NCHARS		62
43 #define MAX_LEN1	62
44 #define MAX_LEN2	(62 * 62)
45 #define MAX_LEN3	(62 * 62 * 62)
46 #define MAX_NAMES	(MAX_LEN1 + MAX_LEN2 + MAX_LEN3)
47 
create_dir(void)48 void create_dir(void)
49 {
50 #ifdef HAVE_MKDIRAT
51 	if (mkdirat(parent_fd, name, S_IRWXU)) {
52 		perror("mkdir");
53 		exit(1);
54 	}
55 #else
56 	fprintf(stderr, "System lacks mkdirat() call.\n");
57 	exit(1);
58 #endif
59 }
60 
61 /*
62  * create_1 - create length-1 directory names
63  * @n: how name names to be created
64  */
create_1(int n)65 void create_1(int n)
66 {
67 	int i;
68 
69 	name[1] = '\0';
70 	for (i = 0; i < NCHARS; i++) {
71 		name[0] = chars[i];
72 		create_dir();
73 		if (--n == 0)
74 			return;
75 	}
76 }
77 
78 /*
79  * create_2 - generate length-2 directory names
80  * @n: how many names to be created
81  */
create_2(int n)82 void create_2(int n)
83 {
84 	int i, j;
85 
86 	name[2] = '\0';
87 	for (i = 0; i < NCHARS; i++) {
88 		name[0] = chars[i];
89 		for (j = 0; j < NCHARS; j++) {
90 			name[1] = chars[j];
91 			create_dir();
92 			if (--n == 0)
93 				return;
94 		}
95 	}
96 }
97 
98 /*
99  * create_3 - generate length-3 directory names
100  * @n: how many names to be created
101  */
create_3(int n)102 void create_3(int n)
103 {
104 	int i, j, k;
105 
106 	name[3] = '\0';
107 	for (i = 0; i < NCHARS; i++) {
108 		name[0] = chars[i];
109 		for (j = 0; j < NCHARS; j++) {
110 			name[1] = chars[j];
111 			for (k = 0; k < NCHARS; k++) {
112 				name[2] = chars[k];
113 				create_dir();
114 				if (--n == 0)
115 					return;
116 			}
117 		}
118 	}
119 }
120 
usage()121 void usage()
122 {
123 	fprintf(stderr, "Usage: create_short_dirs nr_dirs parent_dir\n");
124 }
125 
126 /*
127  * Create short-name directoriess
128  * @argv[1]: director number
129  * @argv[2]: the parent directory
130  */
main(int argc,char * argv[])131 int main(int argc, char *argv[])
132 {
133 	if (argc != 3) {
134 		usage();
135 		return 1;
136 	}
137 
138 	names = atoi(argv[1]);
139 	if (names > MAX_NAMES || names <= 0) {
140 		usage();
141 		return 1;
142 	}
143 
144 	parent_fd = open(argv[2], O_RDONLY);
145 	if (parent_fd == -1) {
146 		perror("open parent dir");
147 		return 1;
148 	}
149 
150 	create_1(names);
151 	if (names <= MAX_LEN1)
152 		return 0;
153 
154 	names -= MAX_LEN1;
155 	create_2(names);
156 	if (names <= MAX_LEN2)
157 		return 0;
158 
159 	names -= MAX_LEN2;
160 	create_3(names);
161 
162 	return 0;
163 }
164