Implement ReadAll(SharedFD, std::string*)
This allows reading the full contents of a SharedFD into a string. It
assumes that while the ReadAll function is running, the given SharedFD
will close.
Test: N/A
Bug: 137304531
Change-Id: I60935e7d5daed07cd484a2d207ebd34347fe9a52
diff --git a/common/libs/fs/Android.bp b/common/libs/fs/Android.bp
index 01e5f32..59075da 100644
--- a/common/libs/fs/Android.bp
+++ b/common/libs/fs/Android.bp
@@ -17,6 +17,7 @@
name: "libcuttlefish_fs",
srcs: [
"gce_fs.cpp",
+ "shared_buf.cc",
"shared_fd.cpp",
],
shared: {
@@ -50,6 +51,7 @@
],
srcs: [
"gce_fs.cpp",
+ "shared_buf.cc",
"shared_fd.cpp",
],
shared_libs: [
diff --git a/common/libs/fs/shared_buf.cc b/common/libs/fs/shared_buf.cc
new file mode 100644
index 0000000..e929f3b
--- /dev/null
+++ b/common/libs/fs/shared_buf.cc
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2019 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 <sstream>
+#include <string>
+#include <thread>
+#include <vector>
+
+#include "common/libs/fs/shared_buf.h"
+#include "common/libs/fs/shared_fd.h"
+
+namespace cvd {
+
+namespace {
+
+const size_t BUFF_SIZE = 1 << 14;
+
+} // namespace
+
+ssize_t ReadAll(SharedFD fd, std::string* buf) {
+ char buff[BUFF_SIZE];
+ std::stringstream ss;
+ ssize_t read;
+ while ((read = fd->Read(buff, BUFF_SIZE - 1)) > 0) {
+ // this is necessary to avoid problems with having a '\0' in the middle of the buffer
+ ss << std::string(buff, read);
+ }
+ if (read < 0) {
+ return read;
+ }
+ *buf = ss.str();
+ return buf->size();
+}
+
+} // namespace cvd
diff --git a/common/libs/fs/shared_buf.h b/common/libs/fs/shared_buf.h
new file mode 100644
index 0000000..c1ecf37
--- /dev/null
+++ b/common/libs/fs/shared_buf.h
@@ -0,0 +1,27 @@
+/*
+ * Copyright (C) 2019 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 <string>
+#include <thread>
+#include <vector>
+
+#include "common/libs/fs/shared_fd.h"
+
+namespace cvd {
+
+ssize_t ReadAll(SharedFD fd, std::string* buf);
+
+} // namespace cvd