• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define LOG_TAG "RdbSqlUtils"
17 #include "rdb_sql_utils.h"
18 
19 #include <sys/stat.h>
20 #include <unistd.h>
21 
22 #include <algorithm>
23 #include <cstdio>
24 
25 #include "acl.h"
26 #include "logger.h"
27 #include "rdb_errno.h"
28 #include "rdb_platform.h"
29 #include "sqlite_sql_builder.h"
30 #include "sqlite_utils.h"
31 #include "rdb_fault_hiview_reporter.h"
32 
33 namespace OHOS {
34 using namespace Rdb;
35 namespace NativeRdb {
36 using namespace OHOS::DATABASE_UTILS;
37 constexpr int32_t SERVICE_GID = 3012;
CreateDirectory(const std::string & databaseDir)38 int RdbSqlUtils::CreateDirectory(const std::string &databaseDir)
39 {
40     std::string tempDirectory = databaseDir;
41     std::vector<std::string> directories;
42 
43     size_t pos = tempDirectory.find('/');
44     while (pos != std::string::npos) {
45         std::string directory = tempDirectory.substr(0, pos);
46         directories.push_back(directory);
47         tempDirectory = tempDirectory.substr(pos + 1);
48         pos = tempDirectory.find('/');
49     }
50     directories.push_back(tempDirectory);
51 
52     std::string databaseDirectory;
53     for (const std::string &directory : directories) {
54         databaseDirectory = databaseDirectory + "/" + directory;
55         if (access(databaseDirectory.c_str(), F_OK) != 0) {
56             if (MkDir(databaseDirectory)) {
57                 LOG_ERROR("failed to mkdir errno[%{public}d] %{public}s", errno,
58                     SqliteUtils::Anonymous(databaseDirectory).c_str());
59                 RdbFaultHiViewReporter::ReportFault(RdbFaultEvent(FT_EX_FILE, E_CREATE_FOLDER_FAIL, BUNDLE_NAME_COMMON,
60                     "failed to mkdir errno[ " + std::to_string(errno) + "]," + databaseDirectory));
61                 return E_CREATE_FOLDER_FAIL;
62             }
63             // Set the default ACL attribute to the database root directory to ensure that files created by the server
64             // also have permission to operate on the client side.
65             Acl acl(databaseDirectory);
66             acl.SetDefaultUser(GetUid(), Acl::R_RIGHT | Acl::W_RIGHT);
67             acl.SetDefaultGroup(SERVICE_GID, Acl::R_RIGHT | Acl::W_RIGHT);
68         }
69     }
70     return E_OK;
71 }
72 
GetCustomDatabasePath(const std::string & rootDir,const std::string & name,const std::string & customDir)73 std::pair<std::string, int> RdbSqlUtils::GetCustomDatabasePath(
74     const std::string &rootDir, const std::string &name, const std::string &customDir)
75 {
76     std::string databasePath;
77     databasePath.append(rootDir).append("/").append(customDir).append("/").append(name);
78 
79     struct stat fileStat;
80     if (stat(databasePath.c_str(), &fileStat) != 0) {
81         LOG_ERROR("File state error. path: %{public}s, errno: %{public}d",
82             SqliteUtils::Anonymous(databasePath).c_str(), errno);
83         return std::make_pair("", E_INVALID_FILE_PATH);
84     }
85     return std::make_pair(databasePath, E_OK);
86 }
87 
88 /**
89  * @brief get custom data base path.
90  */
GetDefaultDatabasePath(const std::string & baseDir,const std::string & name,const std::string & customDir)91 std::pair<std::string, int> RdbSqlUtils::GetDefaultDatabasePath(
92     const std::string &baseDir, const std::string &name, const std::string &customDir)
93 {
94     int errorCode = E_OK;
95     if (customDir.empty()) {
96         return std::make_pair(GetDefaultDatabasePath(baseDir, name, errorCode), errorCode);
97     }
98 
99     std::string databaseDir;
100     databaseDir.append(baseDir).append("/rdb/").append(customDir);
101 
102     errorCode = CreateDirectory(databaseDir);
103     if (errorCode != E_OK) {
104         LOG_ERROR("failed errno[%{public}d] baseDir : %{public}s name : %{public}s customDir : %{public}s", errno,
105             SqliteUtils::Anonymous(baseDir).c_str(), SqliteUtils::Anonymous(name).c_str(),
106             SqliteUtils::Anonymous(customDir).c_str());
107     }
108     return std::make_pair(databaseDir.append("/").append(name), errorCode);
109 }
110 
111 /**
112  * Get and Check default path.
113  */
GetDefaultDatabasePath(const std::string & baseDir,const std::string & name,int & errorCode)114 std::string RdbSqlUtils::GetDefaultDatabasePath(const std::string &baseDir, const std::string &name, int &errorCode)
115 {
116     std::string databaseDir = baseDir + "/rdb";
117     errorCode = CreateDirectory(databaseDir);
118     if (errorCode != E_OK) {
119         LOG_ERROR("failed errno[%{public}d] baseDir : %{public}s name : %{public}s", errno,
120             SqliteUtils::Anonymous(baseDir).c_str(), SqliteUtils::Anonymous(name).c_str());
121     }
122     return databaseDir.append("/").append(name);
123 }
124 
BuildQueryString(const AbsRdbPredicates & predicates,const std::vector<std::string> & columns)125 std::string RdbSqlUtils::BuildQueryString(const AbsRdbPredicates &predicates, const std::vector<std::string> &columns)
126 {
127     return SqliteSqlBuilder::BuildQueryString(predicates, columns);
128 }
129 } // namespace NativeRdb
130 } // namespace OHOS