Delete stale chunks from safe-browsing downloads store.
[chromium-blink-merge.git] / base / os_compat_android.cc
blobd434c5bd7def635adc6789d9bb4d07ee3b95d527
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/os_compat_android.h"
7 #include <errno.h>
8 #include <math.h>
9 #include <sys/stat.h>
10 #include <time64.h>
12 #include "base/rand_util.h"
13 #include "base/string_piece.h"
14 #include "base/stringprintf.h"
16 // There is no futimes() avaiable in Bionic, so we provide our own
17 // implementation until it is there.
18 extern "C" {
20 int 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);
25 // Android has only timegm64() and no timegm().
26 // We replicate the behaviour of timegm() when the result overflows time_t.
27 time_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;
37 // 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).
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:
43 // double c, s;
44 // c = cos(angle);
45 // s = sin(angle);
47 // Will generate machine code that looks like:
49 // double c, s;
50 // sincos(angle, &s, &c);
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!).
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.
63 #if defined(__GNUC__) && !defined(__clang__)
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.
70 #define GCC_NO_OPTIMIZE __attribute__((optimize("O0")))
72 GCC_NO_OPTIMIZE
73 void sincos(double angle, double* s, double *c) {
74 *c = cos(angle);
75 *s = sin(angle);
78 GCC_NO_OPTIMIZE
79 void sincosf(float angle, float* s, float* c) {
80 *c = cosf(angle);
81 *s = sinf(angle);
84 #endif // __GNUC__ && !__clang__
86 // An implementation of mkdtemp, since it is not exposed by the NDK
87 // for native API level 9 that we target.
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.
93 char* mkdtemp(char* path) {
94 if (path == NULL) {
95 errno = EINVAL;
96 return NULL;
99 const int path_len = strlen(path);
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;
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;
117 *dirsep = '\0'; // Terminating directory path temporarily
119 ret = stat(path, &st);
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;
130 // Max number of tries using different random suffixes.
131 const int kMaxTries = 100;
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;
141 if (mkdir(path, 0700) == 0) {
142 // We just created the directory succesfully.
143 return path;
145 if (errno != EEXIST) {
146 // The directory doesn't exist, but an error occured
147 return NULL;
151 // We reached the max number of tries.
152 return NULL;
155 } // extern "C"