• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // mkdtemp() wasn't declared in <stdlib.h> until NDK r9b due to a simple
31 // packaging bug (the function has always been implemented in all versions
32 // of the C library). This header is provided to build Breakpad with earlier
33 // NDK revisions (e.g. the one used by Chromium). It may be removed in the
34 // future once all major projects upgrade to use a more recent NDK.
35 //
36 // The reason this is inlined here is to avoid linking a new object file
37 // into each unit test program (i.e. keep build files simple).
38 
39 #ifndef GOOGLE_BREAKPAD_COMMON_ANDROID_TESTING_MKDTEMP_H
40 #define GOOGLE_BREAKPAD_COMMON_ANDROID_TESTING_MKDTEMP_H
41 
42 #include <assert.h>
43 #include <errno.h>
44 #include <stdlib.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include <sys/stat.h>
48 
49 // Using a macro renaming trick here is necessary when building against
50 // NDK r9b. Otherwise the compiler will complain that calls to mkdtemp()
51 // are ambiguous.
52 #define mkdtemp breakpad_mkdtemp
53 
54 namespace {
55 
breakpad_mkdtemp(char * path)56 char* breakpad_mkdtemp(char* path) {
57   if (path == NULL) {
58     errno = EINVAL;
59     return NULL;
60   }
61 
62   // 'path' must be terminated with six 'X'
63   const char kSuffix[] = "XXXXXX";
64   const size_t kSuffixLen = strlen(kSuffix);
65   char* path_end = path + strlen(path);
66 
67   if (static_cast<size_t>(path_end - path) < kSuffixLen ||
68       memcmp(path_end - kSuffixLen, kSuffix, kSuffixLen) != 0) {
69     errno = EINVAL;
70     return NULL;
71   }
72 
73   // If 'path' contains a directory separator, check that it exists to
74   // avoid looping later.
75   char* sep = strrchr(path, '/');
76   if (sep != NULL) {
77     struct stat st;
78     int ret;
79     *sep = '\0';  // temporarily zero-terminate the dirname.
80     ret = stat(path, &st);
81     *sep = '/';   // restore full path.
82     if (ret < 0)
83       return NULL;
84     if (!S_ISDIR(st.st_mode)) {
85       errno = ENOTDIR;
86       return NULL;
87     }
88   }
89 
90   // Loop. On each iteration, replace the XXXXXX suffix with a random
91   // number.
92   int tries;
93   for (tries = 128; tries > 0; tries--) {
94     int random = rand() % 1000000;
95 
96     snprintf(path_end - kSuffixLen, kSuffixLen + 1, "%0d", random);
97     if (mkdir(path, 0700) == 0)
98       return path;  // Success
99 
100     if (errno != EEXIST)
101       return NULL;
102   }
103 
104   assert(errno == EEXIST);
105   return NULL;
106 }
107 
108 }  // namespace
109 
110 #endif  // GOOGLE_BREAKPAD_COMMON_ANDROID_TESTING_MKDTEMP_H
111