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