1 /*
2 * Copyright (c) 2023 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 "benchmarks/file_utils.h"
17 #include <fstream>
18 #include <filesystem>
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include "platform/common/rs_log.h"
22
23 namespace OHOS {
24 namespace Rosen {
25 namespace Benchmarks {
IsValidFile(const std::string & realPathStr,const std::string & validFile)26 bool IsValidFile(const std::string& realPathStr, const std::string& validFile)
27 {
28 return realPathStr.find(validFile) == 0;
29 }
30
GetRealAndValidPath(const std::string & filePath)31 std::string GetRealAndValidPath(const std::string& filePath)
32 {
33 std::string realPathStr = std::filesystem::path(filePath).lexically_normal().string();
34 if (IsValidFile(realPathStr)) {
35 return realPathStr;
36 } else {
37 RS_LOGE("FileUtils: The file path is not valid!");
38 return "";
39 }
40 }
41
CreateFile(const std::string & filePath)42 bool CreateFile(const std::string& filePath)
43 {
44 std::string realPath = GetRealAndValidPath(filePath);
45 if (realPath.empty()) {
46 return false;
47 }
48 std::ofstream outFile(realPath);
49 if (!outFile.is_open()) {
50 RS_LOGE("FileUtils: file %s open failed!", realPath.c_str());
51 return false;
52 }
53 outFile.clear();
54 outFile.close();
55 return true;
56 }
57
WriteToFile(uintptr_t data,size_t size,const std::string & filePath)58 bool WriteToFile(uintptr_t data, size_t size, const std::string& filePath)
59 {
60 if (!CreateFile(filePath)) {
61 return false;
62 }
63 if (filePath.empty()) {
64 return false;
65 }
66 int fd = open(filePath.c_str(), O_RDWR | O_CREAT, static_cast<mode_t>(0600));
67 if (fd < 0) {
68 RS_LOGE("FileUtils: %s failed. file: %s, fd = %d", __func__, filePath.c_str(), fd);
69 return false;
70 }
71 ssize_t nwrite = write(fd, reinterpret_cast<uint8_t *>(data), size);
72 if (nwrite < 0) {
73 RS_LOGE("FileUtils: %s failed to persist data, size = %d, fd = %d",
74 __func__, size, fd);
75 }
76 close(fd);
77 return true;
78 }
79
WriteStringToFile(int fd,const std::string & str)80 bool WriteStringToFile(int fd, const std::string& str)
81 {
82 const char* p = str.data();
83 ssize_t remaining = static_cast<ssize_t>(str.size());
84 while (remaining > 0) {
85 ssize_t n = write(fd, p, remaining);
86 if (n == -1) {
87 return false;
88 }
89 p += n;
90 remaining -= n;
91 }
92 p = nullptr;
93 return true;
94 }
95
WriteStringToFile(const std::string & str,const std::string & filePath)96 bool WriteStringToFile(const std::string& str, const std::string& filePath)
97 {
98 if (!CreateFile(filePath)) {
99 return false;
100 }
101 if (filePath.empty()) {
102 return false;
103 }
104 int fd = open(filePath.c_str(), O_RDWR | O_CREAT, static_cast<mode_t>(0600));
105 if (fd < 0) {
106 RS_LOGE("FileUtils: %s failed. file: %s, fd = %d", __func__, filePath.c_str(), fd);
107 return false;
108 }
109 bool result = WriteStringToFile(fd, str);
110 close(fd);
111 return result;
112 }
113 }
114 } // namespace Rosen
115 } // namespace OHOS