Index: webrtc/base/file_posix.cc |
diff --git a/webrtc/base/file_posix.cc b/webrtc/base/file_posix.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f43e568eedf48a213471178d5bd21720a8f075ed |
--- /dev/null |
+++ b/webrtc/base/file_posix.cc |
@@ -0,0 +1,62 @@ |
+/* |
+ * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
+ * |
+ * Use of this source code is governed by a BSD-style license |
+ * that can be found in the LICENSE file in the root of the source |
+ * tree. An additional intellectual property rights grant can be found |
+ * in the file PATENTS. All contributing project authors may |
+ * be found in the AUTHORS file in the root of the source tree. |
+ */ |
+ |
+#include "webrtc/base/file.h" |
+ |
+#include <unistd.h> |
+#include <errno.h> |
+ |
+namespace rtc { |
+ |
+#define HANDLE_EINTR(x) \ |
+ ({ \ |
+ decltype(x) eintr_wrapper_result; \ |
+ do { \ |
+ eintr_wrapper_result = (x); \ |
+ } while (eintr_wrapper_result == -1 && errno == EINTR); \ |
+ eintr_wrapper_result; \ |
+ }) |
sprang_webrtc
2016/08/15 09:43:07
I think I would actually preffer having explicit c
|
+ |
+size_t File::WriteNoBestEffort(const uint8_t* data, size_t length) { |
+ ssize_t rv = HANDLE_EINTR(write(file_, data, length)); |
+ return rv < 0 ? 0 : size_t{rv}; |
sprang_webrtc
2016/08/15 09:43:07
Prefer static_cast<size_t>() to size_t{}
Also don'
palmkvist
2016/08/16 08:55:35
Done.
|
+} |
+ |
+size_t File::ReadNoBestEffort(uint8_t* buffer, size_t length) { |
+ ssize_t rv = HANDLE_EINTR(read(file_, buffer, length)); |
+ return rv < 0 ? 0 : size_t{rv}; |
+} |
+ |
+size_t File::WriteAtNoBestEffort(const uint8_t* data, |
+ size_t length, |
+ size_t offset) { |
+ ssize_t rv = HANDLE_EINTR(pwrite(file_, data, length, offset)); |
+ return rv < 0 ? 0 : size_t{rv}; |
+} |
+ |
+size_t File::ReadAtNoBestEffort(uint8_t* data, size_t length, size_t offset) { |
+ ssize_t rv = HANDLE_EINTR(pread(file_, data, length, offset)); |
+ return rv < 0 ? 0 : size_t{rv}; |
+} |
+ |
+bool File::Seek(size_t offset) { |
+ static_assert(sizeof(size_t) <= sizeof(off_t), "size_t must fit in off_t"); |
+ return lseek(file_, off_t{offset}, SEEK_SET) != ((off_t)-1); |
+} |
+ |
+bool File::Close() { |
+ if (file_ == rtc::kInvalidPlatformFileValue) |
+ return false; |
+ bool ret = close(file_) == 0; |
+ file_ = rtc::kInvalidPlatformFileValue; |
+ return ret; |
+} |
+ |
+} // namespace rtc |