|
1 // Copyright (c) 2012, Google Inc. |
|
2 // All rights reserved. |
|
3 // |
|
4 // Redistribution and use in source and binary forms, with or without |
|
5 // modification, are permitted provided that the following conditions are |
|
6 // met: |
|
7 // |
|
8 // * Redistributions of source code must retain the above copyright |
|
9 // notice, this list of conditions and the following disclaimer. |
|
10 // * Redistributions in binary form must reproduce the above |
|
11 // copyright notice, this list of conditions and the following disclaimer |
|
12 // in the documentation and/or other materials provided with the |
|
13 // distribution. |
|
14 // * Neither the name of Google Inc. nor the names of its |
|
15 // contributors may be used to endorse or promote products derived from |
|
16 // this software without specific prior written permission. |
|
17 // |
|
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
29 |
|
30 // Android doesn't provide mkdtemp(). Keep this implementation in an |
|
31 // C++ anonymous namespace to avoid conflicts on Chromium (which |
|
32 // already provides an extern "C" mkdtemp function). |
|
33 // |
|
34 // The reason this is inlined here is to avoid linking a new object file |
|
35 // into each unit test program (i.e. keep build files simple). |
|
36 |
|
37 #ifndef GOOGLE_BREAKPAD_COMMON_ANDROID_TESTING_MKDTEMP_H |
|
38 #define GOOGLE_BREAKPAD_COMMON_ANDROID_TESTING_MKDTEMP_H |
|
39 |
|
40 #include <assert.h> |
|
41 #include <errno.h> |
|
42 #include <stdlib.h> |
|
43 #include <stdio.h> |
|
44 #include <string.h> |
|
45 #include <sys/stat.h> |
|
46 |
|
47 namespace { |
|
48 |
|
49 char* mkdtemp(char* path) { |
|
50 if (path == NULL) { |
|
51 errno = EINVAL; |
|
52 return NULL; |
|
53 } |
|
54 |
|
55 // 'path' must be terminated with six 'X' |
|
56 const char kSuffix[] = "XXXXXX"; |
|
57 const size_t kSuffixLen = strlen(kSuffix); |
|
58 char* path_end = path + strlen(path); |
|
59 |
|
60 if (static_cast<size_t>(path_end - path) < kSuffixLen || |
|
61 memcmp(path_end - kSuffixLen, kSuffix, kSuffixLen) != 0) { |
|
62 errno = EINVAL; |
|
63 return NULL; |
|
64 } |
|
65 |
|
66 // If 'path' contains a directory separator, check that it exists to |
|
67 // avoid looping later. |
|
68 char* sep = strrchr(path, '/'); |
|
69 if (sep != NULL) { |
|
70 struct stat st; |
|
71 int ret; |
|
72 *sep = '\0'; // temporarily zero-terminate the dirname. |
|
73 ret = stat(path, &st); |
|
74 *sep = '/'; // restore full path. |
|
75 if (ret < 0) |
|
76 return NULL; |
|
77 if (!S_ISDIR(st.st_mode)) { |
|
78 errno = ENOTDIR; |
|
79 return NULL; |
|
80 } |
|
81 } |
|
82 |
|
83 // Loop. On each iteration, replace the XXXXXX suffix with a random |
|
84 // number. |
|
85 int tries; |
|
86 for (tries = 128; tries > 0; tries--) { |
|
87 int random = rand() % 1000000; |
|
88 |
|
89 snprintf(path_end - kSuffixLen, kSuffixLen + 1, "%0d", random); |
|
90 if (mkdir(path, 0700) == 0) |
|
91 return path; // Success |
|
92 |
|
93 if (errno != EEXIST) |
|
94 return NULL; |
|
95 } |
|
96 |
|
97 assert(errno == EEXIST); |
|
98 return NULL; |
|
99 } |
|
100 |
|
101 } // namespace |
|
102 |
|
103 #endif // GOOGLE_BREAKPAD_COMMON_ANDROID_TESTING_MKDTEMP_H |