Line data Source code
1 : /**
2 : * Copyright (c) 2026 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #ifndef INNER_INC_PLUGIN_PKG_VERSION_H
12 : #define INNER_INC_PLUGIN_PKG_VERSION_H
13 :
14 : #include <cstdint>
15 : #include <string>
16 :
17 : namespace tsd {
18 :
19 : // 插件包更新策略,对应DRV设备管理模块设置
20 : enum class PluginUpdateStrategy : uint32_t {
21 : PLUGIN_NOT_FORCE_UPDATE = 0U, // 不强制更新:版本比较决定是否更新
22 : PLUGIN_FORCE_UPDATE = 1U, // 强制更新:忽略版本/hash,直接更新
23 : PLUGIN_NO_UPDATE = 2U // 不更新:不加载插件包
24 : };
25 :
26 : // 插件包(.compat.tar.gz)版本信息,由同路径下同名.ini配置文件描述
27 : struct PluginPkgVersion {
28 : std::string version; // 例如 8.5.0
29 : std::string timestamp; // 例如 20260114_115609804
30 :
31 8 : bool Empty() const { return version.empty() && timestamp.empty(); }
32 : };
33 :
34 : class PluginPkgVersionUtil {
35 : public:
36 : // 根据插件包文件名 (cann-xxx-compat.tar.gz) 得到对应ini配置文件名 (cann-xxx-compat.ini)
37 : static std::string GetIniNameByPkgName(const std::string& pkgName);
38 :
39 : // 从一行配置 (key=value) 中提取key 与 value,成功返回 true
40 : static bool ParseLine(const std::string& line, std::string& key, std::string& value);
41 :
42 : // 解析 .ini 文件得到 version / timestamp
43 : static bool ParseIniFile(const std::string& iniPath, PluginPkgVersion& info);
44 :
45 : // 比较两个版本号,按 '.' 分割逐段数值比较
46 : // 返回 -1 表示 a < b, 0 表示 ==, 1 表示 a > b
47 : static int32_t CompareVersion(const std::string& versionA, const std::string& versionB);
48 :
49 : // 比较两个时间戳,简单字典序比较 (时间戳格式 YYYYMMDD_HHMMSSXXX 单调)
50 : static int32_t CompareTimestamp(const std::string& timestampA, const std::string& timestampB);
51 :
52 : // 综合 version + timestamp 比较,version 优先
53 : static int32_t Compare(const PluginPkgVersion& a, const PluginPkgVersion& b);
54 : };
55 :
56 : } // namespace tsd
57 :
58 : #endif // INNER_INC_PLUGIN_PKG_VERSION_H
|