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_operation.h"
17
18#include <cstdio>
19#include <string>
20#include <fcntl.h>
21#include <mutex>
22#include <unistd.h>
23#include <sys/stat.h>
24#include "bits/fcntl.h"
25#include "errors.h"
26#include "thermal_log.h"
27
28namespace OHOS {
29namespace PowerMgr {
30static std::mutex g_mutex;
31int32_t FileOperation::CreateNodeDir(std::string dir)
32{
33    THERMAL_HILOGD(COMP_SVC, "Enter");
34    if (access(dir.c_str(), 0) != ERR_OK) {
35        int flag = mkdir(dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH| S_IXOTH);
36        if (flag == ERR_OK) {
37            THERMAL_HILOGI(COMP_SVC, "Create directory successfully.");
38        } else {
39            THERMAL_HILOGE(COMP_SVC, "Fail to create directory, flag: %{public}d", flag);
40            return flag;
41        }
42    } else {
43        THERMAL_HILOGE(COMP_SVC, "This directory already exists.");
44    }
45    return ERR_OK;
46}
47
48int32_t FileOperation::CreateNodeFile(std::string filePath)
49{
50    if (access(filePath.c_str(), 0) != 0) {
51        int32_t fd = open(filePath.c_str(), O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH);
52        if (fd < ERR_OK) {
53            THERMAL_HILOGE(COMP_SVC, "open failed to file. path=%{public}s", filePath.c_str());
54            return fd;
55        }
56        close(fd);
57    } else {
58        THERMAL_HILOGI(COMP_SVC, "the file already exists. path=%{public}s", filePath.c_str());
59    }
60    return ERR_OK;
61}
62
63int32_t FileOperation::WriteFile(std::string path, std::string buf, size_t size)
64{
65    FILE *stream = fopen(path.c_str(), "w+");
66    if (stream == nullptr) {
67        return ERR_INVALID_VALUE;
68    }
69    size_t ret = fwrite(buf.c_str(), strlen(buf.c_str()), 1, stream);
70    if (ret == ERR_OK) {
71        THERMAL_HILOGE(COMP_SVC, "ret=%{public}zu", ret);
72    }
73    int32_t state = fseek(stream, 0, SEEK_SET);
74    if (state != ERR_OK) {
75        fclose(stream);
76        return state;
77    }
78    state = fclose(stream);
79    if (state != ERR_OK) {
80        return state;
81    }
82    return ERR_OK;
83}
84
85int32_t FileOperation::ReadFile(const char *path, char *buf, size_t size)
86{
87    std::lock_guard<std::mutex> lck(g_mutex);
88    int32_t ret = -1;
89
90    int32_t fd = open(path, O_RDONLY, S_IRUSR | S_IRGRP | S_IROTH);
91    if (fd < ERR_OK) {
92        THERMAL_HILOGE(COMP_SVC, "open failed to file.");
93        return fd;
94    }
95
96    ret = read(fd, buf, size);
97    if (ret < ERR_OK) {
98        THERMAL_HILOGE(COMP_SVC, "failed to read file.");
99        close(fd);
100        return ret;
101    }
102
103    close(fd);
104    buf[(size > 0) ? (size - 1) : 0] = '\0';
105    return ERR_OK;
106}
107
108int32_t FileOperation::ConvertInt(const std::string &value)
109{
110    return std::stoi(value);
111}
112} // namespace PowerMgr
113} // namespace OHOS