1 /*
2 * Copyright (c) 2024 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 #define LOG_TAG "CorruptReporter"
16 #include "utils/corrupt_reporter.h"
17
18 #include "log_print.h"
19 #include "utils/anonymous.h"
20 #include <fcntl.h>
21 #include <sys/stat.h>
22 #include <unistd.h>
23
24 namespace OHOS::DistributedData {
25 constexpr const char *DB_CORRUPTED_POSTFIX = ".corruptedflg";
26
CreateCorruptedFlag(const std::string & path,const std::string & dbName)27 bool CorruptReporter::CreateCorruptedFlag(const std::string &path, const std::string &dbName)
28 {
29 if (path.empty() || dbName.empty()) {
30 ZLOGW("The path or dbName is empty, path:%{public}s, dbName:%{public}s", Anonymous::Change(path).c_str(),
31 Anonymous::Change(dbName).c_str());
32 return false;
33 }
34 std::string flagFileName = path + "/" + dbName + DB_CORRUPTED_POSTFIX;
35 if (access(flagFileName.c_str(), F_OK) == 0) {
36 return true;
37 }
38 int fd = creat(flagFileName.c_str(), S_IRUSR | S_IWUSR);
39 if (fd == -1) {
40 ZLOGW("Create corrupted flag fail, flagFileName:%{public}s, errno:%{public}d",
41 Anonymous::Change(flagFileName).c_str(), errno);
42 return false;
43 }
44 close(fd);
45 return true;
46 }
47
HasCorruptedFlag(const std::string & path,const std::string & dbName)48 bool CorruptReporter::HasCorruptedFlag(const std::string &path, const std::string &dbName)
49 {
50 if (path.empty() || dbName.empty()) {
51 ZLOGW("The path or dbName is empty, path:%{public}s, dbName:%{public}s", Anonymous::Change(path).c_str(),
52 Anonymous::Change(dbName).c_str());
53 return false;
54 }
55 std::string flagFileName = path + "/" + dbName + DB_CORRUPTED_POSTFIX;
56 return access(flagFileName.c_str(), F_OK) == 0;
57 }
58
DeleteCorruptedFlag(const std::string & path,const std::string & dbName)59 bool CorruptReporter::DeleteCorruptedFlag(const std::string &path, const std::string &dbName)
60 {
61 if (path.empty() || dbName.empty()) {
62 ZLOGW("The path or dbName is empty, path:%{public}s, dbName:%{public}s", Anonymous::Change(path).c_str(),
63 Anonymous::Change(dbName).c_str());
64 return false;
65 }
66 std::string flagFileName = path + "/" + dbName + DB_CORRUPTED_POSTFIX;
67 int result = remove(flagFileName.c_str());
68 if (result != 0) {
69 ZLOGW("Remove corrupted flag fail, flagFileName:%{public}s, errno:%{public}d",
70 Anonymous::Change(flagFileName).c_str(), errno);
71 return false;
72 }
73 return true;
74 }
75 } // namespace OHOS::DistributedData
76