Michael J. Spencer | 3ef91c5 | 2010-11-29 22:29:04 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/Support/Path.cpp - Path tests ------------------------===// |
Michael J. Spencer | 9884778 | 2010-11-24 19:20:05 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Rafael Espindola | 3bc8e71 | 2013-06-11 22:21:28 +0000 | [diff] [blame] | 10 | #include "llvm/Support/Path.h" |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 11 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | 130cec2 | 2012-12-04 10:23:08 +0000 | [diff] [blame] | 12 | #include "llvm/Support/FileSystem.h" |
Rafael Espindola | 84ab9b3 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 13 | #include "llvm/Support/MemoryBuffer.h" |
Michael J. Spencer | 1f06360 | 2011-01-06 05:57:54 +0000 | [diff] [blame] | 14 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 9884778 | 2010-11-24 19:20:05 +0000 | [diff] [blame] | 15 | #include "gtest/gtest.h" |
| 16 | |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 17 | using namespace llvm; |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 18 | using namespace llvm::sys; |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 19 | |
Michael J. Spencer | 3b264ba | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 20 | #define ASSERT_NO_ERROR(x) \ |
Michael J. Spencer | 1f06360 | 2011-01-06 05:57:54 +0000 | [diff] [blame] | 21 | if (error_code ASSERT_NO_ERROR_ec = x) { \ |
| 22 | SmallString<128> MessageStorage; \ |
| 23 | raw_svector_ostream Message(MessageStorage); \ |
| 24 | Message << #x ": did not return errc::success.\n" \ |
| 25 | << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ |
| 26 | << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ |
| 27 | GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ |
Michael J. Spencer | 3b264ba | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 28 | } else {} |
| 29 | |
Michael J. Spencer | 9884778 | 2010-11-24 19:20:05 +0000 | [diff] [blame] | 30 | namespace { |
| 31 | |
Zhanyong Wan | 606bb1a | 2011-02-11 21:24:40 +0000 | [diff] [blame] | 32 | TEST(is_separator, Works) { |
| 33 | EXPECT_TRUE(path::is_separator('/')); |
| 34 | EXPECT_FALSE(path::is_separator('\0')); |
| 35 | EXPECT_FALSE(path::is_separator('-')); |
| 36 | EXPECT_FALSE(path::is_separator(' ')); |
| 37 | |
| 38 | #ifdef LLVM_ON_WIN32 |
| 39 | EXPECT_TRUE(path::is_separator('\\')); |
| 40 | #else |
| 41 | EXPECT_FALSE(path::is_separator('\\')); |
| 42 | #endif |
| 43 | } |
| 44 | |
Michael J. Spencer | 3ef91c5 | 2010-11-29 22:29:04 +0000 | [diff] [blame] | 45 | TEST(Support, Path) { |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 46 | SmallVector<StringRef, 40> paths; |
| 47 | paths.push_back(""); |
| 48 | paths.push_back("."); |
| 49 | paths.push_back(".."); |
| 50 | paths.push_back("foo"); |
| 51 | paths.push_back("/"); |
| 52 | paths.push_back("/foo"); |
| 53 | paths.push_back("foo/"); |
| 54 | paths.push_back("/foo/"); |
| 55 | paths.push_back("foo/bar"); |
| 56 | paths.push_back("/foo/bar"); |
| 57 | paths.push_back("//net"); |
| 58 | paths.push_back("//net/foo"); |
| 59 | paths.push_back("///foo///"); |
| 60 | paths.push_back("///foo///bar"); |
| 61 | paths.push_back("/."); |
| 62 | paths.push_back("./"); |
| 63 | paths.push_back("/.."); |
| 64 | paths.push_back("../"); |
| 65 | paths.push_back("foo/."); |
| 66 | paths.push_back("foo/.."); |
| 67 | paths.push_back("foo/./"); |
| 68 | paths.push_back("foo/./bar"); |
| 69 | paths.push_back("foo/.."); |
| 70 | paths.push_back("foo/../"); |
| 71 | paths.push_back("foo/../bar"); |
| 72 | paths.push_back("c:"); |
| 73 | paths.push_back("c:/"); |
| 74 | paths.push_back("c:foo"); |
| 75 | paths.push_back("c:/foo"); |
| 76 | paths.push_back("c:foo/"); |
| 77 | paths.push_back("c:/foo/"); |
| 78 | paths.push_back("c:/foo/bar"); |
| 79 | paths.push_back("prn:"); |
| 80 | paths.push_back("c:\\"); |
| 81 | paths.push_back("c:foo"); |
| 82 | paths.push_back("c:\\foo"); |
| 83 | paths.push_back("c:foo\\"); |
| 84 | paths.push_back("c:\\foo\\"); |
| 85 | paths.push_back("c:\\foo/"); |
| 86 | paths.push_back("c:/foo\\bar"); |
| 87 | |
| 88 | for (SmallVector<StringRef, 40>::const_iterator i = paths.begin(), |
| 89 | e = paths.end(); |
| 90 | i != e; |
| 91 | ++i) { |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 92 | for (sys::path::const_iterator ci = sys::path::begin(*i), |
| 93 | ce = sys::path::end(*i); |
| 94 | ci != ce; |
| 95 | ++ci) { |
Michael J. Spencer | b5ca644 | 2010-12-03 02:22:34 +0000 | [diff] [blame] | 96 | ASSERT_FALSE(ci->empty()); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 97 | } |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 98 | |
Michael J. Spencer | 75942f9 | 2010-12-01 22:28:42 +0000 | [diff] [blame] | 99 | #if 0 // Valgrind is whining about this. |
Michael J. Spencer | 545cbdf | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 100 | outs() << " Reverse Iteration: ["; |
| 101 | for (sys::path::reverse_iterator ci = sys::path::rbegin(*i), |
| 102 | ce = sys::path::rend(*i); |
| 103 | ci != ce; |
| 104 | ++ci) { |
| 105 | outs() << *ci << ','; |
| 106 | } |
| 107 | outs() << "]\n"; |
Michael J. Spencer | 75942f9 | 2010-12-01 22:28:42 +0000 | [diff] [blame] | 108 | #endif |
Michael J. Spencer | 545cbdf | 2010-11-30 23:28:07 +0000 | [diff] [blame] | 109 | |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 110 | path::has_root_path(*i); |
| 111 | path::root_path(*i); |
| 112 | path::has_root_name(*i); |
| 113 | path::root_name(*i); |
| 114 | path::has_root_directory(*i); |
| 115 | path::root_directory(*i); |
| 116 | path::has_parent_path(*i); |
| 117 | path::parent_path(*i); |
| 118 | path::has_filename(*i); |
| 119 | path::filename(*i); |
| 120 | path::has_stem(*i); |
| 121 | path::stem(*i); |
| 122 | path::has_extension(*i); |
| 123 | path::extension(*i); |
| 124 | path::is_absolute(*i); |
| 125 | path::is_relative(*i); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 126 | |
Michael J. Spencer | 3b264ba | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 127 | SmallString<128> temp_store; |
Michael J. Spencer | b5ca644 | 2010-12-03 02:22:34 +0000 | [diff] [blame] | 128 | temp_store = *i; |
Michael J. Spencer | 3b264ba | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 129 | ASSERT_NO_ERROR(fs::make_absolute(temp_store)); |
Michael J. Spencer | b5ca644 | 2010-12-03 02:22:34 +0000 | [diff] [blame] | 130 | temp_store = *i; |
Michael J. Spencer | 1e090f0 | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 131 | path::remove_filename(temp_store); |
Michael J. Spencer | 4d0c6fd | 2010-12-01 06:03:33 +0000 | [diff] [blame] | 132 | |
Michael J. Spencer | b5ca644 | 2010-12-03 02:22:34 +0000 | [diff] [blame] | 133 | temp_store = *i; |
Michael J. Spencer | 1e090f0 | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 134 | path::replace_extension(temp_store, "ext"); |
Michael J. Spencer | 4d0c6fd | 2010-12-01 06:03:33 +0000 | [diff] [blame] | 135 | StringRef filename(temp_store.begin(), temp_store.size()), stem, ext; |
Michael J. Spencer | f616b21 | 2010-12-07 17:04:04 +0000 | [diff] [blame] | 136 | stem = path::stem(filename); |
| 137 | ext = path::extension(filename); |
Michael J. Spencer | 4d0c6fd | 2010-12-01 06:03:33 +0000 | [diff] [blame] | 138 | EXPECT_EQ(*(--sys::path::end(filename)), (stem + ext).str()); |
| 139 | |
Michael J. Spencer | 1e090f0 | 2010-12-07 03:57:37 +0000 | [diff] [blame] | 140 | path::native(*i, temp_store); |
Michael J. Spencer | ebad2f9 | 2010-11-29 22:28:51 +0000 | [diff] [blame] | 141 | } |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 142 | } |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 143 | |
Tareq A. Siraj | 73537ea | 2013-08-12 17:10:49 +0000 | [diff] [blame] | 144 | TEST(Support, RelativePathIterator) { |
| 145 | SmallString<64> Path(StringRef("c/d/e/foo.txt")); |
| 146 | typedef SmallVector<StringRef, 4> PathComponents; |
| 147 | PathComponents ExpectedPathComponents; |
| 148 | PathComponents ActualPathComponents; |
| 149 | |
| 150 | StringRef(Path).split(ExpectedPathComponents, "/"); |
| 151 | |
| 152 | for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; |
| 153 | ++I) { |
| 154 | ActualPathComponents.push_back(*I); |
| 155 | } |
| 156 | |
| 157 | ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); |
| 158 | |
| 159 | for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) { |
| 160 | EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str()); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | TEST(Support, AbsolutePathIterator) { |
| 165 | SmallString<64> Path(StringRef("/c/d/e/foo.txt")); |
| 166 | typedef SmallVector<StringRef, 4> PathComponents; |
| 167 | PathComponents ExpectedPathComponents; |
| 168 | PathComponents ActualPathComponents; |
| 169 | |
| 170 | StringRef(Path).split(ExpectedPathComponents, "/"); |
| 171 | |
| 172 | // The root path will also be a component when iterating |
| 173 | ExpectedPathComponents[0] = "/"; |
| 174 | |
| 175 | for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; |
| 176 | ++I) { |
| 177 | ActualPathComponents.push_back(*I); |
| 178 | } |
| 179 | |
| 180 | ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); |
| 181 | |
| 182 | for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) { |
| 183 | EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str()); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | #ifdef LLVM_ON_WIN32 |
| 188 | TEST(Support, AbsolutePathIteratorWin32) { |
| 189 | SmallString<64> Path(StringRef("c:\\c\\e\\foo.txt")); |
| 190 | typedef SmallVector<StringRef, 4> PathComponents; |
| 191 | PathComponents ExpectedPathComponents; |
| 192 | PathComponents ActualPathComponents; |
| 193 | |
| 194 | StringRef(Path).split(ExpectedPathComponents, "\\"); |
| 195 | |
| 196 | // The root path (which comes after the drive name) will also be a component |
| 197 | // when iterating. |
| 198 | ExpectedPathComponents.insert(ExpectedPathComponents.begin()+1, "\\"); |
| 199 | |
| 200 | for (path::const_iterator I = path::begin(Path), E = path::end(Path); I != E; |
| 201 | ++I) { |
| 202 | ActualPathComponents.push_back(*I); |
| 203 | } |
| 204 | |
| 205 | ASSERT_EQ(ExpectedPathComponents.size(), ActualPathComponents.size()); |
| 206 | |
| 207 | for (size_t i = 0; i <ExpectedPathComponents.size(); ++i) { |
| 208 | EXPECT_EQ(ExpectedPathComponents[i].str(), ActualPathComponents[i].str()); |
| 209 | } |
| 210 | } |
| 211 | #endif // LLVM_ON_WIN32 |
| 212 | |
Ben Langmuir | 8d11639 | 2014-03-05 19:56:30 +0000 | [diff] [blame] | 213 | TEST(Support, AbsolutePathIteratorEnd) { |
| 214 | // Trailing slashes are converted to '.' unless they are part of the root path. |
| 215 | SmallVector<StringRef, 4> Paths; |
| 216 | Paths.push_back("/foo/"); |
| 217 | Paths.push_back("/foo//"); |
| 218 | Paths.push_back("//net//"); |
| 219 | #ifdef LLVM_ON_WIN32 |
| 220 | Paths.push_back("c:\\\\"); |
| 221 | #endif |
| 222 | |
| 223 | for (StringRef Path : Paths) { |
| 224 | StringRef LastComponent = *--path::end(Path); |
| 225 | EXPECT_EQ(".", LastComponent); |
| 226 | } |
| 227 | |
| 228 | SmallVector<StringRef, 3> RootPaths; |
| 229 | RootPaths.push_back("/"); |
| 230 | RootPaths.push_back("//net/"); |
| 231 | #ifdef LLVM_ON_WIN32 |
| 232 | RootPaths.push_back("c:\\"); |
| 233 | #endif |
| 234 | |
| 235 | for (StringRef Path : RootPaths) { |
| 236 | StringRef LastComponent = *--path::end(Path); |
| 237 | EXPECT_EQ(1u, LastComponent.size()); |
| 238 | EXPECT_TRUE(path::is_separator(LastComponent[0])); |
| 239 | } |
| 240 | } |
| 241 | |
Peter Collingbourne | f7d4101 | 2014-01-31 23:46:06 +0000 | [diff] [blame] | 242 | TEST(Support, HomeDirectory) { |
| 243 | #ifdef LLVM_ON_UNIX |
| 244 | // This test only makes sense on Unix if $HOME is set. |
| 245 | if (::getenv("HOME")) { |
| 246 | #endif |
| 247 | SmallString<128> HomeDir; |
| 248 | EXPECT_TRUE(path::home_directory(HomeDir)); |
| 249 | EXPECT_FALSE(HomeDir.empty()); |
| 250 | #ifdef LLVM_ON_UNIX |
| 251 | } |
| 252 | #endif |
| 253 | } |
| 254 | |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 255 | class FileSystemTest : public testing::Test { |
| 256 | protected: |
| 257 | /// Unique temporary directory in which all created filesystem entities must |
| 258 | /// be placed. It is recursively removed at the end of each test. |
| 259 | SmallString<128> TestDirectory; |
| 260 | |
| 261 | virtual void SetUp() { |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 262 | ASSERT_NO_ERROR( |
Rafael Espindola | 7ffacc4 | 2013-06-27 03:45:31 +0000 | [diff] [blame] | 263 | fs::createUniqueDirectory("file-system-test", TestDirectory)); |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 264 | // We don't care about this specific file. |
Michael J. Spencer | 42368cc | 2011-01-05 16:39:46 +0000 | [diff] [blame] | 265 | errs() << "Test Directory: " << TestDirectory << '\n'; |
| 266 | errs().flush(); |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | virtual void TearDown() { |
Rafael Espindola | 78dcc03 | 2014-01-10 20:36:42 +0000 | [diff] [blame] | 270 | ASSERT_NO_ERROR(fs::remove(TestDirectory.str())); |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 271 | } |
| 272 | }; |
| 273 | |
Aaron Ballman | 9fbe530 | 2013-06-19 21:03:50 +0000 | [diff] [blame] | 274 | TEST_F(FileSystemTest, Unique) { |
| 275 | // Create a temp file. |
| 276 | int FileDescriptor; |
| 277 | SmallString<64> TempPath; |
| 278 | ASSERT_NO_ERROR( |
Rafael Espindola | 155cf0f | 2013-07-05 20:14:52 +0000 | [diff] [blame] | 279 | fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); |
Aaron Ballman | 9fbe530 | 2013-06-19 21:03:50 +0000 | [diff] [blame] | 280 | |
| 281 | // The same file should return an identical unique id. |
Rafael Espindola | 7f822a9 | 2013-07-29 21:26:49 +0000 | [diff] [blame] | 282 | fs::UniqueID F1, F2; |
Rafael Espindola | 7cf7c51 | 2013-06-20 15:06:35 +0000 | [diff] [blame] | 283 | ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F1)); |
| 284 | ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), F2)); |
Aaron Ballman | 9fbe530 | 2013-06-19 21:03:50 +0000 | [diff] [blame] | 285 | ASSERT_EQ(F1, F2); |
| 286 | |
| 287 | // Different files should return different unique ids. |
| 288 | int FileDescriptor2; |
| 289 | SmallString<64> TempPath2; |
| 290 | ASSERT_NO_ERROR( |
Rafael Espindola | 155cf0f | 2013-07-05 20:14:52 +0000 | [diff] [blame] | 291 | fs::createTemporaryFile("prefix", "temp", FileDescriptor2, TempPath2)); |
| 292 | |
Rafael Espindola | 7f822a9 | 2013-07-29 21:26:49 +0000 | [diff] [blame] | 293 | fs::UniqueID D; |
Rafael Espindola | 7cf7c51 | 2013-06-20 15:06:35 +0000 | [diff] [blame] | 294 | ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D)); |
Aaron Ballman | 9fbe530 | 2013-06-19 21:03:50 +0000 | [diff] [blame] | 295 | ASSERT_NE(D, F1); |
| 296 | ::close(FileDescriptor2); |
| 297 | |
| 298 | ASSERT_NO_ERROR(fs::remove(Twine(TempPath2))); |
| 299 | |
| 300 | // Two paths representing the same file on disk should still provide the |
| 301 | // same unique id. We can test this by making a hard link. |
| 302 | ASSERT_NO_ERROR(fs::create_hard_link(Twine(TempPath), Twine(TempPath2))); |
Rafael Espindola | 7f822a9 | 2013-07-29 21:26:49 +0000 | [diff] [blame] | 303 | fs::UniqueID D2; |
Rafael Espindola | 7cf7c51 | 2013-06-20 15:06:35 +0000 | [diff] [blame] | 304 | ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D2)); |
Aaron Ballman | 9fbe530 | 2013-06-19 21:03:50 +0000 | [diff] [blame] | 305 | ASSERT_EQ(D2, F1); |
| 306 | |
| 307 | ::close(FileDescriptor); |
Rafael Espindola | a5932af | 2013-07-30 20:25:53 +0000 | [diff] [blame] | 308 | |
| 309 | SmallString<128> Dir1; |
| 310 | ASSERT_NO_ERROR( |
| 311 | fs::createUniqueDirectory("dir1", Dir1)); |
| 312 | ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F1)); |
| 313 | ASSERT_NO_ERROR(fs::getUniqueID(Dir1.c_str(), F2)); |
| 314 | ASSERT_EQ(F1, F2); |
| 315 | |
| 316 | SmallString<128> Dir2; |
| 317 | ASSERT_NO_ERROR( |
| 318 | fs::createUniqueDirectory("dir2", Dir2)); |
| 319 | ASSERT_NO_ERROR(fs::getUniqueID(Dir2.c_str(), F2)); |
| 320 | ASSERT_NE(F1, F2); |
Aaron Ballman | 9fbe530 | 2013-06-19 21:03:50 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 323 | TEST_F(FileSystemTest, TempFiles) { |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 324 | // Create a temp file. |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 325 | int FileDescriptor; |
| 326 | SmallString<64> TempPath; |
Michael J. Spencer | 3b264ba | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 327 | ASSERT_NO_ERROR( |
Rafael Espindola | 155cf0f | 2013-07-05 20:14:52 +0000 | [diff] [blame] | 328 | fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 329 | |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 330 | // Make sure it exists. |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 331 | bool TempFileExists; |
Michael J. Spencer | 3b264ba | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 332 | ASSERT_NO_ERROR(sys::fs::exists(Twine(TempPath), TempFileExists)); |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 333 | EXPECT_TRUE(TempFileExists); |
| 334 | |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 335 | // Create another temp tile. |
| 336 | int FD2; |
| 337 | SmallString<64> TempPath2; |
Rafael Espindola | 155cf0f | 2013-07-05 20:14:52 +0000 | [diff] [blame] | 338 | ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD2, TempPath2)); |
Rafael Espindola | d3c8904 | 2013-07-25 15:00:17 +0000 | [diff] [blame] | 339 | ASSERT_TRUE(TempPath2.endswith(".temp")); |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 340 | ASSERT_NE(TempPath.str(), TempPath2.str()); |
| 341 | |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 342 | fs::file_status A, B; |
| 343 | ASSERT_NO_ERROR(fs::status(Twine(TempPath), A)); |
| 344 | ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B)); |
| 345 | EXPECT_FALSE(fs::equivalent(A, B)); |
| 346 | |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 347 | ::close(FD2); |
Rafael Espindola | 213c4cb | 2013-07-18 03:29:51 +0000 | [diff] [blame] | 348 | |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 349 | // Remove Temp2. |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 350 | ASSERT_NO_ERROR(fs::remove(Twine(TempPath2))); |
| 351 | ASSERT_NO_ERROR(fs::remove(Twine(TempPath2))); |
| 352 | ASSERT_EQ(fs::remove(Twine(TempPath2), false), |
| 353 | errc::no_such_file_or_directory); |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 354 | |
Rafael Espindola | 107b74c | 2013-07-31 00:10:25 +0000 | [diff] [blame] | 355 | error_code EC = fs::status(TempPath2.c_str(), B); |
| 356 | EXPECT_EQ(EC, errc::no_such_file_or_directory); |
| 357 | EXPECT_EQ(B.type(), fs::file_type::file_not_found); |
| 358 | |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 359 | // Make sure Temp2 doesn't exist. |
Michael J. Spencer | 3b264ba | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 360 | ASSERT_NO_ERROR(fs::exists(Twine(TempPath2), TempFileExists)); |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 361 | EXPECT_FALSE(TempFileExists); |
| 362 | |
Rafael Espindola | d3c8904 | 2013-07-25 15:00:17 +0000 | [diff] [blame] | 363 | SmallString<64> TempPath3; |
| 364 | ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "", TempPath3)); |
| 365 | ASSERT_FALSE(TempPath3.endswith(".")); |
| 366 | |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 367 | // Create a hard link to Temp1. |
Michael J. Spencer | 3b264ba | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 368 | ASSERT_NO_ERROR(fs::create_hard_link(Twine(TempPath), Twine(TempPath2))); |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 369 | bool equal; |
Michael J. Spencer | 3b264ba | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 370 | ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal)); |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 371 | EXPECT_TRUE(equal); |
Michael J. Spencer | 203d780 | 2011-12-12 06:04:28 +0000 | [diff] [blame] | 372 | ASSERT_NO_ERROR(fs::status(Twine(TempPath), A)); |
| 373 | ASSERT_NO_ERROR(fs::status(Twine(TempPath2), B)); |
| 374 | EXPECT_TRUE(fs::equivalent(A, B)); |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 375 | |
| 376 | // Remove Temp1. |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 377 | ::close(FileDescriptor); |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 378 | ASSERT_NO_ERROR(fs::remove(Twine(TempPath))); |
Michael J. Spencer | 4571040 | 2010-12-03 01:21:28 +0000 | [diff] [blame] | 379 | |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 380 | // Remove the hard link. |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 381 | ASSERT_NO_ERROR(fs::remove(Twine(TempPath2))); |
Michael J. Spencer | 4fb115d | 2010-12-04 03:18:42 +0000 | [diff] [blame] | 382 | |
| 383 | // Make sure Temp1 doesn't exist. |
Michael J. Spencer | 3b264ba | 2011-01-04 17:00:18 +0000 | [diff] [blame] | 384 | ASSERT_NO_ERROR(fs::exists(Twine(TempPath), TempFileExists)); |
Benjamin Kramer | 23e6bdf | 2010-12-03 12:33:32 +0000 | [diff] [blame] | 385 | EXPECT_FALSE(TempFileExists); |
Aaron Ballman | fcdf9a8 | 2013-03-16 15:00:51 +0000 | [diff] [blame] | 386 | |
| 387 | #ifdef LLVM_ON_WIN32 |
| 388 | // Path name > 260 chars should get an error. |
| 389 | const char *Path270 = |
| 390 | "abcdefghijklmnopqrstuvwxyz9abcdefghijklmnopqrstuvwxyz8" |
| 391 | "abcdefghijklmnopqrstuvwxyz7abcdefghijklmnopqrstuvwxyz6" |
| 392 | "abcdefghijklmnopqrstuvwxyz5abcdefghijklmnopqrstuvwxyz4" |
| 393 | "abcdefghijklmnopqrstuvwxyz3abcdefghijklmnopqrstuvwxyz2" |
| 394 | "abcdefghijklmnopqrstuvwxyz1abcdefghijklmnopqrstuvwxyz0"; |
Rafael Espindola | 2eb1ae4 | 2013-07-05 22:32:33 +0000 | [diff] [blame] | 395 | EXPECT_EQ(fs::createUniqueFile(Twine(Path270), FileDescriptor, TempPath), |
Aaron Ballman | fcdf9a8 | 2013-03-16 15:00:51 +0000 | [diff] [blame] | 396 | windows_error::path_not_found); |
| 397 | #endif |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 398 | } |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 399 | |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 400 | TEST_F(FileSystemTest, CreateDir) { |
| 401 | ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo")); |
| 402 | ASSERT_NO_ERROR(fs::create_directory(Twine(TestDirectory) + "foo")); |
| 403 | ASSERT_EQ(fs::create_directory(Twine(TestDirectory) + "foo", false), |
| 404 | errc::file_exists); |
| 405 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "foo")); |
| 406 | } |
| 407 | |
Michael J. Spencer | 346a133 | 2011-01-05 16:39:05 +0000 | [diff] [blame] | 408 | TEST_F(FileSystemTest, DirectoryIteration) { |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 409 | error_code ec; |
Michael J. Spencer | 1f06360 | 2011-01-06 05:57:54 +0000 | [diff] [blame] | 410 | for (fs::directory_iterator i(".", ec), e; i != e; i.increment(ec)) |
| 411 | ASSERT_NO_ERROR(ec); |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 412 | |
| 413 | // Create a known hierarchy to recurse over. |
Rafael Espindola | 5c20ac0 | 2014-02-23 13:56:14 +0000 | [diff] [blame] | 414 | ASSERT_NO_ERROR( |
| 415 | fs::create_directories(Twine(TestDirectory) + "/recursive/a0/aa1")); |
| 416 | ASSERT_NO_ERROR( |
| 417 | fs::create_directories(Twine(TestDirectory) + "/recursive/a0/ab1")); |
| 418 | ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory) + |
| 419 | "/recursive/dontlookhere/da1")); |
| 420 | ASSERT_NO_ERROR( |
| 421 | fs::create_directories(Twine(TestDirectory) + "/recursive/z0/za1")); |
| 422 | ASSERT_NO_ERROR( |
| 423 | fs::create_directories(Twine(TestDirectory) + "/recursive/pop/p1")); |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 424 | typedef std::vector<std::string> v_t; |
| 425 | v_t visited; |
| 426 | for (fs::recursive_directory_iterator i(Twine(TestDirectory) |
| 427 | + "/recursive", ec), e; i != e; i.increment(ec)){ |
| 428 | ASSERT_NO_ERROR(ec); |
NAKAMURA Takumi | 0cfb367 | 2011-12-09 23:20:03 +0000 | [diff] [blame] | 429 | if (path::filename(i->path()) == "p1") { |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 430 | i.pop(); |
NAKAMURA Takumi | 0cfb367 | 2011-12-09 23:20:03 +0000 | [diff] [blame] | 431 | // FIXME: recursive_directory_iterator should be more robust. |
| 432 | if (i == e) break; |
| 433 | } |
Michael J. Spencer | 9b54a3e | 2011-12-09 01:14:41 +0000 | [diff] [blame] | 434 | if (path::filename(i->path()) == "dontlookhere") |
| 435 | i.no_push(); |
Michael J. Spencer | 0a7625d | 2011-12-08 22:50:09 +0000 | [diff] [blame] | 436 | visited.push_back(path::filename(i->path())); |
| 437 | } |
| 438 | v_t::const_iterator a0 = std::find(visited.begin(), visited.end(), "a0"); |
| 439 | v_t::const_iterator aa1 = std::find(visited.begin(), visited.end(), "aa1"); |
| 440 | v_t::const_iterator ab1 = std::find(visited.begin(), visited.end(), "ab1"); |
| 441 | v_t::const_iterator dontlookhere = std::find(visited.begin(), visited.end(), |
| 442 | "dontlookhere"); |
| 443 | v_t::const_iterator da1 = std::find(visited.begin(), visited.end(), "da1"); |
| 444 | v_t::const_iterator z0 = std::find(visited.begin(), visited.end(), "z0"); |
| 445 | v_t::const_iterator za1 = std::find(visited.begin(), visited.end(), "za1"); |
| 446 | v_t::const_iterator pop = std::find(visited.begin(), visited.end(), "pop"); |
| 447 | v_t::const_iterator p1 = std::find(visited.begin(), visited.end(), "p1"); |
| 448 | |
| 449 | // Make sure that each path was visited correctly. |
| 450 | ASSERT_NE(a0, visited.end()); |
| 451 | ASSERT_NE(aa1, visited.end()); |
| 452 | ASSERT_NE(ab1, visited.end()); |
| 453 | ASSERT_NE(dontlookhere, visited.end()); |
| 454 | ASSERT_EQ(da1, visited.end()); // Not visited. |
| 455 | ASSERT_NE(z0, visited.end()); |
| 456 | ASSERT_NE(za1, visited.end()); |
| 457 | ASSERT_NE(pop, visited.end()); |
| 458 | ASSERT_EQ(p1, visited.end()); // Not visited. |
| 459 | |
| 460 | // Make sure that parents were visited before children. No other ordering |
| 461 | // guarantees can be made across siblings. |
| 462 | ASSERT_LT(a0, aa1); |
| 463 | ASSERT_LT(a0, ab1); |
| 464 | ASSERT_LT(z0, za1); |
Rafael Espindola | 78dcc03 | 2014-01-10 20:36:42 +0000 | [diff] [blame] | 465 | |
| 466 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/aa1")); |
| 467 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0/ab1")); |
| 468 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/a0")); |
| 469 | ASSERT_NO_ERROR( |
| 470 | fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere/da1")); |
| 471 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/dontlookhere")); |
| 472 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop/p1")); |
| 473 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/pop")); |
| 474 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0/za1")); |
| 475 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive/z0")); |
| 476 | ASSERT_NO_ERROR(fs::remove(Twine(TestDirectory) + "/recursive")); |
Michael J. Spencer | 1f06360 | 2011-01-06 05:57:54 +0000 | [diff] [blame] | 477 | } |
Michael J. Spencer | 1d3c4a7 | 2011-01-06 05:58:02 +0000 | [diff] [blame] | 478 | |
Rui Ueyama | 5e3de7a | 2013-11-13 21:55:41 +0000 | [diff] [blame] | 479 | const char archive[] = "!<arch>\x0A"; |
| 480 | const char bitcode[] = "\xde\xc0\x17\x0b"; |
Rui Ueyama | 829c439 | 2013-11-14 22:09:08 +0000 | [diff] [blame] | 481 | const char coff_object[] = "\x00\x00......"; |
Rui Ueyama | e448f9e | 2013-11-15 21:22:02 +0000 | [diff] [blame] | 482 | const char coff_import_library[] = "\x00\x00\xff\xff...."; |
Rui Ueyama | 5e3de7a | 2013-11-13 21:55:41 +0000 | [diff] [blame] | 483 | const char elf_relocatable[] = { 0x7f, 'E', 'L', 'F', 1, 2, 1, 0, 0, |
| 484 | 0, 0, 0, 0, 0, 0, 0, 0, 1 }; |
| 485 | const char macho_universal_binary[] = "\xca\xfe\xba\xbe...\0x00"; |
| 486 | const char macho_object[] = "\xfe\xed\xfa\xce..........\x00\x01"; |
| 487 | const char macho_executable[] = "\xfe\xed\xfa\xce..........\x00\x02"; |
| 488 | const char macho_fixed_virtual_memory_shared_lib[] = |
| 489 | "\xfe\xed\xfa\xce..........\x00\x03"; |
| 490 | const char macho_core[] = "\xfe\xed\xfa\xce..........\x00\x04"; |
| 491 | const char macho_preload_executable[] = "\xfe\xed\xfa\xce..........\x00\x05"; |
| 492 | const char macho_dynamically_linked_shared_lib[] = |
| 493 | "\xfe\xed\xfa\xce..........\x00\x06"; |
| 494 | const char macho_dynamic_linker[] = "\xfe\xed\xfa\xce..........\x00\x07"; |
| 495 | const char macho_bundle[] = "\xfe\xed\xfa\xce..........\x00\x08"; |
| 496 | const char macho_dsym_companion[] = "\xfe\xed\xfa\xce..........\x00\x0a"; |
| 497 | const char windows_resource[] = "\x00\x00\x00\x00\x020\x00\x00\x00\xff"; |
Michael J. Spencer | b8055cb | 2013-04-05 20:10:04 +0000 | [diff] [blame] | 498 | |
Michael J. Spencer | 1d3c4a7 | 2011-01-06 05:58:02 +0000 | [diff] [blame] | 499 | TEST_F(FileSystemTest, Magic) { |
| 500 | struct type { |
| 501 | const char *filename; |
| 502 | const char *magic_str; |
Michael J. Spencer | b8055cb | 2013-04-05 20:10:04 +0000 | [diff] [blame] | 503 | size_t magic_str_len; |
| 504 | fs::file_magic magic; |
Rui Ueyama | 5e3de7a | 2013-11-13 21:55:41 +0000 | [diff] [blame] | 505 | } types[] = { |
| 506 | #define DEFINE(magic) \ |
| 507 | { #magic, magic, sizeof(magic), fs::file_magic::magic } |
| 508 | DEFINE(archive), |
| 509 | DEFINE(bitcode), |
Rui Ueyama | 829c439 | 2013-11-14 22:09:08 +0000 | [diff] [blame] | 510 | DEFINE(coff_object), |
Rui Ueyama | e448f9e | 2013-11-15 21:22:02 +0000 | [diff] [blame] | 511 | DEFINE(coff_import_library), |
Rui Ueyama | 5e3de7a | 2013-11-13 21:55:41 +0000 | [diff] [blame] | 512 | DEFINE(elf_relocatable), |
| 513 | DEFINE(macho_universal_binary), |
| 514 | DEFINE(macho_object), |
| 515 | DEFINE(macho_executable), |
| 516 | DEFINE(macho_fixed_virtual_memory_shared_lib), |
| 517 | DEFINE(macho_core), |
| 518 | DEFINE(macho_preload_executable), |
| 519 | DEFINE(macho_dynamically_linked_shared_lib), |
| 520 | DEFINE(macho_dynamic_linker), |
| 521 | DEFINE(macho_bundle), |
| 522 | DEFINE(macho_dsym_companion), |
| 523 | DEFINE(windows_resource) |
| 524 | #undef DEFINE |
| 525 | }; |
Michael J. Spencer | 1d3c4a7 | 2011-01-06 05:58:02 +0000 | [diff] [blame] | 526 | |
| 527 | // Create some files filled with magic. |
| 528 | for (type *i = types, *e = types + (sizeof(types) / sizeof(type)); i != e; |
| 529 | ++i) { |
| 530 | SmallString<128> file_pathname(TestDirectory); |
| 531 | path::append(file_pathname, i->filename); |
| 532 | std::string ErrMsg; |
Rafael Espindola | 90c7f1c | 2014-02-24 18:20:12 +0000 | [diff] [blame] | 533 | raw_fd_ostream file(file_pathname.c_str(), ErrMsg, sys::fs::F_None); |
Michael J. Spencer | 1d3c4a7 | 2011-01-06 05:58:02 +0000 | [diff] [blame] | 534 | ASSERT_FALSE(file.has_error()); |
| 535 | StringRef magic(i->magic_str, i->magic_str_len); |
| 536 | file << magic; |
Michael J. Spencer | b587180 | 2011-01-15 18:52:49 +0000 | [diff] [blame] | 537 | file.close(); |
Michael J. Spencer | 1d3c4a7 | 2011-01-06 05:58:02 +0000 | [diff] [blame] | 538 | bool res = false; |
| 539 | ASSERT_NO_ERROR(fs::has_magic(file_pathname.c_str(), magic, res)); |
| 540 | EXPECT_TRUE(res); |
Michael J. Spencer | b8055cb | 2013-04-05 20:10:04 +0000 | [diff] [blame] | 541 | EXPECT_EQ(i->magic, fs::identify_magic(magic)); |
Rafael Espindola | 78dcc03 | 2014-01-10 20:36:42 +0000 | [diff] [blame] | 542 | ASSERT_NO_ERROR(fs::remove(Twine(file_pathname))); |
Michael J. Spencer | 7ecd94c | 2010-12-06 04:28:42 +0000 | [diff] [blame] | 543 | } |
Michael J. Spencer | 9884778 | 2010-11-24 19:20:05 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Rafael Espindola | 84ab9b3 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 546 | #ifdef LLVM_ON_WIN32 |
| 547 | TEST_F(FileSystemTest, CarriageReturn) { |
| 548 | SmallString<128> FilePathname(TestDirectory); |
| 549 | std::string ErrMsg; |
| 550 | path::append(FilePathname, "test"); |
| 551 | |
| 552 | { |
Rafael Espindola | 90c7f1c | 2014-02-24 18:20:12 +0000 | [diff] [blame] | 553 | raw_fd_ostream File(FilePathname.c_str(), ErrMsg, sys::fs::F_Text); |
Rafael Espindola | 84ab9b3 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 554 | EXPECT_EQ(ErrMsg, ""); |
| 555 | File << '\n'; |
| 556 | } |
| 557 | { |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 558 | std::unique_ptr<MemoryBuffer> Buf; |
Rafael Espindola | e5bf246 | 2013-10-25 19:47:55 +0000 | [diff] [blame] | 559 | MemoryBuffer::getFile(FilePathname.c_str(), Buf); |
Rafael Espindola | 84ab9b3 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 560 | EXPECT_EQ(Buf->getBuffer(), "\r\n"); |
| 561 | } |
| 562 | |
| 563 | { |
Rafael Espindola | 90c7f1c | 2014-02-24 18:20:12 +0000 | [diff] [blame] | 564 | raw_fd_ostream File(FilePathname.c_str(), ErrMsg, sys::fs::F_None); |
Rafael Espindola | 84ab9b3 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 565 | EXPECT_EQ(ErrMsg, ""); |
| 566 | File << '\n'; |
| 567 | } |
| 568 | { |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 569 | std::unique_ptr<MemoryBuffer> Buf; |
Rafael Espindola | e5bf246 | 2013-10-25 19:47:55 +0000 | [diff] [blame] | 570 | MemoryBuffer::getFile(FilePathname.c_str(), Buf); |
Rafael Espindola | 84ab9b3 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 571 | EXPECT_EQ(Buf->getBuffer(), "\n"); |
| 572 | } |
Rafael Espindola | 78dcc03 | 2014-01-10 20:36:42 +0000 | [diff] [blame] | 573 | ASSERT_NO_ERROR(fs::remove(Twine(FilePathname))); |
Rafael Espindola | 84ab9b3 | 2013-07-19 14:41:25 +0000 | [diff] [blame] | 574 | } |
| 575 | #endif |
| 576 | |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 577 | TEST_F(FileSystemTest, FileMapping) { |
| 578 | // Create a temp file. |
| 579 | int FileDescriptor; |
| 580 | SmallString<64> TempPath; |
| 581 | ASSERT_NO_ERROR( |
Rafael Espindola | 155cf0f | 2013-07-05 20:14:52 +0000 | [diff] [blame] | 582 | fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath)); |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 583 | // Map in temp file and add some content |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 584 | error_code EC; |
| 585 | StringRef Val("hello there"); |
| 586 | { |
| 587 | fs::mapped_file_region mfr(FileDescriptor, |
Michael J. Spencer | 01ac34e | 2013-03-14 00:33:37 +0000 | [diff] [blame] | 588 | true, |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 589 | fs::mapped_file_region::readwrite, |
| 590 | 4096, |
| 591 | 0, |
| 592 | EC); |
| 593 | ASSERT_NO_ERROR(EC); |
| 594 | std::copy(Val.begin(), Val.end(), mfr.data()); |
| 595 | // Explicitly add a 0. |
| 596 | mfr.data()[Val.size()] = 0; |
| 597 | // Unmap temp file |
| 598 | } |
Rui Ueyama | 89d1bdb | 2013-11-13 20:31:21 +0000 | [diff] [blame] | 599 | |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 600 | // Map it back in read-only |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 601 | fs::mapped_file_region mfr(Twine(TempPath), |
| 602 | fs::mapped_file_region::readonly, |
| 603 | 0, |
| 604 | 0, |
| 605 | EC); |
| 606 | ASSERT_NO_ERROR(EC); |
Rui Ueyama | 89d1bdb | 2013-11-13 20:31:21 +0000 | [diff] [blame] | 607 | |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 608 | // Verify content |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 609 | EXPECT_EQ(StringRef(mfr.const_data()), Val); |
Rui Ueyama | 89d1bdb | 2013-11-13 20:31:21 +0000 | [diff] [blame] | 610 | |
Nick Kledzik | 18497e9 | 2012-06-20 00:28:54 +0000 | [diff] [blame] | 611 | // Unmap temp file |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 612 | |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 613 | fs::mapped_file_region m(Twine(TempPath), |
| 614 | fs::mapped_file_region::readonly, |
| 615 | 0, |
| 616 | 0, |
| 617 | EC); |
| 618 | ASSERT_NO_ERROR(EC); |
| 619 | const char *Data = m.const_data(); |
Chandler Carruth | 002da5d | 2014-03-02 04:08:41 +0000 | [diff] [blame] | 620 | fs::mapped_file_region mfrrv(std::move(m)); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 621 | EXPECT_EQ(mfrrv.const_data(), Data); |
Michael J. Spencer | ef2284f | 2012-08-15 19:05:47 +0000 | [diff] [blame] | 622 | } |
Michael J. Spencer | 9884778 | 2010-11-24 19:20:05 +0000 | [diff] [blame] | 623 | } // anonymous namespace |