Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(519)

Unified Diff: webrtc/base/file_posix.cc

Issue 2214763002: Initial version of new file wrapper (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Rebase Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « webrtc/base/file.cc ('k') | webrtc/base/file_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..0dad56deb18a177ca8df54a1b47a32068fcacd69
--- /dev/null
+++ b/webrtc/base/file_posix.cc
@@ -0,0 +1,96 @@
+/*
+ * 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 <errno.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+namespace rtc {
+
+File File::Open(const std::string& path) {
+ return File(::open(path.c_str(), O_RDWR));
+}
+
+size_t File::Write(const uint8_t* data, size_t length) {
+ size_t total_written = 0;
+ do {
+ ssize_t written;
+ do {
+ written = ::write(file_, data + total_written, length - total_written);
+ } while (written == -1 && errno == EINTR);
+ if (written == -1)
+ break;
+ total_written += written;
+ } while (total_written < length);
+ return total_written;
+}
+
+size_t File::Read(uint8_t* buffer, size_t length) {
+ size_t total_read = 0;
+ do {
+ ssize_t read;
+ do {
+ read = ::read(file_, buffer + total_read, length - total_read);
+ } while (read == -1 && errno == EINTR);
+ if (read == -1)
+ break;
+ total_read += read;
+ } while (total_read < length);
+ return total_read;
+}
+
+size_t File::WriteAt(const uint8_t* data, size_t length, size_t offset) {
+ size_t total_written = 0;
+ do {
+ ssize_t written;
+ do {
+ written = ::pwrite(file_, data + total_written, length - total_written,
+ offset + total_written);
+ } while (written == -1 && errno == EINTR);
+ if (written == -1)
+ break;
+ total_written += written;
+ } while (total_written < length);
+ return total_written;
+}
+
+size_t File::ReadAt(uint8_t* buffer, size_t length, size_t offset) {
+ size_t total_read = 0;
+ do {
+ ssize_t read;
+ do {
+ read = ::pread(file_, buffer + total_read, length - total_read,
+ offset + total_read);
+ } while (read == -1 && errno == EINTR);
+ if (read == -1)
+ break;
+ total_read += read;
+ } while (total_read < length);
+ return total_read;
+}
+
+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
« no previous file with comments | « webrtc/base/file.cc ('k') | webrtc/base/file_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698