1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "sys_installer_server.h"
17 
18 #include "iservice_registry.h"
19 #include "log/log.h"
20 #include "securec.h"
21 #include "system_ability_definition.h"
22 #include "utils.h"
23 
24 namespace OHOS {
25 namespace SysInstaller {
26 REGISTER_SYSTEM_ABILITY_BY_ID(SysInstallerServer, SYS_INSTALLER_DISTRIBUTED_SERVICE_ID, false)
27 
28 using namespace Updater;
29 
SysInstallerServer(int32_t systemAbilityId,bool runOnCreate)30 SysInstallerServer::SysInstallerServer(int32_t systemAbilityId, bool runOnCreate)
31     : SystemAbility(systemAbilityId, runOnCreate)
32 {
33 }
34 
~SysInstallerServer()35 SysInstallerServer::~SysInstallerServer()
36 {
37 }
38 
SysInstallerInit()39 int32_t SysInstallerServer::SysInstallerInit()
40 {
41     std::lock_guard<std::mutex> lock(sysInstallerServerLock_);
42     LOG(INFO) << "SysInstallerInit";
43     if (!logInit_) {
44         (void)Utils::MkdirRecursive(SYS_LOG_DIR, 0777); // 0777 : rwxrwxrwx
45         InitUpdaterLogger("SysInstaller", SYS_LOG_FILE, SYS_STAGE_FILE, SYS_ERROR_FILE);
46         logInit_ = true;
47     }
48 
49     InstallerManager::GetInstance().SysInstallerInit();
50     return 0;
51 }
52 
StartUpdatePackageZip(const std::string & pkgPath)53 int32_t SysInstallerServer::StartUpdatePackageZip(const std::string &pkgPath)
54 {
55     LOG(INFO) << "StartUpdatePackageZip";
56     return InstallerManager::GetInstance().StartUpdatePackageZip(pkgPath);
57 }
58 
SetUpdateCallback(const sptr<ISysInstallerCallback> & updateCallback)59 int32_t SysInstallerServer::SetUpdateCallback(const sptr<ISysInstallerCallback> &updateCallback)
60 {
61     LOG(INFO) << "SetUpdateCallback";
62     return InstallerManager::GetInstance().SetUpdateCallback(updateCallback);
63 }
64 
GetUpdateStatus()65 int32_t SysInstallerServer::GetUpdateStatus()
66 {
67     LOG(INFO) << "GetUpdateStatus";
68     return InstallerManager::GetInstance().GetUpdateStatus();
69 }
70 
StartUpdateParaZip(const std::string & pkgPath,const std::string & location,const std::string & cfgDir)71 int32_t SysInstallerServer::StartUpdateParaZip(const std::string &pkgPath,
72     const std::string &location, const std::string &cfgDir)
73 {
74     LOG(INFO) << "StartUpdateParaZip";
75     return InstallerManager::GetInstance().StartUpdateParaZip(pkgPath, location, cfgDir);
76 }
77 
StartDeleteParaZip(const std::string & location,const std::string & cfgDir)78 int32_t SysInstallerServer::StartDeleteParaZip(const std::string &location, const std::string &cfgDir)
79 {
80     LOG(INFO) << "StartDeleteParaZip";
81     return InstallerManager::GetInstance().StartDeleteParaZip(location, cfgDir);
82 }
83 
AccDecompressAndVerifyPkg(const std::string & srcPath,const std::string & dstPath,const uint32_t type)84 int32_t SysInstallerServer::AccDecompressAndVerifyPkg(const std::string &srcPath,
85     const std::string &dstPath, const uint32_t type)
86 {
87     LOG(INFO) << "AccDecompressAndVerifyPkg";
88     return InstallerManager::GetInstance().AccDecompressAndVerifyPkg(srcPath, dstPath, type);
89 }
90 
AccDeleteDir(const std::string & dstPath)91 int32_t SysInstallerServer::AccDeleteDir(const std::string &dstPath)
92 {
93     LOG(INFO) << "AccDeleteDir";
94     return InstallerManager::GetInstance().AccDeleteDir(dstPath);
95 }
96 
OnStart()97 void SysInstallerServer::OnStart()
98 {
99     LOG(INFO) << "OnStart";
100     bool res = Publish(this);
101     if (!res) {
102         LOG(ERROR) << "OnStart failed";
103     }
104 
105     return;
106 }
107 
OnStop()108 void SysInstallerServer::OnStop()
109 {
110     LOG(INFO) << "OnStop";
111 }
112 } // namespace SysInstaller
113 } // namespace OHOS
114