1 /*
2  * Copyright (c) 2021-2022 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 "file_utils.h"
17 
18 #include "common.h"
19 #include "hilog_wrapper.h"
20 
21 namespace OHOS {
22 namespace Contacts {
FileUtils(void)23 FileUtils::FileUtils(void)
24 {
25 }
26 
~FileUtils()27 FileUtils::~FileUtils()
28 {
29 }
30 
IsFolderExist(std::string path)31 int FileUtils::IsFolderExist(std::string path)
32 {
33     DIR *dp;
34     if ((dp = opendir(path.c_str())) == nullptr) {
35         HILOG_ERROR("FileUtils file NULL");
36         return OPERATION_ERROR;
37     }
38     closedir(dp);
39     return OPERATION_OK;
40 }
41 
Mkdir(std::string path)42 void FileUtils::Mkdir(std::string path)
43 {
44     if (IsFolderExist(path) == OPERATION_ERROR) {
45         int isCreate = ::mkdir(path.c_str(), S_IRUSR | S_IWUSR | S_IXUSR | S_IRWXG | S_IRWXO);
46         HILOG_INFO("FileUtils : mkdir = %{public}d", isCreate);
47     }
48 }
49 
WriteStringToFileAppend(std::string dirStr, const std::string str)50 void FileUtils::WriteStringToFileAppend(std::string dirStr, const std::string str)
51 {
52     time_t backupTime = time(nullptr);
53     if (backupTime == 0) {
54         HILOG_ERROR("FileUtils : WriteStringToFileAppend time_t null");
55         return;
56     }
57     dirStr.append("/").append(std::to_string(backupTime)).append(".log");
58     std::ofstream OsWrite(dirStr, std::ofstream::app);
59     OsWrite << str;
60     OsWrite << std::endl;
61     OsWrite.close();
62 }
63 } // namespace Contacts
64 } // namespace OHOS