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