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