if (file_name_.empty())
return;
- int size = GetFileSize(file_name_);
- if (size > rotation_size_)
+ if (GetFileSize(file_name_) > rotation_size_) {
if (!Rotate())
return;
+ }
std::ofstream ofs(file_name_.c_str(), std::ios::app);
ofs << log_stream_->str();
bool FileLogBackend::Rotate() {
for (int i = max_rotation_; i > 0; i--) {
std::string old_log = file_name_ + "." + std::to_string(i);
+
// the oldest log will be removed
if (i == max_rotation_) {
if (std::remove(old_log.c_str()) != 0)
return false;
}
}
+
std::string new_log = file_name_ + ".1";
if (std::rename(file_name_.c_str(), new_log.c_str()) != 0)
return false;
struct tm gmt;
if (!gmtime_r(&seconds, &gmt))
return "|";
+
int32_t miliseconds = ts.tv_nsec / 1000000;
char buf[32];
strftime(buf, sizeof(buf), "%Y%m%d.%H%M%S", &gmt);
+
char timestamp[64];
snprintf(timestamp, sizeof(timestamp), "%s.%03dUTC|", buf, miliseconds);
#define PKGMGR_FOTA_PATH tzplatform_mkpath(TZ_SYS_GLOBALUSER_DATA, \
"pkgmgr/fota")
-
using std::string;
namespace {
constexpr char kTokenPkgidStr[] = "package=";
constexpr char kTokenVersionStr[] = "version=";
constexpr char kTokenRemoveStr[] = "removable=";
-
constexpr char kDefaultVersionStr[] = "0.0.1";
constexpr char kDefaultPkgTypeStr[] = "tpk";
xmlInitParser();
manifest_dir_ = USR_MANIFEST_DIRECTORY;
preload_rw_list_path_ = PRELOAD_RW_LIST_FILE;
+
if (access(kOptZipFile, F_OK) != 0)
return;
- int ret = UnzipFileOnlyToPath(ALL_PRELOAD_RW_PKG_LIST, PKGMGR_FOTA_PATH);
- if (ret != 0) {
+ if (UnzipFileOnlyToPath(ALL_PRELOAD_RW_PKG_LIST, PKGMGR_FOTA_PATH) != 0) {
LOG(ERROR) << "Failed to unzip file from backup";
return;
}
const char* unzip_to) {
const char* unzip_argv[] = { "/usr/bin/unzip", "-joXqq",
kOptZipFile, dest_path, "-d", unzip_to, nullptr };
+
return BackendInvoker::XSystem(unzip_argv);
}
int PkgFinder::PkgidListCb(const pkgmgrinfo_pkginfo_h handle, void* user_data) {
char* pkgid = nullptr;
- int ret = -1;
- ret = pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid);
- if (ret < 0)
+ if (pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgid) != PMINFO_R_OK)
return 0;
char* version = nullptr;
- ret = pkgmgrinfo_pkginfo_get_version(handle, &version);
- if (ret < 0)
+ if (pkgmgrinfo_pkginfo_get_version(handle, &version) != PMINFO_R_OK)
return 0;
char* type = nullptr;
- ret = pkgmgrinfo_pkginfo_get_type(handle, &type);
- if (ret < 0)
+ if (pkgmgrinfo_pkginfo_get_type(handle, &type) != PMINFO_R_OK)
return 0;
PkgFinder* finder = static_cast<PkgFinder*>(user_data);
finder->old_pkgs_.emplace_back(pkgid, version, type, finder->read_only_);
+
return 0;
}
LOG(ERROR) << "pkgmgrinfo_pkginfo_filter_add_bool failed " << ret;
return -1;
}
-
read_only_ = read_only;
+
ret = pkgmgrinfo_pkginfo_filter_foreach_pkginfo(handle_auto.get(),
PkgidListCb, this);
if (ret != PMINFO_R_OK) {
}
while ((entry = readdir(dir)) != nullptr) {
- if (entry->d_name[0] == '.') continue;
+ if (entry->d_name[0] == '.')
+ continue;
string manifest = GetValidManifest(entry->d_name);
- if (manifest.empty()) {
+ if (manifest.empty())
continue;
- }
string buf = xml_directory + "/" + manifest;
string pkgid = FindInfoFromXml(buf, "package");
reader = xmlReaderForFile(manifest.c_str(), nullptr, 0);
- if (reader) {
- if (MoveToChildElement(reader, -1)) {
- node = xmlTextReaderConstName(reader);
- if (!node) {
- LOG(ERROR) << "xmlTextReaderConstName value is NULL";
- goto end;
- }
-
- if (!strcmp(ASCII(node), "manifest")) {
- tmp = xmlTextReaderGetAttribute(reader,
- XMLCHAR(find_info.c_str()));
- if (tmp) {
- info_val = ASCII(tmp);
- free(tmp);
- tmp = nullptr;
- }
- } else {
- LOG(ERROR) << "Manifest Node is not found";
- }
- }
- } else {
+ if (!reader) {
LOG(ERROR) << "xmlReaderForFile value is NULL";
+ return "";
+ }
+ std::unique_ptr<std::remove_pointer<xmlTextReaderPtr>::type,
+ decltype(xmlFreeTextReader)*> reader_auto(
+ reader, xmlFreeTextReader);
+
+ if (!MoveToChildElement(reader_auto.get(), -1))
+ return {};
+
+ node = xmlTextReaderConstName(reader_auto.get());
+ if (!node) {
+ LOG(ERROR) << "xmlTextReaderConstName value is NULL";
+ return {};
}
-end:
- if (reader)
- xmlFreeTextReader(reader);
+ if (!strcmp(ASCII(node), "manifest")) {
+ tmp = xmlTextReaderGetAttribute(reader_auto.get(),
+ XMLCHAR(find_info.c_str()));
+ if (tmp) {
+ info_val = ASCII(tmp);
+ free(tmp);
+ }
+ } else {
+ LOG(ERROR) << "Manifest Node is not found";
+ }
return info_val;
}
-int PkgFinder::MoveToChildElement(xmlTextReaderPtr reader,
- int depth) {
+int PkgFinder::MoveToChildElement(xmlTextReaderPtr reader, int depth) {
int ret = xmlTextReaderRead(reader);
int cur = xmlTextReaderDepth(reader);
+
while (ret == 1) {
switch (xmlTextReaderNodeType(reader)) {
case XML_READER_TYPE_ELEMENT:
ret = xmlTextReaderRead(reader);
cur = xmlTextReaderDepth(reader);
}
+
return ret;
}
int PkgFinder::FindPreloadPkgidFromFile() {
char buf[kBufSize] = {0};
- FILE *fp = nullptr;
+ FILE *fp;
fp = fopen(preload_rw_list_path_.c_str(), "r");
if (fp == nullptr) {
using std::string;
using std::unique_ptr;
+namespace {
+
+enum class UpdateType {
+ kRo2Ro,
+ kRw2Rw,
+ kRw2Ro,
+ kRo2Rw,
+ kUnknown
+};
+
+UpdateType GetUpdateType(
+ common_fota::PkgContext old_pkg, common_fota::PkgContext new_pkg) {
+ if (old_pkg.IsReadOnly() && new_pkg.IsReadOnly())
+ return UpdateType::kRo2Ro;
+
+ if (!old_pkg.IsReadOnly() && !new_pkg.IsReadOnly())
+ return UpdateType::kRw2Rw;
+
+ if (!old_pkg.IsReadOnly() && new_pkg.IsReadOnly())
+ return UpdateType::kRw2Ro;
+
+ if (old_pkg.IsReadOnly() && !new_pkg.IsReadOnly())
+ return UpdateType::kRo2Rw;
+
+ return UpdateType::kUnknown;
+}
+
+} // namespace
+
namespace common_fota {
list<unique_ptr<PkgUpgrader>> PkgUpgraderFactory::MakeList(PkgFinder* finder) {
const auto* old_pkg = FindPkgById(old_pkgs, new_pkg.GetId());
if (old_pkg != nullptr) {
// UPDATE
- if (old_pkg->IsReadOnly() && new_pkg.IsReadOnly()) {
- // RO to RO
+ UpdateType update_type = GetUpdateType(*old_pkg, new_pkg);
+
+ switch(update_type) {
+ case UpdateType::kRo2Ro:
pkgs.emplace_back(std::make_unique<RoUpgrader>(
std::make_unique<SimpleUpgrader>(*old_pkg, PkgOperation::COMPLEX),
std::make_unique<SimpleUpgrader>(new_pkg, PkgOperation::UPDATE)));
- } else if (!old_pkg->IsReadOnly() && !new_pkg.IsReadOnly()) {
- // RW to RW
+ break;
+ case UpdateType::kRw2Rw:
pkgs.emplace_back(std::make_unique<RwUpgrader>(
std::make_unique<SimpleUpgrader>(*old_pkg, PkgOperation::COMPLEX),
std::make_unique<SimpleUpgrader>(new_pkg, PkgOperation::UPDATE)));
- } else if (!old_pkg->IsReadOnly() && new_pkg.IsReadOnly()) {
- // RW to RO
+ break;
+ case UpdateType::kRw2Ro:
pkgs.emplace_back(std::make_unique<Rw2RoUpgrader>(
- std::make_unique<RwUpgrader>(nullptr,
- std::make_unique<SimpleUpgrader>(*old_pkg,
- PkgOperation::UNINSTALL_KEEP_RW_DATA)),
- std::make_unique<RoUpgrader>(nullptr,
- std::make_unique<SimpleUpgrader>(new_pkg,
- PkgOperation::INSTALL))));
- } else if (old_pkg->IsReadOnly() && !new_pkg.IsReadOnly()) {
- // RO to RW
+ std::make_unique<RwUpgrader>(nullptr,
+ std::make_unique<SimpleUpgrader>(*old_pkg,
+ PkgOperation::UNINSTALL_KEEP_RW_DATA)),
+ std::make_unique<RoUpgrader>(nullptr,
+ std::make_unique<SimpleUpgrader>(new_pkg,
+ PkgOperation::INSTALL))));
+ break;
+ case UpdateType::kRo2Rw:
pkgs.emplace_back(std::make_unique<Ro2RwUpgrader>(
std::make_unique<RoUpgrader>(nullptr,
std::make_unique<SimpleUpgrader>(*old_pkg,
std::make_unique<RwUpgrader>(nullptr,
std::make_unique<SimpleUpgrader>(new_pkg,
PkgOperation::INSTALL))));
+ break;
+ default:
+ break;
}
} else {
// INSTALL