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