1a3e0fd82Sopenharmony_ci/*
2a3e0fd82Sopenharmony_ci * Copyright (c) 2020-2022 Huawei Device Co., Ltd.
3a3e0fd82Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4a3e0fd82Sopenharmony_ci * you may not use this file except in compliance with the License.
5a3e0fd82Sopenharmony_ci * You may obtain a copy of the License at
6a3e0fd82Sopenharmony_ci *
7a3e0fd82Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8a3e0fd82Sopenharmony_ci *
9a3e0fd82Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10a3e0fd82Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11a3e0fd82Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12a3e0fd82Sopenharmony_ci * See the License for the specific language governing permissions and
13a3e0fd82Sopenharmony_ci * limitations under the License.
14a3e0fd82Sopenharmony_ci */
15a3e0fd82Sopenharmony_ci
16a3e0fd82Sopenharmony_ci#include "compare_tools.h"
17a3e0fd82Sopenharmony_ci#include <cstring>
18a3e0fd82Sopenharmony_ci#include "common/screen.h"
19a3e0fd82Sopenharmony_ci#include "dock/screen_device_proxy.h"
20a3e0fd82Sopenharmony_ci#include "draw/draw_utils.h"
21a3e0fd82Sopenharmony_ci#include "gfx_utils/file.h"
22a3e0fd82Sopenharmony_ci#include "gfx_utils/graphic_log.h"
23a3e0fd82Sopenharmony_ci#include "gfx_utils/graphic_math.h"
24a3e0fd82Sopenharmony_ci#include "graphic_config.h"
25a3e0fd82Sopenharmony_ci#include "securec.h"
26a3e0fd82Sopenharmony_ci
27a3e0fd82Sopenharmony_ci#ifdef _WIN32
28a3e0fd82Sopenharmony_ci    #define STR(STRING) #STRING
29a3e0fd82Sopenharmony_ci    #define STRPATH(STRING) STR(STRING)
30a3e0fd82Sopenharmony_ci#endif
31a3e0fd82Sopenharmony_ci
32a3e0fd82Sopenharmony_cinamespace OHOS {
33a3e0fd82Sopenharmony_cibool CompareTools::enableLog_ = false;
34a3e0fd82Sopenharmony_cichar* CompareTools::logPath_ = nullptr;
35a3e0fd82Sopenharmony_ci
36a3e0fd82Sopenharmony_civoid CompareTools::WaitSuspend(const uint16_t waitTime)
37a3e0fd82Sopenharmony_ci{
38a3e0fd82Sopenharmony_ci    uint16_t sleepTime = waitTime == 0 ? DEFAULT_WAIT_TIME_MS : waitTime;
39a3e0fd82Sopenharmony_ci#ifdef _WIN32
40a3e0fd82Sopenharmony_ci    Sleep(sleepTime);
41a3e0fd82Sopenharmony_ci#else
42a3e0fd82Sopenharmony_ci    usleep(1000 * sleepTime); // 1000: us to ms
43a3e0fd82Sopenharmony_ci#endif // _WIN32
44a3e0fd82Sopenharmony_ci}
45a3e0fd82Sopenharmony_ci
46a3e0fd82Sopenharmony_cibool CompareTools::StrnCatPath(char* filePath, size_t pathMax, const char* fileName, size_t count)
47a3e0fd82Sopenharmony_ci{
48a3e0fd82Sopenharmony_ci    if ((filePath == nullptr) || (pathMax > DEFAULT_FILE_NAME_MAX_LENGTH)) {
49a3e0fd82Sopenharmony_ci        return false;
50a3e0fd82Sopenharmony_ci    }
51a3e0fd82Sopenharmony_ci#ifdef _WIN32
52a3e0fd82Sopenharmony_ci    char dest[DEFAULT_FILE_NAME_MAX_LENGTH] = STRPATH(AUTO_TEST_RESOURCE_PATH);
53a3e0fd82Sopenharmony_ci#else
54a3e0fd82Sopenharmony_ci    char dest[DEFAULT_FILE_NAME_MAX_LENGTH] = AUTO_TEST_RESOURCE_PATH;
55a3e0fd82Sopenharmony_ci#endif // _WIN32
56a3e0fd82Sopenharmony_ci    if (strncat_s(dest, DEFAULT_FILE_NAME_MAX_LENGTH, fileName, count) != EOK) {
57a3e0fd82Sopenharmony_ci        return false;
58a3e0fd82Sopenharmony_ci    }
59a3e0fd82Sopenharmony_ci    if (memcpy_s(static_cast<void *>(filePath), pathMax, dest, DEFAULT_FILE_NAME_MAX_LENGTH) != EOK) {
60a3e0fd82Sopenharmony_ci        return false;
61a3e0fd82Sopenharmony_ci    }
62a3e0fd82Sopenharmony_ci    return true;
63a3e0fd82Sopenharmony_ci}
64a3e0fd82Sopenharmony_ci
65a3e0fd82Sopenharmony_cibool CompareTools::CompareByBit(uint32_t fd)
66a3e0fd82Sopenharmony_ci{
67a3e0fd82Sopenharmony_ci    ImageInfo imageBit;
68a3e0fd82Sopenharmony_ci    if (!(Screen::GetInstance().GetCurrentScreenBitmap(imageBit))) {
69a3e0fd82Sopenharmony_ci        return false;
70a3e0fd82Sopenharmony_ci    }
71a3e0fd82Sopenharmony_ci    struct BitmapInfoHeader bitmapInfo = {0};
72a3e0fd82Sopenharmony_ci    lseek(fd, sizeof(uint16_t), SEEK_SET);
73a3e0fd82Sopenharmony_ci    if (read(fd, &bitmapInfo, sizeof(bitmapInfo)) < 0) {
74a3e0fd82Sopenharmony_ci        ImageCacheFree(imageBit);
75a3e0fd82Sopenharmony_ci        return false;
76a3e0fd82Sopenharmony_ci    }
77a3e0fd82Sopenharmony_ci    if (bitmapInfo.biSizeImage != imageBit.dataSize) {
78a3e0fd82Sopenharmony_ci        ImageCacheFree(imageBit);
79a3e0fd82Sopenharmony_ci        return false;
80a3e0fd82Sopenharmony_ci    }
81a3e0fd82Sopenharmony_ci    bool flag = true;
82a3e0fd82Sopenharmony_ci    uint32_t buffSize = bitmapInfo.biSizeImage / MATH_ABS(bitmapInfo.biHeight);
83a3e0fd82Sopenharmony_ci    auto buff = new uint8_t[buffSize];
84a3e0fd82Sopenharmony_ci    for (uint32_t i = 0; i < MATH_ABS(bitmapInfo.biHeight); i++) {
85a3e0fd82Sopenharmony_ci        if (flag && (memset_s(buff, buffSize, 0, buffSize) != EOK)) {
86a3e0fd82Sopenharmony_ci            flag = false;
87a3e0fd82Sopenharmony_ci            break;
88a3e0fd82Sopenharmony_ci        }
89a3e0fd82Sopenharmony_ci        int32_t ret = read(fd, buff, buffSize);
90a3e0fd82Sopenharmony_ci        if (ret < 0) {
91a3e0fd82Sopenharmony_ci            flag = false;
92a3e0fd82Sopenharmony_ci            break;
93a3e0fd82Sopenharmony_ci        }
94a3e0fd82Sopenharmony_ci        for (uint32_t j = 0; j < ret; j++) {
95a3e0fd82Sopenharmony_ci            if (buff[j] != imageBit.data[i * buffSize + j]) {
96a3e0fd82Sopenharmony_ci                flag = false;
97a3e0fd82Sopenharmony_ci                break;
98a3e0fd82Sopenharmony_ci            }
99a3e0fd82Sopenharmony_ci        }
100a3e0fd82Sopenharmony_ci    }
101a3e0fd82Sopenharmony_ci    ImageCacheFree(imageBit);
102a3e0fd82Sopenharmony_ci    delete [] buff;
103a3e0fd82Sopenharmony_ci    buff = nullptr;
104a3e0fd82Sopenharmony_ci    return flag;
105a3e0fd82Sopenharmony_ci}
106a3e0fd82Sopenharmony_ci
107a3e0fd82Sopenharmony_cibool CompareTools::CompareByBitmap(const BitmapInfoHeader bitmapInfoBase,
108a3e0fd82Sopenharmony_ci    const BitmapInfoHeader bitmapInfoRun, uint32_t fdBase, uint32_t fdRun)
109a3e0fd82Sopenharmony_ci{
110a3e0fd82Sopenharmony_ci    bool flag = true;
111a3e0fd82Sopenharmony_ci    uint32_t buffSizeBase = bitmapInfoBase.biSizeImage / MATH_ABS(bitmapInfoBase.biHeight);
112a3e0fd82Sopenharmony_ci    auto buffBase = new uint8_t[buffSizeBase];
113a3e0fd82Sopenharmony_ci
114a3e0fd82Sopenharmony_ci    uint32_t buffSizeRun = bitmapInfoRun.biSizeImage / MATH_ABS(bitmapInfoRun.biHeight);
115a3e0fd82Sopenharmony_ci    auto buffRun = new uint8_t[buffSizeRun];
116a3e0fd82Sopenharmony_ci
117a3e0fd82Sopenharmony_ci    for (uint32_t i = 0; i < MATH_ABS(bitmapInfoBase.biHeight); i++) {
118a3e0fd82Sopenharmony_ci        if (flag && (memset_s(buffBase, buffSizeBase, 0, buffSizeBase) != EOK)) {
119a3e0fd82Sopenharmony_ci            flag = false;
120a3e0fd82Sopenharmony_ci            break;
121a3e0fd82Sopenharmony_ci        }
122a3e0fd82Sopenharmony_ci        if (flag && (memset_s(buffRun, buffSizeRun, 0, buffSizeRun) != EOK)) {
123a3e0fd82Sopenharmony_ci            flag = false;
124a3e0fd82Sopenharmony_ci            break;
125a3e0fd82Sopenharmony_ci        }
126a3e0fd82Sopenharmony_ci        int32_t retBase = read(fdBase, buffBase, buffSizeBase);
127a3e0fd82Sopenharmony_ci        if (retBase < 0) {
128a3e0fd82Sopenharmony_ci            flag = false;
129a3e0fd82Sopenharmony_ci            break;
130a3e0fd82Sopenharmony_ci        }
131a3e0fd82Sopenharmony_ci        int32_t retRun = read(fdRun, buffRun, buffSizeBase);
132a3e0fd82Sopenharmony_ci        if (retRun < 0) {
133a3e0fd82Sopenharmony_ci            flag = false;
134a3e0fd82Sopenharmony_ci            break;
135a3e0fd82Sopenharmony_ci        }
136a3e0fd82Sopenharmony_ci        if (retBase != retRun) {
137a3e0fd82Sopenharmony_ci            flag = false;
138a3e0fd82Sopenharmony_ci            break;
139a3e0fd82Sopenharmony_ci        }
140a3e0fd82Sopenharmony_ci
141a3e0fd82Sopenharmony_ci        for (uint32_t j = 0; j < retBase; j++) {
142a3e0fd82Sopenharmony_ci            if (buffBase[j] != buffRun[j]) {
143a3e0fd82Sopenharmony_ci                flag = false;
144a3e0fd82Sopenharmony_ci                break;
145a3e0fd82Sopenharmony_ci            }
146a3e0fd82Sopenharmony_ci        }
147a3e0fd82Sopenharmony_ci    }
148a3e0fd82Sopenharmony_ci
149a3e0fd82Sopenharmony_ci    delete [] buffBase;
150a3e0fd82Sopenharmony_ci    buffBase = nullptr;
151a3e0fd82Sopenharmony_ci    delete [] buffRun;
152a3e0fd82Sopenharmony_ci    buffRun = nullptr;
153a3e0fd82Sopenharmony_ci
154a3e0fd82Sopenharmony_ci    return flag;
155a3e0fd82Sopenharmony_ci}
156a3e0fd82Sopenharmony_ci
157a3e0fd82Sopenharmony_ci
158a3e0fd82Sopenharmony_cibool CompareTools::CompareFile(const char* fileBasePath, const char* fileRunPath)
159a3e0fd82Sopenharmony_ci{
160a3e0fd82Sopenharmony_ci    if (fileBasePath == nullptr || fileRunPath == nullptr) {
161a3e0fd82Sopenharmony_ci        return false;
162a3e0fd82Sopenharmony_ci    }
163a3e0fd82Sopenharmony_ci#ifdef _WIN32
164a3e0fd82Sopenharmony_ci    uint32_t fdBase = open(fileBasePath, O_RDONLY | O_BINARY);
165a3e0fd82Sopenharmony_ci    uint32_t fdRun = open(fileRunPath, O_RDONLY | O_BINARY);
166a3e0fd82Sopenharmony_ci#else
167a3e0fd82Sopenharmony_ci    uint32_t fdBase = open(fileBasePath, O_RDONLY);
168a3e0fd82Sopenharmony_ci    uint32_t fdRun = open(fileRunPath, O_RDONLY);
169a3e0fd82Sopenharmony_ci#endif
170a3e0fd82Sopenharmony_ci    struct BitmapInfoHeader bitmapInfoBase = {0};
171a3e0fd82Sopenharmony_ci    lseek(fdBase, sizeof(uint16_t), SEEK_SET);
172a3e0fd82Sopenharmony_ci    if (read(fdBase, &bitmapInfoBase, sizeof(bitmapInfoBase)) < 0) {
173a3e0fd82Sopenharmony_ci        close(fdBase);
174a3e0fd82Sopenharmony_ci        close(fdRun);
175a3e0fd82Sopenharmony_ci        return false;
176a3e0fd82Sopenharmony_ci    }
177a3e0fd82Sopenharmony_ci
178a3e0fd82Sopenharmony_ci    struct BitmapInfoHeader bitmapInfoRun = {0};
179a3e0fd82Sopenharmony_ci    lseek(fdRun, sizeof(uint16_t), SEEK_SET);
180a3e0fd82Sopenharmony_ci    if (read(fdRun, &bitmapInfoRun, sizeof(bitmapInfoRun)) < 0) {
181a3e0fd82Sopenharmony_ci        close(fdBase);
182a3e0fd82Sopenharmony_ci        close(fdRun);
183a3e0fd82Sopenharmony_ci        return false;
184a3e0fd82Sopenharmony_ci    }
185a3e0fd82Sopenharmony_ci
186a3e0fd82Sopenharmony_ci    if (bitmapInfoBase.biSizeImage != bitmapInfoRun.biSizeImage) {
187a3e0fd82Sopenharmony_ci        close(fdBase);
188a3e0fd82Sopenharmony_ci        close(fdRun);
189a3e0fd82Sopenharmony_ci        return false;
190a3e0fd82Sopenharmony_ci    }
191a3e0fd82Sopenharmony_ci
192a3e0fd82Sopenharmony_ci    if (!CompareByBitmap(bitmapInfoBase, bitmapInfoRun, fdBase, fdRun)) {
193a3e0fd82Sopenharmony_ci        close(fdBase);
194a3e0fd82Sopenharmony_ci        close(fdRun);
195a3e0fd82Sopenharmony_ci        return false;
196a3e0fd82Sopenharmony_ci    }
197a3e0fd82Sopenharmony_ci
198a3e0fd82Sopenharmony_ci    close(fdBase);
199a3e0fd82Sopenharmony_ci    close(fdRun);
200a3e0fd82Sopenharmony_ci    return true;
201a3e0fd82Sopenharmony_ci}
202a3e0fd82Sopenharmony_ci
203a3e0fd82Sopenharmony_cibool CompareTools::CompareFile(const char* filePath, size_t length)
204a3e0fd82Sopenharmony_ci{
205a3e0fd82Sopenharmony_ci    if ((filePath == nullptr) || (length > DEFAULT_FILE_NAME_MAX_LENGTH)) {
206a3e0fd82Sopenharmony_ci        return false;
207a3e0fd82Sopenharmony_ci    }
208a3e0fd82Sopenharmony_ci#ifdef _WIN32
209a3e0fd82Sopenharmony_ci    uint32_t fd = open(filePath, O_RDONLY | O_BINARY);
210a3e0fd82Sopenharmony_ci#else
211a3e0fd82Sopenharmony_ci    uint32_t fd = open(filePath, O_RDONLY);
212a3e0fd82Sopenharmony_ci#endif
213a3e0fd82Sopenharmony_ci    if (fd == -1) {
214a3e0fd82Sopenharmony_ci        return false;
215a3e0fd82Sopenharmony_ci    }
216a3e0fd82Sopenharmony_ci    bool flag = CompareByBit(fd);
217a3e0fd82Sopenharmony_ci    close(fd);
218a3e0fd82Sopenharmony_ci    if (flag) {
219a3e0fd82Sopenharmony_ci        GRAPHIC_LOGI("[COMPARE_SUCCESS]:fileName=%s", filePath);
220a3e0fd82Sopenharmony_ci        if (enableLog_) {
221a3e0fd82Sopenharmony_ci            char logInfo[DEFAULT_FILE_NAME_MAX_LENGTH] = {0};
222a3e0fd82Sopenharmony_ci            if (sprintf_s(logInfo, sizeof(logInfo), "[COMPARE_SUCCESS]:fileName=%s\n", filePath) < 0) {
223a3e0fd82Sopenharmony_ci                return false;
224a3e0fd82Sopenharmony_ci            }
225a3e0fd82Sopenharmony_ci            SaveLog(logInfo, strlen(logInfo));
226a3e0fd82Sopenharmony_ci        }
227a3e0fd82Sopenharmony_ci    } else {
228a3e0fd82Sopenharmony_ci        GRAPHIC_LOGI("[COMPARE_FAILURE]:fileName=%s", filePath);
229a3e0fd82Sopenharmony_ci        if (enableLog_) {
230a3e0fd82Sopenharmony_ci            char logInfo[DEFAULT_FILE_NAME_MAX_LENGTH] = {0};
231a3e0fd82Sopenharmony_ci            if (sprintf_s(logInfo, sizeof(logInfo), "[COMPARE_FAILURE]:fileName=%s\n", filePath) < 0) {
232a3e0fd82Sopenharmony_ci                return false;
233a3e0fd82Sopenharmony_ci            }
234a3e0fd82Sopenharmony_ci            SaveLog(logInfo, strlen(logInfo));
235a3e0fd82Sopenharmony_ci        }
236a3e0fd82Sopenharmony_ci    }
237a3e0fd82Sopenharmony_ci    return flag;
238a3e0fd82Sopenharmony_ci}
239a3e0fd82Sopenharmony_ci
240a3e0fd82Sopenharmony_cibool CompareTools::SaveByBit(uint32_t fd)
241a3e0fd82Sopenharmony_ci{
242a3e0fd82Sopenharmony_ci    ImageInfo imageBit;
243a3e0fd82Sopenharmony_ci    if (!(Screen::GetInstance().GetCurrentScreenBitmap(imageBit))) {
244a3e0fd82Sopenharmony_ci        return false;
245a3e0fd82Sopenharmony_ci    }
246a3e0fd82Sopenharmony_ci    bool flag = false;
247a3e0fd82Sopenharmony_ci    uint8_t sizeByColorMode = DrawUtils::GetByteSizeByColorMode(ScreenDeviceProxy::GetInstance()->GetBufferMode());
248a3e0fd82Sopenharmony_ci    uint16_t bfType = 0x4D42;
249a3e0fd82Sopenharmony_ci    struct BitmapInfoHeader bitmapInfo = {0};
250a3e0fd82Sopenharmony_ci    bitmapInfo.bfSize = imageBit.dataSize + BITMAP_HEADER_SIZE;
251a3e0fd82Sopenharmony_ci    bitmapInfo.bfOffBits = BITMAP_HEADER_SIZE;
252a3e0fd82Sopenharmony_ci    bitmapInfo.biSize = 40; // 40: bitmap information header size
253a3e0fd82Sopenharmony_ci    bitmapInfo.biWidth = imageBit.header.width;
254a3e0fd82Sopenharmony_ci    bitmapInfo.biHeight = -imageBit.header.height;
255a3e0fd82Sopenharmony_ci    bitmapInfo.biPlanes = 1;
256a3e0fd82Sopenharmony_ci    bitmapInfo.biBitCount = sizeByColorMode * 8; // 8: uint8_t bit
257a3e0fd82Sopenharmony_ci    bitmapInfo.biSizeImage = imageBit.dataSize;
258a3e0fd82Sopenharmony_ci    if (write(fd, &bfType, sizeof(bfType)) > 0) {
259a3e0fd82Sopenharmony_ci        if (write(fd, &bitmapInfo, sizeof(bitmapInfo)) > 0) {
260a3e0fd82Sopenharmony_ci            if (write(fd, imageBit.data, imageBit.dataSize) > 0) {
261a3e0fd82Sopenharmony_ci                flag = true;
262a3e0fd82Sopenharmony_ci            }
263a3e0fd82Sopenharmony_ci        }
264a3e0fd82Sopenharmony_ci    }
265a3e0fd82Sopenharmony_ci    ImageCacheFree(imageBit);
266a3e0fd82Sopenharmony_ci    return flag;
267a3e0fd82Sopenharmony_ci}
268a3e0fd82Sopenharmony_ci
269a3e0fd82Sopenharmony_civoid CompareTools::SaveResultLog(const char* filePath, const char* buff, size_t bufSize)
270a3e0fd82Sopenharmony_ci{
271a3e0fd82Sopenharmony_ci    if (filePath == nullptr || buff == nullptr || bufSize <= 0) {
272a3e0fd82Sopenharmony_ci        return;
273a3e0fd82Sopenharmony_ci    }
274a3e0fd82Sopenharmony_ci
275a3e0fd82Sopenharmony_ci    SaveLog(buff, bufSize, filePath);
276a3e0fd82Sopenharmony_ci}
277a3e0fd82Sopenharmony_ci
278a3e0fd82Sopenharmony_cibool CompareTools::SaveFile(const char* filePath, size_t length)
279a3e0fd82Sopenharmony_ci{
280a3e0fd82Sopenharmony_ci    if ((filePath == nullptr) || (length > DEFAULT_FILE_NAME_MAX_LENGTH)) {
281a3e0fd82Sopenharmony_ci        return false;
282a3e0fd82Sopenharmony_ci    }
283a3e0fd82Sopenharmony_ci#ifdef _WIN32
284a3e0fd82Sopenharmony_ci    uint32_t fd = open(filePath, O_WRONLY | O_CREAT | O_BINARY, DEFAULT_FILE_PERMISSION);
285a3e0fd82Sopenharmony_ci#else
286a3e0fd82Sopenharmony_ci    uint32_t fd = open(filePath, O_WRONLY | O_CREAT, DEFAULT_FILE_PERMISSION);
287a3e0fd82Sopenharmony_ci#endif
288a3e0fd82Sopenharmony_ci    if (fd == -1) {
289a3e0fd82Sopenharmony_ci        return false;
290a3e0fd82Sopenharmony_ci    }
291a3e0fd82Sopenharmony_ci    bool flag = SaveByBit(fd);
292a3e0fd82Sopenharmony_ci    close(fd);
293a3e0fd82Sopenharmony_ci    return flag;
294a3e0fd82Sopenharmony_ci}
295a3e0fd82Sopenharmony_ci
296a3e0fd82Sopenharmony_cibool CompareTools::CheckFileExist(const char* filePath, size_t length)
297a3e0fd82Sopenharmony_ci{
298a3e0fd82Sopenharmony_ci    if ((filePath == nullptr) || (length > DEFAULT_FILE_NAME_MAX_LENGTH)) {
299a3e0fd82Sopenharmony_ci        return false;
300a3e0fd82Sopenharmony_ci    }
301a3e0fd82Sopenharmony_ci    uint32_t fd = open(filePath, O_RDONLY);
302a3e0fd82Sopenharmony_ci    if (fd == -1) {
303a3e0fd82Sopenharmony_ci        return false;
304a3e0fd82Sopenharmony_ci    }
305a3e0fd82Sopenharmony_ci    close(fd);
306a3e0fd82Sopenharmony_ci    return true;
307a3e0fd82Sopenharmony_ci}
308a3e0fd82Sopenharmony_ci
309a3e0fd82Sopenharmony_civoid CompareTools::SetLogPath(const char* filePath, size_t length)
310a3e0fd82Sopenharmony_ci{
311a3e0fd82Sopenharmony_ci    if (logPath_ == nullptr) {
312a3e0fd82Sopenharmony_ci        logPath_ = new char[length];
313a3e0fd82Sopenharmony_ci        if (logPath_ == nullptr) {
314a3e0fd82Sopenharmony_ci            return;
315a3e0fd82Sopenharmony_ci        }
316a3e0fd82Sopenharmony_ci        if (memcpy_s(logPath_, length, filePath, length) != EOK) {
317a3e0fd82Sopenharmony_ci            GRAPHIC_LOGE("memcpy filepath failed");
318a3e0fd82Sopenharmony_ci            return;
319a3e0fd82Sopenharmony_ci        }
320a3e0fd82Sopenharmony_ci        enableLog_ = true;
321a3e0fd82Sopenharmony_ci    }
322a3e0fd82Sopenharmony_ci}
323a3e0fd82Sopenharmony_ci
324a3e0fd82Sopenharmony_civoid CompareTools::UnsetLogPath()
325a3e0fd82Sopenharmony_ci{
326a3e0fd82Sopenharmony_ci    if (logPath_ != nullptr) {
327a3e0fd82Sopenharmony_ci        delete[] logPath_;
328a3e0fd82Sopenharmony_ci        logPath_ = nullptr;
329a3e0fd82Sopenharmony_ci        enableLog_ = false;
330a3e0fd82Sopenharmony_ci    }
331a3e0fd82Sopenharmony_ci}
332a3e0fd82Sopenharmony_ci
333a3e0fd82Sopenharmony_cibool CompareTools::SaveLog(const char* buff, size_t bufSize, const char* filePath)
334a3e0fd82Sopenharmony_ci{
335a3e0fd82Sopenharmony_ci    if (buff == nullptr) {
336a3e0fd82Sopenharmony_ci        return false;
337a3e0fd82Sopenharmony_ci    }
338a3e0fd82Sopenharmony_ci
339a3e0fd82Sopenharmony_ci    const char* useLogPath = filePath == nullptr ? logPath_ : filePath;
340a3e0fd82Sopenharmony_ci    if (useLogPath == nullptr) {
341a3e0fd82Sopenharmony_ci        return false;
342a3e0fd82Sopenharmony_ci    }
343a3e0fd82Sopenharmony_ci
344a3e0fd82Sopenharmony_ci    uint32_t logFd = open(useLogPath, O_WRONLY | O_CREAT | O_APPEND, DEFAULT_FILE_PERMISSION);
345a3e0fd82Sopenharmony_ci    if (logFd == -1) {
346a3e0fd82Sopenharmony_ci        GRAPHIC_LOGE("open log failed");
347a3e0fd82Sopenharmony_ci        return false;
348a3e0fd82Sopenharmony_ci    }
349a3e0fd82Sopenharmony_ci    if (write(logFd, buff, bufSize) < 0) {
350a3e0fd82Sopenharmony_ci        close(logFd);
351a3e0fd82Sopenharmony_ci        GRAPHIC_LOGE("write log failed");
352a3e0fd82Sopenharmony_ci        return false;
353a3e0fd82Sopenharmony_ci    }
354a3e0fd82Sopenharmony_ci    close(logFd);
355a3e0fd82Sopenharmony_ci    return true;
356a3e0fd82Sopenharmony_ci}
357a3e0fd82Sopenharmony_ci} // namespace OHOS
358