[libcxx-commits] [PATCH] D91145: [12/N] [libcxx] Sanitize paths before creating symlinks on windows

Martin Storsjö via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Tue Nov 10 01:49:01 PST 2020


mstorsjo created this revision.
mstorsjo added reviewers: libc++, rnk.
Herald added a project: libc++.
Herald added 1 blocking reviewer(s): libc++.
mstorsjo requested review of this revision.

The STL does even more cleanup (corresponding to lexically_normal I think), but this seems to be the very minimum needed for making the symlinks work when the target path contains non-native paths.

I guess this could be squashed into patch 11, but this adds a test case for making sure the created symlinks really work.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D91145

Files:
  libcxx/src/filesystem/operations.cpp
  libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.create_symlink/create_symlink.pass.cpp


Index: libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.create_symlink/create_symlink.pass.cpp
===================================================================
--- libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.create_symlink/create_symlink.pass.cpp
+++ libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.create_symlink/create_symlink.pass.cpp
@@ -71,5 +71,25 @@
     }
 }
 
+TEST_CASE(create_symlink_dest_cleanup)
+{
+    scoped_test_env env;
+    const path dir = env.create_dir("dir");
+    const path file = env.create_file("file", 42);
+    const path sym = dir / "link";
+    // The target path has to be normalized to backslashes before creating
+    // the link on windows, otherwise the link isn't dereferencable.
+    const path sym_target = "../file";
+    path sym_target_normalized = sym_target;
+    sym_target_normalized.make_preferred();
+    std::error_code ec;
+    fs::create_symlink(sym_target, sym, ec);
+    TEST_REQUIRE(!ec);
+    TEST_CHECK(equivalent(sym, file, ec));
+    const path ret = fs::read_symlink(sym, ec);
+    TEST_CHECK(!ec);
+    TEST_CHECK(ret.native() == sym_target_normalized.native());
+}
+
 
 TEST_SUITE_END()
Index: libcxx/src/filesystem/operations.cpp
===================================================================
--- libcxx/src/filesystem/operations.cpp
+++ libcxx/src/filesystem/operations.cpp
@@ -438,6 +438,9 @@
 
 int symlink_file_dir(const wchar_t *oldname, const wchar_t *newname,
                      bool is_dir) {
+  path dest(oldname);
+  dest.make_preferred();
+  oldname = dest.c_str();
   DWORD flags = is_dir ? SYMBOLIC_LINK_FLAG_DIRECTORY : 0;
   if (CreateSymbolicLinkW(newname, oldname,
                           flags | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D91145.304101.patch
Type: text/x-patch
Size: 1773 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20201110/7a6f8355/attachment-0001.bin>


More information about the libcxx-commits mailing list