Support/FileSystem: Implement canonicalize.
authorMichael J. Spencer <bigcheesegs@gmail.com>
Mon, 12 Dec 2011 06:04:01 +0000 (06:04 +0000)
committerMichael J. Spencer <bigcheesegs@gmail.com>
Mon, 12 Dec 2011 06:04:01 +0000 (06:04 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@146363 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Support/FileSystem.h
lib/Support/Unix/PathV2.inc
lib/Support/Windows/PathV2.inc
lib/Support/Windows/Windows.h
unittests/Support/Path.cpp

index 42e61dcba72a80a84fc55aba993850ee2bc95267..6db4554aea5751b31bad9cca28c7ebc220c0ec25 100644 (file)
@@ -370,8 +370,12 @@ error_code unique_file(const Twine &model, int &result_fd,
 
 /// @brief Canonicalize path.
 ///
 
 /// @brief Canonicalize path.
 ///
-/// Sets result to the file system's idea of what path is. The result is always
-/// absolute and has the same capitalization as the file system.
+/// Sets result to the file system's idea of what path is. Path must be
+/// absolute. The result has the same case as the file system.
+///
+/// Example: Give a file system with "C:\a\b\c\file.txt".
+///
+/// C:\A\b\C\fIlE.TxT => C:\a\b\c\file.txt
 ///
 /// @param path Input path.
 /// @param result Set to the canonicalized version of \a path.
 ///
 /// @param path Input path.
 /// @param result Set to the canonicalized version of \a path.
index 7477b4f2a08bf37fde8dad7bfe1e31326cdaf5b5..41ed49fec7db9a57cac9fa12baa3a8960f38a8e1 100644 (file)
@@ -439,6 +439,13 @@ rety_open_create:
   return success;
 }
 
   return success;
 }
 
+error_code canonicalize(const Twine &path, SmallVectorImpl<char> &result) {
+  // Paths are already canonicalized on posix systems.
+  assert(path::is_absolute(path) && "path must be absolute!");
+  path.toVector(result);
+  return success;
+}
+
 error_code detail::directory_iterator_construct(detail::DirIterState &it,
                                                 StringRef path){
   SmallString<128> path_null(path);
 error_code detail::directory_iterator_construct(detail::DirIterState &it,
                                                 StringRef path){
   SmallString<128> path_null(path);
index afb5533dc82eaf05f32a69a8429f180ca6e1606c..abb53c2ce18e34e7ba2c5c95eb237d0f82573b4a 100644 (file)
@@ -638,6 +638,44 @@ retry_create_file:
   return success;
 }
 
   return success;
 }
 
+error_code canonicalize(const Twine &path, SmallVectorImpl<char> &result) {
+  assert(path::is_absolute(path) && "path must be absolute!");
+  SmallString<128> path_storage;
+  StringRef p = path.toStringRef(path_storage);
+  SmallVector<wchar_t, 128> path_utf16;
+  result.set_size(0);
+
+  // Convert path to UTF-16.
+  if (error_code ec = UTF8ToUTF16(p, path_utf16))
+    return ec;
+
+  DWORD size = ::GetShortPathNameW(c_str(path_utf16), NULL, 0);
+  SmallVector<wchar_t, 128> short_path;
+  short_path.reserve(size + 1);
+  size = ::GetShortPathNameW( c_str(path_utf16)
+                            , short_path.data()
+                            , short_path.capacity());
+  if (!size)
+    return windows_error(::GetLastError());
+
+  short_path.set_size(size);
+
+  size = ::GetLongPathNameW(c_str(short_path), NULL, 0);
+  path_utf16.reserve(size + 1);
+  size = ::GetLongPathNameW( c_str(short_path)
+                           , path_utf16.data()
+                           , path_utf16.capacity());
+  if (!size)
+    return windows_error(::GetLastError());
+
+  path_utf16.set_size(size);
+
+  if (error_code ec = UTF16ToUTF8(path_utf16.data(), path_utf16.size(), result))
+    return ec;
+
+  return success;
+}
+
 error_code get_magic(const Twine &path, uint32_t len,
                      SmallVectorImpl<char> &result) {
   SmallString<128> path_storage;
 error_code get_magic(const Twine &path, uint32_t len,
                      SmallVectorImpl<char> &result) {
   SmallString<128> path_storage;
index 5c1da0d617aa776132943a8042571c346eb8569e..275407503559ef57b0b22c507acab3ba30410cca 100644 (file)
@@ -128,6 +128,24 @@ struct FindHandleTraits : CommonHandleTraits {
   }
 };
 
   }
 };
 
+struct FileMappingHandleTraits : CommonHandleTraits {
+  static handle_type GetInvalid() {
+    return 0;
+  }
+};
+
+struct MappedViewOfFileHandleTraits : CommonHandleTraits {
+  typedef LPVOID handle_type;
+
+  static handle_type GetInvalid() {
+    return 0;
+  }
+
+  static void Close(handle_type h) {
+    ::UnmapViewOfFile(h);
+  }
+};
+
 struct FileHandleTraits : CommonHandleTraits {};
 
 typedef ScopedHandle<CommonHandleTraits> ScopedCommonHandle;
 struct FileHandleTraits : CommonHandleTraits {};
 
 typedef ScopedHandle<CommonHandleTraits> ScopedCommonHandle;
@@ -135,6 +153,8 @@ typedef ScopedHandle<FileHandleTraits>   ScopedFileHandle;
 typedef ScopedHandle<CryptContextTraits> ScopedCryptContext;
 typedef ScopedHandle<FindHandleTraits>   ScopedFindHandle;
 typedef ScopedHandle<JobHandleTraits>    ScopedJobHandle;
 typedef ScopedHandle<CryptContextTraits> ScopedCryptContext;
 typedef ScopedHandle<FindHandleTraits>   ScopedFindHandle;
 typedef ScopedHandle<JobHandleTraits>    ScopedJobHandle;
+typedef ScopedHandle<FileMappingHandleTraits> ScopedFileMappingHandle;
+typedef ScopedHandle<MappedViewOfFileHandleTraits> ScopedMappedViewOfFileHandle;
 
 namespace llvm {
 template <class T>
 
 namespace llvm {
 template <class T>
index c791184c837bad661f2471ea96bccc38b2bbb3ae..4982ed0940d41e0e9507234d1eb19ba8fe939c89 100644 (file)
@@ -304,4 +304,32 @@ TEST_F(FileSystemTest, Magic) {
   }
 }
 
   }
 }
 
+TEST_F(FileSystemTest, Canonicalize) {
+  SmallString<128> file_pathname(TestDirectory);
+  path::append(file_pathname, "canonicalize", "a0", "aa1");
+
+  bool existed;
+  ASSERT_NO_ERROR(fs::create_directories(Twine(TestDirectory)
+                  + "/canonicalize/a0/aa1", existed));
+
+  {
+    path::append(file_pathname, "file.txt");
+    std::string ErrMsg;
+    raw_fd_ostream file(file_pathname.c_str(), ErrMsg);
+    file << "hello\n";
+  }
+
+  SmallString<0> res;
+  ASSERT_NO_ERROR(fs::canonicalize(Twine(TestDirectory)
+                                   + "/cAnOnIcAlIzE/A0/aA1/fIlE.TxT", res));
+  // Only check if we actually found the file. As we won't find it on case
+  // sensitive file systems.
+  if (fs::exists(res.str())) {
+    ASSERT_TRUE(res.str().find("canonicalize") != StringRef::npos);
+    ASSERT_TRUE(res.str().find("a0") != StringRef::npos);
+    ASSERT_TRUE(res.str().find("aa1") != StringRef::npos);
+    ASSERT_TRUE(res.str().find("file.txt") != StringRef::npos);
+  }
+}
+
 } // anonymous namespace
 } // anonymous namespace