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