Skip to content

Commit af5ddbd

Browse files
Merge pull request #4342 from tanzislam:prefer-tmpdir-on-android
PiperOrigin-RevId: 565230380 Change-Id: I6e91eea46d05413d4d87e73a11941786604d9f27
2 parents eab0e7e + edf9b49 commit af5ddbd

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

googletest/src/gtest-port.cc

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,13 @@ size_t GetThreadCount() {
158158
// we cannot detect it.
159159
size_t GetThreadCount() {
160160
int mib[] = {
161-
CTL_KERN,
162-
KERN_PROC,
163-
KERN_PROC_PID,
164-
getpid(),
161+
CTL_KERN,
162+
KERN_PROC,
163+
KERN_PROC_PID,
164+
getpid(),
165165
#ifdef GTEST_OS_NETBSD
166-
sizeof(struct kinfo_proc),
167-
1,
166+
sizeof(struct kinfo_proc),
167+
1,
168168
#endif
169169
};
170170
u_int miblen = sizeof(mib) / sizeof(mib[0]);
@@ -1028,6 +1028,16 @@ GTEST_DISABLE_MSC_DEPRECATED_PUSH_()
10281028

10291029
#if GTEST_HAS_STREAM_REDIRECTION
10301030

1031+
namespace {
1032+
1033+
#if defined(GTEST_OS_LINUX_ANDROID) || defined(GTEST_OS_IOS)
1034+
bool EndsWithPathSeparator(const std::string& path) {
1035+
return !path.empty() && path.back() == GTEST_PATH_SEP_[0];
1036+
}
1037+
#endif
1038+
1039+
} // namespace
1040+
10311041
// Object that captures an output stream (stdout/stderr).
10321042
class CapturedStream {
10331043
public:
@@ -1064,7 +1074,13 @@ class CapturedStream {
10641074
// The location /data/local/tmp is directly accessible from native code.
10651075
// '/sdcard' and other variants cannot be relied on, as they are not
10661076
// guaranteed to be mounted, or may have a delay in mounting.
1067-
name_template = "/data/local/tmp/";
1077+
//
1078+
// However, prefer using the TMPDIR environment variable if set, as newer
1079+
// devices may have /data/local/tmp read-only.
1080+
name_template = TempDir();
1081+
if (!EndsWithPathSeparator(name_template))
1082+
name_template.push_back(GTEST_PATH_SEP_[0]);
1083+
10681084
#elif defined(GTEST_OS_IOS)
10691085
char user_temp_dir[PATH_MAX + 1];
10701086

@@ -1084,7 +1100,7 @@ class CapturedStream {
10841100
::confstr(_CS_DARWIN_USER_TEMP_DIR, user_temp_dir, sizeof(user_temp_dir));
10851101

10861102
name_template = user_temp_dir;
1087-
if (name_template.back() != GTEST_PATH_SEP_[0])
1103+
if (!EndsWithPathSeparator(name_template))
10881104
name_template.push_back(GTEST_PATH_SEP_[0]);
10891105
#else
10901106
name_template = "/tmp/";

0 commit comments

Comments
 (0)