Adds an overload to SharedFD::CopyFrom that takes the length
Test: local & gce
Change-Id: I175dc3c3408c939d3ff57db6c9ac021e3ca82914
diff --git a/common/libs/fs/shared_fd.cpp b/common/libs/fs/shared_fd.cpp
index 690507c..d2b2ea0 100644
--- a/common/libs/fs/shared_fd.cpp
+++ b/common/libs/fs/shared_fd.cpp
@@ -22,6 +22,7 @@
#include <fcntl.h>
#include <netinet/in.h>
#include <unistd.h>
+#include <algorithm>
#include "common/libs/auto_resources/auto_resources.h"
#include "common/libs/glog/logging.h"
@@ -76,6 +77,23 @@
return true;
}
+bool FileInstance::CopyFrom(FileInstance& in, size_t length) {
+ AutoFreeBuffer buffer;
+ buffer.Resize(8192);
+ while (length > 0) {
+ ssize_t num_read = in.Read(buffer.data(), std::min(buffer.size(), length));
+ length -= num_read;
+ if (num_read <= 0) {
+ return false;
+ }
+ if (Write(buffer.data(), num_read) != num_read) {
+ // The caller will have to log an appropriate message.
+ return false;
+ }
+ }
+ return true;
+}
+
void FileInstance::Close() {
AutoFreeBuffer message;
if (fd_ == -1) {
diff --git a/common/libs/fs/shared_fd.h b/common/libs/fs/shared_fd.h
index 58d3db1..f5dd451 100644
--- a/common/libs/fs/shared_fd.h
+++ b/common/libs/fs/shared_fd.h
@@ -223,6 +223,7 @@
// The non-const reference is needed to avoid binding this to a particular
// reference type.
bool CopyFrom(FileInstance& in);
+ bool CopyFrom(FileInstance& in, size_t length);
int UNMANAGED_Dup() {
errno = 0;