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 #include "string_operation.h"
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
28 namespace OHOS {
29 namespace PowerMgr {
30 static std::mutex g_mutex;
CreateNodeDir(std::string dir)31 int32_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
CreateNodeFile(std::string filePath)48 int32_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 fdsan_exchange_owner_tag(fd, 0, DOMAIN_SERVICE);
57 fdsan_close_with_tag(fd, DOMAIN_SERVICE);
58 } else {
59 THERMAL_HILOGI(COMP_SVC, "the file already exists. path=%{public}s", filePath.c_str());
60 }
61 return ERR_OK;
62 }
63
WriteFile(std::string path,std::string buf,size_t size)64 int32_t FileOperation::WriteFile(std::string path, std::string buf, size_t size)
65 {
66 FILE *stream = fopen(path.c_str(), "w+");
67 if (stream == nullptr) {
68 return ERR_INVALID_VALUE;
69 }
70 size_t ret = fwrite(buf.c_str(), strlen(buf.c_str()), 1, stream);
71 if (ret == ERR_OK) {
72 THERMAL_HILOGE(COMP_SVC, "ret=%{public}zu", ret);
73 }
74 int32_t state = fseek(stream, 0, SEEK_SET);
75 if (state != ERR_OK) {
76 fclose(stream);
77 return state;
78 }
79 state = fclose(stream);
80 if (state != ERR_OK) {
81 return state;
82 }
83 return ERR_OK;
84 }
85
ReadFile(const char * path,char * buf,size_t size)86 int32_t FileOperation::ReadFile(const char *path, char *buf, size_t size)
87 {
88 std::lock_guard<std::mutex> lck(g_mutex);
89 int32_t ret = -1;
90
91 int32_t fd = open(path, O_RDONLY, S_IRUSR | S_IRGRP | S_IROTH);
92 if (fd < ERR_OK) {
93 THERMAL_HILOGE(COMP_SVC, "open failed to file.");
94 return fd;
95 }
96 fdsan_exchange_owner_tag(fd, 0, DOMAIN_SERVICE);
97
98 ret = read(fd, buf, size);
99 if (ret < ERR_OK) {
100 THERMAL_HILOGE(COMP_SVC, "failed to read file.");
101 fdsan_close_with_tag(fd, DOMAIN_SERVICE);
102 return ret;
103 }
104
105 fdsan_close_with_tag(fd, DOMAIN_SERVICE);
106 buf[(size > 0) ? (size - 1) : 0] = '\0';
107 return ERR_OK;
108 }
109
ConvertInt(const std::string & value)110 int32_t FileOperation::ConvertInt(const std::string &value)
111 {
112 int64_t result = 0;
113 if (!StringOperation::ParseStrtollResult(value, result)) {
114 return ERR_INVALID_VALUE;
115 }
116 return static_cast<int32_t>(result);
117 }
118 } // namespace PowerMgr
119 } // namespace OHOS