1fc223305Sopenharmony_ci/* 2fc223305Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 3fc223305Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4fc223305Sopenharmony_ci * you may not use this file except in compliance with the License. 5fc223305Sopenharmony_ci * You may obtain a copy of the License at 6fc223305Sopenharmony_ci * 7fc223305Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8fc223305Sopenharmony_ci * 9fc223305Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10fc223305Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11fc223305Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fc223305Sopenharmony_ci * See the License for the specific language governing permissions and 13fc223305Sopenharmony_ci * limitations under the License. 14fc223305Sopenharmony_ci */ 15fc223305Sopenharmony_ci 16fc223305Sopenharmony_ci#include "preferences_test_utils.h" 17fc223305Sopenharmony_ci#include "oh_preferences.h" 18fc223305Sopenharmony_ci#include "convertor_error_code.h" 19fc223305Sopenharmony_ci#include "log_print.h" 20fc223305Sopenharmony_ci#include "oh_preferences_err_code.h" 21fc223305Sopenharmony_ci#include "oh_preferences_impl.h" 22fc223305Sopenharmony_ci#include "oh_preferences_value_impl.h" 23fc223305Sopenharmony_ci#include "oh_preferences_value.h" 24fc223305Sopenharmony_ci#include "preferences_file_operation.h" 25fc223305Sopenharmony_ci#include "preferences_helper.h" 26fc223305Sopenharmony_ci 27fc223305Sopenharmony_cinamespace OHOS { 28fc223305Sopenharmony_cinamespace PreferencesNdk { 29fc223305Sopenharmony_ci 30fc223305Sopenharmony_civoid NdkTestUtils::CreateDirectoryRecursively(const std::string &path) 31fc223305Sopenharmony_ci{ 32fc223305Sopenharmony_ci std::string::size_type pos = path.find_last_of('/'); 33fc223305Sopenharmony_ci if (pos == std::string::npos || path.front() != '/') { 34fc223305Sopenharmony_ci printf("path can not be relative path.\n"); 35fc223305Sopenharmony_ci } 36fc223305Sopenharmony_ci std::string dir = path.substr(0, pos); 37fc223305Sopenharmony_ci 38fc223305Sopenharmony_ci std::string tempDirectory = dir; 39fc223305Sopenharmony_ci std::vector<std::string> directories; 40fc223305Sopenharmony_ci 41fc223305Sopenharmony_ci pos = tempDirectory.find('/'); 42fc223305Sopenharmony_ci while (pos != std::string::npos) { 43fc223305Sopenharmony_ci std::string directory = tempDirectory.substr(0, pos); 44fc223305Sopenharmony_ci if (!directory.empty()) { 45fc223305Sopenharmony_ci directories.push_back(directory); 46fc223305Sopenharmony_ci } 47fc223305Sopenharmony_ci tempDirectory = tempDirectory.substr(pos + 1); 48fc223305Sopenharmony_ci pos = tempDirectory.find('/'); 49fc223305Sopenharmony_ci } 50fc223305Sopenharmony_ci directories.push_back(tempDirectory); 51fc223305Sopenharmony_ci 52fc223305Sopenharmony_ci std::string databaseDirectory; 53fc223305Sopenharmony_ci for (const std::string& directory : directories) { 54fc223305Sopenharmony_ci databaseDirectory = databaseDirectory + "/" + directory; 55fc223305Sopenharmony_ci if (OHOS::NativePreferences::Access(databaseDirectory.c_str()) != F_OK) { 56fc223305Sopenharmony_ci if (OHOS::NativePreferences::Mkdir(databaseDirectory)) { 57fc223305Sopenharmony_ci printf("failed to mkdir, errno %d, %s \n", errno, databaseDirectory.c_str()); 58fc223305Sopenharmony_ci return; 59fc223305Sopenharmony_ci } 60fc223305Sopenharmony_ci } 61fc223305Sopenharmony_ci } 62fc223305Sopenharmony_ci} 63fc223305Sopenharmony_ci 64fc223305Sopenharmony_ci} // End of namespace PreferencesNdk 65fc223305Sopenharmony_ci} // End of namespace OHOS