From 2952b5d65d8920ab3ee41433399915b0b20b51a2 Mon Sep 17 00:00:00 2001 From: tangjie02 Date: Thu, 29 Aug 2024 09:58:19 +0800 Subject: [PATCH 25/27] fix(timedate): Fix the ntp service load order logic error. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修复ntp服务加载顺序逻辑错误问题,之前的代码加载顺序是随机的 - 去重逻辑有问题,std::unique只会对相邻的元素进行去重 Signed-off-by: tangjie02 --- plugins/timedate/timedate-manager.cpp | 42 ++++++++++++++++++--------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/plugins/timedate/timedate-manager.cpp b/plugins/timedate/timedate-manager.cpp index fdd9acc..1a28877 100644 --- a/plugins/timedate/timedate-manager.cpp +++ b/plugins/timedate/timedate-manager.cpp @@ -1,14 +1,14 @@ /** - * Copyright (c) 2020 ~ 2021 KylinSec Co., Ltd. + * Copyright (c) 2020 ~ 2021 KylinSec Co., Ltd. * kiran-cc-daemon is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: - * http://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, - * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, - * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - * + * http://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + * * Author: tangjie02 */ @@ -387,7 +387,7 @@ void TimedateManager::init_ntp_units() this->ntp_unit_name_.clear(); for (auto &ntp_unit : ntp_units) { - if (ntp_unit == ntp_units.back()) + if (ntp_unit == ntp_units.front()) { this->ntp_unit_name_ = ntp_unit; continue; @@ -426,11 +426,21 @@ std::vector TimedateManager::get_ntp_units() { auto &unit_dir = *iter; Glib::Dir dir(unit_dir); + std::vector paths; for (auto dir_iter = dir.begin(); dir_iter != dir.end(); ++dir_iter) { auto entry = *dir_iter; auto path = fmt::format("{0}/{1}", unit_dir, entry); + paths.push_back(path); + } + + // 排序确定加载优先级,字母序越大的优先使用 + std::sort(paths.begin(), paths.end(), std::greater()); + + for (auto iter = paths.begin(); iter != paths.end(); ++iter) + { + auto path = *iter; std::string contents; try @@ -460,8 +470,15 @@ std::vector TimedateManager::get_ntp_units() continue; } - KLOG_DEBUG_TIMEDATE("Insert ntp unit: %s %s.", line.c_str(), entry.c_str()); - ntp_units.push_back(line); + if (std::find(ntp_units.begin(), ntp_units.end(), line) == ntp_units.end()) + { + KLOG_DEBUG_TIMEDATE("Insert ntp unit: %s %s.", line.c_str(), path.c_str()); + ntp_units.push_back(line); + } + else + { + KLOG_DEBUG_TIMEDATE("Ignore duplication ntp unit: %s %s.", line.c_str(), path.c_str()); + } } } } @@ -471,9 +488,6 @@ std::vector TimedateManager::get_ntp_units() } } - // Remove duplicates. - auto iter = std::unique(ntp_units.begin(), ntp_units.end()); - ntp_units.erase(iter, ntp_units.end()); return ntp_units; } -- 2.27.0