Line data Source code
1 : /**
2 : * Copyright (c) 2025 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 : #include "base_package_worker.h"
12 :
13 : #include <unistd.h>
14 : #include <sstream>
15 : #include "tsd_log.h"
16 : #include "tsd_scope_guard.h"
17 : #include "tsd_util_func.h"
18 : #include "package_worker_utils.h"
19 :
20 : namespace tsd {
21 2 : uint64_t BasePackageWorker::GetPackageCheckCode()
22 : {
23 2 : const std::lock_guard<std::mutex> lk(packageMtx_);
24 2 : return checkCode_;
25 2 : }
26 :
27 0 : void BasePackageWorker::PreProcessPackage(const std::string& packagePath, const std::string& packageName)
28 : {
29 : // The preprocess such as set the origin and decom path for package, calc the package size, must done in this func
30 0 : DefaultPreProcessPackage(packagePath, packageName);
31 :
32 0 : return;
33 : }
34 :
35 12 : void BasePackageWorker::DefaultPreProcessPackage(const std::string& packagePath, const std::string& packageName)
36 : {
37 : // set the package path
38 12 : SetOriginPackagePath(packagePath, packageName);
39 12 : SetDecompressPackagePath();
40 :
41 : // calc and set the package original size
42 12 : const uint64_t packageSize = PackageWorkerUtils::GetFileSize(originPackagePath_.realPath);
43 12 : if (packageSize == 0UL) {
44 10 : TSD_RUN_WARN("Origin package size is 0, path=%s", originPackagePath_.realPath.c_str());
45 : }
46 12 : SetOriginPackageSize(packageSize);
47 :
48 12 : return;
49 : }
50 :
51 0 : bool BasePackageWorker::IsNeedLoadPackage()
52 : {
53 0 : const size_t fileSize = GetOriginPackageSize();
54 0 : if (fileSize == GetCheckCode()) {
55 0 : TSD_RUN_INFO("No need load package, packageSize=%lu, checkCode=%lu", fileSize, GetCheckCode());
56 0 : return false;
57 : }
58 :
59 0 : return true;
60 : }
61 :
62 7 : TSD_StatusT BasePackageWorker::MoveOriginPackageToDecompressDir() const
63 : {
64 7 : if (decomPackagePath_.path.empty()) {
65 0 : TSD_ERROR("Decompress path is empty");
66 0 : return TSD_INTERNAL_ERROR;
67 : }
68 :
69 7 : const TSD_StatusT ret = PackageWorkerUtils::MakeDirectory(decomPackagePath_.path);
70 7 : if (ret != TSD_OK) {
71 0 : TSD_ERROR("Make decompress dir failed, dir=%s", decomPackagePath_.path.c_str());
72 0 : return TSD_INTERNAL_ERROR;
73 : }
74 :
75 7 : const std::string cmd = GetMovePackageToDecompressDirCmd();
76 7 : const int32_t cmdRet = PackSystem(cmd.c_str());
77 7 : if (cmdRet != 0) {
78 1 : TSD_RUN_WARN(
79 : "Moving the origin package to the decompress path was not successful, ret=%d, cmd=%s, reason=%s", cmdRet,
80 : cmd.c_str(), strerror(errno));
81 : }
82 7 : TSD_INFO("Move origin package to decompress path end, ret=%d, cmd=%s", cmdRet, cmd.c_str());
83 :
84 7 : return TSD_OK;
85 7 : }
86 :
87 6 : TSD_StatusT BasePackageWorker::DecompressPackage() const
88 : {
89 6 : const std::string cmd = GetDecompressPackageCmd();
90 6 : TSD_INFO("Start decompress package, cmd=%s", cmd.c_str());
91 6 : const int32_t ret = PackSystem(cmd.c_str());
92 6 : if (ret != 0) {
93 0 : TSD_RUN_WARN("Decompress package failed, ret=%d, cmd=%s, reason=%s", ret, cmd.c_str(), strerror(errno));
94 : }
95 :
96 6 : TSD_INFO("End decompress package, ret=%d, cmd=%s", ret, cmd.c_str());
97 :
98 6 : return TSD_OK;
99 6 : }
100 :
101 4 : TSD_StatusT BasePackageWorker::PostProcessPackage()
102 : {
103 4 : SetDecompressTimeToNow();
104 4 : SetCheckCode(GetOriginPackageSize());
105 :
106 4 : return TSD_OK;
107 : }
108 :
109 2 : void BasePackageWorker::Clear()
110 : {
111 2 : DefaultClear();
112 :
113 2 : return;
114 : }
115 :
116 2 : void BasePackageWorker::DefaultClear()
117 : {
118 2 : originPackagePath_.Clear();
119 2 : decomPackagePath_.Clear();
120 2 : SetCheckCode(0UL);
121 2 : SetOriginPackageSize(0UL);
122 :
123 2 : return;
124 : }
125 :
126 1 : bool BasePackageWorker::IsNeedUnloadPackage()
127 : {
128 1 : if (GetCheckCode() == 0UL) {
129 1 : return false;
130 : }
131 :
132 0 : return true;
133 : }
134 :
135 : } // namespace tsd
|