Skip to content

Commit 202fb85

Browse files
committed
try to directly create file in /tmp when filepath is too long
1 parent c458f92 commit 202fb85

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

libcxx/test/support/filesystem_test_helper.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <chrono>
1919
#include <cstdint>
2020
#include <cstdio> // for printf
21+
#include <ctime>
2122
#include <string>
2223
#include <system_error>
2324
#include <type_traits>
@@ -324,10 +325,22 @@ struct scoped_test_env
324325

325326
::sockaddr_un address;
326327
address.sun_family = AF_UNIX;
328+
329+
// If file.size() is too big, try to create a file directly inside
330+
// /tmp to make sure file path is short enough.
331+
if (file.size() <= sizeof(address.sun_path) && utils::exists("/tmp")) {
332+
fs::path const tmp = "/tmp";
333+
std::size_t i = std::hash<std::string>()(std::to_string(std::time(nullptr)));
334+
fs::path p = tmp / ("libcxx-socket-" + file + "-" + std::to_string(i));
335+
while (utils::exists(p.string())) {
336+
p = tmp / ("libcxx-socket-" + file + "-" + std::to_string(++i));
337+
}
338+
file = p.string();
339+
}
327340
assert(file.size() <= sizeof(address.sun_path));
328341
::strncpy(address.sun_path, file.c_str(), sizeof(address.sun_path));
329342
int fd = ::socket(AF_UNIX, SOCK_STREAM, 0);
330-
::bind(fd, reinterpret_cast<::sockaddr*>(&address), sizeof(address));
343+
assert(::bind(fd, reinterpret_cast<::sockaddr*>(&address), sizeof(address)) == 0);
331344
return file;
332345
}
333346
#endif

0 commit comments

Comments
 (0)