Skip to content

Commit 4544d99

Browse files
weivincewangGerrit Code Review
authored and
Gerrit Code Review
committed
Merge "init: add a copy_per_line built-in command"
2 parents 15cd1cd + 49d2598 commit 4544d99

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

init/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,10 @@ Commands
451451
exist. And it will be truncated if dst file is a normal regular file and
452452
already exists.
453453

454+
`copy_per_line <src> <dst>`
455+
> Copies a file line by line. Similar to copy, but useful for dst is a sysfs node
456+
that doesn't handle multiple lines of data.
457+
454458
`domainname <name>`
455459
> Set the domain name.
456460

init/builtins.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ using namespace std::literals::string_literals;
8888

8989
using android::base::Basename;
9090
using android::base::SetProperty;
91+
using android::base::Split;
9192
using android::base::StartsWith;
9293
using android::base::StringPrintf;
9394
using android::base::unique_fd;
@@ -968,6 +969,23 @@ static Result<void> do_copy(const BuiltinArguments& args) {
968969
return {};
969970
}
970971

972+
static Result<void> do_copy_per_line(const BuiltinArguments& args) {
973+
std::string file_contents;
974+
if (!android::base::ReadFileToString(args[1], &file_contents, true)) {
975+
return Error() << "Could not read input file '" << args[1] << "'";
976+
}
977+
auto lines = Split(file_contents, "\n");
978+
for (const auto& line : lines) {
979+
auto result = WriteFile(args[2], line);
980+
if (!result.ok()) {
981+
LOG(VERBOSE) << "Could not write to output file '" << args[2] << "' with '" << line
982+
<< "' : " << result.error();
983+
}
984+
}
985+
986+
return {};
987+
}
988+
971989
static Result<void> do_chown(const BuiltinArguments& args) {
972990
auto uid = DecodeUid(args[1]);
973991
if (!uid.ok()) {
@@ -1366,6 +1384,7 @@ const BuiltinFunctionMap& GetBuiltinFunctionMap() {
13661384
{"class_start_post_data", {1, 1, {false, do_class_start_post_data}}},
13671385
{"class_stop", {1, 1, {false, do_class_stop}}},
13681386
{"copy", {2, 2, {true, do_copy}}},
1387+
{"copy_per_line", {2, 2, {true, do_copy_per_line}}},
13691388
{"domainname", {1, 1, {true, do_domainname}}},
13701389
{"enable", {1, 1, {false, do_enable}}},
13711390
{"exec", {1, kMax, {false, do_exec}}},

0 commit comments

Comments
 (0)