From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2140) id A1F6738327EE; Thu, 23 Jun 2022 07:15:40 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A1F6738327EE Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Alexandre Oliva To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc(refs/users/aoliva/heads/testme)] libstdc++: retry removal of dir entries if dir removal fails X-Act-Checkin: gcc X-Git-Author: Alexandre Oliva X-Git-Refname: refs/users/aoliva/heads/testme X-Git-Oldrev: ba8cfb0770665015bfc22385d6c6e6ee777b930b X-Git-Newrev: ae6e31b9ce7e733c3e12c1363990490873935577 Message-Id: <20220623071540.A1F6738327EE@sourceware.org> Date: Thu, 23 Jun 2022 07:15:40 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 23 Jun 2022 07:15:40 -0000 https://gcc.gnu.org/g:ae6e31b9ce7e733c3e12c1363990490873935577 commit ae6e31b9ce7e733c3e12c1363990490873935577 Author: Alexandre Oliva Date: Mon Jun 20 20:19:55 2022 -0300 libstdc++: retry removal of dir entries if dir removal fails On some target systems (e.g. rtems6.0), removing directory components while iterating over directory entries may cause some of the directory entries to be skipped, which prevents the removal of the parent directory from succeeding. Advancing the iterator before removing a member proved not to be enough, so I've instead arranged for remove_all to retry the removal of components if the removal of the parent dir fails after removing at least one entry. The fail will be permanent only if no components got removed in the current try. for libstdc++-v3/ChangeLog * src/c++17/fs_ops.cc (remove_all): Retry removal of directory entries. Diff: --- libstdc++-v3/src/c++17/fs_ops.cc | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/libstdc++-v3/src/c++17/fs_ops.cc b/libstdc++-v3/src/c++17/fs_ops.cc index 435368fa5c5..b3390310132 100644 --- a/libstdc++-v3/src/c++17/fs_ops.cc +++ b/libstdc++-v3/src/c++17/fs_ops.cc @@ -1286,6 +1286,8 @@ fs::remove_all(const path& p) { error_code ec; uintmax_t count = 0; + retry: + uintmax_t init_count = count; recursive_directory_iterator dir(p, directory_options{64|128}, ec); switch (ec.value()) // N.B. assumes ec.category() == std::generic_category() { @@ -1303,7 +1305,7 @@ fs::remove_all(const path& p) break; case ENOENT: // Our work here is done. - return 0; + return count; case ENOTDIR: case ELOOP: // Not a directory, will remove below. @@ -1313,6 +1315,18 @@ fs::remove_all(const path& p) _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot remove all", p, ec)); } + if (count > init_count) + { + if (int last = fs::remove(p, ec); !ec) + return count + last; + else + // Some systems seem to skip entries in the dir iteration if + // you remove dir entries while iterating, so if we removed + // anything in the dir in this round, and failed to remove + // the dir (presumably because it wasn't empty), retry. + goto retry; + } + // Remove p itself, which is either a non-directory or is now empty. return count + fs::remove(p); } @@ -1321,6 +1335,8 @@ std::uintmax_t fs::remove_all(const path& p, error_code& ec) { uintmax_t count = 0; + retry: + uintmax_t init_count = count; recursive_directory_iterator dir(p, directory_options{64|128}, ec); switch (ec.value()) // N.B. assumes ec.category() == std::generic_category() { @@ -1341,7 +1357,7 @@ fs::remove_all(const path& p, error_code& ec) case ENOENT: // Our work here is done. ec.clear(); - return 0; + return count; case ENOTDIR: case ELOOP: // Not a directory, will remove below. @@ -1354,6 +1370,8 @@ fs::remove_all(const path& p, error_code& ec) // Remove p itself, which is either a non-directory or is now empty. if (int last = fs::remove(p, ec); !ec) return count + last; + if (count > init_count) + goto retry; return -1; }