• 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)84 bool WriteFileSync(const char *path, const uint8_t *data, size_t size)
85 {
86     FILE *f = fopen(path, "w");
87     if (f == nullptr) {
88         LOGE("open %{public}s failed, errno %{public}d", path, errno);
89         return false;
90     }
91     ChMod(path, S_IRUSR | S_IWUSR);
92     int fd = fileno(f);
93     if (fd == -1) {
94         LOGE("open %{public}s failed, errno %{public}d", path, errno);
95         (void)fclose(f);
96         return false;
97     }
98 
99     long len = write(fd, data, size);
100     if (len < 0) {
101         LOGE("write %{public}s failed, errno %{public}d", path, errno);
102         (void)fclose(f);
103         return false;
104     }
105     if (static_cast<size_t>(len) != size) {
106         LOGE("write return len %{public}ld, not equal to content length %{public}zu", len, size);
107         (void)fclose(f);
108         return false;
109     }
110 
111     if (fsync(fd) != 0) {
112         LOGE("fsync %{public}s failed, errno %{public}d", path, errno);
113         (void)fclose(f);
114         return false;
115     }
116     (void)fclose(f);
117     return true;
118 }
119 
SaveStringToFileSync(const std::string & path,const std::string & data)120 bool SaveStringToFileSync(const std::string &path, const std::string &data)
121 {
122     if (path.empty() || data.empty()) {
123         return false;
124     }
125     LOGI("enter %{public}s, size=%{public}zu", path.c_str(), data.length());
126     return WriteFileSync(path.c_str(), reinterpret_cast<const uint8_t *>(data.c_str()), data.size());
127 }
128 
StringIsNumber(const std::string & content)129 bool StringIsNumber(const std::string &content)
130 {
131     if (content.empty()) {
132         return false;
133     }
134     bool isNum = true;
135     for (char c : content) {
136         if (!isdigit(c)) {
137             isNum = false;
138             break;
139         }
140     }
141     return isNum;
142 }
143 
IsStringExist(const std::list<std::string> & strList,const std::string & content)144 bool IsStringExist(const std::list<std::string> &strList, const std::string &content)
145 {
146     auto it = std::find(strList.begin(), strList.end(), content);
147     return it != strList.end();
148 }
149 
ListToString(const std::list<std::string> & strList)150 std::string ListToString(const std::list<std::string> &strList)
151 {
152     if (strList.empty()) {
153         return "";
154     }
155     std::string result;
156     for (auto &iter : strList) {
157         result += iter + ",";
158     }
159     return result.empty() ? "" : result.substr(0, result.length() -1);
160 }
161 } // namespace StorageDaemon
162 } // namespace OHOS
163