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