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 "document_store_manager.h"
17 
18 #include "db_config.h"
19 #include "doc_errno.h"
20 #include "grd_base/grd_type_export.h"
21 #include "kv_store_manager.h"
22 #include "os_api.h"
23 #include "rd_log_print.h"
24 
25 namespace DocumentDB {
26 namespace {
CheckDBOpenFlag(unsigned int flag)27 bool CheckDBOpenFlag(unsigned int flag)
28 {
29     unsigned int mask = ~(GRD_DB_OPEN_CREATE | GRD_DB_OPEN_CHECK_FOR_ABNORMAL | GRD_DB_OPEN_CHECK |
30         GRD_DB_OPEN_SHARED_READ_ONLY);
31     unsigned int invalidOpt = (GRD_DB_OPEN_CHECK_FOR_ABNORMAL | GRD_DB_OPEN_CHECK);
32     return ((flag & mask) == 0x00) && ((flag & invalidOpt) != invalidOpt);
33 }
34 
CheckDBCloseFlag(unsigned int flag)35 bool CheckDBCloseFlag(unsigned int flag)
36 {
37     return (flag == GRD_DB_CLOSE) || (flag == GRD_DB_CLOSE_IGNORE_ERROR);
38 }
39 
CheckDBCreate(uint32_t flags,const std::string & path)40 bool CheckDBCreate(uint32_t flags, const std::string &path)
41 {
42     if ((flags & GRD_DB_OPEN_CREATE) == 0 && !OSAPI::IsPathExist(path)) {
43         return false;
44     }
45     return true;
46 }
47 } // namespace
48 
49 std::mutex DocumentStoreManager::openCloseMutex_;
50 std::map<std::string, int> DocumentStoreManager::dbConnCount_;
51 
GetDocumentStore(const std::string & path,const std::string & config,uint32_t flags,DocumentStore * & store)52 int DocumentStoreManager::GetDocumentStore(const std::string &path, const std::string &config, uint32_t flags,
53     DocumentStore *&store)
54 {
55     std::string canonicalPath;
56     std::string dbName;
57     int errCode = CheckDBPath(path, canonicalPath, dbName);
58     if (errCode != E_OK) {
59         GLOGE("Check document db file path failed.");
60         return errCode;
61     }
62 
63     DBConfig dbConfig = DBConfig::ReadConfig(config, errCode);
64     if (errCode != E_OK) {
65         GLOGE("Read db config str failed. %d", errCode);
66         return errCode;
67     }
68 
69     if (!CheckDBOpenFlag(flags)) {
70         GLOGE("Check document db open flags failed.");
71         return -E_INVALID_ARGS;
72     }
73     if (!CheckDBCreate(flags, path)) {
74         GLOGE("Open db failed, file no exists.");
75         return -E_INVALID_ARGS;
76     }
77 
78     std::lock_guard<std::mutex> lock(openCloseMutex_);
79 
80     std::string dbRealPath = canonicalPath + "/" + dbName;
81     auto it = dbConnCount_.find(dbRealPath);
82     bool isFirstOpen = (it == dbConnCount_.end() || it->second == 0);
83 
84     KvStoreExecutor *executor = nullptr;
85     errCode = KvStoreManager::GetKvStore(dbRealPath, dbConfig, isFirstOpen, executor);
86     if (errCode != E_OK) {
87         GLOGE("Open document store failed. %d", errCode);
88         return errCode;
89     }
90 
91     store = new (std::nothrow) DocumentStore(executor);
92     if (store == nullptr) {
93         delete executor;
94         GLOGE("Memory allocation failed!");
95         return -E_FAILED_MEMORY_ALLOCATE;
96     }
97 
98     store->OnClose([dbRealPath]() {
99         dbConnCount_[dbRealPath]--;
100     });
101 
102     if (isFirstOpen) {
103         dbConnCount_[dbRealPath] = 1;
104     } else {
105         dbConnCount_[dbRealPath]++;
106     }
107     return errCode;
108 }
109 
CloseDocumentStore(DocumentStore * store,uint32_t flags)110 int DocumentStoreManager::CloseDocumentStore(DocumentStore *store, uint32_t flags)
111 {
112     if (!CheckDBCloseFlag(flags)) {
113         GLOGE("Check document db close flags failed.");
114         return -E_INVALID_ARGS;
115     }
116 
117     std::lock_guard<std::mutex> lock(openCloseMutex_);
118     int errCode = store->Close(flags);
119     if (errCode != E_OK) {
120         GLOGE("Close document store failed. %d", errCode);
121         return errCode;
122     }
123 
124     delete store;
125     return E_OK;
126 }
127 
CheckDBPath(const std::string & path,std::string & canonicalPath,std::string & dbName)128 int DocumentStoreManager::CheckDBPath(const std::string &path, std::string &canonicalPath, std::string &dbName)
129 {
130     if (path.empty()) {
131         GLOGE("Invalid path empty");
132         return -E_INVALID_ARGS;
133     }
134 
135     if (path.back() == '/') {
136         GLOGE("Invalid path end with slash");
137         return -E_INVALID_ARGS;
138     }
139 
140     std::string dirPath;
141     OSAPI::SplitFilePath(path, dirPath, dbName);
142 
143     int errCode = OSAPI::GetRealPath(dirPath, canonicalPath);
144     if (errCode != E_OK) {
145         GLOGE("Get real path failed. %d", errCode);
146         return -E_FILE_OPERATION;
147     }
148 
149     if (!OSAPI::CheckPathPermission(canonicalPath)) {
150         GLOGE("Check path permission failed.");
151         return -E_FILE_OPERATION;
152     }
153 
154     return E_OK;
155 }
156 } // namespace DocumentDB