Re-organized sub-directory by category
[staging/basesystem.git] / service / system / logger_service / server / src / ss_logger_util.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    Provide support for SS Logger module utility functions.
20 ///
21 ///////////////////////////////////////////////////////////////////////////////
22 ///////////////////////////////////////////////////////////////////////////////
23 // INCLUDES
24 ///////////////////////////////////////////////////////////////////////////////
25 #include "ss_logger_util.h"
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <stdio.h>
30 #include <dirent.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <errno.h>
34 #include <stub/Clock_API.h>
35 #include <iomanip>
36 #include <sstream>
37 #include <string>
38 #include <algorithm>
39 #include <vector>
40 #include "ss_logger_common.h"
41
42 #include <system_service/ss_logger_store_logs.h>
43
44 #define SS_STORE_FILENAME_SYSILG_TERM "_SYS_ILG_RESET.tar.gz"
45 #define SS_STORE_FILENAME__CWORD52__TERM "__CWORD52_.log"
46
47
48 char CLoggerUtil::m_usbpath[USB_PATH_SIZE];
49
50 ///////////////////////////////////////////////////////////////////////////////
51 // CLASS METHODS
52 ///////////////////////////////////////////////////////////////////////////////
53 CLoggerUtil::CLoggerUtil(void)
54     : m_pCfg(NULL),
55       m_Milage(0),
56       m_bootCount(0) {
57   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
58   m_usbpath[0] = '\0';
59   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
60 }
61
62 CLoggerUtil::~CLoggerUtil(void) {  // LCOV_EXCL_START 14:globle instance
63   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
64   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
65   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
66 }
67 // LCOV_EXCL_STOP
68
69 EFrameworkunifiedStatus CLoggerUtil::Initialize(CLoggerCfg *f_pLoggerCfg) {
70   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
71   EFrameworkunifiedStatus l_eStatus;
72
73   m_pCfg = f_pLoggerCfg;
74   l_eStatus = (NULL != f_pLoggerCfg) ? eFrameworkunifiedStatusOK : eFrameworkunifiedStatusInvldParam;
75
76   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
77   return (l_eStatus);
78 }
79
80 std::string CLoggerUtil::getDestination(TLoggerErrorEvent &f_eventNtfData) {
81   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+: %d ", f_eventNtfData.EventType);
82   std::string l_path = "";
83   EFrameworkunifiedStatus l_eStatus;
84
85   switch (f_eventNtfData.EventType) {
86     case eErrorEventTypeProcessCrash:
87     case eErrorEventTypeHeartBeatFailure:
88     case eErrorEventTypeSystemLowMemory:
89     case eErrorEventTypeBootMicroReset:
90     case eErrorEventTypeProcessExit:
91     case eErrorEventTypeUserInvokedUserForceReset:
92     case eErrorEventTypeModConnFailed:
93     case eErrorEventTypeStartRespFailed:
94     case eErrorEventTypeUserInvokedCollectDevLogs:
95     case eErrorEventTypeModuleInvokedResetRequest:
96     case eErrorEventTypeModuleInvokedCollectDebugLogs:
97     case eErrorEventTypeDtcEvent:
98     case eErrorEventTypeUserInvokedClearLogs:
99     case eErrorEventTypeGroupRelaunch:
100       l_eStatus = m_pCfg->GetEmmcDestination(l_path);
101       break;
102
103     case eErrorEventTypeUserInvokedCollectNaviLog:
104       l_eStatus = m_pCfg->GetEmmcNaviLogDestination(l_path);
105       break;
106
107     case eErrorEventTypeEelExport:
108       l_path = f_eventNtfData.ModuleName;
109       l_eStatus = eFrameworkunifiedStatusOK;
110       break;
111
112     case eErrorEventTypeInterfaceunifiedEmmcLogs:
113       l_path = f_eventNtfData.ModuleName;
114       l_eStatus = eFrameworkunifiedStatusOK;
115       break;
116
117     case eErrorEventTypeUserInvokedCollectAllLogs:
118     case eErrorEventTypeUserInvokedCollectScreenShot:
119     case eErrorEventTypeUserInvokedCollectInterfaceunifiedLogs:
120     case eErrorEventTypeCanEvent:
121       l_eStatus = m_pCfg->GetUserInvokedDestination(l_path);
122       break;
123
124     case eErrorEventTypeDiagEvent:
125       l_path = f_eventNtfData.ModuleName;
126       l_eStatus = eFrameworkunifiedStatusOK;
127       break;
128
129     default:
130       l_eStatus = eFrameworkunifiedStatusFail;
131       break;
132   }
133   if (eFrameworkunifiedStatusOK != l_eStatus) {
134     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Error. Could not get filepath ");
135   }
136   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-: %s", l_path.c_str());
137   return (l_path);
138 }
139 EFrameworkunifiedStatus CLoggerUtil::checkDestinationAvailable(
140     TLoggerErrorEvent &f_eventNtfData) {
141   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
142   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusFail;
143   std::string l_path = CLoggerUtil::getDestination(f_eventNtfData);
144   std::string::size_type ret;
145
146   if (l_path.length() > 0) {
147     ret = l_path.find(DEBUG_USB_PATH);
148     if (ret != std::string::npos && ret == 0) {
149       if (0 == access(m_usbpath, W_OK)) {  // LCOV_EXCL_BR_LINE 5:C code
150         // LCOV_EXCL_START 5:C code
151         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
152         std::string path = l_path.substr(sizeof(DEBUG_USB_PATH) - 1);
153         f_eventNtfData.ModuleName = m_usbpath;
154         f_eventNtfData.ModuleName.append(path);
155         l_eStatus = eFrameworkunifiedStatusOK;
156         // LCOV_EXCL_STOP
157       }
158     } else {
159       if (0 == access(l_path.c_str(), W_OK)) {  // LCOV_EXCL_BR_LINE 5:C code
160         l_eStatus = eFrameworkunifiedStatusOK;
161       }
162     }
163   }
164
165   if (eFrameworkunifiedStatusOK != l_eStatus) {
166     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
167            " Error. Failed the check path: l_eStatus(%d), l_path(%s)",
168            l_eStatus, l_path.c_str());  // LCOV_EXCL_BR_LINE 11:except,C++ STL
169   }
170   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
171   return (l_eStatus);
172 }
173
174 static void SetSuffixFromType(std::string &pathAndName, EErrorEventType type) {  // NOLINT (runtime/references)
175   switch (type) {
176     case eErrorEventTypeProcessCrash:
177     case eErrorEventTypeHeartBeatFailure:
178     case eErrorEventTypeSystemLowMemory:
179     case eErrorEventTypeBootMicroReset:
180     case eErrorEventTypeProcessExit:
181     case eErrorEventTypeModConnFailed:
182     case eErrorEventTypeStartRespFailed:
183     case eErrorEventTypeModuleInvokedResetRequest:
184       pathAndName.insert(pathAndName.find(".tar.gz"), "_ERR");
185       break;
186     case eErrorEventTypeUserInvokedUserForceReset:
187     case eErrorEventTypeUserInvokedCollectAllLogs:
188     case eErrorEventTypeUserInvokedCollectScreenShot:
189     case eErrorEventTypeUserInvokedCollectInterfaceunifiedLogs:
190     case eErrorEventTypeDiagEvent:
191     case eErrorEventTypeCanEvent:
192     case eErrorEventTypeDtcEvent:
193     case eErrorEventTypeModuleInvokedCollectDebugLogs:
194       pathAndName.insert(pathAndName.find(".tar.gz"), "_DIAG");
195       break;
196     case eErrorEventTypeUserInvokedCollectDevLogs:
197       pathAndName.insert(pathAndName.find(".tar.gz"), "_DEBUG");
198       break;
199     case eErrorEventTypeGroupRelaunch:
200       pathAndName.insert(pathAndName.find(".tar.gz"), "_GRP_RELAUNCH");
201       break;
202     default:
203       break;
204   }
205 }
206
207 EFrameworkunifiedStatus CLoggerUtil::getFilePathAndName(HANDLE f_hApp,
208                                            TLoggerErrorEvent &f_eventNtfData,
209                                            uint32_t f_time,
210                                            std::string &f_pathAndName) {
211   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
212   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
213   f_pathAndName = "";
214
215   switch (f_eventNtfData.EventType) {  // LCOV_EXCL_BR_LINE 200: eErrorEventTypeEelExport ex. can not be run
216     case eErrorEventTypeProcessCrash:
217     case eErrorEventTypeProcessExit:
218     case eErrorEventTypeHeartBeatFailure:
219     case eErrorEventTypeSystemLowMemory:
220     case eErrorEventTypeBootMicroReset:
221     case eErrorEventTypeModConnFailed:
222     case eErrorEventTypeStartRespFailed:
223     case eErrorEventTypeGroupRelaunch:
224       l_eStatus = m_pCfg->GetEmmcDestination(f_pathAndName);
225       if (eFrameworkunifiedStatusOK != l_eStatus) {  // LCOV_EXCL_BR_LINE 6:it will aways return ok
226         // LCOV_EXCL_START 6:it will aways return ok
227         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
228         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
229                " Error. GetEmmcDestination() returned %d.",
230                l_eStatus);
231         // LCOV_EXCL_STOP
232       } else {
233         f_pathAndName.append("/");
234         f_pathAndName.append(GetEmmcFilename(f_time));
235       }
236       break;
237
238     case eErrorEventTypeUserInvokedUserForceReset:
239     case eErrorEventTypeUserInvokedCollectDevLogs:
240       l_eStatus = m_pCfg->GetEmmcDestination(f_pathAndName);
241       if (eFrameworkunifiedStatusOK != l_eStatus) {  // LCOV_EXCL_BR_LINE 6:it will aways return ok
242         // LCOV_EXCL_START 6:it will aways return ok
243         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
244         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
245                " Error. GetEmmcDestination() returned %d.",
246                l_eStatus);
247         // LCOV_EXCL_STOP
248       } else {
249         f_pathAndName.append("/");
250         f_pathAndName.append(GetEmmcFilename(f_time));
251       }
252       break;
253     case eErrorEventTypeModuleInvokedResetRequest:
254     case eErrorEventTypeModuleInvokedCollectDebugLogs:
255     case eErrorEventTypeDtcEvent:
256       l_eStatus = m_pCfg->GetEmmcDestination(f_pathAndName);
257       if (eFrameworkunifiedStatusOK != l_eStatus) {  // LCOV_EXCL_BR_LINE 6:it will aways return ok
258         // LCOV_EXCL_START 6:it will aways return ok
259         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
260         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
261                " Error. GetEmmcDestination() returned %d.",
262                l_eStatus);
263         // LCOV_EXCL_STOP
264       } else {
265         f_pathAndName.append("/");
266         f_pathAndName.append(GetEmmcFilename(f_time));
267         if (!f_eventNtfData.ModuleName.empty()) {  // LCOV_EXCL_BR_LINE 6: ModuleName can not be empty
268           size_t l_pos = f_pathAndName.find(".tar.gz");
269           if (l_pos != std::string::npos) {  // LCOV_EXCL_BR_LINE 6: ".tar.gz" is aways exist
270             f_pathAndName.insert(l_pos, f_eventNtfData.ModuleName);
271           }
272         }
273       }
274       break;
275     case eErrorEventTypeEelExport:  // LCOV_EXCL_START 6: impossible to confirm because it can not get in
276       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
277       f_pathAndName = f_eventNtfData.ModuleName;
278       f_pathAndName.append("/");
279       f_pathAndName.append("loggerservicelogs_export_");
280       f_pathAndName.append(GetTimeString(f_time));
281       f_pathAndName.append(".tar.gz");
282       break;
283       // LCOV_EXCL_STOP
284     case eErrorEventTypeInterfaceunifiedEmmcLogs:  // LCOV_EXCL_START 6: impossible to confirm because it can not get in
285       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
286       // store the target location on event trigger
287       f_pathAndName = f_eventNtfData.ModuleName;
288       break;
289       // LCOV_EXCL_STOP
290     case eErrorEventTypeUserInvokedCollectAllLogs:
291       l_eStatus = m_pCfg->GetUserInvokedDestination(f_pathAndName);
292       if (eFrameworkunifiedStatusOK != l_eStatus) {  // LCOV_EXCL_BR_LINE 6: impossible to confirm because it can not be fail
293         // LCOV_EXCL_START 6: impossible to confirm because it can not be fail
294         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
295         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
296                " Error. GetUserInvokedDestination() returned %d.",
297                l_eStatus);
298         // LCOV_EXCL_STOP
299       } else {
300         std::string l_emmc_path = m_pCfg->getEmmcOutputPath();
301         f_pathAndName.append(l_emmc_path);
302         f_pathAndName.append("/");
303         f_pathAndName.append(GetUserInvokedFilename(f_hApp, f_time));
304         if (!f_eventNtfData.ModuleName.empty()) {  // LCOV_EXCL_BR_LINE 6: it can not be empty
305           size_t l_pos = f_pathAndName.find(".tar.gz");
306           if (l_pos != std::string::npos) {  // LCOV_EXCL_BR_LINE 6: ".tar.gz" is aways exist
307             f_pathAndName.insert(l_pos, f_eventNtfData.ModuleName);
308           }
309         }
310       }
311       break;
312
313     case eErrorEventTypeUserInvokedCollectScreenShot:
314     case eErrorEventTypeUserInvokedCollectInterfaceunifiedLogs:
315     case eErrorEventTypeCanEvent:
316       l_eStatus = m_pCfg->GetUserInvokedDestination(f_pathAndName);
317       if (eFrameworkunifiedStatusOK != l_eStatus) {  // LCOV_EXCL_BR_LINE 6: impossible to confirm because it can not be fail
318         // LCOV_EXCL_START 6: impossible to confirm because it can not be fail
319         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
320         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
321                " Error. GetUserInvokedDestination() returned %d.",
322                l_eStatus);
323         // LCOV_EXCL_STOP
324       } else {
325         f_pathAndName.append("/");
326         f_pathAndName.append(GetUserInvokedFilename(f_hApp, f_time));
327       }
328       break;
329
330     case eErrorEventTypeDiagEvent:  // LCOV_EXCL_START 6: impossible to confirm because it can not get in
331       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
332       f_pathAndName = f_eventNtfData.ModuleName;
333       f_pathAndName.append("/");
334       f_pathAndName.append(GetUserInvokedFilename(f_hApp, f_time));
335       l_eStatus = eFrameworkunifiedStatusOK;
336       break;
337       // LCOV_EXCL_STOP
338     case eErrorEventTypeUserInvokedClearLogs:
339     case eErrorEventTypeUserInvokedCollectNaviLog:
340       l_eStatus = eFrameworkunifiedStatusOK;
341       break;
342
343     default:
344       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Error. Unknown event type: %d.",
345              f_eventNtfData.EventType);
346       l_eStatus = eFrameworkunifiedStatusFail;
347       break;
348   }
349
350   SetSuffixFromType(f_pathAndName, f_eventNtfData.EventType);
351
352   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
353   return (l_eStatus);
354 }
355
356 // LCOV_EXCL_START 13:
357 EFrameworkunifiedStatus CLoggerUtil::getNaviLogFilePathAndName(std::string &f_prefix,
358                                                   uint32_t f_time,
359                                                   std::string &f_pathAndName) {  // LCOV_EXCL_BR_LINE 13:
360   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
361   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
362   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
363
364   l_eStatus = m_pCfg->GetEmmcNaviLogDestination(f_pathAndName);
365   if (eFrameworkunifiedStatusOK != l_eStatus) {
366     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
367            " Error. GetEmmNaviLogDestination() returned %d.",
368            l_eStatus);
369   } else {
370     f_pathAndName.append("/");
371     f_pathAndName.append(f_prefix);
372     f_pathAndName.append(GetEmmcNaviLogFilename(f_time));
373   }
374
375   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
376   return (l_eStatus);
377 }
378 // LCOV_EXCL_STOP
379
380 // LCOV_EXCL_START 200:
381 EFrameworkunifiedStatus CLoggerUtil::getEmmcNaviLogParams(
382     uint32_t f_time, EPWR_SHUTDOWN_TRIGGER_TYPE errorType,
383     std::string &f_pathName, UI_32 &f_logMax) {
384   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
385   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
386   DIR *l_dirp = NULL;
387   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
388   std::string l_prefix = "";
389
390   std::vector<std::string> l_vector;
391   std::string l_path = m_pCfg->getEmmcOutputNaviLogPath();
392
393   if ((errorType == epssdmsdtGENERIC_ERROR_RESET)
394       || (errorType == epssdmsdtFATAL_ERROR_RESET)) {
395     /*When reset occured !!*/
396     f_logMax = m_pCfg->m_emmcOutputNaviLogErrMax;
397     l_prefix = "ERR_";
398   } else {
399     /* ACC OFF occured*/
400     f_logMax = m_pCfg->m_emmcOutputNaviLogNmlMax;
401     l_prefix = "NML_";
402   }
403
404   if (f_logMax == 0) {
405     l_eStatus = eFrameworkunifiedStatusOK;
406     return l_eStatus;
407   }
408
409   l_dirp = opendir(l_path.c_str());
410   if (l_dirp != NULL) {
411     struct dirent l_dirent;
412     struct dirent* next;
413
414     while (0 == readdir_r(l_dirp, &l_dirent, &next) && next != NULL) {
415       std::string l_findFileName = l_dirent.d_name;
416       size_t l_find_pos_top = l_findFileName.find(l_prefix.c_str());
417       size_t l_find_pos = l_findFileName.find(".log");
418       if (std::string::npos != l_find_pos
419           && std::string::npos != l_find_pos_top) {
420         l_vector.push_back(l_findFileName);
421       }
422     }
423     closedir(l_dirp);
424   }
425   std::sort(l_vector.begin(), l_vector.end());
426
427   for (UI_32 i = static_cast<UI_32>(l_vector.size()); i != 0; i--) {
428     std::string l_FilePath = l_path;
429     std::string l_FileName = l_vector.at(i - 1);
430     l_FilePath.append("/");
431     l_FilePath.append(l_FileName.c_str());
432
433     if (i < f_logMax) {
434       size_t l_pos = l_FileName.find("_", l_prefix.size());
435       if (std::string::npos != l_pos) {
436         std::string l_renameFilePath = l_path;
437         std::stringstream l_numStream;
438         l_numStream << std::setw(5) << std::setfill('0') << std::right << std::dec << (i + 1);
439         l_renameFilePath.append("/");
440         l_renameFilePath.append(l_prefix);
441         l_renameFilePath.append(l_numStream.str());
442         l_renameFilePath.append(l_FileName, l_pos, (l_FileName.size() - l_pos));
443         if (0 != rename(l_FilePath.c_str(), l_renameFilePath.c_str())) {
444           FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Error. rename navilog file %s:%d.",
445                  l_FilePath.c_str(), errno);
446         }
447       }
448     } else {
449       if (0 != unlink(l_FilePath.c_str())) {
450         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Error. delete navilog file %s:%d.",
451                l_FilePath.c_str(), errno);
452       }
453     }
454   }
455   // sync parent directory
456   SyncDir(l_path);
457
458   l_eStatus = getNaviLogFilePathAndName(l_prefix, f_time, f_pathName);
459   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
460   return l_eStatus;
461 }
462 // LCOV_EXCL_STOP
463
464 // LCOV_EXCL_START 200: can not be called
465 size_t CLoggerUtil::deleteRequestLogs(std::string f_emmcpath,
466                                       ELOGGERSERVICELOGTYPE f_reqtype, size_t f_reqsize,
467                                       ELOGGERSERVICELOGTYPE f_deltype) {
468   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
469   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
470   DIR* l_dirp = NULL;
471   std::vector<std::string> vect;
472   std::vector<std::string>::iterator it;
473   UI_32 l_count = 0;
474   size_t l_totalsize = 0;
475
476   // create saved file list on eMMC
477   l_dirp = opendir(f_emmcpath.c_str());
478   if (l_dirp != NULL) {
479     struct dirent l_dirent;
480     struct dirent* next;
481     while (0 == readdir_r(l_dirp, &l_dirent, &next) && next != NULL) {
482       std::string l_filename = l_dirent.d_name;
483       if (CLoggerUtil::QueryLoggerservicelogType(l_filename) == f_deltype) {
484         vect.push_back(l_filename);
485       }
486     }
487     closedir(l_dirp);
488   } else {
489     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " open failed eMMC path: %s",
490            f_emmcpath.c_str());
491     FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
492     return 0;
493   }
494
495   // get num of delete logs
496   std::sort(vect.begin(), vect.end());
497   for (it = vect.begin(); it != vect.end(); it++) {
498     l_count++;
499
500     std::string l_filename = *it;
501     struct stat st_buf;
502     std::string l_tmpstr = f_emmcpath;
503     l_tmpstr.append(l_filename.c_str());
504     if (stat(l_tmpstr.c_str(), &st_buf) != -1) {
505       l_totalsize += st_buf.st_size;
506     }
507
508     if (l_totalsize > f_reqsize) {
509       break;
510     }
511   }
512
513   // start delete logs
514   if ((l_totalsize > f_reqsize) || (f_reqtype == eLoggerservicelogTypeAbnormal)
515       || (f_reqtype == eLoggerservicelogTypeGrpRelaunch)) {
516     for (it = vect.begin(); it != vect.end() && l_count > 0; it++) {
517       l_count--;
518
519       std::string l_filename = *it;
520       std::string l_tmpstr = f_emmcpath;
521       l_tmpstr.append(l_filename.c_str());
522       if (unlink(l_tmpstr.c_str()) != 0) {
523         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " unlink failed %s:%d.",
524                l_tmpstr.c_str(), errno);
525       }
526       FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, " del-file=%s total=%d", l_tmpstr.c_str(),
527              l_totalsize);
528     }
529     SyncDir(f_emmcpath);
530   } else {
531     l_totalsize = 0;
532     FRAMEWORKUNIFIEDLOG(
533         ZONE_INFO,
534         __FUNCTION__,
535         " nothing to delete logs req-type=%d del-logtype=%d total=%d, log_count=%d",
536         f_reqtype, f_deltype, l_totalsize, l_count);
537   }
538
539   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
540   return l_totalsize;
541 }
542 // LCOV_EXCL_STOP
543
544 // LCOV_EXCL_START 200: can not be called
545 EFrameworkunifiedStatus CLoggerUtil::forceDeleteOldLog(std::string f_archive_destination,
546                                           size_t f_req_size) {
547   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
548   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
549   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusFail;
550   std::string l_emmc_path;
551   size_t l_delsize = 0;
552   ELOGGERSERVICELOGTYPE l_logtype = eLoggerservicelogTypeOther;
553
554   size_t l_pos = f_archive_destination.find_last_of('/');
555   if (std::string::npos != l_pos) {
556     l_emmc_path = f_archive_destination.substr(0, l_pos);
557     l_emmc_path.append("/");
558     l_logtype = CLoggerUtil::QueryLoggerservicelogType(
559         f_archive_destination.substr(l_pos + 1,
560                                      f_archive_destination.size() - l_pos + 1));
561   }
562
563   if (l_logtype == eLoggerservicelogTypeOther) {
564     // not found
565     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Invalid Parameter. dest=%s",
566            f_archive_destination.c_str());
567     FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
568     return eFrameworkunifiedStatusFail;
569   }
570
571   // remove dirty-file if exist
572   if (unlink(f_archive_destination.c_str()) != 0) {
573     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " unlink failed %s:%d.",
574            f_archive_destination.c_str(), errno);
575   }
576
577   // delete normal log
578   l_delsize = deleteRequestLogs(l_emmc_path, l_logtype, f_req_size,
579                                 eLoggerservicelogTypeNormal);
580   if (l_delsize >= f_req_size) {
581     FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__,
582            " delete normal logs success: req-size=%d, del-size=%d", f_req_size,
583            l_delsize);
584     l_eStatus = eFrameworkunifiedStatusOK;
585   } else if (l_logtype == eLoggerservicelogTypeAbnormal) {
586   // delete abnormal log if request type is abnormal
587     FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__,
588            " continue delete abnormal logs: req-size=%d, del-size=%d",
589            f_req_size, l_delsize);
590     size_t l_tmpsize = f_req_size;
591     l_tmpsize -= l_delsize;
592     l_delsize = deleteRequestLogs(l_emmc_path, l_logtype, l_tmpsize,
593                                   eLoggerservicelogTypeAbnormal);
594     if (l_delsize >= l_tmpsize) {
595       FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__,
596              " delete abnormal logs success: req-size=%d, del-size=%d",
597              f_req_size, l_delsize);
598       l_eStatus = eFrameworkunifiedStatusOK;
599     } else {
600       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
601              " abnormal log delete not complete. req-size=%d, del-size=%d",
602              f_req_size, l_delsize);
603     }
604   } else if (l_logtype == eLoggerservicelogTypeGrpRelaunch) {
605     FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__,
606            " continue delete group relaunch logs: req-size=%d, del-size=%d",
607            f_req_size, l_delsize);
608     size_t l_tmpsize = f_req_size;
609     l_tmpsize -= l_delsize;
610     l_delsize = deleteRequestLogs(l_emmc_path, l_logtype, l_tmpsize,
611                                   eLoggerservicelogTypeGrpRelaunch);
612     if (l_delsize >= l_tmpsize) {
613       FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__,
614              " delete group relaunch logs success: req-size=%d, del-size=%d",
615              f_req_size, l_delsize);
616       l_eStatus = eFrameworkunifiedStatusOK;
617     } else {
618       FRAMEWORKUNIFIEDLOG(
619           ZONE_ERR, __FUNCTION__,
620           " group relaunch log delete not complete. req-size=%d, del-size=%d",
621           f_req_size, l_delsize);
622     }
623   } else {
624     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
625            " normal log del not complete. req-size=%d, del-size=%d", f_req_size,
626            l_delsize);
627   }
628
629   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
630   return l_eStatus;
631 }
632 // LCOV_EXCL_STOP
633
634 EFrameworkunifiedStatus CLoggerUtil::deleteOldLog(std::string log_path,
635                                      std::vector<std::string>& l_vector,
636                                      std::string& f_archive_destination,
637                                      UI_32 max_num) {
638   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
639   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
640   size_t l_pos = f_archive_destination.find_last_of('/');
641   if (std::string::npos != l_pos) {  // LCOV_EXCL_BR_LINE 200: there is always '/' in f_destination
642     std::sort(l_vector.begin(), l_vector.end());
643     std::vector<std::string>::iterator itr = l_vector.begin();
644     for (UI_32 i = static_cast<UI_32>(l_vector.size()); (max_num - 1) < i; i--, itr++) {
645       if (itr == l_vector.end()) {  // LCOV_EXCL_BR_LINE 200: (max_num - 1) can not be -1.
646         break;
647       }
648       std::string l_FilePath = log_path;
649       std::string l_FileName = *itr;
650       l_FilePath.append("/");
651       l_FilePath.append(l_FileName.c_str());
652       if (0 != unlink(l_FilePath.c_str())) {
653         l_eStatus = eFrameworkunifiedStatusAccessError;
654         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Error. delete old log file %s:%d.",
655                l_FilePath.c_str(), errno);
656       }
657     }
658     SyncDir(log_path);
659   } else {
660     // LCOV_EXCL_START 200: there is always '/' in f_destination
661     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
662     l_eStatus = eFrameworkunifiedStatusFail;
663     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Error. delete target not found. %s",
664            f_archive_destination.c_str());
665     // LCOV_EXCL_STOP
666   }
667   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
668   return l_eStatus;
669 }
670
671 EFrameworkunifiedStatus CLoggerUtil::deleteOldEmmcLog(std::string& f_archive_destination,
672                                          EErrorEventType type) {
673   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
674   DIR *l_dirp = NULL;
675   std::vector<std::string> vect_abnrm; /* ILLEGAL RESET LOG INFO or ERROR EXIT LOG INFO */
676   std::vector<std::string> vect_nrm; /* NORMAL LOG INFO */
677   std::vector<std::string> vect_rlnc; /* RELAUNCH LOG INFO */
678   std::string l_emmc_path = m_pCfg->getEmmcOutputPath();
679   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
680
681   UI_32 abnrm_count = 0;
682
683   l_dirp = opendir(l_emmc_path.c_str());
684   if (l_dirp != NULL) {  // LCOV_EXCL_BR_LINE 5: opendir's error case.
685     struct dirent l_dirent;
686     struct dirent* next;
687     while (0 == readdir_r(l_dirp, &l_dirent, &next) && next != NULL) {
688       std::string l_findFileName = l_dirent.d_name;
689
690 //      if ((std::string::npos != l_findFileName.find("_ILG_RESET.tar.gz"))
691 //          || (std::string::npos != l_findFileName.find("_ERR.tar.gz"))) {
692 //        vect_abnrm.push_back(l_findFileName);
693       if ((l_findFileName == ".") || (l_findFileName == "..")) {
694         continue;
695       } else if (std::string::npos != l_findFileName.find(SS_STORE_FILENAME_SYSILG_TERM)) {
696         vect_abnrm.push_back(l_findFileName);
697       } else if(std::string::npos != l_findFileName.find(SS_STORE_FILENAME__CWORD52__TERM)) {  // LCOV_EXCL_BR_LINE 8: SS_STORE_FILENAME__CWORD52__TERM can not be set  // NOLINT[whitespace/line_length]
698         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
699         vect_abnrm.push_back(l_findFileName);  // LCOV_EXCL_LINE 8: SS_STORE_FILENAME_SYSILG_TERM can not be set  // NOLINT[whitespace/line_length]
700       } else if((std::string::npos != l_findFileName.find("_ILG_RESET.tar.gz")) ||
701                 (std::string::npos != l_findFileName.find("_ERR.tar.gz"))) {
702         vect_abnrm.push_back(l_findFileName);
703         abnrm_count++;
704
705       } else if (std::string::npos
706           != l_findFileName.find("_GRP_RELAUNCH.tar.gz")) {
707         vect_rlnc.push_back(l_findFileName);
708       } else if (std::string::npos != l_findFileName.find(".tar.gz")) {
709         vect_nrm.push_back(l_findFileName);
710       }
711     }
712     closedir(l_dirp);
713   }
714
715   switch (type) {
716     case eErrorEventTypeProcessCrash:
717     case eErrorEventTypeProcessExit:
718     case eErrorEventTypeHeartBeatFailure:
719     case eErrorEventTypeSystemLowMemory:
720     case eErrorEventTypeBootMicroReset:
721     case eErrorEventTypeModConnFailed:
722     case eErrorEventTypeStartRespFailed:
723     case eErrorEventTypeModuleInvokedResetRequest:
724
725 //      if ((UI_32) m_pCfg->m_emmcOutputErrMax <= vect_abnrm.size()) {
726 //        l_eStatus = deleteOldLog(l_emmc_path, vect_abnrm, f_archive_destination,
727 //                                 (UI_32) m_pCfg->m_emmcOutputErrMax);
728       if ((UI_32)m_pCfg->m_emmcOutputErrMax <= abnrm_count) {
729         l_eStatus = SS_LoggerStoreLogs_deleteOldLogAbnrm(l_emmc_path, vect_abnrm, f_archive_destination,
730                                                          (UI_32)m_pCfg->m_emmcOutputErrMax, abnrm_count);
731
732       }
733       break;
734     case eErrorEventTypeGroupRelaunch:
735       if ((UI_32) m_pCfg->m_emmcOutputGrpRelaunchMax <= vect_rlnc.size()) {
736         l_eStatus = deleteOldLog(l_emmc_path, vect_rlnc, f_archive_destination,
737                                  (UI_32) m_pCfg->m_emmcOutputGrpRelaunchMax);
738       }
739       break;
740     default:
741       if ((UI_32) m_pCfg->m_emmcOutputMax <= vect_nrm.size()) {
742         l_eStatus = deleteOldLog(l_emmc_path, vect_nrm, f_archive_destination,
743                                  (UI_32) m_pCfg->m_emmcOutputMax);
744       }
745       break;
746   }
747
748   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
749   return l_eStatus;
750 }
751
752 void CLoggerUtil::SetMilage(UI_32 f_Milage) {
753   m_Milage = f_Milage;
754 }
755
756 std::string CLoggerUtil::GetUserInvokedFilename(HANDLE f_hApp, uint32_t f_time) {
757   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
758   std::string l_ret = "";
759   std::stringstream l_nameStream;
760   UI_32 l_seqNumber = 0;
761
762   EFrameworkunifiedStatus l_eStatus = m_pCfg->GetIncrementAndPersistUserInvokedCounter(
763       f_hApp, l_seqNumber);
764   LOG_STATUS_IF_ERRORED(
765       l_eStatus,
766       "GetIncrementAndPersistUserInvokedCounter()");
767
768   /* new counter value was not persisted if l_eStatus != eFrameworkunifiedStatusOK */
769
770   l_nameStream << std::setw(5) << std::setfill('0') << std::right << std::dec << l_seqNumber << '_';
771   l_nameStream << std::setw(8) << std::setfill('0') << std::right << std::dec << m_Milage << '_';
772   l_nameStream << std::setw(5) << std::setfill('0') << std::right << std::dec << m_bootCount << '_';
773   l_nameStream << std::setw(15) << GetTimeString(f_time) << ".tar.gz";
774
775   l_ret = l_nameStream.str();
776
777   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
778   return (l_ret);
779 }
780
781 std::string CLoggerUtil::GetEmmcFilename(uint32_t f_time) {
782   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
783   std::string l_ret = "";
784   std::stringstream l_nameStream;
785   UI_32 l_counter = 0;
786
787   m_pCfg->GetAndIncrementEmmcCounter(l_counter);
788   l_nameStream << std::setw(5) << std::setfill('0') << std::right << std::dec << l_counter << '_';
789   l_nameStream << std::setw(15) << this->GetTimeString(f_time) << ".tar.gz";
790
791   l_ret = l_nameStream.str();
792
793   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
794   return (l_ret);
795 }
796
797 // LCOV_EXCL_START 8: dead code
798 std::string CLoggerUtil::GetEmmcErrorFilename(uint32_t f_time) {
799   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
800   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
801   std::string l_ret = "";
802   std::stringstream l_nameStream;
803   UI_32 l_counter = 0;
804
805   m_pCfg->GetAndIncrementEmmcCounter(l_counter);
806   l_nameStream << std::setw(5) << std::setfill('0') << std::right << std::dec << l_counter << '_';
807   l_nameStream << std::setw(15) << this->GetTimeString(f_time) << "_ERR.tar.gz";
808
809   l_ret = l_nameStream.str();
810
811   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
812   return (l_ret);
813 }
814 // LCOV_EXCL_STOP
815
816 // LCOV_EXCL_START 13:
817 std::string CLoggerUtil::GetEmmcNaviLogFilename(uint32_t f_time) {
818   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
819   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
820   std::string l_ret = "";
821   std::stringstream l_nameStream;
822   UI_32 l_counter = 1;
823
824   l_nameStream << std::setw(5) << std::setfill('0') << std::right << std::dec << l_counter << '_';
825   l_nameStream << std::setw(15) << this->GetTimeString(f_time) << ".log";
826
827   l_ret = l_nameStream.str();
828
829   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
830   return (l_ret);
831 }
832 // LCOV_EXCL_STOP
833
834 std::string CLoggerUtil::GetTimeString(uint32_t f_time) {
835   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
836   std::string l_ret = "00000000_000000";
837   struct tm local_time;
838   Clock_getLocalTimeY2K38(&f_time, &local_time);
839   char buffer[20];
840   if (0
841       != strftime(buffer, sizeof(buffer), "%Y%m%d_%H%M%S",  // LCOV_EXCL_BR_LINE 5: c code
842                   &local_time)) {
843     l_ret = buffer;
844   }
845
846   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
847   return (l_ret);
848 }
849
850 void CLoggerUtil::SetUsbMountPath(std::string usbpath) {
851   strncpy(m_usbpath, usbpath.c_str(), USB_PATH_SIZE - 1);
852   m_usbpath[USB_PATH_SIZE - 1] = '\0';
853 }
854 #ifdef RELEASE_BUILD
855 EFrameworkunifiedStatus CLoggerUtil::PathCheckAndCopyFile(std::string f_source,
856     std::string f_destination) {
857   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusFail;
858   std::string::size_type ret;
859
860   ret = f_destination.find(m_usbpath);
861   l_eStatus = CopyFile(f_source, f_destination);
862
863   return l_eStatus;
864 }
865 #endif  // RELEASE_BUILD
866 /////////////////////////////////////////////////////////////////////////
867 ///// Static Functions
868 /////////////////////////////////////////////////////////////////////////
869
870 EFrameworkunifiedStatus CLoggerUtil::CopyFile(std::string f_source,
871                                  std::string f_destination) {
872   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
873   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
874   ssize_t l_nrd;
875   ssize_t l_nwr;
876   char l_buf[BUF_MAX_SIZE];
877
878   int l_if = open(f_source.c_str(), O_RDONLY | O_CLOEXEC, 0);
879   if (-1 == l_if) {  // LCOV_EXCL_BR_LINE 5: open's error case.
880     // LCOV_EXCL_START 5: open's error case.
881     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
882     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "src open fail:%s,errno:%d,msg:%s",
883            f_source.c_str(), errno, strerror(errno));
884     l_eStatus = eFrameworkunifiedStatusFail;
885     // LCOV_EXCL_STOP
886   } else {
887     bool isWebdav = false;
888     std::string l_tmpDest;
889     size_t l_pos = f_destination.find_last_of('/');
890
891     l_tmpDest = f_destination;
892     if (f_destination.find("/mnt/import") != std::string::npos) {  // LCOV_EXCL_BR_LINE 13:
893       // if output to webdav's directory, don't create temporary file.
894       // because of being unable to rename the file on it.
895       isWebdav = true;
896     }
897
898     int l_of = open(l_tmpDest.c_str(), O_WRONLY | O_CREAT | O_CLOEXEC, 0640);
899     if (-1 == l_of) {  // LCOV_EXCL_BR_LINE 5: open's error case.
900       // LCOV_EXCL_START 5: open's error case.
901       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
902       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "dest open fail:%s,errno:%d,msg:%s",
903              l_tmpDest.c_str(), errno, strerror(errno));
904       l_eStatus = eFrameworkunifiedStatusFail;
905       // LCOV_EXCL_STOP
906     } else {
907       do {
908         l_nrd = read(l_if, l_buf, sizeof(l_buf));
909         if (l_nrd == -1) {  // LCOV_EXCL_BR_LINE 5: read's error case.
910           // LCOV_EXCL_START 5: read's error case.
911           AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
912           FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "read fail:%s,errno:%d,msg:%s",
913                  f_source.c_str(), errno, strerror(errno));
914           l_eStatus = eFrameworkunifiedStatusAccessError;
915           break;
916           // LCOV_EXCL_STOP
917         }
918
919         l_nwr = write(l_of, l_buf, l_nrd);
920         if (l_nwr == -1) {  // LCOV_EXCL_BR_LINE 5: write's error case.
921           // LCOV_EXCL_START 5: write's error case.
922           AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
923           if (ENOSPC == errno) {
924             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
925                    "write fail(ENOSPC):%s,errno:%d,msg:%s", l_tmpDest.c_str(),
926                    errno, strerror(errno));
927             l_eStatus = eFrameworkunifiedStatusErrNoEAGAIN;
928           } else {
929             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "write fail:%s,errno:%d,msg%s",
930                    l_tmpDest.c_str(), errno, strerror(errno));
931             l_eStatus = eFrameworkunifiedStatusAccessError;
932           }
933           break;
934           // LCOV_EXCL_STOP
935         }
936       } while (l_nrd > 0);
937
938       fsync(l_of);
939       close(l_of);
940
941       if (l_eStatus != eFrameworkunifiedStatusOK) {  // LCOV_EXCL_BR_LINE 6: can not be fail
942         // LCOV_EXCL_START 6: can not be fail
943         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
944         close(l_if);
945         return l_eStatus;
946         // LCOV_EXCL_STOP
947       }
948
949       {
950         struct stat istat_buf, ostat_buf;
951         int ret;
952         ret = fstat(l_if, &istat_buf);
953         if (ret < 0) {  // LCOV_EXCL_BR_LINE 5: fstat's error case.
954           // LCOV_EXCL_START 5: fstat's error case.
955           AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
956           FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Error. stat(%s). errno: %d, %s",
957                  f_source.c_str(), errno, strerror(errno));
958           // LCOV_EXCL_STOP
959         } else {
960           while (1) {
961             ret = stat(l_tmpDest.c_str(), &ostat_buf);
962             if (ret < 0) {  // LCOV_EXCL_BR_LINE 5: stat's error case.
963               // LCOV_EXCL_START 5: stat's error case.
964               AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
965               FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " Error. stat(%s). errno: %d, %s",
966                      l_tmpDest.c_str(), errno, strerror(errno));
967               break;
968               // LCOV_EXCL_STOP
969             }
970             if (ostat_buf.st_size >= istat_buf.st_size) {  // LCOV_EXCL_BR_LINE 5: read and write can not fail, so the size is same  // NOLINT[whitespace/line_length]
971               break;
972             }
973             usleep(10000);  // interval
974           }
975         }
976       }
977     }
978     close(l_if);
979
980     if (!isWebdav) {  // LCOV_EXCL_BR_LINE 6: isWebdav is aways false
981       if (rename(l_tmpDest.c_str(), f_destination.c_str()) != 0) {  // LCOV_EXCL_BR_LINE 5: rename's error case.
982         // LCOV_EXCL_START 5: rename's error case.
983         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
984         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "rename %s to %s fail,errno:%d,msg:%s",
985                l_tmpDest.c_str(), f_destination.c_str(), errno, strerror(errno));
986         // LCOV_EXCL_STOP
987       }
988     }
989
990     if (std::string::npos != l_pos) {  // LCOV_EXCL_BR_LINE 200: there is always '/' in f_destination
991       std::string l_destPath = f_destination.substr(0, l_pos);
992       SyncDir(l_destPath);
993     } else {
994       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
995       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "dest invalid:%s", f_destination.c_str());  // LCOV_EXCL_LINE 200: there is always '/' in f_destination  // NOLINT[whitespace/line_length]
996     }
997   }
998
999   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
1000   return (l_eStatus);
1001 }
1002
1003 // LCOV_EXCL_START 8: can not be called
1004 EFrameworkunifiedStatus CLoggerUtil::CopyUntyped(std::string f_source,
1005                                     std::string f_destination) {
1006   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1007   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
1008   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
1009   struct stat l_stat;
1010   int l_statRetVal;
1011   if (0 != access(f_source.c_str(), F_OK)) {
1012     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
1013            " Error. The specified file path and name does not exist: %s",
1014            f_source.c_str());
1015   } else if (0 != (l_statRetVal = lstat(f_source.c_str(), &l_stat))) {
1016     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
1017            " Error. stat() for file or directory returned error: %s -> %d",
1018            f_source.c_str(), l_statRetVal);
1019   } else if (0 != S_ISDIR(l_stat.st_mode)) {
1020     l_eStatus = CopyDirectory(f_source, f_destination);
1021   } else if (0 != S_ISLNK(l_stat.st_mode)) {
1022       // avoid duplicate copy for eErrorEventTypeInterfaceunifiedEmmcLogs: /nv/log/awlog -> /nv/BS/ss/logger_service/rwdata/log2/awlog
1023       if (f_source.find("/nv/BS/ss/logger_service/rwdata/awlog") == std::string::npos) {
1024         l_eStatus = CopyDirectory(f_source, f_destination);
1025       }
1026   } else if (0 != S_ISREG(l_stat.st_mode)) {
1027 #ifdef RELEASE_BUILD
1028     l_eStatus = PathCheckAndCopyFile(f_source, f_destination);
1029 #else
1030     l_eStatus = CopyFile(f_source, f_destination);
1031 #endif  // RELEASE_BUILD
1032   } else {
1033     FRAMEWORKUNIFIEDLOG(
1034         ZONE_ERR,
1035         __FUNCTION__,
1036         " Error. specified file may be a symbolic link or other non 'regular' type. File: %s, mode: 0x%04X.",
1037         f_source.c_str(), l_stat.st_mode);
1038   }
1039   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
1040   return (l_eStatus);
1041 }
1042 // LCOV_EXCL_STOP
1043
1044 // LCOV_EXCL_START 8: can not be called
1045 EFrameworkunifiedStatus CLoggerUtil::CopyDirectory(std::string f_source,
1046                                       std::string f_destination) {
1047   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1048   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
1049   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
1050   DIR * l_pDir = opendir(f_source.c_str());
1051
1052   if (l_pDir == NULL) {
1053     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Could not open directory: %s",
1054            f_source.c_str());
1055   } else {
1056     if (mkdir(f_destination.c_str(), 0775) == -1) {
1057       if (errno == EEXIST) {
1058         FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "Directory: %s already exists.",
1059                f_destination.c_str());
1060       } else {
1061         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
1062                "Could not create directory: %s. Errno: %s",
1063                f_destination.c_str(), strerror(errno));
1064         l_eStatus = eFrameworkunifiedStatusFail;
1065       }
1066     } else {
1067       SyncDir(f_destination);
1068       size_t l_pos = f_destination.find_last_of('/');
1069       if (l_pos != std::string::npos) {
1070         std::string l_destPath = f_destination.substr(0, l_pos);
1071         SyncDir(l_destPath);
1072       }
1073     }
1074
1075     struct dirent l_pDirent;
1076     struct dirent* next;
1077     while (0 == readdir_r(l_pDir, &l_pDirent, &next) && next != NULL) {
1078       if ((0 != strcmp(l_pDirent.d_name, ".")) && /* Ignore special  . directory.                     */
1079       (0 != strcmp(l_pDirent.d_name, "..")) && /* Ignore special .. directory.                     */
1080       (0 != strcmp(l_pDirent.d_name, "lost+found")) && /* Ignore lost+found.                     */
1081       ('.' != l_pDirent.d_name[0])) {  /* Ignore hidden files                              */
1082         std::string l_fileSource = f_source;
1083         std::string l_fileDestination = f_destination;
1084         l_fileSource.append("/");
1085         l_fileSource.append(l_pDirent.d_name);
1086         l_fileDestination.append("/");
1087         l_fileDestination.append(l_pDirent.d_name);
1088         l_eStatus = CopyUntyped(l_fileSource, l_fileDestination);
1089       }
1090     }
1091
1092     closedir(l_pDir);
1093   }
1094   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
1095   return (l_eStatus);
1096 }
1097 // LCOV_EXCL_STOP
1098
1099 // LCOV_EXCL_START 8: can not be called
1100 EFrameworkunifiedStatus CLoggerUtil::MoveFile(std::string f_source,
1101                                  std::string f_destination) {
1102   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1103   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
1104   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
1105 #ifdef RELEASE_BUILD
1106   l_eStatus = CLoggerUtil::PathCheckAndCopyFile(f_source, f_destination);
1107   LOG_STATUS_IF_ERRORED(l_eStatus, "CLoggerUtil::PathCheckAndCopyFile()");
1108 #else
1109   l_eStatus = CLoggerUtil::CopyFile(f_source, f_destination);
1110   LOG_STATUS_IF_ERRORED(l_eStatus, "CLoggerUtil::CopyFile()");
1111 #endif  // RELEASE_BUILD
1112   if (eFrameworkunifiedStatusOK == l_eStatus) {
1113     l_eStatus = (0 == remove(f_source.c_str())) ? eFrameworkunifiedStatusOK : eFrameworkunifiedStatusFail;
1114     if (l_eStatus == eFrameworkunifiedStatusOK) {
1115       size_t l_pos = f_source.find_last_of('/');
1116       if (std::string::npos != l_pos) {
1117         std::string l_emmc_path = f_source.substr(0, l_pos);
1118         SyncDir(l_emmc_path);
1119       }
1120     }
1121   }
1122   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
1123   return (l_eStatus);
1124 }
1125 // LCOV_EXCL_STOP
1126
1127 // LCOV_EXCL_START 8: can not be called
1128 EFrameworkunifiedStatus CLoggerUtil::MoveUntyped(std::string f_source,
1129                                     std::string f_destination) {
1130   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1131   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
1132   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusFail;
1133   struct stat l_stat;
1134   int l_statRetVal;
1135   if (0 != access(f_source.c_str(), F_OK)) {
1136     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
1137            " Error. The specified file path and name does not exist: %s",
1138            f_source.c_str());
1139   } else if (0 != (l_statRetVal = stat(f_source.c_str(), &l_stat))) {
1140     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
1141            " Error. stat() for file or directory returned error: %s -> %d",
1142            f_source.c_str(), l_statRetVal);
1143   } else if (0 != S_ISDIR(l_stat.st_mode)) {
1144     l_eStatus = CLoggerUtil::MoveDirectory(f_source, f_destination);
1145     LOG_STATUS_IF_ERRORED(
1146         l_eStatus, "CLoggerUtil::MoveDirectory(f_source, f_destination);");
1147   } else if (0 != S_ISREG(l_stat.st_mode)) {
1148     l_eStatus = CLoggerUtil::MoveFile(f_source, f_destination);
1149     LOG_STATUS_IF_ERRORED(l_eStatus,
1150                           "CLoggerUtil::MoveFile(f_source, f_destination)");
1151   } else {
1152     FRAMEWORKUNIFIEDLOG(
1153         ZONE_ERR,
1154         __FUNCTION__,
1155         " Error. specified file may be a symbolic link or other non 'regular' type. File: %s, mode: 0x%04X.",
1156         f_source.c_str(), l_stat.st_mode);
1157   }
1158   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
1159   return (l_eStatus);
1160 }
1161 // LCOV_EXCL_STOP
1162
1163 // LCOV_EXCL_START 8: can not be called
1164 EFrameworkunifiedStatus CLoggerUtil::MoveDirectory(std::string f_source,
1165                                       std::string f_destination) {
1166   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1167   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
1168   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
1169   DIR * l_pDir = opendir(f_source.c_str());
1170
1171   if (l_pDir == NULL) {
1172     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Could not open directory: %s",
1173            f_source.c_str());
1174     l_eStatus = eFrameworkunifiedStatusFail;
1175   } else {
1176     errno = 0;
1177     if (mkdir(f_destination.c_str(), 0666) == -1) {
1178       if (errno == EEXIST) {
1179         FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "Directory: %s already exists.",
1180                f_destination.c_str());
1181       } else {
1182         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
1183                "Could not create directory: %s. Errno: %s",
1184                f_destination.c_str(), strerror(errno));
1185         l_eStatus = eFrameworkunifiedStatusFail;
1186       }
1187       errno = 0;
1188     } else {
1189       SyncDir(f_destination);
1190       size_t l_pos = f_destination.find_last_of('/');
1191       if (l_pos != std::string::npos) {
1192         std::string l_destPath = f_destination.substr(0, l_pos);
1193         SyncDir(l_destPath);
1194       }
1195     }
1196
1197     if (eFrameworkunifiedStatusOK == l_eStatus) {  /* if directory already exists or has been newly created */
1198       struct dirent l_pDirent;
1199       struct dirent* next;
1200
1201       while (0 == readdir_r(l_pDir, &l_pDirent, &next) && next != NULL) {
1202         if ((0 != strcmp(l_pDirent.d_name, ".")) && /* Ignore special  . directory.                     */
1203         (0 != strcmp(l_pDirent.d_name, "..")) && /* Ignore special .. directory.                     */
1204         (0 != strcmp(l_pDirent.d_name, "lost+found")) && /* Ignore lost+found.                     */
1205         ('.' != l_pDirent.d_name[0])) {  /* Ignore hidden files                              */
1206           std::string l_fileSource = f_source;
1207           std::string l_fileDestination = f_destination;
1208           l_fileSource.append("/");
1209           l_fileSource.append(l_pDirent.d_name);
1210           l_fileDestination.append("/");
1211           l_fileDestination.append(l_pDirent.d_name);
1212           l_eStatus = MoveUntyped(l_fileSource, l_fileDestination);
1213         }
1214       }
1215     } else {
1216     }
1217     closedir(l_pDir);
1218   }
1219   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
1220   return (l_eStatus);
1221 }
1222 // LCOV_EXCL_STOP
1223
1224 // LCOV_EXCL_START 200: can not be called
1225 ELOGGERSERVICELOGTYPE CLoggerUtil::QueryLoggerservicelogType(std::string f_logname) {
1226   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1227 //  if (f_logname.find("_ILG_RESET.tar.gz") != std::string::npos
1228   if (f_logname.find(SS_STORE_FILENAME_SYSILG_TERM) != std::string::npos) {
1229     return eLoggerservicelogTypeAbnormal;
1230   } else if (f_logname.find(SS_STORE_FILENAME__CWORD52__TERM) != std::string::npos) {
1231     return eLoggerservicelogTypeAbnormal;
1232   } else if (f_logname.find("_ILG_RESET.tar.gz") != std::string::npos
1233
1234       || f_logname.find("_ERR.tar.gz") != std::string::npos) {
1235     return eLoggerservicelogTypeAbnormal;
1236   } else if (f_logname.find("_GRP_RELAUNCH.tar.gz") != std::string::npos) {
1237     return eLoggerservicelogTypeGrpRelaunch;
1238   } else if (f_logname.find(".tar.gz") != std::string::npos) {
1239     return eLoggerservicelogTypeNormal;
1240   } else {
1241     return eLoggerservicelogTypeOther;
1242   }
1243 }
1244 // LCOV_EXCL_STOP
1245
1246 void CLoggerUtil::SyncDir(std::string f_dir_path) {
1247   int fd = open(f_dir_path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC);
1248 #define OPE_ERR_MSG "%s %s failed. errno:%d,msg:%s"
1249   if (fd == -1) {  // LCOV_EXCL_BR_LINE 5: open's error case.
1250     // LCOV_EXCL_START 5: open's error case.
1251     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1252     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, OPE_ERR_MSG, f_dir_path.c_str(), "open",
1253            errno, strerror(errno));
1254     // LCOV_EXCL_STOP
1255   } else {
1256     if (fsync(fd) == -1) {  // LCOV_EXCL_BR_LINE 5: open's error case.
1257     // LCOV_EXCL_START 5: open's error case.
1258     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1259       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, OPE_ERR_MSG, f_dir_path.c_str(), "fsync",
1260              errno, strerror(errno));
1261     // LCOV_EXCL_STOP
1262     }
1263     if (close(fd) == -1) {  // LCOV_EXCL_BR_LINE 5: open's error case.
1264     // LCOV_EXCL_START 5: open's error case.
1265     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1266       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, OPE_ERR_MSG, f_dir_path.c_str(), "close",
1267              errno, strerror(errno));
1268     // LCOV_EXCL_STOP
1269     }
1270   }
1271 #undef OPE_ERR_MSG
1272 }
1273