Merge "init: Use std::string for write_file()" am: 815578cef2

am: 5ff631620e

Change-Id: I340cd41c89c251d03121079df8457a901228d7b2
This commit is contained in:
Tom Cherry 2017-04-04 02:38:01 +00:00 committed by android-build-merger
commit 5981eae421
5 changed files with 29 additions and 12 deletions

View file

@ -155,7 +155,7 @@ static int do_class_restart(const std::vector<std::string>& args) {
} }
static int do_domainname(const std::vector<std::string>& args) { static int do_domainname(const std::vector<std::string>& args) {
return write_file("/proc/sys/kernel/domainname", args[1].c_str()) ? 0 : 1; return write_file("/proc/sys/kernel/domainname", args[1]) ? 0 : 1;
} }
static int do_enable(const std::vector<std::string>& args) { static int do_enable(const std::vector<std::string>& args) {
@ -179,7 +179,7 @@ static int do_export(const std::vector<std::string>& args) {
} }
static int do_hostname(const std::vector<std::string>& args) { static int do_hostname(const std::vector<std::string>& args) {
return write_file("/proc/sys/kernel/hostname", args[1].c_str()) ? 0 : 1; return write_file("/proc/sys/kernel/hostname", args[1]) ? 0 : 1;
} }
static int do_ifup(const std::vector<std::string>& args) { static int do_ifup(const std::vector<std::string>& args) {
@ -696,9 +696,7 @@ static int do_verity_update_state(const std::vector<std::string>& args) {
} }
static int do_write(const std::vector<std::string>& args) { static int do_write(const std::vector<std::string>& args) {
const char* path = args[1].c_str(); return write_file(args[1], args[2]) ? 0 : 1;
const char* value = args[2].c_str();
return write_file(path, value) ? 0 : 1;
} }
static int do_copy(const std::vector<std::string>& args) { static int do_copy(const std::vector<std::string>& args) {

View file

@ -96,7 +96,7 @@ bool Parser::ParseConfigFile(const std::string& path) {
LOG(INFO) << "Parsing file " << path << "..."; LOG(INFO) << "Parsing file " << path << "...";
Timer t; Timer t;
std::string data; std::string data;
if (!read_file(path.c_str(), &data)) { if (!read_file(path, &data)) {
return false; return false;
} }

View file

@ -163,10 +163,11 @@ out_unlink:
return -1; return -1;
} }
bool read_file(const char* path, std::string* content) { bool read_file(const std::string& path, std::string* content) {
content->clear(); content->clear();
android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC))); android::base::unique_fd fd(
TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC)));
if (fd == -1) { if (fd == -1) {
return false; return false;
} }
@ -186,9 +187,9 @@ bool read_file(const char* path, std::string* content) {
return android::base::ReadFdToString(fd, content); return android::base::ReadFdToString(fd, content);
} }
bool write_file(const char* path, const char* content) { bool write_file(const std::string& path, const std::string& content) {
android::base::unique_fd fd( android::base::unique_fd fd(
TEMP_FAILURE_RETRY(open(path, O_WRONLY | O_CREAT | O_NOFOLLOW | O_CLOEXEC, 0600))); TEMP_FAILURE_RETRY(open(path.c_str(), O_WRONLY | O_CREAT | O_NOFOLLOW | O_CLOEXEC, 0600)));
if (fd == -1) { if (fd == -1) {
PLOG(ERROR) << "write_file: Unable to open '" << path << "'"; PLOG(ERROR) << "write_file: Unable to open '" << path << "'";
return false; return false;

View file

@ -35,8 +35,8 @@ using namespace std::chrono_literals;
int create_socket(const char *name, int type, mode_t perm, int create_socket(const char *name, int type, mode_t perm,
uid_t uid, gid_t gid, const char *socketcon); uid_t uid, gid_t gid, const char *socketcon);
bool read_file(const char* path, std::string* content); bool read_file(const std::string& path, std::string* content);
bool write_file(const char* path, const char* content); bool write_file(const std::string& path, const std::string& content);
class Timer { class Timer {
public: public:

View file

@ -18,6 +18,7 @@
#include <errno.h> #include <errno.h>
#include <android-base/test_utils.h>
#include <gtest/gtest.h> #include <gtest/gtest.h>
TEST(util, read_file_ENOENT) { TEST(util, read_file_ENOENT) {
@ -37,6 +38,23 @@ TEST(util, read_file_success) {
EXPECT_STREQ("Linux", s.c_str()); EXPECT_STREQ("Linux", s.c_str());
} }
TEST(util, write_file_binary) {
std::string contents("abcd");
contents.push_back('\0');
contents.push_back('\0');
contents.append("dcba");
ASSERT_EQ(10u, contents.size());
TemporaryFile tf;
ASSERT_TRUE(tf.fd != -1);
EXPECT_TRUE(write_file(tf.path, contents)) << strerror(errno);
std::string read_back_contents;
EXPECT_TRUE(read_file(tf.path, &read_back_contents)) << strerror(errno);
EXPECT_EQ(contents, read_back_contents);
EXPECT_EQ(10u, read_back_contents.size());
}
TEST(util, decode_uid) { TEST(util, decode_uid) {
EXPECT_EQ(0U, decode_uid("root")); EXPECT_EQ(0U, decode_uid("root"));
EXPECT_EQ(UINT_MAX, decode_uid("toot")); EXPECT_EQ(UINT_MAX, decode_uid("toot"));