diff --git a/cmds/Android.bp b/cmds/Android.bp index 333bca8..0ab6a7f 100644 --- a/cmds/Android.bp +++ b/cmds/Android.bp @@ -123,3 +123,19 @@ cc_binary { "libhidlbase", ], } + +cc_binary { + name: "persistprops", + srcs: [ + "persistent_properties.proto", + "persistprops.cpp", + ], + static_executable: true, + proto: { + type: "lite", + static: true, + }, + static_libs: [ + "libprotobuf-cpp-lite" + ], +} diff --git a/cmds/persistent_properties.proto b/cmds/persistent_properties.proto new file mode 100644 index 0000000..c8d2e3a --- /dev/null +++ b/cmds/persistent_properties.proto @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2017 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. + */ + +syntax = "proto2"; +option optimize_for = LITE_RUNTIME; + +message PersistentProperties { + message PersistentPropertyRecord { + optional string name = 1; + optional string value = 2; + } + + repeated PersistentPropertyRecord properties = 1; +} diff --git a/cmds/persistprops.cpp b/cmds/persistprops.cpp new file mode 100644 index 0000000..bc376a6 --- /dev/null +++ b/cmds/persistprops.cpp @@ -0,0 +1,55 @@ +#include +#include +#include +#include + +#include "device/phh/treble/cmds/persistent_properties.pb.h" +#include +#include + +int main(int argc, char **argv) { + int fd = open("persistent_properties", O_RDWR); + off_t size = lseek(fd, 0, SEEK_END); + lseek(fd, 0, SEEK_SET); + char *data = (char*) malloc(size); + int ret = read(fd, data, size); + + PersistentProperties props; + bool parsed = props.ParseFromArray(data, size); + std::cout << "Currently has " << props.properties_size() << " props." << std::endl; + for(auto prop: props.properties()) { + std::cout << prop.name() << ":" << prop.value() << std::endl; + } + + if(argc == 1) { + close(fd); + return 0; + } + if(argc != 3) { + std::cout << "Usage: " << argv[0] << " [prop value]" << std::endl; + return -1; + } + + std::string property(argv[1]); + std::string value(argv[2]); + + auto p = props.mutable_properties(); + auto it = std::find_if(p->begin(), p->end(), [=](const auto& v) { return v.name() == property; }); + if(it == p->end()) { + std::cout << "Property not found, adding it" << std::endl; + auto *record = p->Add();; + record->set_name(property); + record->set_value(value); + } else { + std::cout << "Property found, replacing it" << std::endl; + it->set_value(value); + } + + size_t write_size = props.ByteSize(); + char *write_buffer = (char*) malloc(write_size); + props.SerializeToArray(write_buffer, write_size); + ftruncate(fd, 0); + lseek(fd, 0, SEEK_SET); + write(fd, write_buffer, write_size); + close(fd); +}