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