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