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
16 #define LOG_TAG "DBCommon"
17
18 #include "db_common.h"
19
20 #include "acl.h"
21 #include "log_print.h"
22
23 namespace OHOS::CollaborationEdit {
24 using namespace OHOS::DATABASE_UTILS;
25 constexpr int32_t SERVICE_GID = 3012;
CreateDirectory(const std::string & dbPath)26 int DBCommon::CreateDirectory(const std::string &dbPath)
27 {
28 std::string tempPath = dbPath;
29 std::vector<std::string> directories;
30
31 size_t pos = tempPath.find('/');
32 while (pos != std::string::npos) {
33 std::string dir = tempPath.substr(0, pos);
34 directories.push_back(dir);
35 tempPath = tempPath.substr(pos + 1);
36 pos = tempPath.find('/');
37 }
38 directories.push_back(tempPath);
39
40 std::string targetDir;
41 for (const std::string &directory : directories) {
42 targetDir = targetDir + "/" + directory;
43 if (access(targetDir.c_str(), F_OK) != 0) {
44 if (MkDir(targetDir)) {
45 LOG_ERROR("[CreateDirectory] mkdir errno[%{public}d] %{public}s", errno, targetDir.c_str());
46 return -1;
47 }
48
49 Acl acl(targetDir);
50 acl.SetDefaultUser(GetUid(), Acl::R_RIGHT | Acl::W_RIGHT);
51 acl.SetDefaultGroup(SERVICE_GID, Acl::R_RIGHT | Acl::W_RIGHT);
52 }
53 }
54 return 0;
55 }
56 } // namespace OHOS::CollaborationEdit
57