Replace AutoFreeBuffer uses for SharedFD
Uses a combination of std::stringstream, std::string, and
std::vector<char>.
Bug: 144387776
Test: Build and run locally
Change-Id: I61f696c729b5895e922429cec550054196a01199
diff --git a/common/libs/fs/shared_fd.cpp b/common/libs/fs/shared_fd.cpp
index dde0604..6db27ee 100644
--- a/common/libs/fs/shared_fd.cpp
+++ b/common/libs/fs/shared_fd.cpp
@@ -23,6 +23,7 @@
#include <netinet/in.h>
#include <unistd.h>
#include <algorithm>
+#include <vector>
#include "common/libs/auto_resources/auto_resources.h"
#include "common/libs/glog/logging.h"
@@ -57,8 +58,7 @@
namespace cvd {
bool FileInstance::CopyFrom(FileInstance& in) {
- AutoFreeBuffer buffer;
- buffer.Resize(8192);
+ std::vector<char> buffer(8192);
while (true) {
ssize_t num_read = in.Read(buffer.data(), buffer.size());
if (!num_read) {
@@ -78,8 +78,7 @@
}
bool FileInstance::CopyFrom(FileInstance& in, size_t length) {
- AutoFreeBuffer buffer;
- buffer.Resize(8192);
+ std::vector<char> buffer(8192);
while (length > 0) {
ssize_t num_read = in.Read(buffer.data(), std::min(buffer.size(), length));
length -= num_read;
@@ -95,30 +94,34 @@
}
void FileInstance::Close() {
- AutoFreeBuffer message;
+ std::stringstream message;
if (fd_ == -1) {
errno_ = EBADF;
} else if (close(fd_) == -1) {
errno_ = errno;
if (identity_.size()) {
- message.PrintF("%s: %s failed (%s)", __FUNCTION__, identity_.data(),
- StrError());
- Log(message.data());
+ message << __FUNCTION__ << ": " << identity_ << " failed (" << StrError() << ")";
+ std::string message_str = message.str();
+ Log(message_str.c_str());
}
} else {
if (identity_.size()) {
- message.PrintF("%s: %s succeeded", __FUNCTION__, identity_.data());
- Log(message.data());
+ message << __FUNCTION__ << ": " << identity_ << "succeeded";
+ std::string message_str = message.str();
+ Log(message_str.c_str());
}
}
fd_ = -1;
}
void FileInstance::Identify(const char* identity) {
- identity_.PrintF("fd=%d @%p is %s", fd_, this, identity);
- AutoFreeBuffer message;
- message.PrintF("%s: %s", __FUNCTION__, identity_.data());
- Log(message.data());
+ std::stringstream identity_stream;
+ identity_stream << "fd=" << fd_ << " @" << this << " is " << identity;
+ identity_ = identity_stream.str();
+ std::stringstream message;
+ message << __FUNCTION__ << ": " << identity_;
+ std::string message_str = message.str();
+ Log(message_str.c_str());
}
bool FileInstance::IsSet(fd_set* in) const {
diff --git a/common/libs/fs/shared_fd.h b/common/libs/fs/shared_fd.h
index b811f4a..b64ad09 100644
--- a/common/libs/fs/shared_fd.h
+++ b/common/libs/fs/shared_fd.h
@@ -33,6 +33,7 @@
#include <sys/un.h>
#include <memory>
+#include <sstream>
#include <errno.h>
#include <fcntl.h>
@@ -515,7 +516,9 @@
// Ensure every file descriptor managed by a FileInstance has the CLOEXEC
// flag
TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD, FD_CLOEXEC));
- identity_.PrintF("fd=%d @%p", fd, this);
+ std::stringstream identity;
+ identity << "fd=" << fd << " @" << this;
+ identity_ = identity.str();
}
FileInstance* Accept(struct sockaddr* addr, socklen_t* addrlen) const {
@@ -529,7 +532,7 @@
int fd_;
int errno_;
- AutoFreeBuffer identity_;
+ std::string identity_;
char strerror_buf_[160];
};