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

Commit f8d62f06 authored by Mathieu Chartier's avatar Mathieu Chartier Committed by Gerrit Code Review
Browse files

Merge "libbase: add Fdopendir that takes a unique_fd."

parents 6d7c8fcc 1666bafa
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@
#include <fcntl.h>

#if !defined(_WIN32)
#include <dirent.h>
#include <sys/socket.h>
#endif

@@ -211,6 +212,17 @@ inline FILE* Fdopen(unique_fd&& ufd, const char* mode) {
  return file;
}

// Using fdopendir with unique_fd correctly is more annoying than it should be,
// because fdopen doesn't close the file descriptor received upon failure.
inline DIR* Fdopendir(unique_fd&& ufd) {
  int fd = ufd.release();
  DIR* dir = fdopendir(fd);
  if (dir == nullptr) {
    close(fd);
  }
  return dir;
}

#endif  // !defined(_WIN32)

}  // namespace base