Add `persistprops` to edit persistent properties offline

This commit is contained in:
Pierre-Hugues Husson 2019-06-11 16:30:37 +02:00
parent 9b40bccd82
commit 9fcb8428ac
3 changed files with 98 additions and 0 deletions

View File

@ -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"
],
}

View File

@ -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;
}

55
cmds/persistprops.cpp Normal file
View File

@ -0,0 +1,55 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "device/phh/treble/cmds/persistent_properties.pb.h"
#include <algorithm>
#include <iostream>
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);
}