[libcxx-commits] [PATCH] D137132: [libc++][Android] Use fopen/ftruncate64/off64_t on 32-bit Bionic

Ryan Prichard via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Wed Nov 2 16:37:13 PDT 2022


rprichard updated this revision to Diff 472804.
rprichard added a comment.

Factor out utils::{off64_t, fopen64, ftruncate64}.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D137132/new/

https://reviews.llvm.org/D137132

Files:
  libcxx/test/support/filesystem_test_helper.h


Index: libcxx/test/support/filesystem_test_helper.h
===================================================================
--- libcxx/test/support/filesystem_test_helper.h
+++ libcxx/test/support/filesystem_test_helper.h
@@ -99,6 +99,27 @@
     }
 #endif
 
+#if defined(__LP64__) || defined(_WIN32) || defined(__MVS__)
+    // N.B. libc might define some of these identifiers using macros from
+    // foo64 -> foo or vice versa.
+    using off64_t = ::off_t;
+    inline FILE* fopen64(const char* pathname, const char* mode) {
+        return ::fopen(pathname, mode);
+    }
+    inline int ftruncate64(int fd, off64_t length) {
+        return ::ftruncate(fd, length);
+    }
+#elif defined(__BIONIC__)
+    // Bionic does not distinguish between fopen and fopen64, and it has an
+    // ftruncate64 that accepts off64_t.
+    using ::off64_t, ::ftruncate64;
+    inline FILE* fopen64(const char* pathname, const char* mode) {
+        return ::fopen(pathname, mode);
+    }
+#else
+    using ::off64_t, ::fopen64, ::ftruncate64;
+#endif
+
     inline std::string getcwd() {
         // Assume that path lengths are not greater than this.
         // This should be fine for testing purposes.
@@ -185,22 +206,11 @@
     // off_t). On a 32-bit system this allows us to create a file larger than
     // 2GB.
     std::string create_file(fs::path filename_path, uintmax_t size = 0) {
-        std::string filename = filename_path.string();
-#if defined(__LP64__) || defined(_WIN32) || defined(__MVS__)
-        auto large_file_fopen = fopen;
-        auto large_file_ftruncate = utils::ftruncate;
-        using large_file_offset_t = off_t;
-#else
-        auto large_file_fopen = fopen64;
-        auto large_file_ftruncate = ftruncate64;
-        using large_file_offset_t = off64_t;
-#endif
-
-        filename = sanitize_path(std::move(filename));
+        std::string filename = sanitize_path(filename_path.string());
 
         if (size >
-            static_cast<typename std::make_unsigned<large_file_offset_t>::type>(
-                std::numeric_limits<large_file_offset_t>::max())) {
+            static_cast<typename std::make_unsigned<utils::off64_t>::type>(
+                std::numeric_limits<utils::off64_t>::max())) {
             fprintf(stderr, "create_file(%s, %ju) too large\n",
                     filename.c_str(), size);
             abort();
@@ -211,15 +221,15 @@
 #else
 #  define FOPEN_CLOEXEC_FLAG "e"
 #endif
-        FILE* file = large_file_fopen(filename.c_str(), "w" FOPEN_CLOEXEC_FLAG);
+        FILE* file = utils::fopen64(filename.c_str(), "w" FOPEN_CLOEXEC_FLAG);
         if (file == nullptr) {
             fprintf(stderr, "fopen %s failed: %s\n", filename.c_str(),
                     strerror(errno));
             abort();
         }
 
-        if (large_file_ftruncate(
-                fileno(file), static_cast<large_file_offset_t>(size)) == -1) {
+        if (utils::ftruncate64(
+                fileno(file), static_cast<utils::off64_t>(size)) == -1) {
             fprintf(stderr, "ftruncate %s %ju failed: %s\n", filename.c_str(),
                     size, strerror(errno));
             fclose(file);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D137132.472804.patch
Type: text/x-patch
Size: 3153 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20221102/a4ac89b0/attachment.bin>


More information about the libcxx-commits mailing list