| OLD | NEW |
| 1 // Copyright 2015 The Crashpad Authors. All rights reserved. | 1 // Copyright 2015 The Crashpad Authors. All rights reserved. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. | 13 // limitations under the License. |
| 14 | 14 |
| 15 #include "test/scoped_temp_dir.h" | 15 #include "test/scoped_temp_dir.h" |
| 16 | 16 |
| 17 #include <dirent.h> | 17 #include <dirent.h> |
| 18 #include <stdlib.h> |
| 18 #include <string.h> | 19 #include <string.h> |
| 19 #include <unistd.h> | 20 #include <unistd.h> |
| 20 | 21 |
| 21 #include "base/logging.h" | 22 #include "base/logging.h" |
| 23 #include "build/build_config.h" |
| 22 #include "gtest/gtest.h" | 24 #include "gtest/gtest.h" |
| 23 #include "test/errors.h" | 25 #include "test/errors.h" |
| 24 | 26 |
| 25 namespace crashpad { | 27 namespace crashpad { |
| 26 namespace test { | 28 namespace test { |
| 27 | 29 |
| 28 void ScopedTempDir::Rename() { | 30 void ScopedTempDir::Rename() { |
| 29 base::FilePath move_to = CreateTemporaryDirectory(); | 31 base::FilePath move_to = CreateTemporaryDirectory(); |
| 30 PCHECK(rename(path_.value().c_str(), move_to.value().c_str()) == 0); | 32 PCHECK(rename(path_.value().c_str(), move_to.value().c_str()) == 0); |
| 31 path_ = move_to; | 33 path_ = move_to; |
| 32 } | 34 } |
| 33 | 35 |
| 34 // static | 36 // static |
| 35 base::FilePath ScopedTempDir::CreateTemporaryDirectory() { | 37 base::FilePath ScopedTempDir::CreateTemporaryDirectory() { |
| 36 char dir_template[] = "/tmp/org.chromium.crashpad.test.XXXXXX"; | 38 char* tmpdir = getenv("TMPDIR"); |
| 37 PCHECK(mkdtemp(dir_template)) << "mkdtemp " << dir_template; | 39 std::string dir; |
| 38 return base::FilePath(dir_template); | 40 if (tmpdir && tmpdir[0] != '\0') { |
| 41 dir.assign(tmpdir); |
| 42 } else { |
| 43 #if defined(OS_ANDROID) |
| 44 dir.assign("/data/local/tmp"); |
| 45 #else |
| 46 dir.assign("/tmp"); |
| 47 #endif |
| 48 } |
| 49 |
| 50 if (dir[dir.size() - 1] != '/') { |
| 51 dir.append(1, '/'); |
| 52 } |
| 53 dir.append("org.chromium.crashpad.test.XXXXXX"); |
| 54 |
| 55 PCHECK(mkdtemp(&dir[0])) << "mkdtemp " << dir; |
| 56 return base::FilePath(dir); |
| 39 } | 57 } |
| 40 | 58 |
| 41 // static | 59 // static |
| 42 void ScopedTempDir::RecursivelyDeleteTemporaryDirectory( | 60 void ScopedTempDir::RecursivelyDeleteTemporaryDirectory( |
| 43 const base::FilePath& path) { | 61 const base::FilePath& path) { |
| 44 DIR* dir = opendir(path.value().c_str()); | 62 DIR* dir = opendir(path.value().c_str()); |
| 45 ASSERT_TRUE(dir) << ErrnoMessage("opendir") << " " << path.value(); | 63 ASSERT_TRUE(dir) << ErrnoMessage("opendir") << " " << path.value(); |
| 46 | 64 |
| 47 dirent* entry; | 65 dirent* entry; |
| 48 while ((entry = readdir(dir))) { | 66 while ((entry = readdir(dir))) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 60 } | 78 } |
| 61 | 79 |
| 62 EXPECT_EQ(0, closedir(dir)) | 80 EXPECT_EQ(0, closedir(dir)) |
| 63 << ErrnoMessage("closedir") << " " << path.value(); | 81 << ErrnoMessage("closedir") << " " << path.value(); |
| 64 EXPECT_EQ(0, rmdir(path.value().c_str())) | 82 EXPECT_EQ(0, rmdir(path.value().c_str())) |
| 65 << ErrnoMessage("rmdir") << " " << path.value(); | 83 << ErrnoMessage("rmdir") << " " << path.value(); |
| 66 } | 84 } |
| 67 | 85 |
| 68 } // namespace test | 86 } // namespace test |
| 69 } // namespace crashpad | 87 } // namespace crashpad |
| OLD | NEW |