Switch over to the google3 unix_file File*.
I also moved macros.h to base/macros.h to ease google3 porting, at
the expense of a larger than necessary change. (I learned my lesson,
though, and didn't make the equivalent base/logging.h change.)
I'm not sure whether we want to keep the unix_file MappedFile given
our existing MemMap, but it's easier to bring it over and then remove
it (and possibly revert the removal) than to bring it over later.
Change-Id: Id50a66faa5ab17b9bc936cc9043dbc26f791f0ca
diff --git a/src/base/unix_file/fd_file.cc b/src/base/unix_file/fd_file.cc
new file mode 100644
index 0000000..73130a3
--- /dev/null
+++ b/src/base/unix_file/fd_file.cc
@@ -0,0 +1,134 @@
+/*
+ * Copyright (C) 2009 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "base/unix_file/fd_file.h"
+#include <errno.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include "logging.h"
+
+namespace unix_file {
+
+FdFile::FdFile() : fd_(-1), auto_close_(true) {
+}
+
+FdFile::FdFile(int fd) : fd_(fd), auto_close_(true) {
+}
+
+FdFile::FdFile(int fd, const std::string& path) : fd_(fd), file_path_(path), auto_close_(true) {
+}
+
+FdFile::~FdFile() {
+ if (auto_close_ && fd_ != -1) {
+ Close();
+ }
+}
+
+void FdFile::DisableAutoClose() {
+ auto_close_ = false;
+}
+
+bool FdFile::Open(const std::string& path, int flags) {
+ return Open(path, flags, 0640);
+}
+
+bool FdFile::Open(const std::string& path, int flags, mode_t mode) {
+ CHECK_EQ(fd_, -1) << path;
+ fd_ = TEMP_FAILURE_RETRY(open(path.c_str(), flags, mode));
+ if (fd_ == -1) {
+ return false;
+ }
+ file_path_ = path;
+ return true;
+}
+
+int FdFile::Close() {
+ int result = TEMP_FAILURE_RETRY(close(fd_));
+ if (result == -1) {
+ return -errno;
+ } else {
+ fd_ = -1;
+ file_path_ = "";
+ return 0;
+ }
+}
+
+int FdFile::Flush() {
+ int rc = TEMP_FAILURE_RETRY(fdatasync(fd_));
+ return (rc == -1) ? -errno : rc;
+}
+
+int64_t FdFile::Read(char* buf, int64_t byte_count, int64_t offset) const {
+ int rc = TEMP_FAILURE_RETRY(pread64(fd_, buf, byte_count, offset));
+ return (rc == -1) ? -errno : rc;
+}
+
+int FdFile::SetLength(int64_t new_length) {
+ int rc = TEMP_FAILURE_RETRY(ftruncate64(fd_, new_length));
+ return (rc == -1) ? -errno : rc;
+}
+
+int64_t FdFile::GetLength() const {
+ struct stat s;
+ int rc = TEMP_FAILURE_RETRY(fstat(fd_, &s));
+ return (rc == -1) ? -errno : s.st_size;
+}
+
+int64_t FdFile::Write(const char* buf, int64_t byte_count, int64_t offset) {
+ int rc = TEMP_FAILURE_RETRY(pwrite64(fd_, buf, byte_count, offset));
+ return (rc == -1) ? -errno : rc;
+}
+
+int FdFile::Fd() const {
+ return fd_;
+}
+
+bool FdFile::IsOpened() const {
+ return fd_ >= 0;
+}
+
+std::string FdFile::GetPath() const {
+ return file_path_;
+}
+
+bool FdFile::ReadFully(void* buffer, int64_t byte_count) {
+ char* ptr = static_cast<char*>(buffer);
+ while (byte_count > 0) {
+ int bytes_read = TEMP_FAILURE_RETRY(read(fd_, ptr, byte_count));
+ if (bytes_read <= 0) {
+ return false;
+ }
+ byte_count -= bytes_read; // Reduce the number of remaining bytes.
+ ptr += bytes_read; // Move the buffer forward.
+ }
+ return true;
+}
+
+bool FdFile::WriteFully(const void* buffer, int64_t byte_count) {
+ const char* ptr = static_cast<const char*>(buffer);
+ while (byte_count > 0) {
+ int bytes_read = TEMP_FAILURE_RETRY(write(fd_, ptr, byte_count));
+ if (bytes_read < 0) {
+ return false;
+ }
+ byte_count -= bytes_read; // Reduce the number of remaining bytes.
+ ptr += bytes_read; // Move the buffer forward.
+ }
+ return true;
+}
+
+} // namespace unix_file