1 /* Check if internal buffer reallocation work for large paths (BZ #28126)
2    Copyright (C) 2021-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library 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 the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #include <errno.h>
20 #include <ftw.h>
21 #include <limits.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <support/check.h>
25 #include <support/support.h>
26 #include <support/temp_file.h>
27 #include <support/xunistd.h>
28 #include <stdio.h>
29 
30 static int
my_func(const char * file,const struct stat * sb,int flag)31 my_func (const char *file, const struct stat *sb, int flag)
32 {
33   return 0;
34 }
35 
36 static const char folder[NAME_MAX] = { [0 ... 253] = 'a', [254] = '\0' };
37 
38 #define NSUBFOLDERS 16
39 static int nsubfolders;
40 
41 static void
do_cleanup(void)42 do_cleanup (void)
43 {
44   xchdir ("..");
45   for (int i = 0; i < nsubfolders; i++)
46     {
47       remove (folder);
48       xchdir ("..");
49     }
50   remove (folder);
51 }
52 #define CLEANUP_HANDLER do_cleanup
53 
54 static void
check_mkdir(const char * path)55 check_mkdir (const char *path)
56 {
57   int r = mkdir (path, 0777);
58   /* Some filesystem such as overlayfs does not support larger path required
59      to trigger the internal buffer reallocation.  */
60   if (r != 0)
61     {
62       if (errno == ENAMETOOLONG)
63 	FAIL_UNSUPPORTED ("the filesystem does not support the required"
64 			  "large path");
65       else
66 	FAIL_EXIT1 ("mkdir (\"%s\", 0%o): %m", folder, 0777);
67     }
68 }
69 
70 static int
do_test(void)71 do_test (void)
72 {
73   char *tempdir = support_create_temp_directory ("tst-bz28126");
74 
75   /* Create path with various subfolders to force an internal buffer
76      reallocation within ntfw.  */
77   char *path = xasprintf ("%s/%s", tempdir, folder);
78   check_mkdir (path);
79   xchdir (path);
80   free (path);
81   for (int i = 0; i < NSUBFOLDERS - 1; i++)
82     {
83       check_mkdir (folder);
84       xchdir (folder);
85       nsubfolders++;
86     }
87 
88   TEST_COMPARE (ftw (tempdir, my_func, 20), 0);
89 
90   free (tempdir);
91 
92   do_cleanup ();
93 
94   return 0;
95 }
96 
97 #include <support/test-driver.c>
98