93dc2d
commit 062ff490c1467059f6cd64bb9c3d85f6cc6cf97a
93dc2d
Author: Siddhesh Poyarekar <siddhesh@sourceware.org>
93dc2d
Date:   Tue Jan 18 13:29:36 2022 +0530
93dc2d
93dc2d
    support: Add helpers to create paths longer than PATH_MAX
93dc2d
    
93dc2d
    Add new helpers support_create_and_chdir_toolong_temp_directory and
93dc2d
    support_chdir_toolong_temp_directory to create and descend into
93dc2d
    directory trees longer than PATH_MAX.
93dc2d
    
93dc2d
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
93dc2d
    Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
93dc2d
    (cherry picked from commit fb7bff12e81c677a6622f724edd4d4987dd9d971)
93dc2d
93dc2d
diff --git a/support/temp_file.c b/support/temp_file.c
93dc2d
index c6df641876285bb2..e41128c2d43fe903 100644
93dc2d
--- a/support/temp_file.c
93dc2d
+++ b/support/temp_file.c
93dc2d
@@ -1,5 +1,6 @@
93dc2d
 /* Temporary file handling for tests.
93dc2d
-   Copyright (C) 1998-2021 Free Software Foundation, Inc.
93dc2d
+   Copyright (C) 1998-2022 Free Software Foundation, Inc.
93dc2d
+   Copyright The GNU Tools Authors.
93dc2d
    This file is part of the GNU C Library.
93dc2d
 
93dc2d
    The GNU C Library is free software; you can redistribute it and/or
93dc2d
@@ -20,15 +21,17 @@
93dc2d
    some 32-bit platforms. */
93dc2d
 #define _FILE_OFFSET_BITS 64
93dc2d
 
93dc2d
+#include <support/check.h>
93dc2d
 #include <support/temp_file.h>
93dc2d
 #include <support/temp_file-internal.h>
93dc2d
 #include <support/support.h>
93dc2d
 
93dc2d
+#include <errno.h>
93dc2d
 #include <paths.h>
93dc2d
 #include <stdio.h>
93dc2d
 #include <stdlib.h>
93dc2d
 #include <string.h>
93dc2d
-#include <unistd.h>
93dc2d
+#include <xunistd.h>
93dc2d
 
93dc2d
 /* List of temporary files.  */
93dc2d
 static struct temp_name_list
93dc2d
@@ -36,14 +39,20 @@ static struct temp_name_list
93dc2d
   struct temp_name_list *next;
93dc2d
   char *name;
93dc2d
   pid_t owner;
93dc2d
+  bool toolong;
93dc2d
 } *temp_name_list;
93dc2d
 
93dc2d
 /* Location of the temporary files.  Set by the test skeleton via
93dc2d
    support_set_test_dir.  The string is not be freed.  */
93dc2d
 static const char *test_dir = _PATH_TMP;
93dc2d
 
93dc2d
-void
93dc2d
-add_temp_file (const char *name)
93dc2d
+/* Name of subdirectories in a too long temporary directory tree.  */
93dc2d
+static char toolong_subdir[NAME_MAX + 1];
93dc2d
+static bool toolong_initialized;
93dc2d
+static size_t toolong_path_max;
93dc2d
+
93dc2d
+static void
93dc2d
+add_temp_file_internal (const char *name, bool toolong)
93dc2d
 {
93dc2d
   struct temp_name_list *newp
93dc2d
     = (struct temp_name_list *) xcalloc (sizeof (*newp), 1);
93dc2d
@@ -53,12 +62,19 @@ add_temp_file (const char *name)
93dc2d
       newp->name = newname;
93dc2d
       newp->next = temp_name_list;
93dc2d
       newp->owner = getpid ();
93dc2d
+      newp->toolong = toolong;
93dc2d
       temp_name_list = newp;
93dc2d
     }
93dc2d
   else
93dc2d
     free (newp);
93dc2d
 }
93dc2d
 
93dc2d
+void
93dc2d
+add_temp_file (const char *name)
93dc2d
+{
93dc2d
+  add_temp_file_internal (name, false);
93dc2d
+}
93dc2d
+
93dc2d
 int
93dc2d
 create_temp_file_in_dir (const char *base, const char *dir, char **filename)
93dc2d
 {
93dc2d
@@ -90,8 +106,8 @@ create_temp_file (const char *base, char **filename)
93dc2d
   return create_temp_file_in_dir (base, test_dir, filename);
93dc2d
 }
93dc2d
 
93dc2d
-char *
93dc2d
-support_create_temp_directory (const char *base)
93dc2d
+static char *
93dc2d
+create_temp_directory_internal (const char *base, bool toolong)
93dc2d
 {
93dc2d
   char *path = xasprintf ("%s/%sXXXXXX", test_dir, base);
93dc2d
   if (mkdtemp (path) == NULL)
93dc2d
@@ -99,16 +115,132 @@ support_create_temp_directory (const char *base)
93dc2d
       printf ("error: mkdtemp (\"%s\"): %m", path);
93dc2d
       exit (1);
93dc2d
     }
93dc2d
-  add_temp_file (path);
93dc2d
+  add_temp_file_internal (path, toolong);
93dc2d
   return path;
93dc2d
 }
93dc2d
 
93dc2d
-/* Helper functions called by the test skeleton follow.  */
93dc2d
+char *
93dc2d
+support_create_temp_directory (const char *base)
93dc2d
+{
93dc2d
+  return create_temp_directory_internal (base, false);
93dc2d
+}
93dc2d
+
93dc2d
+static void
93dc2d
+ensure_toolong_initialized (void)
93dc2d
+{
93dc2d
+  if (!toolong_initialized)
93dc2d
+    FAIL_EXIT1 ("uninitialized toolong directory tree\n");
93dc2d
+}
93dc2d
+
93dc2d
+static void
93dc2d
+initialize_toolong (const char *base)
93dc2d
+{
93dc2d
+  long name_max = pathconf (base, _PC_NAME_MAX);
93dc2d
+  name_max = (name_max < 0 ? 64
93dc2d
+	      : (name_max < sizeof (toolong_subdir) ? name_max
93dc2d
+		 : sizeof (toolong_subdir) - 1));
93dc2d
+
93dc2d
+  long path_max = pathconf (base, _PC_PATH_MAX);
93dc2d
+  path_max = (path_max < 0 ? 1024
93dc2d
+	      : path_max <= PTRDIFF_MAX ? path_max : PTRDIFF_MAX);
93dc2d
+
93dc2d
+  /* Sanity check to ensure that the test does not create temporary directories
93dc2d
+     in different filesystems because this API doesn't support it.  */
93dc2d
+  if (toolong_initialized)
93dc2d
+    {
93dc2d
+      if (name_max != strlen (toolong_subdir))
93dc2d
+	FAIL_UNSUPPORTED ("name_max: Temporary directories in different"
93dc2d
+			  " filesystems not supported yet\n");
93dc2d
+      if (path_max != toolong_path_max)
93dc2d
+	FAIL_UNSUPPORTED ("path_max: Temporary directories in different"
93dc2d
+			  " filesystems not supported yet\n");
93dc2d
+      return;
93dc2d
+    }
93dc2d
+
93dc2d
+  toolong_path_max = path_max;
93dc2d
+
93dc2d
+  size_t len = name_max;
93dc2d
+  memset (toolong_subdir, 'X', len);
93dc2d
+  toolong_initialized = true;
93dc2d
+}
93dc2d
+
93dc2d
+char *
93dc2d
+support_create_and_chdir_toolong_temp_directory (const char *basename)
93dc2d
+{
93dc2d
+  char *base = create_temp_directory_internal (basename, true);
93dc2d
+  xchdir (base);
93dc2d
+
93dc2d
+  initialize_toolong (base);
93dc2d
+
93dc2d
+  size_t sz = strlen (toolong_subdir);
93dc2d
+
93dc2d
+  /* Create directories and descend into them so that the final path is larger
93dc2d
+     than PATH_MAX.  */
93dc2d
+  for (size_t i = 0; i <= toolong_path_max / sz; i++)
93dc2d
+    {
93dc2d
+      int ret = mkdir (toolong_subdir, S_IRWXU);
93dc2d
+      if (ret != 0 && errno == ENAMETOOLONG)
93dc2d
+	FAIL_UNSUPPORTED ("Filesystem does not support creating too long "
93dc2d
+			  "directory trees\n");
93dc2d
+      else if (ret != 0)
93dc2d
+	FAIL_EXIT1 ("Failed to create directory tree: %m\n");
93dc2d
+      xchdir (toolong_subdir);
93dc2d
+    }
93dc2d
+  return base;
93dc2d
+}
93dc2d
 
93dc2d
 void
93dc2d
-support_set_test_dir (const char *path)
93dc2d
+support_chdir_toolong_temp_directory (const char *base)
93dc2d
 {
93dc2d
-  test_dir = path;
93dc2d
+  ensure_toolong_initialized ();
93dc2d
+
93dc2d
+  xchdir (base);
93dc2d
+
93dc2d
+  size_t sz = strlen (toolong_subdir);
93dc2d
+  for (size_t i = 0; i <= toolong_path_max / sz; i++)
93dc2d
+    xchdir (toolong_subdir);
93dc2d
+}
93dc2d
+
93dc2d
+/* Helper functions called by the test skeleton follow.  */
93dc2d
+
93dc2d
+static void
93dc2d
+remove_toolong_subdirs (const char *base)
93dc2d
+{
93dc2d
+  ensure_toolong_initialized ();
93dc2d
+
93dc2d
+  if (chdir (base) != 0)
93dc2d
+    {
93dc2d
+      printf ("warning: toolong cleanup base failed: chdir (\"%s\"): %m\n",
93dc2d
+	      base);
93dc2d
+      return;
93dc2d
+    }
93dc2d
+
93dc2d
+  /* Descend.  */
93dc2d
+  int levels = 0;
93dc2d
+  size_t sz = strlen (toolong_subdir);
93dc2d
+  for (levels = 0; levels <= toolong_path_max / sz; levels++)
93dc2d
+    if (chdir (toolong_subdir) != 0)
93dc2d
+      {
93dc2d
+	printf ("warning: toolong cleanup failed: chdir (\"%s\"): %m\n",
93dc2d
+		toolong_subdir);
93dc2d
+	break;
93dc2d
+      }
93dc2d
+
93dc2d
+  /* Ascend and remove.  */
93dc2d
+  while (--levels >= 0)
93dc2d
+    {
93dc2d
+      if (chdir ("..") != 0)
93dc2d
+	{
93dc2d
+	  printf ("warning: toolong cleanup failed: chdir (\"..\"): %m\n");
93dc2d
+	  return;
93dc2d
+	}
93dc2d
+      if (remove (toolong_subdir) != 0)
93dc2d
+	{
93dc2d
+	  printf ("warning: could not remove subdirectory: %s: %m\n",
93dc2d
+		  toolong_subdir);
93dc2d
+	  return;
93dc2d
+	}
93dc2d
+    }
93dc2d
 }
93dc2d
 
93dc2d
 void
93dc2d
@@ -123,6 +255,9 @@ support_delete_temp_files (void)
93dc2d
 	 around, to prevent PID reuse.)  */
93dc2d
       if (temp_name_list->owner == pid)
93dc2d
 	{
93dc2d
+	  if (temp_name_list->toolong)
93dc2d
+	    remove_toolong_subdirs (temp_name_list->name);
93dc2d
+
93dc2d
 	  if (remove (temp_name_list->name) != 0)
93dc2d
 	    printf ("warning: could not remove temporary file: %s: %m\n",
93dc2d
 		    temp_name_list->name);
93dc2d
@@ -147,3 +282,9 @@ support_print_temp_files (FILE *f)
93dc2d
       fprintf (f, ")\n");
93dc2d
     }
93dc2d
 }
93dc2d
+
93dc2d
+void
93dc2d
+support_set_test_dir (const char *path)
93dc2d
+{
93dc2d
+  test_dir = path;
93dc2d
+}
93dc2d
diff --git a/support/temp_file.h b/support/temp_file.h
93dc2d
index f3a7fb6f9ca44d19..a22964c6fa11abd2 100644
93dc2d
--- a/support/temp_file.h
93dc2d
+++ b/support/temp_file.h
93dc2d
@@ -44,6 +44,15 @@ int create_temp_file_in_dir (const char *base, const char *dir,
93dc2d
    returns.  The caller should free this string.  */
93dc2d
 char *support_create_temp_directory (const char *base);
93dc2d
 
93dc2d
+/* Create a temporary directory tree that is longer than PATH_MAX and schedule
93dc2d
+   it for deletion.  BASENAME is used as a prefix for the unique directory
93dc2d
+   name, which the function returns.  The caller should free this string.  */
93dc2d
+char *support_create_and_chdir_toolong_temp_directory (const char *basename);
93dc2d
+
93dc2d
+/* Change into the innermost directory of the directory tree BASE, which was
93dc2d
+   created using support_create_and_chdir_toolong_temp_directory.  */
93dc2d
+void support_chdir_toolong_temp_directory (const char *base);
93dc2d
+
93dc2d
 __END_DECLS
93dc2d
 
93dc2d
 #endif /* SUPPORT_TEMP_FILE_H */