OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011, 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 // Utility class for creating a temporary directory for unit tests |
| 31 // that is deleted in the destructor. |
| 32 #ifndef GOOGLE_BREAKPAD_COMMON_TESTS_AUTO_TEMPDIR |
| 33 #define GOOGLE_BREAKPAD_COMMON_TESTS_AUTO_TEMPDIR |
| 34 |
| 35 #include <dirent.h> |
| 36 #include <sys/types.h> |
| 37 |
| 38 #include <string> |
| 39 |
| 40 #include "breakpad_googletest_includes.h" |
| 41 |
| 42 #if !defined(__ANDROID__) |
| 43 #define TEMPDIR "/tmp" |
| 44 #else |
| 45 #define TEMPDIR "/data/local/tmp" |
| 46 #endif |
| 47 |
| 48 namespace google_breakpad { |
| 49 |
| 50 class AutoTempDir { |
| 51 public: |
| 52 AutoTempDir() { |
| 53 char temp_dir[] = TEMPDIR "/breakpad.XXXXXXXXXX"; |
| 54 EXPECT_TRUE(mkdtemp(temp_dir) != NULL); |
| 55 path_.assign(temp_dir); |
| 56 } |
| 57 |
| 58 ~AutoTempDir() { |
| 59 DeleteRecursively(path_); |
| 60 } |
| 61 |
| 62 const std::string& path() { |
| 63 return path_; |
| 64 } |
| 65 |
| 66 private: |
| 67 std::string path_; |
| 68 |
| 69 void DeleteRecursively(const std::string& path) { |
| 70 // First remove any files in the dir |
| 71 DIR* dir = opendir(path.c_str()); |
| 72 if (!dir) |
| 73 return; |
| 74 |
| 75 dirent* entry; |
| 76 while ((entry = readdir(dir)) != NULL) { |
| 77 if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) |
| 78 continue; |
| 79 std::string entry_path = path + "/" + entry->d_name; |
| 80 struct stat stats; |
| 81 EXPECT_TRUE(lstat(entry_path.c_str(), &stats) == 0); |
| 82 if (S_ISDIR(stats.st_mode)) |
| 83 DeleteRecursively(entry_path); |
| 84 else |
| 85 EXPECT_TRUE(unlink(entry_path.c_str()) == 0); |
| 86 } |
| 87 EXPECT_TRUE(closedir(dir) == 0); |
| 88 EXPECT_TRUE(rmdir(path.c_str()) == 0); |
| 89 } |
| 90 |
| 91 // prevent copy construction and assignment |
| 92 AutoTempDir(const AutoTempDir&); |
| 93 AutoTempDir& operator=(const AutoTempDir&); |
| 94 }; |
| 95 |
| 96 } // namespace google_breakpad |
| 97 |
| 98 #endif // GOOGLE_BREAKPAD_COMMON_TESTS_AUTO_TEMPDIR |
OLD | NEW |