flock locks in folly::File, FileUtil, Exception.h fixes and tests
[folly.git] / folly / Exception.h
index a9300ce451121768d7fb7e680aa545ac31763bf8..fad0b691b969990bc8c4a5aa34b6ad5ef8349968 100644 (file)
 
 #include <errno.h>
 
+#include <cstdio>
 #include <stdexcept>
 #include <system_error>
 
 #include "folly/Conv.h"
+#include "folly/FBString.h"
 #include "folly/Likely.h"
 #include "folly/Portability.h"
 
 namespace folly {
 
+// Various helpers to throw appropriate std::system_error exceptions from C
+// library errors (returned in errno, as positive return values (many POSIX
+// functions), or as negative return values (Linux syscalls))
+//
+// The *Explicit functions take an explicit value for errno.
+
 // Helper to throw std::system_error
-void throwSystemError(int err, const char* msg) FOLLY_NORETURN;
-inline void throwSystemError(int err, const char* msg) {
+void throwSystemErrorExplicit(int err, const char*) FOLLY_NORETURN;
+inline void throwSystemErrorExplicit(int err, const char* msg) {
   throw std::system_error(err, std::system_category(), msg);
 }
 
-// Helper to throw std::system_error from errno
-void throwSystemError(const char* msg) FOLLY_NORETURN;
-inline void throwSystemError(const char* msg) {
-  throwSystemError(errno, msg);
+template <class... Args>
+void throwSystemErrorExplicit(int, Args&&... args) FOLLY_NORETURN;
+template <class... Args>
+void throwSystemErrorExplicit(int err, Args&&... args) {
+  throwSystemErrorExplicit(
+      err, to<fbstring>(std::forward<Args>(args)...).c_str());
 }
 
 // Helper to throw std::system_error from errno and components of a string
 template <class... Args>
-void throwSystemError(Args... args) FOLLY_NORETURN;
+void throwSystemError(Args&&... args) FOLLY_NORETURN;
 template <class... Args>
-inline void throwSystemError(Args... args) {
-  throwSystemError(errno, folly::to<std::string>(args...));
+void throwSystemError(Args&&... args) {
+  throwSystemErrorExplicit(errno, std::forward<Args>(args)...);
 }
 
 // Check a Posix return code (0 on success, error number on error), throw
 // on error.
-inline void checkPosixError(int err, const char* msg) {
+template <class... Args>
+void checkPosixError(int err, Args&&... args) {
   if (UNLIKELY(err != 0)) {
-    throwSystemError(err, msg);
+    throwSystemErrorExplicit(err, std::forward<Args>(args)...);
   }
 }
 
 // Check a Linux kernel-style return code (>= 0 on success, negative error
 // number on error), throw on error.
-inline void checkKernelError(ssize_t ret, const char* msg) {
+template <class... Args>
+void checkKernelError(ssize_t ret, Args&&... args) {
   if (UNLIKELY(ret < 0)) {
-    throwSystemError(-ret, msg);
+    throwSystemErrorExplicit(-ret, std::forward<Args>(args)...);
   }
 }
 
 // Check a traditional Unix return code (-1 and sets errno on error), throw
 // on error.
-inline void checkUnixError(ssize_t ret, const char* msg) {
+template <class... Args>
+void checkUnixError(ssize_t ret, Args&&... args) {
   if (UNLIKELY(ret == -1)) {
-    throwSystemError(msg);
+    throwSystemError(std::forward<Args>(args)...);
   }
 }
-inline void checkUnixError(ssize_t ret, int savedErrno, const char* msg) {
+
+template <class... Args>
+void checkUnixErrorExplicit(ssize_t ret, int savedErrno, Args&&... args) {
   if (UNLIKELY(ret == -1)) {
-    throwSystemError(savedErrno, msg);
+    throwSystemErrorExplicit(savedErrno, std::forward<Args>(args)...);
+  }
+}
+
+// Check the return code from a fopen-style function (returns a non-nullptr
+// FILE* on success, nullptr on error, sets errno).  Works with fopen, fdopen,
+// freopen, tmpfile, etc.
+template <class... Args>
+void checkFopenError(FILE* fp, Args&&... args) {
+  if (UNLIKELY(!fp)) {
+    throwSystemError(std::forward<Args>(args)...);
+  }
+}
+
+template <class... Args>
+void checkFopenErrorExplicit(FILE* fp, int savedErrno, Args&&... args) {
+  if (UNLIKELY(!fp)) {
+    throwSystemErrorExplicit(savedErrno, std::forward<Args>(args)...);
   }
 }