blob: d434c5bd7def635adc6789d9bb4d07ee3b95d527 [file] [log] [blame]
[email protected]4870fe562012-07-16 16:12:161// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]f7d69972011-06-21 22:34:502// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/os_compat_android.h"
6
[email protected]4870fe562012-07-16 16:12:167#include <errno.h>
8#include <math.h>
9#include <sys/stat.h>
[email protected]c5395182011-12-16 03:18:1310#include <time64.h>
11
[email protected]4870fe562012-07-16 16:12:1612#include "base/rand_util.h"
13#include "base/string_piece.h"
[email protected]4d0f93c2011-09-29 04:43:5414#include "base/stringprintf.h"
[email protected]f7d69972011-06-21 22:34:5015
16// There is no futimes() avaiable in Bionic, so we provide our own
17// implementation until it is there.
18extern "C" {
19
20int futimes(int fd, const struct timeval tv[2]) {
21 const std::string fd_path = StringPrintf("/proc/self/fd/%d", fd);
22 return utimes(fd_path.c_str(), tv);
23}
24
[email protected]c5395182011-12-16 03:18:1325// Android has only timegm64() and no timegm().
26// We replicate the behaviour of timegm() when the result overflows time_t.
27time_t timegm(struct tm* const t) {
28 // time_t is signed on Android.
29 static const time_t kTimeMax = ~(1 << (sizeof(time_t) * CHAR_BIT - 1));
30 static const time_t kTimeMin = (1 << (sizeof(time_t) * CHAR_BIT - 1));
31 time64_t result = timegm64(t);
32 if (result < kTimeMin || result > kTimeMax)
33 return -1;
34 return result;
35}
36
[email protected]4870fe562012-07-16 16:12:1637// The following is only needed when building with GCC 4.6 or higher
38// (i.e. not with Android GCC 4.4.3, nor with Clang).
39//
40// GCC is now capable of optimizing successive calls to sin() and cos() into
41// a single call to sincos(). This means that source code that looks like:
42//
43// double c, s;
44// c = cos(angle);
45// s = sin(angle);
46//
47// Will generate machine code that looks like:
48//
49// double c, s;
50// sincos(angle, &s, &c);
51//
52// Unfortunately, sincos() and friends are not part of the Android libm.so
53// library provided by the NDK for API level 9. When the optimization kicks
54// in, it makes the final build fail with a puzzling message (puzzling
55// because 'sincos' doesn't appear anywhere in the sources!).
56//
57// To solve this, we provide our own implementation of the sincos() function
58// and related friends. Note that we must also explicitely tell GCC to disable
59// optimizations when generating these. Otherwise, the generated machine code
60// for each function would simply end up calling itself, resulting in a
61// runtime crash due to stack overflow.
62//
63#if defined(__GNUC__) && !defined(__clang__)
64
65// For the record, Clang does not support the 'optimize' attribute.
66// In the unlikely event that it begins performing this optimization too,
67// we'll have to find a different way to achieve this. NOTE: Tested with O1
68// which still performs the optimization.
69//
70#define GCC_NO_OPTIMIZE __attribute__((optimize("O0")))
71
72GCC_NO_OPTIMIZE
73void sincos(double angle, double* s, double *c) {
74 *c = cos(angle);
75 *s = sin(angle);
76}
77
78GCC_NO_OPTIMIZE
79void sincosf(float angle, float* s, float* c) {
80 *c = cosf(angle);
81 *s = sinf(angle);
82}
83
84#endif // __GNUC__ && !__clang__
85
86// An implementation of mkdtemp, since it is not exposed by the NDK
87// for native API level 9 that we target.
88//
89// For any changes in the mkdtemp function, you should manually run the unittest
90// OsCompatAndroidTest.DISABLED_TestMkdTemp in your local machine to check if it
91// passes. Please don't enable it, since it creates a directory and may be
92// source of flakyness.
93char* mkdtemp(char* path) {
94 if (path == NULL) {
95 errno = EINVAL;
96 return NULL;
97 }
98
99 const int path_len = strlen(path);
100
101 // The last six characters of 'path' must be XXXXXX.
102 const base::StringPiece kSuffix("XXXXXX");
103 const int kSuffixLen = kSuffix.length();
104 if (!base::StringPiece(path, path_len).ends_with(kSuffix)) {
105 errno = EINVAL;
106 return NULL;
107 }
108
109 // If the path contains a directory, as in /tmp/foo/XXXXXXXX, make sure
110 // that /tmp/foo exists, otherwise we're going to loop a really long
111 // time for nothing below
112 char* dirsep = strrchr(path, '/');
113 if (dirsep != NULL) {
114 struct stat st;
115 int ret;
116
117 *dirsep = '\0'; // Terminating directory path temporarily
118
119 ret = stat(path, &st);
120
121 *dirsep = '/'; // Restoring directory separator
122 if (ret < 0) // Directory probably does not exist
123 return NULL;
124 if (!S_ISDIR(st.st_mode)) { // Not a directory
125 errno = ENOTDIR;
126 return NULL;
127 }
128 }
129
130 // Max number of tries using different random suffixes.
131 const int kMaxTries = 100;
132
133 // Now loop until we CAN create a directory by that name or we reach the max
134 // number of tries.
135 for (int i = 0; i < kMaxTries; ++i) {
136 // Fill the suffix XXXXXX with a random string composed of a-z chars.
137 for (int pos = 0; pos < kSuffixLen; ++pos) {
138 char rand_char = static_cast<char>(base::RandInt('a', 'z'));
139 path[path_len - kSuffixLen + pos] = rand_char;
140 }
141 if (mkdir(path, 0700) == 0) {
142 // We just created the directory succesfully.
143 return path;
144 }
145 if (errno != EEXIST) {
146 // The directory doesn't exist, but an error occured
147 return NULL;
148 }
149 }
150
151 // We reached the max number of tries.
152 return NULL;
153}
154
[email protected]f7d69972011-06-21 22:34:50155} // extern "C"