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/string_file.cc b/src/base/unix_file/string_file.cc
new file mode 100644
index 0000000..5d47b17
--- /dev/null
+++ b/src/base/unix_file/string_file.cc
@@ -0,0 +1,98 @@
+/*
+ * 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/string_file.h"
+#include <errno.h>
+#include <algorithm>
+#include "logging.h"
+
+namespace unix_file {
+
+StringFile::StringFile() {
+}
+
+StringFile::~StringFile() {
+}
+
+int StringFile::Close() {
+  return 0;
+}
+
+int StringFile::Flush() {
+  return 0;
+}
+
+int64_t StringFile::Read(char *buf, int64_t byte_count, int64_t offset) const {
+  CHECK(buf);
+  CHECK_GE(byte_count, 0);
+
+  if (offset < 0) {
+    return -EINVAL;
+  }
+
+  const int64_t available_bytes = std::min(byte_count, GetLength() - offset);
+  if (available_bytes < 0) {
+    return 0;  // Not an error, but nothing for us to do, either.
+  }
+  memcpy(buf, data_.data() + offset, available_bytes);
+  return available_bytes;
+}
+
+int StringFile::SetLength(int64_t new_length) {
+  if (new_length < 0) {
+    return -EINVAL;
+  }
+  data_.resize(new_length);
+  return 0;
+}
+
+int64_t StringFile::GetLength() const {
+  return data_.size();
+}
+
+int64_t StringFile::Write(const char *buf, int64_t byte_count, int64_t offset) {
+  CHECK(buf);
+  CHECK_GE(byte_count, 0);
+
+  if (offset < 0) {
+    return -EINVAL;
+  }
+
+  if (byte_count == 0) {
+    return 0;
+  }
+
+  // FUSE seems happy to allow writes past the end. (I'd guess it doesn't
+  // synthesize a write of zero bytes so that we're free to implement sparse
+  // files.) GNU as(1) seems to require such writes. Those files are small.
+  const int64_t bytes_past_end = offset - GetLength();
+  if (bytes_past_end > 0) {
+    data_.append(bytes_past_end, '\0');
+  }
+
+  data_.replace(offset, byte_count, buf, byte_count);
+  return byte_count;
+}
+
+void StringFile::Assign(const art::StringPiece &new_data) {
+  data_.assign(new_data.data(), new_data.size());
+}
+
+const art::StringPiece StringFile::ToStringPiece() const {
+  return data_;
+}
+
+}  // namespace unix_file