[libcxx-commits] [PATCH] D106217: [libcxx] Fix create_directories returning false but actually creating the directories, if the path name is with trailing seperators

Cao Jingfan via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Sat Jul 17 04:10:05 PDT 2021


caojingfan created this revision.
caojingfan added a reviewer: curdeius.
caojingfan requested review of this revision.
Herald added a project: libc++.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

if the path name is with trailing seperators like 'dir1/dir2/dir3/',
create_directories should process it just like 'dir1/dir2/dir3',
but according to https://godbolt.org/z/Wh3axvYM5, the trailing seperators
case was handled mistakenly, this patch fix it.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D106217

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


Index: libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.create_directories/create_directories.pass.cpp
===================================================================
--- libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.create_directories/create_directories.pass.cpp
+++ libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.create_directories/create_directories.pass.cpp
@@ -150,6 +150,18 @@
                             fs::create_directories(path{}));
 }
 
+TEST_CASE(dest_is_with_trailing_sep) {
+  scoped_test_env env;
+  const std::string dir_name = "dir1/dir2/dir3/";
+  const path dir = env.make_env_path(dir_name);
+  TEST_CHECK(*dir.string().crbegin() == '/');
+  TEST_CHECK(!exists(dir));
+  std::error_code ec = GetTestEC();
+  TEST_CHECK(fs::create_directories(dir, ec) == true);
+  TEST_CHECK(!ec);
+  TEST_CHECK(exists(dir));
+}
+
 #ifdef _WIN32
 TEST_CASE(nonexistent_root)
 {
Index: libcxx/src/filesystem/operations.cpp
===================================================================
--- libcxx/src/filesystem/operations.cpp
+++ libcxx/src/filesystem/operations.cpp
@@ -1590,6 +1590,8 @@
     --PP;
     if (PP.RawEntry.data() == __pn_.data())
       return {};
+    if (PP.State == PathParser::PS_InTrailingSep)
+      --PP;
     --PP;
     return createView(__pn_.data(), &PP.RawEntry.back());
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D106217.359552.patch
Type: text/x-patch
Size: 1351 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20210717/0e155c5b/attachment.bin>


More information about the libcxx-commits mailing list