1 /*
2  * Copyright (c) 2023 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 "config_subscriber.h"
17 
18 #include <fstream>
19 #include <mutex>
20 
21 #include "directory_ex.h"
22 #include "string_ex.h"
23 
24 #include "bigdata.h"
25 #include "event_config.h"
26 #include "config_define.h"
27 #include "config_manager.h"
28 #include "model_config.h"
29 #include "security_guard_log.h"
30 #include "security_guard_utils.h"
31 
32 namespace OHOS::Security::SecurityGuard {
33 
UpdateConfig(const std::string &file)34 bool ConfigSubscriber::UpdateConfig(const std::string &file)
35 {
36     ConfigUpdateEvent event{};
37     bool isSuccess = false;
38     if (file == CONFIG_CACHE_FILES[EVENT_CFG_INDEX]) {
39         isSuccess = ConfigManager::UpdataConfig<EventConfig>();
40     } else if (file == CONFIG_CACHE_FILES[MODEL_CFG_INDEX]) {
41         isSuccess = ConfigManager::UpdataConfig<ModelConfig>();
42     }
43     event.path = file;
44     event.time = SecurityGuardUtils::GetDate();
45     event.ret = isSuccess ? SUCCESS : FAILED;
46     SGLOGD("file path=%{public}s, TIME=%{public}s, ret=%{public}d", event.path.c_str(), event.time.c_str(),
47         event.ret);
48     BigData::ReportConfigUpdateEvent(event);
49 
50     if (file != CONFIG_CACHE_FILES[EVENT_CFG_INDEX] && file != CONFIG_CACHE_FILES[MODEL_CFG_INDEX]) {
51         return true;
52     }
53     if (!RemoveFile(file)) {
54         SGLOGE("remove file error, %{public}s", strerror(errno));
55     }
56     return isSuccess;
57 }
58 } // OHOS::Security::SecurityGuard
59