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 namespace OHOS {
32 using namespace Rdb;
33 namespace NativeRdb {
34 using namespace OHOS::DATABASE_UTILS;
35 constexpr int32_t SERVICE_GID = 3012;
CreateDirectory(const std::string & databaseDir)36 int RdbSqlUtils::CreateDirectory(const std::string &databaseDir)
37 {
38 std::string tempDirectory = databaseDir;
39 std::vector<std::string> directories;
40
41 size_t pos = tempDirectory.find('/');
42 while (pos != std::string::npos) {
43 std::string directory = tempDirectory.substr(0, pos);
44 directories.push_back(directory);
45 tempDirectory = tempDirectory.substr(pos + 1);
46 pos = tempDirectory.find('/');
47 }
48 directories.push_back(tempDirectory);
49
50 std::string databaseDirectory;
51 for (const std::string& directory : directories) {
52 databaseDirectory = databaseDirectory + "/" + directory;
53 if (access(databaseDirectory.c_str(), F_OK) != 0) {
54 if (MkDir(databaseDirectory)) {
55 LOG_ERROR("failed to mkdir errno[%{public}d] %{public}s", errno,
56 SqliteUtils::Anonymous(databaseDirectory).c_str());
57 return E_CREATE_FOLDER_FAIL;
58 }
59 // Set the default ACL attribute to the database root directory to ensure that files created by the server
60 // also have permission to operate on the client side.
61 Acl acl(databaseDirectory);
62 acl.SetDefaultUser(GetUid(), Acl::R_RIGHT | Acl::W_RIGHT);
63 acl.SetDefaultGroup(SERVICE_GID, Acl::R_RIGHT | Acl::W_RIGHT);
64 }
65 }
66 return E_OK;
67 }
68
69 /**
70 * @brief get custom data base path.
71 */
GetDefaultDatabasePath(const std::string & baseDir,const std::string & name,const std::string & customDir)72 std::pair<std::string, int> RdbSqlUtils::GetDefaultDatabasePath(const std::string &baseDir, const std::string &name,
73 const std::string &customDir)
74 {
75 int errorCode = E_OK;
76 if (customDir.empty()) {
77 return std::make_pair(GetDefaultDatabasePath(baseDir, name, errorCode), errorCode);
78 }
79
80 std::string databaseDir;
81 databaseDir.append(baseDir).append("/rdb/").append(customDir);
82
83 errorCode = CreateDirectory(databaseDir);
84 if (errorCode != E_OK) {
85 LOG_ERROR("failed errno[%{public}d] baseDir : %{public}s name : %{public}s customDir : %{public}s", errno,
86 SqliteUtils::Anonymous(baseDir).c_str(), SqliteUtils::Anonymous(name).c_str(),
87 SqliteUtils::Anonymous(customDir).c_str());
88 }
89 return std::make_pair(databaseDir.append("/").append(name), errorCode);
90 }
91
92 /**
93 * Get and Check default path.
94 */
GetDefaultDatabasePath(const std::string & baseDir,const std::string & name,int & errorCode)95 std::string RdbSqlUtils::GetDefaultDatabasePath(const std::string &baseDir, const std::string &name, int &errorCode)
96 {
97 std::string databaseDir = baseDir + "/rdb";
98 errorCode = CreateDirectory(databaseDir);
99 if (errorCode != E_OK) {
100 LOG_ERROR("failed errno[%{public}d] baseDir : %{public}s name : %{public}s", errno,
101 SqliteUtils::Anonymous(baseDir).c_str(), SqliteUtils::Anonymous(name).c_str());
102 }
103 return databaseDir.append("/").append(name);
104 }
105
BuildQueryString(const AbsRdbPredicates & predicates,const std::vector<std::string> & columns)106 std::string RdbSqlUtils::BuildQueryString(const AbsRdbPredicates &predicates, const std::vector<std::string> &columns)
107 {
108 return SqliteSqlBuilder::BuildQueryString(predicates, columns);
109 }
110 } // namespace NativeRdb
111 } // namespace OHOS