llvm.org GIT mirror llvm / c3b00e8
Support/FileSystem: Implement canonicalize. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@146363 91177308-0d34-0410-b5e6-96231b3b80d8 Michael J. Spencer 8 years ago
5 changed file(s) with 99 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
369369
370370 /// @brief Canonicalize path.
371371 ///
372 /// Sets result to the file system's idea of what path is. The result is always
373 /// absolute and has the same capitalization as the file system.
372 /// Sets result to the file system's idea of what path is. Path must be
373 /// absolute. The result has the same case as the file system.
374 ///
375 /// Example: Give a file system with "C:\a\b\c\file.txt".
376 ///
377 /// C:\A\b\C\fIlE.TxT => C:\a\b\c\file.txt
374378 ///
375379 /// @param path Input path.
376380 /// @param result Set to the canonicalized version of \a path.
438438 return success;
439439 }
440440
441 error_code canonicalize(const Twine &path, SmallVectorImpl &result) {
442 // Paths are already canonicalized on posix systems.
443 assert(path::is_absolute(path) && "path must be absolute!");
444 path.toVector(result);
445 return success;
446 }
447
441448 error_code detail::directory_iterator_construct(detail::DirIterState &it,
442449 StringRef path){
443450 SmallString<128> path_null(path);
637637 return success;
638638 }
639639
640 error_code canonicalize(const Twine &path, SmallVectorImpl &result) {
641 assert(path::is_absolute(path) && "path must be absolute!");
642 SmallString<128> path_storage;
643 StringRef p = path.toStringRef(path_storage);
644 SmallVector path_utf16;
645 result.set_size(0);
646
647 // Convert path to UTF-16.
648 if (error_code ec = UTF8ToUTF16(p, path_utf16))
649 return ec;
650
651 DWORD size = ::GetShortPathNameW(c_str(path_utf16), NULL, 0);
652 SmallVector short_path;
653 short_path.reserve(size + 1);
654 size = ::GetShortPathNameW( c_str(path_utf16)
655 , short_path.data()
656 , short_path.capacity());
657 if (!size)
658 return windows_error(::GetLastError());
659
660 short_path.set_size(size);
661
662 size = ::GetLongPathNameW(c_str(short_path), NULL, 0);
663 path_utf16.reserve(size + 1);
664 size = ::GetLongPathNameW( c_str(short_path)
665 , path_utf16.data()
666 , path_utf16.capacity());
667 if (!size)
668 return windows_error(::GetLastError());
669
670 path_utf16.set_size(size);
671
672 if (error_code ec = UTF16ToUTF8(path_utf16.data(), path_utf16.size(), result))
673 return ec;
674
675 return success;
676 }
677
640678 error_code get_magic(const Twine &path, uint32_t len,
641679 SmallVectorImpl &result) {
642680 SmallString<128> path_storage;
127127 }
128128 };
129129
130 struct FileMappingHandleTraits : CommonHandleTraits {
131 static handle_type GetInvalid() {
132 return 0;
133 }
134 };
135
136 struct MappedViewOfFileHandleTraits : CommonHandleTraits {
137 typedef LPVOID handle_type;
138
139 static handle_type GetInvalid() {
140 return 0;
141 }
142
143 static void Close(handle_type h) {
144 ::UnmapViewOfFile(h);
145 }
146 };
147
130148 struct FileHandleTraits : CommonHandleTraits {};
131149
132150 typedef ScopedHandle ScopedCommonHandle;
134152 typedef ScopedHandle ScopedCryptContext;
135153 typedef ScopedHandle ScopedFindHandle;
136154 typedef ScopedHandle ScopedJobHandle;
155 typedef ScopedHandle ScopedFileMappingHandle;
156 typedef ScopedHandle ScopedMappedViewOfFileHandle;
137157
138158 namespace llvm {
139159 template
303303 }
304304 }
305305
306 TEST_F(FileSystemTest, Canonicalize) {
307 SmallString<128> file_pathname(TestDirectory);
308 path::append(file_pathname, "canonicalize", "a0", "aa1");
309
310 bool existed;
311 ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory)
312 + "/canonicalize/a0/aa1", existed));
313
314 {
315 path::append(file_pathname, "file.txt");
316 std::string ErrMsg;
317 raw_fd_ostream file(file_pathname.c_str(), ErrMsg);
318 file << "hello\n";
319 }
320
321 SmallString<0> res;
322 ASSERT_NO_ERROR(fs::canonicalize(Twine(TestDirectory)
323 + "/cAnOnIcAlIzE/A0/aA1/fIlE.TxT", res));
324 // Only check if we actually found the file. As we won't find it on case
325 // sensitive file systems.
326 if (fs::exists(res.str())) {
327 ASSERT_TRUE(res.str().find("canonicalize") != StringRef::npos);
328 ASSERT_TRUE(res.str().find("a0") != StringRef::npos);
329 ASSERT_TRUE(res.str().find("aa1") != StringRef::npos);
330 ASSERT_TRUE(res.str().find("file.txt") != StringRef::npos);
331 }
332 }
333
306334 } // anonymous namespace