1 /*
2 * Copyright (c) 2021-2024 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 "utils/string_utils.h"
17 #include "utils/file_utils.h"
18
19 #include <cstdarg>
20 #include <cstdio>
21 #include <cstring>
22 #include <fcntl.h>
23 #include <vector>
24 #include <unistd.h>
25 #include <cstdio>
26 #include <dirent.h>
27
28 #include "securec.h"
29 #include "storage_service_log.h"
30
31 using namespace std;
32
33 namespace OHOS {
34 namespace StorageDaemon {
35 static constexpr int32_t BUFF_SIZE = 1024;
StringPrintf(const char *format, ...)36 std::string StringPrintf(const char *format, ...)
37 {
38 va_list ap;
39 va_list apBackup;
40 va_start(ap, format);
41 va_copy(apBackup, ap);
42 char buf[BUFF_SIZE] = {0};
43 std::string result;
44
45 int count = vsnprintf_s(buf, sizeof(buf), sizeof(buf), format, apBackup);
46 if (count < 0) {
47 LOGE("vsnprintf_s error, errno %{public}d", errno);
48 } else if (count < BUFF_SIZE) {
49 result.append(buf, count);
50 } else {
51 LOGI("allocate larger buffer, len = %{public}d", count + 1);
52
53 char *newBuf = new char[count + 1];
54 if (newBuf != nullptr) {
55 count = vsnprintf_s(newBuf, count + 1, count + 1, format, ap);
56 if (count >= 0) {
57 result.append(newBuf, count);
58 }
59 }
60
61 delete[] newBuf;
62 }
63
64 va_end(apBackup);
65 va_end(ap);
66
67 return result;
68 }
69
SplitLine(std::string &line, std::string &token)70 std::vector<std::string> SplitLine(std::string &line, std::string &token)
71 {
72 std::vector<std::string> result;
73 std::string::size_type start;
74 std::string::size_type end;
75
76 start = 0;
77 end = line.find(token);
78 while (std::string::npos != end) {
79 result.push_back(line.substr(start, end - start));
80 start = end + token.size();
81 end = line.find(token, start);
82 }
83
84 if (start != line.length()) {
85 result.push_back(line.substr(start));
86 }
87
88 return result;
89 }
90
WriteFileSync(const char *path, const uint8_t *data, size_t size)91 bool WriteFileSync(const char *path, const uint8_t *data, size_t size)
92 {
93 FILE *f = fopen(path, "w");
94 if (f == nullptr) {
95 LOGE("open %{public}s failed, errno %{public}d", path, errno);
96 return false;
97 }
98 ChMod(path, S_IRUSR | S_IWUSR);
99 int fd = fileno(f);
100 if (fd == -1) {
101 LOGE("open %{public}s failed, errno %{public}d", path, errno);
102 return false;
103 }
104
105 long len = write(fd, data, size);
106 if (len < 0) {
107 LOGE("write %{public}s failed, errno %{public}d", path, errno);
108 (void)fclose(f);
109 return false;
110 }
111 if (static_cast<size_t>(len) != size) {
112 LOGE("write return len %{public}ld, not equal to content length %{public}zu", len, size);
113 (void)fclose(f);
114 return false;
115 }
116
117 if (fsync(fd) != 0) {
118 LOGE("fsync %{public}s failed, errno %{public}d", path, errno);
119 (void)fclose(f);
120 return false;
121 }
122 (void)fclose(f);
123 return true;
124 }
125
SaveStringToFileSync(const std::string &path, const std::string &data)126 bool SaveStringToFileSync(const std::string &path, const std::string &data)
127 {
128 if (path.empty() || data.empty()) {
129 return false;
130 }
131 LOGI("enter %{public}s, size=%{public}zu", path.c_str(), data.length());
132 return WriteFileSync(path.c_str(), reinterpret_cast<const uint8_t *>(data.c_str()), data.size());
133 }
134
StringIsNumber(const std::string &content)135 bool StringIsNumber(const std::string &content)
136 {
137 if (content.empty()) {
138 return false;
139 }
140 bool isNum = true;
141 for (char c : content) {
142 if (!isdigit(c)) {
143 isNum = false;
144 break;
145 }
146 }
147 return isNum;
148 }
149 } // namespace StorageDaemon
150 } // namespace OHOS
151