Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c0e6e40c authored by Elliott Hughes's avatar Elliott Hughes
Browse files

Copy the good comment and warn_unused_result from ScopedFd to unique_fd.

Also list all known aliases of this class to increase the chances that
anyone searching for it by another name finds it anyway.

Change-Id: I58ea0a5421987fb69f93cc56252a771e9c34147e
parent ce8bc7b3
Loading
Loading
Loading
Loading
+19 −15
Original line number Diff line number Diff line
@@ -21,18 +21,18 @@

#include <android-base/macros.h>

/* Container for a file descriptor that automatically closes the descriptor as
 * it goes out of scope.
 *
 *      unique_fd ufd(open("/some/path", "r"));
 *
 *      if (ufd.get() < 0) // invalid descriptor
 *          return error;
 *
 *      // Do something useful
 *
 *      return 0; // descriptor is closed here
 */
// Container for a file descriptor that automatically closes the descriptor as
// it goes out of scope.
//
//      unique_fd ufd(open("/some/path", "r"));
//      if (ufd.get() == -1) return error;
//
//      // Do something useful, possibly including 'return'.
//
//      return 0; // Descriptor is closed for you.
//
// unique_fd is also known as ScopedFd/ScopedFD/scoped_fd; mentioned here to help
// you find this class if you're searching for one of those names.
namespace android {
namespace base {

@@ -50,8 +50,12 @@ class unique_fd final {
  }

  void reset(int new_value) {
    if (value_ >= 0)
    if (value_ != -1) {
      // Even if close(2) fails with EINTR, the fd will have been closed.
      // Using TEMP_FAILURE_RETRY will either lead to EBADF or closing someone else's fd.
      // http://lkml.indiana.edu/hypermail/linux/kernel/0509.1/0877.html
      close(value_);
    }
    value_ = new_value;
  }

@@ -61,7 +65,7 @@ class unique_fd final {

  int get() const { return value_; }

  int release() {
  int release() __attribute__((warn_unused_result)) {
    int ret = value_;
    value_ = -1;
    return ret;