common: kill _supported_os
[xfstests-dev.git] / src / t_create_short_dirs.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2009 FUJITSU LIMITED
4  * Author: Li Zefan <lizf@cn.fujitsu.com>
5  */
6
7 #define _POSIX_C_SOURCE 200809L
8 #include <unistd.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <sys/stat.h>
14 #include <sys/types.h>
15 #include "config.h"
16
17 /* NCHARS = 10 + 26 + 26 = 62 */
18 #define NCHARS      62
19 #define MAX_LEN1    62
20 #define MAX_LEN2    (62 * 62)
21 #define MAX_LEN3    (62 * 62 * 62)
22 #define MAX_NAMES   (MAX_LEN1 + MAX_LEN2 + MAX_LEN3)
23
24 /* valid characters for a directory name */
25 char chars[] = "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
26
27 /* to store the generated directory name */
28 char name[10];
29 int names;
30 int parent_fd;
31
32 void create_dir(void)
33 {
34        if (mkdirat(parent_fd, name, S_IRWXU)) {
35                perror("mkdir");
36                exit(1);
37        }
38 }
39
40 /*
41  * create_1 - create length-1 directory names
42  * @n: how name names to be created
43  */
44 void create_1(int n)
45 {
46        int i;
47
48        name[1] = '\0';
49        for (i = 0; i < NCHARS; i++) {
50                name[0] = chars[i];
51                create_dir();
52                if (--n == 0)
53                        return;
54        }
55 }
56
57 /*
58  * create_2 - generate length-2 directory names
59  * @n: how many names to be created
60  */
61 void create_2(int n)
62 {
63        int i, j;
64
65        name[2] = '\0';
66        for (i = 0; i < NCHARS; i++) {
67                name[0] = chars[i];
68                for (j = 0; j < NCHARS; j++) {
69                        name[1] = chars[j];
70                        create_dir();
71                        if (--n == 0)
72                                return;
73                }
74        }
75 }
76
77 /*
78  * create_3 - generate length-3 directory names
79  * @n: how many names to be created
80  */
81 void create_3(int n)
82 {
83        int i, j, k;
84
85        name[3] = '\0';
86        for (i = 0; i < NCHARS; i++) {
87                name[0] = chars[i];
88                for (j = 0; j < NCHARS; j++) {
89                        name[1] = chars[j];
90                        for (k = 0; k < NCHARS; k++) {
91                                name[2] = chars[k];
92                                create_dir();
93                                if (--n == 0)
94                                        return;
95                        }
96                }
97        }
98 }
99
100 void usage()
101 {
102        fprintf(stderr, "Usage: create_short_dirs nr_dirs parent_dir\n");
103 }
104
105 /*
106  * Create short-name directoriess
107  * @argv[1]: director number
108  * @argv[2]: the parent directory
109  */
110 int main(int argc, char *argv[])
111 {
112        if (argc != 3) {
113                usage();
114                return 1;
115        }
116
117        names = atoi(argv[1]);
118        if (names > MAX_NAMES || names <= 0) {
119                usage();
120                return 1;
121        }
122
123        parent_fd = open(argv[2], O_RDONLY);
124        if (parent_fd == -1) {
125                perror("open parent dir failed");
126                return 1;
127        }
128
129        create_1(names);
130        if (names <= MAX_LEN1)
131                return 0;
132
133        names -= MAX_LEN1;
134        create_2(names);
135        if (names <= MAX_LEN2)
136                return 0;
137
138        names -= MAX_LEN2;
139        create_3(names);
140
141        return 0;
142 }