common_library: gettid is multiple declaration in cl_error
[staging/basesystem.git] / video_in_hal / systemservice / logger_service / server / src / ss_logger_error_event_storage.cpp
1 /*
2  * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 ///////////////////////////////////////////////////////////////////////////////
18 /// \ingroup  tag_SS_LoggerService
19 /// \brief    TODO
20 ///
21 ///////////////////////////////////////////////////////////////////////////////
22 ///////////////////////////////////////////////////////////////////////////////
23 // INCLUDES
24 ///////////////////////////////////////////////////////////////////////////////
25 #include "ss_logger_error_event_storage.h"
26 #include <stdlib.h>
27 #include <sched.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <dirent.h>
31 #include <unistd.h>
32 #include <string>
33 #include "ss_logger_util.h"
34 #include "ss_logger_error_event_archive.h"
35 #include "ss_logger_fs_directory.h"
36 #include "ss_logger_common.h"
37
38 CLoggerErrorEventStorage::CLoggerErrorEventStorage()
39     : m_parentMcQueue(NULL),
40       m_thread(-1),
41       m_sendQueue(-1),
42       m_receiveQueue(-1),
43       m_destination(""),
44       m_mutexInit(-1),
45       m_pLoggerCfg(NULL),
46       m_logTime(0) {
47   m_responseVec.clear();
48   m_mqattr.mq_flags = 0;
49   m_mqattr.mq_maxmsg = 1;
50   m_mqattr.mq_msgsize = 32;
51   m_mqattr.mq_curmsgs = 0;
52 }
53
54 CLoggerErrorEventStorage::~CLoggerErrorEventStorage() {  // LCOV_EXCL_START 14:globle instance
55   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
56   this->Cleanup();
57 }
58 // LCOV_EXCL_STOP
59
60 EFrameworkunifiedStatus CLoggerErrorEventStorage::Initialize(HANDLE f_hApp,
61                                                 std::string f_ParentName,
62                                                 CLoggerCfg *f_pLoggerCfg) {
63   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
64   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
65   pthread_attr_t l_attr;
66   struct sched_param l_params;
67
68   /* Open the queue */
69   if (NULL == (m_pLoggerCfg = f_pLoggerCfg)) {  // LCOV_EXCL_BR_LINE 200:As it is not always NULL
70     // LCOV_EXCL_START 200:As it is not always NULL
71     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
72     l_eStatus = eFrameworkunifiedStatusNullPointer;
73     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Error. Parameter f_pLoggerCfg is NULL.");
74     // LCOV_EXCL_STOP
75   } else if (eFrameworkunifiedStatusOK != (l_eStatus = m_pLoggerCfg->GetLoggerStorageInfo(m_loggerStorageInfo))) {  // LCOV_EXCL_BR_LINE 200:To ensure success  // NOLINT[whitespace/line_length]
76     // LCOV_EXCL_START 200:To ensure success
77     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
78     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
79            " Error. GetLoggerStorageInfo(m_loggerStorageInfo)");
80     // LCOV_EXCL_STOP
81   } else if (NULL == (m_parentMcQueue = McOpenSender(f_ParentName.c_str()))) {  // LCOV_EXCL_BR_LINE 4:NSFW
82     // LCOV_EXCL_START 4:NSFW
83     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
84     l_eStatus = eFrameworkunifiedStatusErrOther;
85     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Error. McOpenSender(f_ParentName)");
86     // LCOV_EXCL_STOP
87   } else if (-1 == (this->m_sendQueue = mq_open(m_loggerStorageInfo.Name.c_str(), O_CREAT | O_WRONLY, 0666, &m_mqattr))) {  // LCOV_EXCL_BR_LINE 5:C code eror  // NOLINT[whitespace/line_length]
88     // LCOV_EXCL_START 5:C code eror
89     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
90     l_eStatus = eFrameworkunifiedStatusErrOther;
91     FRAMEWORKUNIFIEDLOG(
92         ZONE_ERR,
93         __FUNCTION__,
94         " Error. mq_open(ERROR_EVENT_STORAGE_QUEUE_NAME,O_CREAT | O_WRONLY, 0666, NULL)");
95     // LCOV_EXCL_STOP
96   } else if (-1 == (this->m_receiveQueue = mq_open(m_loggerStorageInfo.Name.c_str(), O_RDONLY))) {  // LCOV_EXCL_BR_LINE 5:C code eror  // NOLINT[whitespace/line_length]
97     // LCOV_EXCL_START 5:C code eror
98     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
99     l_eStatus = eFrameworkunifiedStatusSessionLimitMaxedOut;
100     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
101            " Error. mq_open(ERROR_EVENT_STORAGE_QUEUE_NAME,O_RDONLY)");
102     // LCOV_EXCL_STOP
103   } else if (EOK != (m_mutexInit = pthread_mutex_init(&this->m_mutex, NULL))) {  // LCOV_EXCL_BR_LINE 5:C code eror
104     // LCOV_EXCL_START 5:C code eror
105     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
106     l_eStatus = eFrameworkunifiedStatusFail;
107     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
108            " Error. pthread_mutex_init(&this->m_mutex, NULL)");
109     // LCOV_EXCL_STOP
110   } else if (EOK != pthread_attr_init(&l_attr)) {  // LCOV_EXCL_BR_LINE 5:C code eror
111     // LCOV_EXCL_START 5:C code eror
112     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
113     l_eStatus = eFrameworkunifiedStatusSemUnlockFail;
114     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Error. pthread_attr_init()");
115     // LCOV_EXCL_STOP
116   } else if (EOK != pthread_attr_getschedparam(&l_attr, &l_params)) {  // LCOV_EXCL_BR_LINE 5:C code eror
117     // LCOV_EXCL_START 5:C code eror
118     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
119     l_eStatus = eFrameworkunifiedStatusSemCloseFail;
120     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Error. pthread_attr_getschedparam()");
121     // LCOV_EXCL_STOP
122   } else {
123     l_params.sched_priority = m_loggerStorageInfo.Priority;
124     if (EOK != pthread_attr_setinheritsched(&l_attr, PTHREAD_EXPLICIT_SCHED)) {  // LCOV_EXCL_BR_LINE 5:C code eror
125       // LCOV_EXCL_START 5:C code eror
126       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
127       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Error. pthread_attr_setinheritsched()");
128       // LCOV_EXCL_STOP
129     } else if (EOK != pthread_attr_setschedparam(&l_attr, &l_params)) {  // LCOV_EXCL_BR_LINE 5:C code eror
130       l_eStatus = eFrameworkunifiedStatusDbResultError;
131       FRAMEWORKUNIFIEDLOG(
132           ZONE_ERR,
133           __FUNCTION__,
134           " Error but continue. pthread_attr_setschedparam(), priority = %d: %s",
135           m_loggerStorageInfo.Priority, strerror(errno));  // LCOV_EXCL_BR_LINE 11:Unexpected branch
136       l_eStatus = eFrameworkunifiedStatusOK;
137     }
138     if (EOK != pthread_create(&this->m_thread, &l_attr, &CLoggerErrorEventStorage::ThreadFunctionWrapper, this)) {  // LCOV_EXCL_BR_LINE 5:C code eror  // NOLINT[whitespace/line_length]
139       // LCOV_EXCL_START 5:C code eror
140       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
141       l_eStatus = eFrameworkunifiedStatusDbRecNotFound;
142       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Error. pthread_create()");
143       // LCOV_EXCL_STOP
144     } else {
145       l_eStatus = eFrameworkunifiedStatusOK;
146     }
147   }
148   if (eFrameworkunifiedStatusOK != l_eStatus) {  // LCOV_EXCL_BR_LINE 200:To ensure success
149     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
150     this->Cleanup();  // LCOV_EXCL_LINE 200:To ensure success
151   }
152
153   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-: %d", l_eStatus);
154   return (l_eStatus);
155 }
156
157 EFrameworkunifiedStatus CLoggerErrorEventStorage::Start(
158     std::string f_target, TArtifactResponseVec f_responseVector,
159     uint32_t f_time) {
160   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
161   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
162   /* Start the thread*/
163   if (EOK == pthread_mutex_lock(&this->m_mutex)) {  // LCOV_EXCL_BR_LINE 200:To ensure success
164     this->m_destination = f_target;
165     this->m_responseVec = f_responseVector;
166     this->m_logTime = f_time;
167     (void) pthread_mutex_unlock(&this->m_mutex);
168   }
169   UI_8 l_cmd[MAX_QUEUE_MSG_SIZE];
170   l_cmd[0] = 1;
171   if (-1 == mq_send(this->m_sendQueue, reinterpret_cast<CHAR*>(l_cmd), m_mqattr.mq_msgsize, 0)) {
172     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
173            " Error. mq_send(this->m_sendQueue %d) return %s", m_sendQueue,
174            strerror(errno));
175     l_eStatus = eFrameworkunifiedStatusFail;
176   }
177
178   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-: %d", l_eStatus);
179   return (l_eStatus);
180 }
181
182 void CLoggerErrorEventStorage::ThreadFunction(void) {
183   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
184   char l_data[MAX_QUEUE_MSG_SIZE] = { };
185   SI_32 l_bytesRead = -1;
186   SI_32 l_oldState;
187   ELoggerStorageThreadCommands l_cmdId = eLoggerStorageThreadCmdWriteFailed;
188   EFrameworkunifiedStatus l_eStatus;
189
190   (void) pthread_setname_np(pthread_self(), ERROR_EVENT_STORAGE_QUEUE_NAME);
191
192   pthread_cleanup_push(&CLoggerErrorEventStorage::CleanupWrapper, this);
193     while (-1  // LCOV_EXCL_BR_LINE 200: mq_send can not send size of -1
194         != (l_bytesRead =
195             static_cast<SI_32>(mq_receive(this->m_receiveQueue, reinterpret_cast<char *>(l_data),
196                        (size_t) m_mqattr.mq_msgsize, NULL)))) {
197       std::string l_target;
198       TArtifactResponseVec l_responseVec;
199       uint32_t l_logTime;
200       uint8_t status;
201       Clock_getSystemTimeY2K38(&l_logTime, &status);
202       (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &l_oldState);
203       if (EOK == pthread_mutex_lock(&this->m_mutex)) {  // LCOV_EXCL_BR_LINE 5: pthread_mutex_lock's error case.
204         l_target = this->m_destination;
205         l_responseVec = this->m_responseVec;
206         l_logTime = this->m_logTime;
207         this->m_responseVec.clear();
208         this->m_destination = "";
209
210         (void) pthread_mutex_unlock(&this->m_mutex);
211       }
212       (void) pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &l_oldState);
213       if (access(l_target.c_str(), F_OK) == 0) {
214         if (CFSDirectory::IsDirectory(l_target)) {  // LCOV_EXCL_BR_LINE 6: can not be a dir
215           // LCOV_EXCL_START 6: new file create, it can not be find
216           AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
217           BOOL isDeleteFiles = FALSE;
218           SI_32 l_filesCopied = this->CopyFiles(l_target, l_responseVec,
219                                                 isDeleteFiles);
220
221           switch (l_filesCopied) {
222             case -1:
223               /*Error while moving files*/
224               l_cmdId = eLoggerStorageThreadCmdWriteFailed;
225               break;
226             case 0:
227               /*No files where moved*/
228               l_cmdId = eLoggerStorageThreadCmdNoWritten;
229               break;
230             default:
231               if (l_filesCopied > 0) {
232                 l_cmdId = eLoggerStorageThreadCmdOK;
233                 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__,
234                        " Info. %d files successfully copied.", l_filesCopied);
235               }
236               break;
237           }
238           // LCOV_EXCL_STOP
239         }
240       } else {
241         if (eFrameworkunifiedStatusOK
242             != (l_eStatus = this->PackageAndPlaceArtifacts(l_target,
243                                                            l_responseVec,
244                                                            l_logTime))) {
245           /*Write Error.*/
246           l_cmdId = eLoggerStorageThreadCmdWriteFailed;
247         } else {
248           l_cmdId = eLoggerStorageThreadCmdOK;
249           FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__,
250                  " Info. Archive successfully written.");
251         }
252         if (eFrameworkunifiedStatusOK != RemoveDeprecatedArtifacts(l_responseVec)) {
253           FRAMEWORKUNIFIEDLOG(
254               ZONE_WARN, __FUNCTION__,
255               " Warning. Wasn't able to delete all deprecated log artifacts.");
256         }
257       }
258
259       std::string l_usb_path;
260       size_t l_usb_path_length = 0;
261       if (m_pLoggerCfg != NULL) {  // LCOV_EXCL_BR_LINE 6: m_pLoggerCfg can not be null
262         l_usb_path = m_pLoggerCfg->getUsb0MountPath();
263         l_usb_path_length = l_usb_path.length();
264         if ((l_usb_path_length > 1)
265             && (l_usb_path[l_usb_path.length() - 1] == '/')) {
266           l_usb_path_length--;
267         }
268       }
269       if ((l_usb_path_length > 0)
270           && (l_target.compare(0, l_usb_path_length, l_usb_path, 0,
271                                l_usb_path_length) != 0)) {
272         l_usb_path_length = 0;
273       }
274       if ((l_usb_path_length == 0)
275           && (l_target.compare(0, sizeof(DEBUG_USB_PATH) - 1, DEBUG_USB_PATH, 0,
276                                sizeof(DEBUG_USB_PATH) - 1) == 0)) {
277         l_usb_path = DEBUG_USB_PATH;
278         l_usb_path_length = sizeof(DEBUG_USB_PATH) - 1;
279       }
280       if (l_usb_path_length > 1) {  // if USB path is '/' only, length is 1.
281                                     // So conditions to be syncfs is greater than 1
282         // sync usb device
283         DIR* l_dirp = opendir(l_usb_path.c_str());
284         if (l_dirp != NULL) {  // LCOV_EXCL_BR_LINE 5: c code error case
285           int fd = dirfd(l_dirp);
286           if (fd != -1) {  // LCOV_EXCL_BR_LINE 5: c code error case
287             if (syncfs(fd) == -1) {
288               FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
289                      " Error. syncfs failed path=%s [%s]", l_usb_path.c_str(),
290                      strerror(errno));
291             }
292           } else {
293             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
294                    " Error. syncfs get fd failed path=%s, [%s]",
295                    l_usb_path.c_str(), strerror(errno));
296           }
297           closedir(l_dirp);
298         } else {
299           FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
300                  " Error. syncfs opendir failed path=%s, [%s]",
301                  l_usb_path.c_str(), strerror(errno));
302         }
303         {
304 #define USB_MEMORY_SYNC_WAITTIME 8  // 8 sec
305           const struct timespec intval = { USB_MEMORY_SYNC_WAITTIME, 0 };
306           FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__,
307                  "%d sec:Waiting for USB Memory Synchronization.(%s)",
308                  USB_MEMORY_SYNC_WAITTIME, l_usb_path.c_str());
309           nanosleep(&intval, NULL);
310         }
311       }
312       if (eFrameworkunifiedStatusOK
313           != McSend(this->m_parentMcQueue, m_loggerStorageInfo.Name.c_str(),
314                     l_cmdId, 0, NULL)) {
315         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Error. McSend(this->m_parentMcQueue)");
316       }
317     }
318     pthread_cleanup_pop(1);
319   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
320 }
321
322 void CLoggerErrorEventStorage::Cleanup(void) {
323   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
324   if (-1 != this->m_receiveQueue) {  // LCOV_EXCL_BR_LINE 200: m_receiveQueue cam not be -1
325     if (-1 == mq_close(this->m_receiveQueue)) {
326       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Error. mq_close()");
327     }
328     this->m_receiveQueue = -1;
329   }
330   if (-1 != this->m_sendQueue) {  // LCOV_EXCL_BR_LINE 200: m_sendQueue cam not be -1
331     if (-1 == mq_close(this->m_sendQueue)) {
332       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Error. mq_close()");
333     }
334     this->m_sendQueue = -1;
335     if (-1 == mq_unlink(m_loggerStorageInfo.Name.c_str())) {
336       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Error. mq_unlink()");
337     }
338   }
339
340   if (-1 != m_mutexInit) {  // LCOV_EXCL_BR_LINE 200: m_mutexInit cam not be -1
341     if (EOK != pthread_mutex_destroy(&this->m_mutex)) {  // LCOV_EXCL_BR_LINE 5: c code error case
342       // LCOV_EXCL_START 5: c code error case
343       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
344       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Error. mq_unlink()");
345       // LCOV_EXCL_STOP
346     }
347     m_mutexInit = -1;
348   }
349
350   if (NULL != m_parentMcQueue) {  // LCOV_EXCL_BR_LINE 200: m_parentMcQueue cam not be null
351     if (eFrameworkunifiedStatusOK != McClose(m_parentMcQueue)) {
352       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Error. McClose()");
353     }
354     this->m_parentMcQueue = NULL;
355   }
356   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
357 }
358
359 void* CLoggerErrorEventStorage::ThreadFunctionWrapper(void* param) {
360   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
361   CLoggerErrorEventStorage* l_pObj =
362       reinterpret_cast<CLoggerErrorEventStorage *>(param);
363
364   l_pObj->ThreadFunction();
365   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
366   return NULL;
367 }
368
369 void CLoggerErrorEventStorage::CleanupWrapper(void* param) {
370   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
371   CLoggerErrorEventStorage* l_pObj =
372       reinterpret_cast<CLoggerErrorEventStorage *>(param);
373
374   l_pObj->Cleanup();
375   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
376 }
377
378 ///////////////////////////////////////////////////////////////////////////
379 //  Function : PackageAndPlaceArtifacts
380 //  brief    : This function creates an archive for all collected artifacts
381 //             and places the archive in the required location depending
382 //             on the event type and required naming criteria.
383 ///////////////////////////////////////////////////////////////////////////
384 EFrameworkunifiedStatus CLoggerErrorEventStorage::PackageAndPlaceArtifacts(
385     std::string f_archiveDestination, TArtifactResponseVec &f_responseVector,
386     uint32_t f_logTime) {
387   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
388   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
389   std::string l_coreFileName = "";
390   SI_32 l_ret = 0;
391
392   if (f_archiveDestination.size() == 0) {
393     FRAMEWORKUNIFIEDLOG(
394         ZONE_ERR,
395         __FUNCTION__,
396         " Error. Unknown package file path and name. Logging artifacts not stored!");
397     l_eStatus = eFrameworkunifiedStatusFail;
398   } else {
399     CErrorEventArchive l_archiver;
400     l_eStatus = l_archiver.openArchive(f_archiveDestination);
401     if (eFrameworkunifiedStatusOK != l_eStatus) {  // LCOV_EXCL_BR_LINE 6: it can not be fail
402       // LCOV_EXCL_START 6: it can not be fail
403       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
404       FRAMEWORKUNIFIEDLOG(
405           ZONE_ERR,
406           __FUNCTION__,
407           " Error. Failed to create logging archive: %s. Log archive will not be stored!",
408           f_archiveDestination.c_str());
409         // LCOV_EXCL_STOP
410     } else {
411       for (UI_32 i = 0; (i < f_responseVector.size()); i++) {
412         if (f_responseVector[i].Filepath != std::string("")) {
413           std::string l_destName = f_responseVector[i].Filepath;
414
415           if ((f_responseVector[i].Filepath.find(".bmp")  // LCOV_EXCL_BR_LINE 200: no bmp file be set
416               == f_responseVector[i].Filepath.find_last_of('.'))
417               && (f_responseVector[i].Filepath.find(".bmp")
418                   != std::string::npos)) {
419             // LCOV_EXCL_START 200: no bmp file be set
420             AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
421             char buffer[20];
422             struct tm local_time;
423             Clock_getLocalTimeY2K38(&f_logTime, &local_time);
424             if (0
425                 != strftime(buffer, sizeof(buffer), "%Y%m%d%H%M%S.bmp",
426                             &local_time)) {
427               l_destName = buffer;
428             }
429             // LCOV_EXCL_STOP
430           } else {
431             l_destName = f_responseVector[i].Filepath.substr(
432                 f_responseVector[i].Filepath.find_last_of('/') + 1);
433           }
434           if (f_responseVector[i].ArtifactId == eArtifactIdPstoreLog) {  // LCOV_EXCL_BR_LINE 200: can not be eArtifactIdPstoreLog  // NOLINT[whitespace/line_length]
435             AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
436             l_destName = KERNEL_LOG_PATH_DIR;  // LCOV_EXCL_LINE 200: can not be eArtifactIdPstoreLog
437           }
438           l_eStatus = l_archiver.addToArchive(f_responseVector[i].Filepath,
439                                               l_destName);
440           if (eFrameworkunifiedStatusOK != l_eStatus) {  // LCOV_EXCL_BR_LINE 200: can not be fail
441             // LCOV_EXCL_START 200: can not be fail
442             AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
443             FRAMEWORKUNIFIEDLOG(
444                 ZONE_ERR,
445                 __FUNCTION__,
446                 " Error. Failed to add artifact: %s to archive %s. Continuing with next artifact.",
447                 f_responseVector[i].Filepath.c_str(),
448                 f_archiveDestination.c_str());
449             // LCOV_EXCL_STOP
450           }
451           l_ret++;
452           if ((l_ret % m_loggerStorageInfo.TimeoutAfter) == 0) {
453             usleep(m_loggerStorageInfo.Timeout);
454           }
455         }
456       }
457
458       if (eFrameworkunifiedStatusOK != l_archiver.closeArchive()) {  // LCOV_EXCL_BR_LINE 200: closeArchive can not be fail
459         // LCOV_EXCL_START 200: closeArchive can not be fail
460         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
461         l_eStatus = eFrameworkunifiedStatusFail;
462         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Error. l_archiver.closeArchive()");
463         // LCOV_EXCL_STOP
464       } else {
465         // Cleanup artifacts that are no longer required.
466         int rc = access(f_archiveDestination.c_str(), F_OK);
467         if (0 != rc) {  // LCOV_EXCL_BR_LINE 5: f_archiveDestination is aways exist
468           // LCOV_EXCL_START 5: f_archiveDestination is aways exist
469           AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
470           FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
471                  " Error. Archive verification failed. Errno: %d, %s.", errno,
472                  strerror(errno));
473           l_eStatus = eFrameworkunifiedStatusFail;
474           // LCOV_EXCL_STOP
475         } else {
476           TEXT(__FUNCTION__,
477                " Error Event: A archive successfully written to: %s.",
478                f_archiveDestination.c_str());
479           l_eStatus = eFrameworkunifiedStatusOK;
480         }
481       }
482     }
483   }
484
485   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
486   return (l_eStatus);
487 }
488
489 ///////////////////////////////////////////////////////////////////////////////////////////
490 ///////////////////////////////////////////////////////////////////////////////////////////
491 ///  File Copy
492 ///  arg:
493 ///  isDeleteFiles : TRUE  - delete original files after copy.
494 ///                  FALSE - not delete after copy.
495 ///  returns:
496 ///  -1: error while writing
497 ///   else, number of files written
498 ///////////////////////////////////////////////////////////////////////////////////////////
499 ///////////////////////////////////////////////////////////////////////////////////////////
500 // LCOV_EXCL_START 8: can not be called
501 SI_32 CLoggerErrorEventStorage::CopyFiles(
502     std::string f_destination, TArtifactResponseVec &f_responseVector,
503     BOOL isDeleteFiles) {
504   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
505   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
506   SI_32 l_ret = 0;
507   BOOL l_isFaile = FALSE;
508   for (UI_32 i = 0; i < f_responseVector.size(); i++) {
509     if (f_responseVector[i].Filepath != "") {
510       if (CFSDirectory::IsDirectory(f_responseVector[i].Filepath)) {
511         DIR * l_pDir = opendir(f_responseVector[i].Filepath.c_str());
512         if (l_pDir == NULL) {
513           FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Could not open directory: %s",
514                  f_responseVector[i].Filepath.c_str());
515         } else {
516           struct dirent l_pDirent;
517           struct dirent* next;
518           while (0 == readdir_r(l_pDir, &l_pDirent, &next) && next != NULL) {
519             if ((0 != strcmp(l_pDirent.d_name, ".")) && /* Ignore special  . directory.                     */
520             (0 != strcmp(l_pDirent.d_name, "..")) && /* Ignore special .. directory.                     */
521             (0 != strcmp(l_pDirent.d_name, "lost+found")) && /* Ignore lost+found.                     */
522             ('.' != l_pDirent.d_name[0])) {  /* Ignore hidden files                              */
523               std::string l_fileSource = f_responseVector[i].Filepath;
524               std::string l_fileDestination = f_destination;
525               l_fileSource.append("/");
526               l_fileSource.append(l_pDirent.d_name);
527               l_fileDestination.append("/");
528               l_fileDestination.append(l_pDirent.d_name);
529               EFrameworkunifiedStatus loggerserviceRet = eFrameworkunifiedStatusOK;
530               if (isDeleteFiles == TRUE) {
531                 loggerserviceRet = CLoggerUtil::MoveUntyped(l_fileSource,
532                                                   l_fileDestination);
533               } else {
534                 loggerserviceRet = CLoggerUtil::CopyUntyped(l_fileSource,
535                                                   l_fileDestination);
536               }
537
538               if (eFrameworkunifiedStatusOK == loggerserviceRet) {
539                 l_ret++;
540                 if ((l_ret % m_loggerStorageInfo.TimeoutAfter) == 0) {
541                   usleep(m_loggerStorageInfo.Timeout);
542                 }
543               } else {
544                 l_isFaile = TRUE;
545                 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Could not move: %s",
546                        l_fileSource.c_str());
547               }
548             }
549           }
550           (void) closedir(l_pDir);
551         }
552       } else {
553         std::string l_filename = f_responseVector[i].Filepath.substr(
554             f_responseVector[i].Filepath.find_last_of('/'));
555         std::string l_destination = std::string(f_destination).append(
556             l_filename);
557         if (eFrameworkunifiedStatusOK
558             == CLoggerUtil::MoveUntyped(f_responseVector[i].Filepath,
559                                         l_destination)) {
560           l_ret++;
561           if ((l_ret % m_loggerStorageInfo.TimeoutAfter) == 0) {
562             usleep(m_loggerStorageInfo.Timeout);
563           }
564         } else {
565           FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Could not move: %s",
566                  f_responseVector[i].Filepath.c_str());
567           l_isFaile = TRUE;
568         }
569       }
570     }
571   }
572   if (l_isFaile)
573     l_ret = -1;
574   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
575   return (l_ret);
576 }
577 // LCOV_EXCL_STOP
578
579 EFrameworkunifiedStatus CLoggerErrorEventStorage::RemoveDeprecatedArtifacts(
580     TArtifactResponseVec &f_responseVector) {
581   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
582   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
583   BOOL l_success = TRUE;
584   for (UI_32 i = 0; i < f_responseVector.size(); i++) {
585     if ((f_responseVector[i].Filepath != "")
586         && (TRUE == f_responseVector[i].Remove)) {
587       if (CFSDirectory::IsDirectory(f_responseVector[i].Filepath)) {
588         if (TRUE
589             != CFSDirectory::RemoveDirectory(f_responseVector[i].Filepath)) {
590           FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__,
591                  " Warning. Directory %s failed to be deleted.",
592                  f_responseVector[i].Filepath.c_str());
593           l_success &= FALSE;
594         } else {
595           FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__,
596                  " Info. Directory %s has been successfully deleted.",
597                  f_responseVector[i].Filepath.c_str());
598         }
599
600       } else {
601         errno = 0;
602         if (0 != remove(f_responseVector[i].Filepath.c_str())) {  // LCOV_EXCL_BR_LINE 5: c code error case
603           // LCOV_EXCL_START 5: c code error case
604           AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
605           if (errno == ENOENT) {
606             FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__,
607                    " Info. %s not available. Skipping file deletion.",
608                    f_responseVector[i].Filepath.c_str());
609           } else {
610             FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__,
611                    " Warning. File %s failed to be deleted.",
612                    f_responseVector[i].Filepath.c_str());
613             l_success &= FALSE;
614           }
615           errno = 0;
616           // LCOV_EXCL_STOP
617         } else {
618           FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__,
619                  " Info. File %s has been successfully deleted.",
620                  f_responseVector[i].Filepath.c_str());
621         }
622       }
623     }
624   }
625   l_eStatus = (l_success == TRUE) ? eFrameworkunifiedStatusOK : eFrameworkunifiedStatusFail;
626   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
627   return l_eStatus;
628 }