]>
git.ipfire.org Git - thirdparty/git.git/blob - t/helper/test-path-utils.c
086238c826aadb2ba65973134a8697b716ded8a1
1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
6 #include "environment.h"
8 #include "read-cache-ll.h"
10 #include "string-list.h"
16 * A "string_list_each_func_t" function that normalizes an entry from
17 * GIT_CEILING_DIRECTORIES. If the path is unusable for some reason,
18 * die with an explanation.
20 static int normalize_ceiling_entry(struct string_list_item
*item
,
23 char *ceil
= item
->string
;
26 die("Empty path is not supported");
27 if (!is_absolute_path(ceil
))
28 die("Path \"%s\" is not absolute", ceil
);
29 if (normalize_path_copy(ceil
, ceil
) < 0)
30 die("Path \"%s\" could not be normalized", ceil
);
34 static void normalize_argv_string(const char **var
, const char *input
)
36 if (!strcmp(input
, "<null>"))
38 else if (!strcmp(input
, "<empty>"))
43 if (*var
&& (**var
== '<' || **var
== '('))
44 die("Bad value: %s", input
);
48 const char *from
; /* input: transform from this ... */
49 const char *to
; /* output: ... to this. */
50 const char *alternative
; /* output: ... or this. */
54 * Compatibility wrappers for OpenBSD, whose basename(3) and dirname(3)
55 * have const parameters.
57 static char *posix_basename(char *path
)
59 return basename(path
);
62 static char *posix_dirname(char *path
)
67 static int test_function(struct test_data
*data
, char *(*func
)(char *input
),
74 for (i
= 0; data
[i
].to
; i
++) {
78 xsnprintf(buffer
, sizeof(buffer
), "%s", data
[i
].from
);
81 if (!strcmp(to
, data
[i
].to
))
83 if (!data
[i
].alternative
)
84 error("FAIL: %s(%s) => '%s' != '%s'",
85 funcname
, data
[i
].from
, to
, data
[i
].to
);
86 else if (!strcmp(to
, data
[i
].alternative
))
89 error("FAIL: %s(%s) => '%s' != '%s', '%s'",
90 funcname
, data
[i
].from
, to
, data
[i
].to
,
97 static struct test_data basename_data
[] = {
98 /* --- POSIX type paths --- */
105 { "///", "/", "//" },
106 { "////", "/", "//" },
111 { "/usr/lib", "lib" },
112 { "usr/lib", "lib" },
113 { "usr/lib///", "lib" },
115 #if defined(__MINGW32__) || defined(_MSC_VER)
116 /* --- win32 type paths --- */
118 { "\\usr\\", "usr" },
119 { "\\usr\\\\", "usr" },
120 { "\\usr\\lib", "lib" },
121 { "usr\\lib", "lib" },
122 { "usr\\lib\\\\\\", "lib" },
125 { "C:/usr/", "usr" },
126 { "C:/usr//", "usr" },
127 { "C:/usr/lib", "lib" },
128 { "C:usr/lib", "lib" },
129 { "C:usr/lib///", "lib" },
135 { "\\\\", "\\", "/" },
136 { "\\\\\\", "\\", "/" },
141 static struct test_data dirname_data
[] = {
142 /* --- POSIX type paths --- */
149 { "///", "/", "//" },
150 { "////", "/", "//" },
155 { "/usr/lib", "/usr" },
156 { "usr/lib", "usr" },
157 { "usr/lib///", "usr" },
159 #if defined(__MINGW32__) || defined(_MSC_VER)
160 /* --- win32 type paths --- */
165 { "\\usr\\\\", "\\" },
166 { "\\usr\\lib", "\\usr" },
167 { "usr\\lib", "usr" },
168 { "usr\\lib\\\\\\", "usr" },
173 { "C:/usr/", "C:/" },
174 { "C:/usr//", "C:/" },
175 { "C:/usr/lib", "C:/usr" },
176 { "C:usr/lib", "C:usr" },
177 { "C:usr/lib///", "C:usr" },
179 { "\\\\\\\\", "\\" },
180 { "C:", "C:.", "." },
185 static int check_dotfile(const char *x
, const char **argv
,
186 int (*is_hfs
)(const char *),
187 int (*is_ntfs
)(const char *))
189 int res
= 0, expect
= 1;
190 for (; *argv
; argv
++) {
191 if (!strcmp("--not", *argv
))
193 else if (expect
!= (is_hfs(*argv
) || is_ntfs(*argv
)))
194 res
= error("'%s' is %s.git%s", *argv
,
195 expect
? "not " : "", x
);
197 fprintf(stderr
, "ok: '%s' is %s.git%s\n",
198 *argv
, expect
? "" : "not ", x
);
203 static int cmp_by_st_size(const void *a
, const void *b
)
205 intptr_t x
= (intptr_t)((struct string_list_item
*)a
)->util
;
206 intptr_t y
= (intptr_t)((struct string_list_item
*)b
)->util
;
208 return x
> y
? -1 : (x
< y
? +1 : 0);
212 * A very simple, reproducible pseudo-random generator. Copied from
213 * `test-genrandom.c`.
215 static uint64_t my_random_value
= 1234;
217 static uint64_t my_random(void)
219 my_random_value
= my_random_value
* 1103515245 + 12345;
220 return my_random_value
;
224 * A fast approximation of the square root, without requiring math.h.
226 * It uses Newton's method to approximate the solution of 0 = x^2 - value.
228 static double my_sqrt(double value
)
230 const double epsilon
= 1e-6;
237 double delta
= (value
/ x
- x
) / 2;
238 if (delta
< epsilon
&& delta
> -epsilon
)
244 static int protect_ntfs_hfs_benchmark(int argc
, const char **argv
)
246 size_t i
, j
, nr
, min_len
= 3, max_len
= 20;
248 int repetitions
= 15, file_mode
= 0100644;
250 double m
[3][2], v
[3][2];
254 if (argc
> 1 && !strcmp(argv
[1], "--with-symlink-mode")) {
260 nr
= argc
> 1 ? strtoul(argv
[1], NULL
, 0) : 1000000;
261 ALLOC_ARRAY(names
, nr
);
264 min_len
= strtoul(argv
[2], NULL
, 0);
266 max_len
= strtoul(argv
[3], NULL
, 0);
267 if (min_len
> max_len
)
268 die("min_len > max_len");
271 for (i
= 0; i
< nr
; i
++) {
272 size_t len
= min_len
+ (my_random() % (max_len
+ 1 - min_len
));
274 names
[i
] = xmallocz(len
);
276 names
[i
][--len
] = (char)(' ' + (my_random() % ('\x7f' - ' ')));
279 for (protect_ntfs
= 0; protect_ntfs
< 2; protect_ntfs
++)
280 for (protect_hfs
= 0; protect_hfs
< 2; protect_hfs
++) {
283 for (i
= 0; i
< repetitions
; i
++) {
284 begin
= getnanotime();
285 for (j
= 0; j
< nr
; j
++)
286 verify_path(names
[j
], file_mode
);
288 printf("protect_ntfs = %d, protect_hfs = %d: %lfms\n", protect_ntfs
, protect_hfs
, (end
-begin
) / (double)1e6
);
289 cumul
+= end
- begin
;
290 cumul2
+= (end
- begin
) * (end
- begin
);
292 m
[protect_ntfs
][protect_hfs
] = cumul
/ (double)repetitions
;
293 v
[protect_ntfs
][protect_hfs
] = my_sqrt(cumul2
/ (double)repetitions
- m
[protect_ntfs
][protect_hfs
] * m
[protect_ntfs
][protect_hfs
]);
294 printf("mean: %lfms, stddev: %lfms\n", m
[protect_ntfs
][protect_hfs
] / (double)1e6
, v
[protect_ntfs
][protect_hfs
] / (double)1e6
);
297 for (protect_ntfs
= 0; protect_ntfs
< 2; protect_ntfs
++)
298 for (protect_hfs
= 0; protect_hfs
< 2; protect_hfs
++)
299 printf("ntfs=%d/hfs=%d: %lf%% slower\n", protect_ntfs
, protect_hfs
, (m
[protect_ntfs
][protect_hfs
] - m
[0][0]) * 100 / m
[0][0]);
304 int cmd__path_utils(int argc
, const char **argv
)
306 if (argc
== 3 && !strcmp(argv
[1], "normalize_path_copy")) {
307 char *buf
= xmallocz(strlen(argv
[2]));
308 int rv
= normalize_path_copy(buf
, argv
[2]);
309 puts(rv
? "++failed++" : buf
);
314 if (argc
>= 2 && !strcmp(argv
[1], "real_path")) {
315 struct strbuf realpath
= STRBUF_INIT
;
317 strbuf_realpath(&realpath
, argv
[2], 1);
322 strbuf_release(&realpath
);
326 if (argc
>= 2 && !strcmp(argv
[1], "readlink")) {
327 struct strbuf target
= STRBUF_INIT
;
329 if (strbuf_readlink(&target
, argv
[2], 0) < 0)
330 die_errno("cannot read link at '%s'", argv
[2]);
335 strbuf_release(&target
);
339 if (argc
>= 2 && !strcmp(argv
[1], "absolute_path")) {
341 puts(absolute_path(argv
[2]));
348 if (argc
== 4 && !strcmp(argv
[1], "longest_ancestor_length")) {
350 struct string_list ceiling_dirs
= STRING_LIST_INIT_DUP
;
351 char *path
= xstrdup(argv
[2]);
354 * We have to normalize the arguments because under
355 * Windows, bash mangles arguments that look like
356 * absolute POSIX paths or colon-separate lists of
357 * absolute POSIX paths into DOS paths (e.g.,
358 * "/foo:/foo/bar" might be converted to
359 * "D:\Src\msysgit\foo;D:\Src\msysgit\foo\bar"),
360 * whereas longest_ancestor_length() requires paths
361 * that use forward slashes.
363 if (normalize_path_copy(path
, path
))
364 die("Path \"%s\" could not be normalized", argv
[2]);
365 string_list_split(&ceiling_dirs
, argv
[3], PATH_SEP
, -1);
366 filter_string_list(&ceiling_dirs
, 0,
367 normalize_ceiling_entry
, NULL
);
368 len
= longest_ancestor_length(path
, &ceiling_dirs
);
369 string_list_clear(&ceiling_dirs
, 0);
375 if (argc
>= 4 && !strcmp(argv
[1], "prefix_path")) {
376 const char *prefix
= argv
[2];
377 int prefix_len
= strlen(prefix
);
379 setup_git_directory_gently(&nongit_ok
);
381 char *pfx
= prefix_path(prefix
, prefix_len
, argv
[3]);
391 if (argc
== 4 && !strcmp(argv
[1], "strip_path_suffix")) {
392 char *prefix
= strip_path_suffix(argv
[2], argv
[3]);
393 printf("%s\n", prefix
? prefix
: "(null)");
398 if (argc
== 3 && !strcmp(argv
[1], "print_path")) {
403 if (argc
== 4 && !strcmp(argv
[1], "relative_path")) {
404 struct strbuf sb
= STRBUF_INIT
;
405 const char *in
, *prefix
, *rel
;
406 normalize_argv_string(&in
, argv
[2]);
407 normalize_argv_string(&prefix
, argv
[3]);
408 rel
= relative_path(in
, prefix
, &sb
);
412 puts(strlen(rel
) > 0 ? rel
: "(empty)");
417 if (argc
== 2 && !strcmp(argv
[1], "basename"))
418 return test_function(basename_data
, posix_basename
, argv
[1]);
420 if (argc
== 2 && !strcmp(argv
[1], "dirname"))
421 return test_function(dirname_data
, posix_dirname
, argv
[1]);
423 if (argc
> 2 && !strcmp(argv
[1], "is_dotgitmodules")) {
424 return check_dotfile("modules", argv
+ 2,
425 is_hfs_dotgitmodules
,
426 is_ntfs_dotgitmodules
);
428 if (argc
> 2 && !strcmp(argv
[1], "is_dotgitignore")) {
429 return check_dotfile("ignore", argv
+ 2,
431 is_ntfs_dotgitignore
);
433 if (argc
> 2 && !strcmp(argv
[1], "is_dotgitattributes")) {
434 return check_dotfile("attributes", argv
+ 2,
435 is_hfs_dotgitattributes
,
436 is_ntfs_dotgitattributes
);
438 if (argc
> 2 && !strcmp(argv
[1], "is_dotmailmap")) {
439 return check_dotfile("mailmap", argv
+ 2,
444 if (argc
> 2 && !strcmp(argv
[1], "file-size")) {
448 for (i
= 2; i
< argc
; i
++)
449 if (stat(argv
[i
], &st
))
450 res
= error_errno("Cannot stat '%s'", argv
[i
]);
452 printf("%"PRIuMAX
"\n", (uintmax_t)st
.st_size
);
456 if (argc
== 4 && !strcmp(argv
[1], "skip-n-bytes")) {
457 int fd
= open(argv
[2], O_RDONLY
), offset
= atoi(argv
[3]);
461 die_errno("could not open '%s'", argv
[2]);
462 if (lseek(fd
, offset
, SEEK_SET
) < 0)
463 die_errno("could not skip %d bytes", offset
);
465 ssize_t count
= read(fd
, buffer
, sizeof(buffer
));
467 die_errno("could not read '%s'", argv
[2]);
470 if (write(1, buffer
, count
) < 0)
471 die_errno("could not write to stdout");
477 if (argc
> 5 && !strcmp(argv
[1], "slice-tests")) {
479 long offset
, stride
, i
;
480 struct string_list list
= STRING_LIST_INIT_NODUP
;
483 offset
= strtol(argv
[2], NULL
, 10);
484 stride
= strtol(argv
[3], NULL
, 10);
487 for (i
= 4; i
< argc
; i
++)
488 if (stat(argv
[i
], &st
))
489 res
= error_errno("Cannot stat '%s'", argv
[i
]);
491 string_list_append(&list
, argv
[i
])->util
=
492 (void *)(intptr_t)st
.st_size
;
493 QSORT(list
.items
, list
.nr
, cmp_by_st_size
);
494 for (i
= offset
; i
< list
.nr
; i
+= stride
)
495 printf("%s\n", list
.items
[i
].string
);
500 if (argc
> 1 && !strcmp(argv
[1], "protect_ntfs_hfs"))
501 return !!protect_ntfs_hfs_benchmark(argc
- 1, argv
+ 1);
503 if (argc
> 1 && !strcmp(argv
[1], "is_valid_path")) {
504 int res
= 0, expect
= 1, i
;
506 for (i
= 2; i
< argc
; i
++)
507 if (!strcmp("--not", argv
[i
]))
509 else if (expect
!= is_valid_path(argv
[i
]))
510 res
= error("'%s' is%s a valid path",
511 argv
[i
], expect
? " not" : "");
514 "'%s' is%s a valid path\n",
515 argv
[i
], expect
? "" : " not");
520 if (argc
> 1 && !strcmp(argv
[1], "is_path_owned_by_current_user")) {
523 for (int i
= 2; i
< argc
; i
++) {
524 struct strbuf buf
= STRBUF_INIT
;
526 if (is_path_owned_by_current_user(argv
[i
], &buf
))
527 printf("'%s' is owned by current SID\n", argv
[i
]);
529 printf("'%s' is not owned by current SID: %s\n", argv
[i
], buf
.buf
);
533 strbuf_release(&buf
);
539 fprintf(stderr
, "%s: unknown function name: %s\n", argv
[0],
540 argv
[1] ? argv
[1] : "(there was none)");