retry flock() if interrupted (EINTR)
authorPhilip Pronin <philipp@fb.com>
Sat, 17 Aug 2013 09:06:35 +0000 (02:06 -0700)
committerSara Golemon <sgolemon@fb.com>
Wed, 28 Aug 2013 21:30:12 +0000 (14:30 -0700)
Test Plan: fbconfig folly/test:file_test && fbmake runtests_opt

Reviewed By: soren@fb.com

FB internal diff: D932782

folly/File.cpp
folly/FileUtil.cpp
folly/FileUtil.h

index aaf109f07aad557b439392f285818b8652d17a06..3c961aaae45f6dd1c2c6100a4256a6d334f5cf68 100644 (file)
 
 #include "folly/File.h"
 
-#include <sys/file.h>
 #include <fcntl.h>
 #include <unistd.h>
 
-#include "folly/Format.h"
 #include "folly/Exception.h"
+#include "folly/FileUtil.h"
+#include "folly/Format.h"
 #include "folly/ScopeGuard.h"
 
 #include <system_error>
@@ -112,11 +112,11 @@ void File::lock_shared() { doLock(LOCK_SH); }
 bool File::try_lock_shared() { return doTryLock(LOCK_SH); }
 
 void File::doLock(int op) {
-  checkUnixError(flock(fd_, op), "flock() failed (lock)");
+  checkUnixError(flockNoInt(fd_, op), "flock() failed (lock)");
 }
 
 bool File::doTryLock(int op) {
-  int r = flock(fd_, op | LOCK_NB);
+  int r = flockNoInt(fd_, op | LOCK_NB);
   // flock returns EWOULDBLOCK if already locked
   if (r == -1 && errno == EWOULDBLOCK) return false;
   checkUnixError(r, "flock() failed (try_lock)");
@@ -124,7 +124,7 @@ bool File::doTryLock(int op) {
 }
 
 void File::unlock() {
-  checkUnixError(flock(fd_, LOCK_UN), "flock() failed (unlock)");
+  checkUnixError(flockNoInt(fd_, LOCK_UN), "flock() failed (unlock)");
 }
 void File::unlock_shared() { unlock(); }
 
index 4a4ca6b6264658c434e4707daeff7806b36fa973..45c313fff59b82ef23497c4f01d9ada2f82b0bc3 100644 (file)
@@ -20,6 +20,7 @@
 #ifdef __APPLE__
 #include <fcntl.h>
 #endif
+#include <sys/file.h>
 
 #include "folly/detail/FileUtilDetail.h"
 
@@ -70,6 +71,10 @@ int truncateNoInt(const char* path, off_t len) {
   return wrapNoInt(truncate, path, len);
 }
 
+int flockNoInt(int fd, int operation) {
+  return wrapNoInt(flock, fd, operation);
+}
+
 ssize_t readNoInt(int fd, void* buf, size_t count) {
   return wrapNoInt(read, fd, buf, count);
 }
index 81974871fc8866493958782b98af54bdfc5ebad2..a542e1c98d7146a5065a7a62766275f12d1a7d45 100644 (file)
@@ -39,6 +39,7 @@ int fsyncNoInt(int fd);
 int fdatasyncNoInt(int fd);
 int ftruncateNoInt(int fd, off_t len);
 int truncateNoInt(const char* path, off_t len);
+int flockNoInt(int fd, int operation);
 
 ssize_t readNoInt(int fd, void* buf, size_t n);
 ssize_t preadNoInt(int fd, void* buf, size_t n, off_t offset);