]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Simplify resource management in directory iterators
authorJonathan Wakely <jwakely@redhat.com>
Tue, 8 Feb 2022 15:57:58 +0000 (15:57 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Tue, 8 Feb 2022 21:04:14 +0000 (21:04 +0000)
This replaces the _Dir constructor that takes ownership of an existing
DIR* resource with one that takes a _Dir_base rvalue instead. This means
a raw DIR* is never passed around, but is always owned by a _Dir_base
object.

libstdc++-v3/ChangeLog:

* src/c++17/fs_dir.cc (_Dir(DIR*, const path&)): Change first
parameter to _Dir_base&&.
* src/filesystem/dir-common.h (_Dir_base(DIR*)): Remove.
* src/filesystem/dir.cc (_Dir(DIR*, const path&)): Change first
parameter to _Dir_base&&.

libstdc++-v3/src/c++17/fs_dir.cc
libstdc++-v3/src/filesystem/dir-common.h
libstdc++-v3/src/filesystem/dir.cc

index 54f135d2baf95063574a2ff7648a88636a788d3f..c67fe76bc14f8c34c72caa6f9c64ea7a2ae6f0f7 100644 (file)
@@ -57,7 +57,7 @@ struct fs::_Dir : _Dir_base
       path = p;
   }
 
-  _Dir(posix::DIR* dirp, const path& p) : _Dir_base(dirp), path(p) { }
+  _Dir(_Dir_base&& d, const path& p) : _Dir_base(std::move(d)), path(p) { }
 
   _Dir(_Dir&&) = default;
 
@@ -140,7 +140,7 @@ struct fs::_Dir : _Dir_base
     _Dir_base d(dirfd, pathname, skip_permission_denied, nofollow, ec);
     // If this->path is empty, the new _Dir should have an empty path too.
     const fs::path& p = this->path.empty() ? this->path : this->entry.path();
-    return _Dir(std::exchange(d.dirp, nullptr), p);
+    return _Dir(std::move(d), p);
   }
 
   bool
index 0b7665a3f701296df3a9645a2ec69f50385809a7..511b988f1c79c0fa5e5c70a11d5654842cb84509 100644 (file)
@@ -89,8 +89,6 @@ is_permission_denied_error(int e)
 
 struct _Dir_base
 {
-  _Dir_base(posix::DIR* dirp = nullptr) : dirp(dirp) { }
-
   // If no error occurs then dirp is non-null,
   // otherwise null (even if a permission denied error is ignored).
   _Dir_base(int fd, const posix::char_type* pathname,
index e838b4bc6bfb71ebf310e828119f22d275f18c0a..b451902c4a1b1b63dfe880bab689f5e81957d5b4 100644 (file)
@@ -59,7 +59,7 @@ struct fs::_Dir : std::filesystem::_Dir_base
       path = p;
   }
 
-  _Dir(posix::DIR* dirp, const path& p) : _Dir_base(dirp), path(p) { }
+  _Dir(_Dir_base&& d, const path& p) : _Dir_base(std::move(d)), path(p) { }
 
   _Dir(_Dir&&) = default;
 
@@ -133,7 +133,7 @@ struct fs::_Dir : std::filesystem::_Dir_base
   {
     auto [dirfd, pathname] = dir_and_pathname();
     _Dir_base d(dirfd, pathname, skip_permission_denied, nofollow, ec);
-    return _Dir(std::exchange(d.dirp, nullptr), entry.path());
+    return _Dir(std::move(d), entry.path());
   }
 
   fs::path             path;