]> git.ipfire.org Git - thirdparty/git.git/blame - t/helper/test-path-utils.c
test-path-utils: offer to run a protectNTFS/protectHFS benchmark
[thirdparty/git.git] / t / helper / test-path-utils.c
CommitLineData
ae299be0 1#include "cache.h"
31171d9e 2#include "string-list.h"
dc2d9ba3 3#include "utf8.h"
ae299be0 4
9e2326c7
MH
5/*
6 * A "string_list_each_func_t" function that normalizes an entry from
7 * GIT_CEILING_DIRECTORIES. If the path is unusable for some reason,
8 * die with an explanation.
9 */
10static int normalize_ceiling_entry(struct string_list_item *item, void *unused)
11{
62f17513 12 char *ceil = item->string;
9e2326c7 13
62f17513 14 if (!*ceil)
9e2326c7 15 die("Empty path is not supported");
9e2326c7
MH
16 if (!is_absolute_path(ceil))
17 die("Path \"%s\" is not absolute", ceil);
62f17513 18 if (normalize_path_copy(ceil, ceil) < 0)
9e2326c7 19 die("Path \"%s\" could not be normalized", ceil);
9e2326c7
MH
20 return 1;
21}
22
203439b2
JX
23static void normalize_argv_string(const char **var, const char *input)
24{
25 if (!strcmp(input, "<null>"))
26 *var = NULL;
27 else if (!strcmp(input, "<empty>"))
28 *var = "";
29 else
30 *var = input;
31
32 if (*var && (**var == '<' || **var == '('))
33 die("Bad value: %s\n", input);
34}
35
7d1aaa68
JS
36struct test_data {
37 const char *from; /* input: transform from this ... */
38 const char *to; /* output: ... to this. */
371471ce 39 const char *alternative; /* output: ... or this. */
7d1aaa68
JS
40};
41
29c2eda8
RS
42/*
43 * Compatibility wrappers for OpenBSD, whose basename(3) and dirname(3)
44 * have const parameters.
45 */
46static char *posix_basename(char *path)
47{
48 return basename(path);
49}
50
51static char *posix_dirname(char *path)
52{
53 return dirname(path);
54}
55
7d1aaa68
JS
56static int test_function(struct test_data *data, char *(*func)(char *input),
57 const char *funcname)
58{
59 int failed = 0, i;
60 char buffer[1024];
61 char *to;
62
63 for (i = 0; data[i].to; i++) {
64 if (!data[i].from)
65 to = func(NULL);
66 else {
7b11a18a 67 xsnprintf(buffer, sizeof(buffer), "%s", data[i].from);
7d1aaa68
JS
68 to = func(buffer);
69 }
371471ce
JS
70 if (!strcmp(to, data[i].to))
71 continue;
72 if (!data[i].alternative)
7d1aaa68
JS
73 error("FAIL: %s(%s) => '%s' != '%s'\n",
74 funcname, data[i].from, to, data[i].to);
371471ce
JS
75 else if (!strcmp(to, data[i].alternative))
76 continue;
77 else
78 error("FAIL: %s(%s) => '%s' != '%s', '%s'\n",
79 funcname, data[i].from, to, data[i].to,
80 data[i].alternative);
81 failed = 1;
7d1aaa68
JS
82 }
83 return failed;
84}
85
86static struct test_data basename_data[] = {
87 /* --- POSIX type paths --- */
88 { NULL, "." },
89 { "", "." },
90 { ".", "." },
91 { "..", ".." },
92 { "/", "/" },
371471ce
JS
93 { "//", "/", "//" },
94 { "///", "/", "//" },
95 { "////", "/", "//" },
7d1aaa68
JS
96 { "usr", "usr" },
97 { "/usr", "usr" },
98 { "/usr/", "usr" },
99 { "/usr//", "usr" },
100 { "/usr/lib", "lib" },
101 { "usr/lib", "lib" },
102 { "usr/lib///", "lib" },
103
104#if defined(__MINGW32__) || defined(_MSC_VER)
7d1aaa68
JS
105 /* --- win32 type paths --- */
106 { "\\usr", "usr" },
107 { "\\usr\\", "usr" },
108 { "\\usr\\\\", "usr" },
109 { "\\usr\\lib", "lib" },
110 { "usr\\lib", "lib" },
111 { "usr\\lib\\\\\\", "lib" },
112 { "C:/usr", "usr" },
113 { "C:/usr", "usr" },
114 { "C:/usr/", "usr" },
115 { "C:/usr//", "usr" },
116 { "C:/usr/lib", "lib" },
117 { "C:usr/lib", "lib" },
118 { "C:usr/lib///", "lib" },
119 { "C:", "." },
120 { "C:a", "a" },
121 { "C:/", "/" },
122 { "C:///", "/" },
371471ce
JS
123 { "\\", "\\", "/" },
124 { "\\\\", "\\", "/" },
125 { "\\\\\\", "\\", "/" },
7d1aaa68
JS
126#endif
127 { NULL, NULL }
128};
129
130static struct test_data dirname_data[] = {
131 /* --- POSIX type paths --- */
132 { NULL, "." },
133 { "", "." },
134 { ".", "." },
135 { "..", "." },
136 { "/", "/" },
371471ce
JS
137 { "//", "/", "//" },
138 { "///", "/", "//" },
139 { "////", "/", "//" },
7d1aaa68
JS
140 { "usr", "." },
141 { "/usr", "/" },
142 { "/usr/", "/" },
143 { "/usr//", "/" },
144 { "/usr/lib", "/usr" },
145 { "usr/lib", "usr" },
146 { "usr/lib///", "usr" },
147
148#if defined(__MINGW32__) || defined(_MSC_VER)
7d1aaa68
JS
149 /* --- win32 type paths --- */
150 { "\\", "\\" },
151 { "\\\\", "\\\\" },
152 { "\\usr", "\\" },
153 { "\\usr\\", "\\" },
154 { "\\usr\\\\", "\\" },
155 { "\\usr\\lib", "\\usr" },
156 { "usr\\lib", "usr" },
157 { "usr\\lib\\\\\\", "usr" },
158 { "C:a", "C:." },
159 { "C:/", "C:/" },
160 { "C:///", "C:/" },
161 { "C:/usr", "C:/" },
162 { "C:/usr/", "C:/" },
163 { "C:/usr//", "C:/" },
164 { "C:/usr/lib", "C:/usr" },
165 { "C:usr/lib", "C:usr" },
166 { "C:usr/lib///", "C:usr" },
167 { "\\\\\\", "\\" },
168 { "\\\\\\\\", "\\" },
371471ce 169 { "C:", "C:.", "." },
7d1aaa68
JS
170#endif
171 { NULL, NULL }
172};
173
dc2d9ba3
JS
174static int is_dotgitmodules(const char *path)
175{
176 return is_hfs_dotgitmodules(path) || is_ntfs_dotgitmodules(path);
177}
178
a62f9d1a
GS
179/*
180 * A very simple, reproducible pseudo-random generator. Copied from
181 * `test-genrandom.c`.
182 */
183static uint64_t my_random_value = 1234;
184
185static uint64_t my_random(void)
186{
187 my_random_value = my_random_value * 1103515245 + 12345;
188 return my_random_value;
189}
190
191/*
192 * A fast approximation of the square root, without requiring math.h.
193 *
194 * It uses Newton's method to approximate the solution of 0 = x^2 - value.
195 */
196static double my_sqrt(double value)
197{
198 const double epsilon = 1e-6;
199 double x = value;
200
201 if (value == 0)
202 return 0;
203
204 for (;;) {
205 double delta = (value / x - x) / 2;
206 if (delta < epsilon && delta > -epsilon)
207 return x + delta;
208 x += delta;
209 }
210}
211
212static int protect_ntfs_hfs_benchmark(int argc, const char **argv)
213{
214 size_t i, j, nr, min_len = 3, max_len = 20;
215 char **names;
216 int repetitions = 15, file_mode = 0100644;
217 uint64_t begin, end;
218 double m[3][2], v[3][2];
219 uint64_t cumul;
220 double cumul2;
221
222 if (argc > 1 && !strcmp(argv[1], "--with-symlink-mode")) {
223 file_mode = 0120000;
224 argc--;
225 argv++;
226 }
227
228 nr = argc > 1 ? strtoul(argv[1], NULL, 0) : 1000000;
229 ALLOC_ARRAY(names, nr);
230
231 if (argc > 2) {
232 min_len = strtoul(argv[2], NULL, 0);
233 if (argc > 3)
234 max_len = strtoul(argv[3], NULL, 0);
235 if (min_len > max_len)
236 die("min_len > max_len");
237 }
238
239 for (i = 0; i < nr; i++) {
240 size_t len = min_len + (my_random() % (max_len + 1 - min_len));
241
242 names[i] = xmallocz(len);
243 while (len > 0)
244 names[i][--len] = (char)(' ' + (my_random() % ('\x7f' - ' ')));
245 }
246
247 for (protect_ntfs = 0; protect_ntfs < 2; protect_ntfs++)
248 for (protect_hfs = 0; protect_hfs < 2; protect_hfs++) {
249 cumul = 0;
250 cumul2 = 0;
251 for (i = 0; i < repetitions; i++) {
252 begin = getnanotime();
253 for (j = 0; j < nr; j++)
254 verify_path(names[j], file_mode);
255 end = getnanotime();
256 printf("protect_ntfs = %d, protect_hfs = %d: %lfms\n", protect_ntfs, protect_hfs, (end-begin) / (double)1e6);
257 cumul += end - begin;
258 cumul2 += (end - begin) * (end - begin);
259 }
260 m[protect_ntfs][protect_hfs] = cumul / (double)repetitions;
261 v[protect_ntfs][protect_hfs] = my_sqrt(cumul2 / (double)repetitions - m[protect_ntfs][protect_hfs] * m[protect_ntfs][protect_hfs]);
262 printf("mean: %lfms, stddev: %lfms\n", m[protect_ntfs][protect_hfs] / (double)1e6, v[protect_ntfs][protect_hfs] / (double)1e6);
263 }
264
265 for (protect_ntfs = 0; protect_ntfs < 2; protect_ntfs++)
266 for (protect_hfs = 0; protect_hfs < 2; protect_hfs++)
267 printf("ntfs=%d/hfs=%d: %lf%% slower\n", protect_ntfs, protect_hfs, (m[protect_ntfs][protect_hfs] - m[0][0]) * 100 / m[0][0]);
268
269 return 0;
270}
271
3f2e2297 272int cmd_main(int argc, const char **argv)
ae299be0 273{
f42302b4 274 if (argc == 3 && !strcmp(argv[1], "normalize_path_copy")) {
62f17513 275 char *buf = xmallocz(strlen(argv[2]));
f42302b4
JS
276 int rv = normalize_path_copy(buf, argv[2]);
277 if (rv)
278 buf = "++failed++";
ae299be0 279 puts(buf);
2cd85c40 280 return 0;
ae299be0
DR
281 }
282
e2a57aac 283 if (argc >= 2 && !strcmp(argv[1], "real_path")) {
d553e737 284 while (argc > 2) {
e2a57aac 285 puts(real_path(argv[2]));
d553e737
DR
286 argc--;
287 argv++;
288 }
2cd85c40 289 return 0;
d553e737
DR
290 }
291
87a246e1
MH
292 if (argc >= 2 && !strcmp(argv[1], "absolute_path")) {
293 while (argc > 2) {
294 puts(absolute_path(argv[2]));
295 argc--;
296 argv++;
297 }
298 return 0;
299 }
300
0454dd93 301 if (argc == 4 && !strcmp(argv[1], "longest_ancestor_length")) {
31171d9e
MH
302 int len;
303 struct string_list ceiling_dirs = STRING_LIST_INIT_DUP;
9e2326c7 304 char *path = xstrdup(argv[2]);
31171d9e 305
9e2326c7
MH
306 /*
307 * We have to normalize the arguments because under
308 * Windows, bash mangles arguments that look like
309 * absolute POSIX paths or colon-separate lists of
310 * absolute POSIX paths into DOS paths (e.g.,
311 * "/foo:/foo/bar" might be converted to
312 * "D:\Src\msysgit\foo;D:\Src\msysgit\foo\bar"),
313 * whereas longest_ancestor_length() requires paths
314 * that use forward slashes.
315 */
316 if (normalize_path_copy(path, path))
317 die("Path \"%s\" could not be normalized", argv[2]);
31171d9e 318 string_list_split(&ceiling_dirs, argv[3], PATH_SEP, -1);
9e2326c7
MH
319 filter_string_list(&ceiling_dirs, 0,
320 normalize_ceiling_entry, NULL);
321 len = longest_ancestor_length(path, &ceiling_dirs);
31171d9e 322 string_list_clear(&ceiling_dirs, 0);
9e2326c7 323 free(path);
0454dd93 324 printf("%d\n", len);
2cd85c40 325 return 0;
0454dd93
DR
326 }
327
9e813723 328 if (argc >= 4 && !strcmp(argv[1], "prefix_path")) {
3f2e2297 329 const char *prefix = argv[2];
9e813723
MH
330 int prefix_len = strlen(prefix);
331 int nongit_ok;
332 setup_git_directory_gently(&nongit_ok);
333 while (argc > 3) {
334 puts(prefix_path(prefix, prefix_len, argv[3]));
335 argc--;
336 argv++;
337 }
338 return 0;
339 }
340
4fcc86b0
JS
341 if (argc == 4 && !strcmp(argv[1], "strip_path_suffix")) {
342 char *prefix = strip_path_suffix(argv[2], argv[3]);
343 printf("%s\n", prefix ? prefix : "(null)");
344 return 0;
345 }
346
7ffd18fc 347 if (argc == 3 && !strcmp(argv[1], "print_path")) {
abd4284b
JX
348 puts(argv[2]);
349 return 0;
350 }
351
203439b2 352 if (argc == 4 && !strcmp(argv[1], "relative_path")) {
e02ca72f 353 struct strbuf sb = STRBUF_INIT;
203439b2
JX
354 const char *in, *prefix, *rel;
355 normalize_argv_string(&in, argv[2]);
356 normalize_argv_string(&prefix, argv[3]);
e02ca72f 357 rel = relative_path(in, prefix, &sb);
203439b2
JX
358 if (!rel)
359 puts("(null)");
360 else
361 puts(strlen(rel) > 0 ? rel : "(empty)");
e02ca72f 362 strbuf_release(&sb);
203439b2
JX
363 return 0;
364 }
365
7d1aaa68 366 if (argc == 2 && !strcmp(argv[1], "basename"))
29c2eda8 367 return test_function(basename_data, posix_basename, argv[1]);
7d1aaa68
JS
368
369 if (argc == 2 && !strcmp(argv[1], "dirname"))
29c2eda8 370 return test_function(dirname_data, posix_dirname, argv[1]);
7d1aaa68 371
dc2d9ba3
JS
372 if (argc > 2 && !strcmp(argv[1], "is_dotgitmodules")) {
373 int res = 0, expect = 1, i;
374 for (i = 2; i < argc; i++)
375 if (!strcmp("--not", argv[i]))
376 expect = !expect;
377 else if (expect != is_dotgitmodules(argv[i]))
378 res = error("'%s' is %s.gitmodules", argv[i],
379 expect ? "not " : "");
380 else
381 fprintf(stderr, "ok: '%s' is %s.gitmodules\n",
382 argv[i], expect ? "" : "not ");
383 return !!res;
384 }
385
a62f9d1a
GS
386 if (argc > 1 && !strcmp(argv[1], "protect_ntfs_hfs"))
387 return !!protect_ntfs_hfs_benchmark(argc - 1, argv + 1);
388
2cd85c40
JS
389 fprintf(stderr, "%s: unknown function name: %s\n", argv[0],
390 argv[1] ? argv[1] : "(there was none)");
391 return 1;
ae299be0 392}