]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - libstdc++-v3/testsuite/util/testsuite_fs.h
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / util / testsuite_fs.h
index 1eadf7fa767775715b44d15c5836be6aad21b6ce..f26ad7297c622af56747d2208ae2f740bbad432a 100644 (file)
@@ -1,7 +1,7 @@
 // -*- C++ -*-
 // Filesystem utils for the C++ library testsuite.
 //
-// Copyright (C) 2014-2021 Free Software Foundation, Inc.
+// Copyright (C) 2014-2024 Free Software Foundation, Inc.
 //
 // This file is part of the GNU ISO C++ Library.  This library is free
 // software; you can redistribute it and/or modify it under the
@@ -32,14 +32,28 @@ namespace test_fs = std::experimental::filesystem;
 #endif
 #include <algorithm>
 #include <fstream>
+#include <random>   // std::random_device
 #include <string>
+#include <system_error>
 #include <cstdio>
-#include <unistd.h> // unlink, close, getpid
+#include <unistd.h> // unlink, close, getpid, geteuid
 
 #if defined(_GNU_SOURCE) || _XOPEN_SOURCE >= 500 || _POSIX_C_SOURCE >= 200112L
 #include <stdlib.h> // mkstemp
-#else
-#include <random>   // std::random_device
+#endif
+
+#ifndef _GLIBCXX_HAVE_SYMLINK
+#define NO_SYMLINKS
+#endif
+
+#if !defined (_GLIBCXX_HAVE_SYS_STATVFS_H) \
+  && !defined (_GLIBCXX_FILESYSTEM_IS_WINDOWS)
+#define NO_SPACE
+#endif
+
+#if !(_GLIBCXX_HAVE_SYS_STAT_H \
+      && (_GLIBCXX_USE_UTIMENSAT || _GLIBCXX_USE_UTIME))
+#define NO_LAST_WRITE_TIME 1
 #endif
 
 namespace __gnu_test
@@ -109,32 +123,52 @@ namespace __gnu_test
     if (pos != file.npos)
       file.erase(0, pos+1);
 
+    file.reserve(file.size() + 40);
+    file.insert(0, "filesystem-test.");
+
+    // A counter, starting from a random value, to be included as part
+    // of the filename being returned, and incremented each time
+    // this function is used.  It allows us to ensure that two calls
+    // to this function can never return the same filename, something
+    // testcases do when they need multiple non-existent filenames
+    // for their purposes.
+    static unsigned counter = std::random_device{}();
+    file += '.';
+    file += std::to_string(counter++);
+    file += '.';
+
     test_fs::path p;
 #if defined(_GNU_SOURCE) || _XOPEN_SOURCE >= 500 || _POSIX_C_SOURCE >= 200112L
-    char tmp[] = "filesystem-test.XXXXXX";
-    int fd = ::mkstemp(tmp);
+
+    // Use mkstemp to determine the name of a file which does not exist yet.
+    //
+    // Note that we have seen on some systems (such as RTEMS, for instance)
+    // that mkstemp behaves very predictably, causing it to always try
+    // the same sequence of file names.  In other words, if we call mkstemp
+    // with a pattern, delete the file it created (which is what we do, here),
+    // and call mkstemp with the same pattern again, it returns the same
+    // filename once more.  While most implementations introduce a degree
+    // of randomness, it is not mandated by the standard, and this is why
+    // we also include a counter in the template passed to mkstemp.
+    file += "XXXXXX";
+    int fd = ::mkstemp(&file[0]);
     if (fd == -1)
       throw test_fs::filesystem_error("mkstemp failed",
          std::error_code(errno, std::generic_category()));
-    ::unlink(tmp);
+    ::unlink(file.c_str());
     ::close(fd);
-    if (!file.empty())
-      file.insert(0, 1, '-');
-    file.insert(0, tmp);
-    p = file;
+    p = std::move(file);
 #else
     if (file.length() > 64)
       file.resize(64);
-    char buf[128];
-    static unsigned counter = std::random_device{}();
-#if _GLIBCXX_USE_C99_STDIO
-    std::snprintf(buf, 128,
-#else
-    std::sprintf(buf,
-#endif
-      "filesystem-test.%u.%lu-%s", counter++, (unsigned long) ::getpid(),
-      file.c_str());
-    p = buf;
+    // The combination of random counter and PID should be unique for a given
+    // run of the testsuite.  N.B. getpid() returns a pointer type on vxworks
+    // in kernel mode.
+    file += std::to_string((unsigned long) ::getpid());
+    p = std::move(file);
+    if (test_fs::exists(p))
+      throw test_fs::filesystem_error("Failed to generate unique pathname", p,
+         std::make_error_code(std::errc::file_exists));
 #endif
     return p;
   }
@@ -160,5 +194,21 @@ namespace __gnu_test
     path_type path;
   };
 
+  inline bool
+  permissions_are_testable(bool print_msg = true)
+  {
+    bool testable = false;
+#if !(defined __MINGW32__ || defined __MINGW64__)
+    if (geteuid() != 0)
+      testable = true;
+    // XXX on Linux the CAP_DAC_OVERRIDE and CAP_DAC_READ_SEARCH capabilities
+    // can give normal users extra permissions for files and directories.
+    // We ignore that possibility here.
+#endif
+    if (print_msg && !testable)
+      std::puts("Skipping tests that depend on filesystem permissions");
+    return testable;
+  }
+
 } // namespace __gnu_test
 #endif