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 #include "preferences_test_utils.h"
17 #include "oh_preferences.h"
18 #include "convertor_error_code.h"
19 #include "log_print.h"
20 #include "oh_preferences_err_code.h"
21 #include "oh_preferences_impl.h"
22 #include "oh_preferences_value_impl.h"
23 #include "oh_preferences_value.h"
24 #include "preferences_file_operation.h"
25 #include "preferences_helper.h"
26 
27 namespace OHOS {
28 namespace PreferencesNdk {
29 
CreateDirectoryRecursively(const std::string & path)30 void NdkTestUtils::CreateDirectoryRecursively(const std::string &path)
31 {
32     std::string::size_type pos = path.find_last_of('/');
33     if (pos == std::string::npos || path.front() != '/') {
34         printf("path can not be relative path.\n");
35     }
36     std::string dir = path.substr(0, pos);
37 
38     std::string tempDirectory = dir;
39     std::vector<std::string> directories;
40 
41     pos = tempDirectory.find('/');
42     while (pos != std::string::npos) {
43         std::string directory = tempDirectory.substr(0, pos);
44         if (!directory.empty()) {
45             directories.push_back(directory);
46         }
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 (OHOS::NativePreferences::Access(databaseDirectory.c_str()) != F_OK) {
56             if (OHOS::NativePreferences::Mkdir(databaseDirectory)) {
57                 printf("failed to mkdir, errno %d, %s \n", errno, databaseDirectory.c_str());
58                 return;
59             }
60         }
61     }
62 }
63 
64 } // End of namespace PreferencesNdk
65 } // End of namespace OHOS