Init basesystem source codes.
[staging/basesystem.git] / video_in_hal / nsframework / notification_persistent_service / server / src / ns_npp_persistence.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 /// \defgroup <<Group Tag>> <<Group Name>>
19 /// \ingroup  tag_NS_NPPService
20 /// .
21 ////////////////////////////////////////////////////////////////////////////////////////////////////
22
23 ////////////////////////////////////////////////////////////////////////////////////////////////////
24 /// \ingroup  tag_NS_NPPService
25 /// \brief    This file contains implementation of class CPersistence.
26 ///
27 ////////////////////////////////////////////////////////////////////////////////////////////////////
28
29 ////////////////////////////////////////////////////////////////////////////////////////////////////
30 // Include Files
31 ////////////////////////////////////////////////////////////////////////////////////////////////////
32 #ifdef AGL_STUB
33 #include <other_service/strlcpy.h>
34 // #include "frameworkunified_stub.h"
35 #endif
36 #include <unistd.h>
37 #include <stdlib.h>  // for getenv()
38 #ifdef AGL_STUB
39 #include <cstdio>
40 #endif
41 #include <utility>
42 #include <string>
43 #include "ns_npp_persistence.h"
44 #include "ns_npp_registry_entry.h"
45
46 ////////////////////////////////////////////////////////////////////////////////////////////////////
47 /// CPersistence
48 /// Constructor of CPersistence class
49 ////////////////////////////////////////////////////////////////////////////////////////////////////
50 CPersistence::CPersistence():
51   m_cStoragePath(GetStoragePath()),
52   m_eCompressionType(ENOTIFICATIONPERSISTENTSERVICEDEFAULTCOMPRESSION),
53   m_hNSWriteToPersistentMem(NULL),
54   m_hNSReadFromPersistentMem(NULL),
55   m_hAppHandle(NULL),
56   m_bPersist(FALSE),
57   m_uiNotificationpersistentservicePersistCategoryFlag(0) {
58 }
59
60 ////////////////////////////////////////////////////////////////////////////////////////////////////
61 /// ~CPersistence
62 /// Destructor of CPersistence class
63 ////////////////////////////////////////////////////////////////////////////////////////////////////
64 CPersistence::~CPersistence() {  // LCOV_EXCL_START 14: Resident process, global instance not released
65   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
66 }
67 // LCOV_EXCL_STOP
68
69 ////////////////////////////////////////////////////////////////////////////////////////////////////
70 /// SetReadThreadHandle
71 /// Set read thread handle.
72 ////////////////////////////////////////////////////////////////////////////////////////////////////
73 VOID CPersistence::SetReadThreadHandle(HANDLE f_hreadthread) {
74   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
75   m_hNSReadFromPersistentMem = f_hreadthread;
76   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
77 }
78
79 ////////////////////////////////////////////////////////////////////////////////////////////////////
80 /// SetWriteThreadHandle
81 /// Set write thread handle.
82 ////////////////////////////////////////////////////////////////////////////////////////////////////
83 VOID CPersistence::SetWriteThreadHandle(HANDLE f_hwritethread) {
84   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
85   m_hNSWriteToPersistentMem = f_hwritethread;
86   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
87 }
88
89 ////////////////////////////////////////////////////////////////////////////////////////////////////
90 /// GetStoragePath
91 ///
92 ////////////////////////////////////////////////////////////////////////////////////////////////////
93 std::string CPersistence::GetStoragePath() {
94   std::string l_cStoragePath(STORAGE_PATH);
95
96   return l_cStoragePath;
97 }
98
99 ////////////////////////////////////////////////////////////////////////////////////////////////////
100 /// Register
101 /// Register tag for persistence.
102 ////////////////////////////////////////////////////////////////////////////////////////////////////
103 EFrameworkunifiedStatus CPersistence::Register(std::string f_crequestorappname, std::string f_ctag, BOOL bisuserpersistence) {
104   EFrameworkunifiedStatus l_estatus = eFrameworkunifiedStatusOK;
105   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
106   if (f_ctag.empty() || f_crequestorappname.empty()) {  // LCOV_EXCL_BR_LINE 6: f_ctag and f_crequestorappname can't be empty  // NOLINT[whitespace/line_length]
107     // LCOV_EXCL_START 6: f_ctag and f_crequestorappname can't be empty
108     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
109     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Invalid tag or requester.");
110     l_estatus = eFrameworkunifiedStatusFail;
111     // LCOV_EXCL_STOP
112   } else {
113     TSourceRegistryListItr l_itRegistry = m_mPersistRegistry.find(f_crequestorappname);
114     if (l_itRegistry != m_mPersistRegistry.end()) {
115       // source available, check for file/folder availability
116       TTagRegistryListItr l_itRegistryList = (l_itRegistry->second).find(f_ctag);
117       if (l_itRegistryList != (l_itRegistry->second).end()) {
118         // found already in the registry
119         FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "Already exists %s , %s", f_ctag.c_str(), f_crequestorappname.c_str());  // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"  // NOLINT[whitespace/line_length]
120       } else {
121         // tag not found .. so add to the registry
122         CRegistryEntry l_objRegistryEntry(f_ctag, f_crequestorappname, m_cStoragePath,
123                                           bisuserpersistence);  // f_eRegisterType, f_cUser);
124
125         (l_itRegistry->second).insert(make_pair(f_ctag, l_objRegistryEntry));
126         FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, " %s found adding tag %s", f_crequestorappname.c_str(), f_ctag.c_str());
127       }
128     } else {
129       // source not found, so creating a new entry
130       CRegistryEntry l_objRegistryEntry(f_ctag, f_crequestorappname, m_cStoragePath,
131                                         bisuserpersistence);  // f_eRegisterType, f_cUser);
132
133       TTagRegistryList l_mRegList;
134       l_mRegList.insert(make_pair(f_ctag, l_objRegistryEntry));   // LCOV_EXCL_BR_LINE 11:except,C++ STL
135
136       m_mPersistRegistry.insert(std::make_pair(f_crequestorappname, l_mRegList));   // LCOV_EXCL_BR_LINE 11:except,C++ STL  // NOLINT[whitespace/line_length]
137
138       FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "none exists %s , %s. So, added entry.",  // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"  // NOLINT[whitespace/line_length]
139              f_ctag.c_str(), f_crequestorappname.c_str());  // LCOV_EXCL_BR_LINE 11: unexpected branch
140     }
141   }
142   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "- l_estatus:0x%x", l_estatus);
143
144   return l_estatus;
145 }
146
147 ////////////////////////////////////////////////////////////////////////////////////////////////////
148 /// ProcessReleaseRequest
149 /// Persist file/folder that need to be persisted.
150 ////////////////////////////////////////////////////////////////////////////////////////////////////
151 EFrameworkunifiedStatus CPersistence::ProcessReleaseRequest(std::string f_crequesterappname,
152                                                std::string f_ctag,
153                                                ENotificationpersistentservicePersistType f_epersisttype,
154                                                std::string f_cmempath,
155                                                EFrameworkunifiedReleaseType enotificationpersistentservicereleasetype,
156                                                std::string f_cusername) {
157   EFrameworkunifiedStatus l_estatus = eFrameworkunifiedStatusOK;
158
159   if (f_cmempath.empty() || f_crequesterappname.empty() || f_ctag.empty()) {    // LCOV_EXCL_BR_LINE 6:f_cmempath, f_crequesterappname and f_ctag can`t be empty  // NOLINT[whitespace/line_length]
160     // LCOV_EXCL_START 6: f_cmempath, f_crequesterappname and f_ctag can`t be empty
161     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
162     FRAMEWORKUNIFIEDLOG0(ZONE_ERR, __FUNCTION__, "Invalid file path, requester or tag.");
163     l_estatus = eFrameworkunifiedStatusInvldParam;
164     // LCOV_EXCL_STOP
165   } else {
166     // Persistence Registry map iterator
167     TSourceRegistryListItr l_itTSourceRegistryListItr = m_mPersistRegistry.find(f_crequesterappname);
168
169     if (l_itTSourceRegistryListItr != m_mPersistRegistry.end()) {
170       // source available, check for file if registered
171       TTagRegistryListItr l_itTRegistryList = (l_itTSourceRegistryListItr->second).find(f_ctag);
172       if (l_itTRegistryList != (l_itTSourceRegistryListItr->second).end()) {
173         if (eFrameworkunifiedNotOnRelease != enotificationpersistentservicereleasetype) {
174           // Set release path
175           l_itTRegistryList->second.SetReleasePath(f_cmempath);
176           // Set persist path
177           l_itTRegistryList->second.SetPersistProperties(f_epersisttype, f_cusername);  // LCOV_EXCL_BR_LINE 11:except,C++ STL  // NOLINT[whitespace/line_length]
178
179           // file/folder found in registry
180           FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__,
181                  "%s released %s. Found in registry. queued up to persist from %s",
182                  f_crequesterappname.c_str(), f_ctag.c_str(), l_itTRegistryList->second.GetReleasePath().c_str());
183
184           // check if persist flag is set due to shutdown or user change
185           if ((m_bPersist     // It will be set to TRUE either on shutdown or userchange
186                && l_itTRegistryList->second.HasntBeenPersisted())  // File/Folder persisted or not
187               || (eFrameworkunifiedPersistInstantly == enotificationpersistentservicereleasetype)) {
188             // reset the data
189             if (((eFrameworkunifiedUserData             == l_itTRegistryList->second.GetPersistentCategory()) &&
190                  (eFrameworkunifiedUserData & m_uiNotificationpersistentservicePersistCategoryFlag)) ||
191                 ((eFrameworkunifiedFactoryData          == l_itTRegistryList->second.GetPersistentCategory()) &&
192                  (eFrameworkunifiedFactoryData & m_uiNotificationpersistentservicePersistCategoryFlag)) ||
193                 ((eFrameworkunifiedFactoryCustomerData  == l_itTRegistryList->second.GetPersistentCategory()) &&
194                  (eFrameworkunifiedFactoryCustomerData & m_uiNotificationpersistentservicePersistCategoryFlag)) ||
195                 ((eFrameworkunifiedDealerData           == l_itTRegistryList->second.GetPersistentCategory()) &&
196                  (eFrameworkunifiedDealerData & m_uiNotificationpersistentservicePersistCategoryFlag))) {
197               // set the status of file/folder persisted as reset has been called on these data
198               l_itTRegistryList->second.SetCurrentAction(LOADTYPE_RELEASE);
199               l_itTRegistryList->second.SetBeingPersisted();
200             } else {  // persist the data
201               if (eFrameworkunifiedStatusOK != (l_estatus = Persist(static_cast<CRegistryEntry &>(l_itTRegistryList->second)))) {  // LCOV_EXCL_BR_LINE 4: NSFW error case.  // NOLINT[whitespace/line_length]
202                 AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
203                 l_estatus = eFrameworkunifiedStatusFail;   // LCOV_EXCL_LINE 4: NSFW error case.
204               }
205             }
206           } else {
207             // Don't persist now. Persist on shutdown.
208           }
209         } else {  // not on release
210           // set the status of file/folder as released and persisted as these data need not to be persisted on shutdown
211           l_itTRegistryList->second.SetReleasePath("");
212           l_itTRegistryList->second.SetCurrentAction(LOADTYPE_RELEASE);
213           l_itTRegistryList->second.SetBeingPersisted();
214         }
215       } else {
216         FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__, "Warning: %s didn't register %s, not persisting",
217                f_crequesterappname.c_str(), f_ctag.c_str());  // LCOV_EXCL_BR_LINE 11: unexpected branch
218         l_estatus = eFrameworkunifiedStatusInvldParam;
219       }
220     } else {
221       // source not registered, cannot persist
222       FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__, "Warning: %s is not registered ignoring release of %s",
223              f_ctag.c_str(), f_crequesterappname.c_str());
224       l_estatus = eFrameworkunifiedStatusInvldParam;
225     }
226   }
227   return l_estatus;
228 }
229
230 EFrameworkunifiedStatus CPersistence::Persist(CRegistryEntry &f_objregistryentry) {
231   EFrameworkunifiedStatus l_estatus = eFrameworkunifiedStatusOK;
232
233   // Persistence Info Structure
234   NSP_CopyInfoCmd l_tCpInfo = {};
235
236
237   std::string l_cSourcePath = f_objregistryentry.GetReleasePath();
238   std::string l_cPersistPath = f_objregistryentry.GetPersistPath();   // LCOV_EXCL_BR_LINE 11:except,C++ STL
239
240   // Fill the persistence info structure
241   AddRequestData(l_tCpInfo,
242                  l_cSourcePath,
243                  l_cPersistPath,
244                  f_objregistryentry.GetRequester(),
245                  f_objregistryentry.GetTag(),
246                  LOADTYPE_RELEASE,
247                  f_objregistryentry.GetPersistType());
248
249   // Persist only if file was released.
250   // Release means application has asked NPPService to persist file at shutdown.)
251   // Persist means actual persisting a file at persistent storage
252   if (f_objregistryentry.IsReleased()) {
253     if (ENOTIFICATIONPERSISTENTSERVICEPERSISTJObSTATEINPROCESS == f_objregistryentry.m_eJobState) {
254       FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "Another job with source: %s and tag %s is in process. "
255              "Therefore adding release request to the pending queue.",
256              f_objregistryentry.GetRequester().c_str(), f_objregistryentry.GetTag().c_str());
257       // if job corresponding to the tag is already in process, add it in pending queue
258       m_lPendingJobs.push_back(l_tCpInfo);
259     } else if (ENOTIFICATIONPERSISTENTSERVICEPERSISTJObSTATEIDLE == f_objregistryentry.m_eJobState) {
260       if (eFrameworkunifiedStatusOK == (SendRequestMessage(l_tCpInfo))) {  // LCOV_EXCL_BR_LINE 4: NSFW error case.
261         FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "Release message passed to writer thread.");  // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"  // NOLINT[whitespace/line_length]
262         f_objregistryentry.SetCurrentAction(LOADTYPE_RELEASE);
263         f_objregistryentry.m_eJobState = ENOTIFICATIONPERSISTENTSERVICEPERSISTJObSTATEINPROCESS;
264       } else {
265         // LCOV_EXCL_START 4: NSFW error case.
266         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
267         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failure in passing release message to writer thread.");
268         l_estatus = eFrameworkunifiedStatusFail;
269         // LCOV_EXCL_STOP
270       }
271     }
272   } else {
273     FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "File/Folder not released yet.");
274   }
275
276   return l_estatus;
277 }
278
279 ////////////////////////////////////////////////////////////////////////////////////////////////////
280 /// ProcessLoadRequest
281 /// Load persisted file/folder.
282 ////////////////////////////////////////////////////////////////////////////////////////////////////
283 EFrameworkunifiedStatus CPersistence::ProcessLoadRequest(std::string f_crequesterappname,
284                                             std::string f_ctag,
285                                             ENotificationpersistentservicePersistType f_epersisttype,
286                                             std::string f_cretrievepath,
287                                             std::string f_cusername) {
288   EFrameworkunifiedStatus l_estatus = eFrameworkunifiedStatusOK;
289
290   // file/folder found in registry
291   FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__,
292          "Load request received: Requester: %s and Tag: %s Load at: %s.",
293          f_crequesterappname.c_str(), f_ctag.c_str(), f_cretrievepath.c_str());  // LCOV_EXCL_BR_LINE 11:except,C++ STL
294
295   if (f_ctag.empty() || f_crequesterappname.empty() || f_cretrievepath.empty()) {   // LCOV_EXCL_BR_LINE 6: both f_ctag and f_crequesterappname and f_cretrievepath can`t be empty  // NOLINT[whitespace/line_length]
296     // LCOV_EXCL_START 6: both f_ctag and f_crequesterappname and f_cretrievepath can`t be empty
297     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
298     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Invalid argument passed (RetrievePath:%s ,tag:%s, requester:%s)",
299            f_cretrievepath.c_str(), f_ctag.c_str(), f_crequesterappname.c_str());
300     l_estatus = eFrameworkunifiedStatusInvldParam;
301     // LCOV_EXCL_STOP
302   } else {
303     // Persistence Registry Map Iterator
304     TSourceRegistryListItr l_itTRegistry = m_mPersistRegistry.find(f_crequesterappname);
305
306     // Persistence Info Struct
307     NSP_CopyInfoCmd l_tCopyInfo = {};
308
309     if (l_itTRegistry != m_mPersistRegistry.end()) {
310       // source available, check for file/folder
311       TTagRegistryListItr l_itTRegistryList = (l_itTRegistry->second).find(f_ctag);
312       if (l_itTRegistryList != (l_itTRegistry->second).end()) {
313         if ((ENOTIFICATIONPERSISTENTSERVICEPERSISTJObSTATERELEASEABORTED == l_itTRegistryList->second.m_eJobState)) {  // LCOV_EXCL_BR_LINE 6: m_eJobState can`t be ENOTIFICATIONPERSISTENTSERVICEPERSISTJObSTATERELEASEABORTED  // NOLINT[whitespace/line_length]
314           // LCOV_EXCL_START 6: m_eJobState can`t be ENOTIFICATIONPERSISTENTSERVICEPERSISTJObSTATERELEASEABORTED  // NOLINT[whitespace/line_length]
315           AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
316           std::string l_cReleasePath(l_itTRegistryList->second.GetReleasePath());
317           // if job was aborted due to some reason like abort shutdown.
318           // Then persistent storage doesn't have updated file/folder. Just restore
319           // aborted released file.
320           // just rename it
321           if (0 == std::rename(l_cReleasePath.c_str(), f_cretrievepath.c_str())) {
322             FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "File loaded at requested location.");
323           } else {
324             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
325                    "Error while processing load request when tried to move. source: %s dest: %s",
326                    l_cReleasePath.c_str(), f_cretrievepath.c_str());
327             l_estatus = eFrameworkunifiedStatusFail;
328           }
329           l_itTRegistryList->second.m_eJobState = ENOTIFICATIONPERSISTENTSERVICEPERSISTJObSTATEIDLE;
330           // LCOV_EXCL_STOP
331         } else {
332           std::string l_cLoadPath(l_itTRegistryList->second.GetLoadPath(f_epersisttype, f_cusername));  // LCOV_EXCL_BR_LINE 11:except,C++ STL  // NOLINT[whitespace/line_length]
333
334           if (0 == access(l_cLoadPath.c_str(), R_OK)) {
335             // file/folder found in registry
336             FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "Found in registry. checking in persistence...(%s)", l_cLoadPath.c_str());
337
338             AddRequestData(l_tCopyInfo,
339                            l_cLoadPath,
340                            f_cretrievepath,
341                            f_crequesterappname,
342                            f_ctag,
343                            LOADTYPE_LOAD,
344                            f_epersisttype);
345
346             if (ENOTIFICATIONPERSISTENTSERVICEPERSISTJObSTATEINPROCESS == (l_itTRegistryList->second).m_eJobState) {
347               FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "Another job with source: %s and tag %s is in process. "
348                      "Therefore adding load request to the pending queue.",
349                      f_crequesterappname.c_str(), f_ctag.c_str());
350               m_lPendingJobs.push_back(l_tCopyInfo);
351             } else if (ENOTIFICATIONPERSISTENTSERVICEPERSISTJObSTATEIDLE == (l_itTRegistryList->second).m_eJobState) {
352               FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "Found in persistence.");
353               if (eFrameworkunifiedStatusOK == (SendRequestMessage(l_tCopyInfo))) {  // LCOV_EXCL_BR_LINE 4: NSFW error case.
354                 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "Load message passed to reader thread. Retrieving..");
355                 // file/folder is requested for loading so reset the persisted flag.
356                 l_itTRegistryList->second.ResetPersistedFlag();
357                 l_itTRegistryList->second.SetCurrentAction(LOADTYPE_LOAD);
358                 l_itTRegistryList->second.m_eJobState = ENOTIFICATIONPERSISTENTSERVICEPERSISTJObSTATEINPROCESS;
359               } else {
360                 // LCOV_EXCL_START 4: NSFW error case.
361                 AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
362                 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failure in passing load message to reader thread.");
363                 l_estatus = eFrameworkunifiedStatusFail;
364                 // LCOV_EXCL_STOP
365               }
366             }
367           } else {
368             FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__, "%s requested by %s. Not found in persistence memory.",  // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"  // NOLINT[whitespace/line_length]
369                    f_ctag.c_str(), f_crequesterappname.c_str());  // LCOV_EXCL_BR_LINE 11:except,C++ STL
370             l_estatus = eFrameworkunifiedStatusAccessError;
371           }
372         }
373       } else {
374         FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__, "Warning: %s requesting %s .. Tag not found",
375                f_crequesterappname.c_str(), f_ctag.c_str());  // LCOV_EXCL_BR_LINE 11:except,C++ STL
376         l_estatus = eFrameworkunifiedStatusFail;
377       }
378     } else {
379       // source not found
380       FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__, "Warning: %s requesting %s .. Requester not found",
381              f_crequesterappname.c_str(), f_ctag.c_str());
382       l_estatus = eFrameworkunifiedStatusFail;
383     }
384   }
385   return l_estatus;
386 }
387
388 VOID CPersistence::AddRequestData(NSP_CopyInfoCmd &f_tcpinfo,
389                                   std::string f_sourcepath,
390                                   std::string f_destpath,
391                                   std::string f_crequesterappname,
392                                   std::string f_ctag,
393                                   ENPS_Loadtype f_eloadtype,
394                                   ENotificationpersistentservicePersistType f_epersisttype) {
395 #ifdef AGL_PosixBasedOS001LEGACY_USED
396   strlcpy(f_tcpinfo.m_csourcepath, f_sourcepath.c_str(), sizeof(f_tcpinfo.m_csourcepath));
397   strlcpy(f_tcpinfo.m_cdestinationpath, f_destpath.c_str(), sizeof(f_tcpinfo.m_cdestinationpath));
398   strlcpy(f_tcpinfo.m_crequesterappname, f_crequesterappname.c_str(), sizeof(f_tcpinfo.m_crequesterappname));
399   strlcpy(f_tcpinfo.m_cpersistenttag, f_ctag.c_str(), sizeof(f_tcpinfo.m_cpersistenttag));
400 #endif
401   f_tcpinfo.m_eloadtype = f_eloadtype;
402   f_tcpinfo.m_epersisttype = f_epersisttype;
403   // f_tcpinfo.m_eCompressionType = ENOTIFICATIONPERSISTENTSERVICECOMPRESSUSINGLIBZ;
404 }
405
406 EFrameworkunifiedStatus CPersistence::SendRequestMessage(NSP_CopyInfoCmd &f_tcpinfo) {
407   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
408   EFrameworkunifiedStatus l_estatus = eFrameworkunifiedStatusOK;
409   HANDLE l_hThreadHandle = NULL;
410   // Worker Command Protocol
411   ENSP_CopyWorkerProtocol l_eWorkerProtocol = CP_WRK_CMD_COPY;
412
413   if (ENOTIFICATIONPERSISTENTSERVICEPERSISTFILE == f_tcpinfo.m_epersisttype) {
414     l_eWorkerProtocol = CP_WRK_CMD_COPY;
415   } else if (ENOTIFICATIONPERSISTENTSERVICEPERSISTFOLDER == f_tcpinfo.m_epersisttype) {
416     l_eWorkerProtocol = AR_CMD_ARCHIVE;
417   }
418
419   if (LOADTYPE_RELEASE == f_tcpinfo.m_eloadtype) {
420     l_hThreadHandle = m_hNSWriteToPersistentMem;
421   } else if (LOADTYPE_LOAD == f_tcpinfo.m_eloadtype) {
422     l_hThreadHandle = m_hNSReadFromPersistentMem;
423   }
424
425   if (l_hThreadHandle) {
426     // issue a copy to the worker thread
427     if (eFrameworkunifiedStatusOK != (l_estatus = McSend(l_hThreadHandle, AppName, l_eWorkerProtocol, sizeof(NSP_CopyInfoCmd), &f_tcpinfo))) {  // LCOV_EXCL_BR_LINE 4: NSFW error case  // NOLINT[whitespace/line_length]
428       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
429       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "McSend for thread failed %d", l_estatus);   // LCOV_EXCL_LINE 4: NSFW error case.
430     }
431   }
432
433   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
434   return l_estatus;
435 }
436
437 ////////////////////////////////////////////////////////////////////////////////////////////
438 /// AckReceivedFromWorker
439 /// This is callback function for ack that file is released to persistenet memory.
440 ////////////////////////////////////////////////////////////////////////////////////////////
441 EFrameworkunifiedStatus CPersistence::AckReceivedFromWorker(PCSTR f_csource, PCSTR f_ctag, BOOL f_bcopystatus,
442                                                ENPS_Loadtype f_eloadtype) {
443   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
444   EFrameworkunifiedStatus retVal = eFrameworkunifiedStatusOK;
445
446   std::string l_cRequester(f_csource);
447   std::string l_cTag(f_ctag);   // LCOV_EXCL_BR_LINE 11:except,C++ STL
448
449   TSourceRegistryListItr l_itTReg = m_mPersistRegistry.find(l_cRequester);
450   if (l_itTReg != m_mPersistRegistry.end()) {
451     // source available, check for file
452     TTagRegistryListItr l_itTRegList = (l_itTReg->second).find(l_cTag);
453     if (l_itTRegList != (l_itTReg->second).end()) {
454       if (LOADTYPE_RELEASE == f_eloadtype) {
455         l_itTRegList->second.SetBeingPersisted();   // LCOV_EXCL_BR_LINE 11: unexpected branch
456
457         if (f_bcopystatus) {
458           FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "File persisted %s / %s", f_csource, f_ctag);
459         } else {
460           FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,  // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
461                  "File not persisted %s / %s, Negative ack received from copy worker thread",
462                  f_csource,
463                  f_ctag);  // LCOV_EXCL_BR_LINE 11:except,C++ STL
464         }
465       }
466
467       // Job processed. Set job state as idle
468       l_itTRegList->second.m_eJobState = ENOTIFICATIONPERSISTENTSERVICEPERSISTJObSTATEIDLE;
469
470       // check if there are any pending jobs for corresponding source and tag
471       if (!m_lPendingJobs.empty()) {
472         FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "Retrieving next pending job for persistence...");
473         TPendingJobsItr l_itrPendingJobs;
474         for (l_itrPendingJobs = m_lPendingJobs.begin(); l_itrPendingJobs != m_lPendingJobs.end(); ++l_itrPendingJobs) {
475           if (0 == std::strcmp((*l_itrPendingJobs).m_crequesterappname, f_csource)
476               && (0 == std::strcmp((*l_itrPendingJobs).m_cpersistenttag, f_ctag))) {
477             if (eFrameworkunifiedStatusOK == (retVal = (SendRequestMessage((*l_itrPendingJobs))))) {  // LCOV_EXCL_BR_LINE 4: NSFW error case.  // NOLINT[whitespace/line_length]
478               // set job state as processing
479               l_itTRegList->second.m_eJobState = ENOTIFICATIONPERSISTENTSERVICEPERSISTJObSTATEINPROCESS;
480               // set current action
481               l_itTRegList->second.SetCurrentAction((*l_itrPendingJobs).m_eloadtype);
482               // Reset persisted flag. It requires to process release request for this tag.
483               // Because, file will be persisted only once. Unless, anyone loads the file again.
484               if (LOADTYPE_LOAD == (*l_itrPendingJobs).m_eloadtype) {
485                 l_itTRegList->second.ResetPersistedFlag();
486               }
487
488               // remove job from pending list
489               m_lPendingJobs.erase(l_itrPendingJobs);
490             } else {
491               // LCOV_EXCL_START 4: NSFW error case.
492               AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
493               FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failure in passing load/release message to the worker threads.");
494               retVal = eFrameworkunifiedStatusFail;
495               // LCOV_EXCL_STOP
496             }
497
498             break;
499           }
500         }
501       } else {
502         FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "No pending jobs");  // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"  // NOLINT[whitespace/line_length]
503       }
504     } else {
505       // tag not found ..
506       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, " ERROR %s, tag %s not found ", f_csource, f_ctag);
507     }
508   } else {
509     // source not found
510     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "ERROR none exists %s , %s", f_csource, f_ctag);
511   }
512
513   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
514   return retVal;
515 }
516
517 ////////////////////////////////////////////////////////////////////////////////////////////
518 /// PersistAllReleaseRequests
519 /// Persist all files which are not persisted yet.
520 ////////////////////////////////////////////////////////////////////////////////////////////
521 EFrameworkunifiedStatus CPersistence::PersistAllReleaseRequests(UI_32 f_uinotificationpersistentservicepersistcategoryflag) {
522   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
523   EFrameworkunifiedStatus l_estatus = eFrameworkunifiedStatusOK;
524
525   m_bPersist = TRUE;  // set to true because persist of all release requests have been triggered.
526   // Trigger reason: shutdown
527
528   m_uiNotificationpersistentservicePersistCategoryFlag = f_uinotificationpersistentservicepersistcategoryflag;
529
530   TSourceRegistryListItr l_itTReg = m_mPersistRegistry.begin();
531
532   for (; l_itTReg != m_mPersistRegistry.end(); ++l_itTReg) {
533     TTagRegistryListItr l_itTRegList = (l_itTReg->second).begin();
534     for (; l_itTRegList != (l_itTReg->second).end(); ++l_itTRegList) {
535       if (l_itTRegList->second.HasntBeenPersisted()) {
536         // reset the data
537         if (((eFrameworkunifiedUserData       == l_itTRegList->second.GetPersistentCategory()) &&
538              (eFrameworkunifiedUserData & f_uinotificationpersistentservicepersistcategoryflag)) ||
539             ((eFrameworkunifiedFactoryData        == l_itTRegList->second.GetPersistentCategory()) &&
540              (eFrameworkunifiedFactoryData & f_uinotificationpersistentservicepersistcategoryflag)) ||
541             ((eFrameworkunifiedFactoryCustomerData  == l_itTRegList->second.GetPersistentCategory()) &&
542              (eFrameworkunifiedFactoryCustomerData & f_uinotificationpersistentservicepersistcategoryflag)) ||
543             ((eFrameworkunifiedDealerData         == l_itTRegList->second.GetPersistentCategory()) &&
544              (eFrameworkunifiedDealerData & f_uinotificationpersistentservicepersistcategoryflag))) {
545           // set the status of file/folder as released and persisted as these data need not to be persisted on shutdown
546           l_itTRegList->second.SetCurrentAction(LOADTYPE_RELEASE);
547           l_itTRegList->second.SetBeingPersisted();
548         } else {  // persist the data
549           if (eFrameworkunifiedStatusOK != Persist(static_cast<CRegistryEntry &>(l_itTRegList->second))) {  // LCOV_EXCL_BR_LINE 4: NSFW error case.  // NOLINT[whitespace/line_length]
550             AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
551             l_estatus = eFrameworkunifiedStatusFail;  // LCOV_EXCL_LINE 4: NSFW error case.
552           }
553         }
554       }
555     }
556   }
557
558   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
559   return l_estatus;
560 }
561
562 BOOL CPersistence::HaveAllReleaseRequestsPersisted(std::string &f_ctagnotreleased) {
563   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
564   BOOL l_bRetVal = TRUE;
565
566   TSourceRegistryListItr l_itTReg = m_mPersistRegistry.begin();
567
568   f_ctagnotreleased.assign("");
569
570   for (; l_itTReg != m_mPersistRegistry.end(); ++l_itTReg) {
571     TTagRegistryListItr l_itTRegList = (l_itTReg->second).begin();
572     for (; l_itTRegList != (l_itTReg->second).end(); ++l_itTRegList) {
573       if (l_itTRegList->second.HasntBeenPersisted()) {
574         l_bRetVal = FALSE;
575
576         if (!l_itTRegList->second.IsReleased()) {
577           f_ctagnotreleased.append("\n");
578           f_ctagnotreleased.append(l_itTRegList->second.GetTag());
579           f_ctagnotreleased.append(" [");
580           f_ctagnotreleased.append(l_itTRegList->second.GetRequester());
581           f_ctagnotreleased.append("]");
582         }
583       }
584     }
585   }
586
587   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
588   return l_bRetVal;
589 }
590
591 ////////////////////////////////////////////////////////////////////////////////////////////
592 /// PersistAllUserRequests
593 /// Persist all user files which are not persisted yet.
594 ////////////////////////////////////////////////////////////////////////////////////////////
595 EFrameworkunifiedStatus CPersistence::PersistAllUserRequests() {
596   EFrameworkunifiedStatus l_estatus = eFrameworkunifiedStatusOK;
597   // TODO(my_username): Persist data on userchange. Uncomment following.
598   //  m_bPersist = TRUE;  // set to true because persist of all user release requests have been triggered.
599   // Trigger reason: user change
600
601   TSourceRegistryListItr l_itTReg = m_mPersistRegistry.begin();
602
603   for (; l_itTReg != m_mPersistRegistry.end(); ++l_itTReg) {
604     TTagRegistryListItr l_itTRegList = (l_itTReg->second).begin();
605     for (; l_itTRegList != (l_itTReg->second).end(); ++l_itTRegList) {
606       if (l_itTRegList->second.IsUserPersistence()) {
607         if (eFrameworkunifiedStatusOK != Persist(static_cast<CRegistryEntry &>(l_itTRegList->second))) {  // LCOV_EXCL_BR_LINE 4: NSFW error case.  // NOLINT[whitespace/line_length]
608           AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
609           l_estatus = eFrameworkunifiedStatusFail;  // LCOV_EXCL_LINE 4: NSFW error case.
610         }
611       }
612     }
613   }
614   return l_estatus;
615 }
616
617 BOOL CPersistence::IsUserPersistence(std::string f_ctag) {
618   BOOL l_bUserPersistence = FALSE;
619
620   TSourceRegistryListItr l_itTReg = m_mPersistRegistry.begin();
621
622   for (; l_itTReg != m_mPersistRegistry.end(); ++l_itTReg) {
623     TTagRegistryListItr l_itTRegList = (l_itTReg->second).begin();
624     for (; l_itTRegList != (l_itTReg->second).end(); ++l_itTRegList) {
625       if (l_itTRegList->second.GetTag() == f_ctag) {
626         if (l_itTRegList->second.IsUserPersistence()) {
627           l_bUserPersistence =  TRUE;
628         }
629         break;
630       }
631     }
632   }
633
634   return l_bUserPersistence;
635 }
636
637 ////////////////////////////////////////////////////////////////////////////////////////////
638 /// SetPersistentCategory
639 /// Sets the persist type of file/folder
640 ////////////////////////////////////////////////////////////////////////////////////////////
641 EFrameworkunifiedStatus CPersistence::SetPersistentCategory(const std::string &f_crequesterappname,
642                                                const std::string &f_ctag,
643                                                EFrameworkunifiedPersistCategory f_epersistcategory) {
644   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
645   EFrameworkunifiedStatus l_estatus = eFrameworkunifiedStatusOK;
646
647   // Persistence Registry map iterator
648   TSourceRegistryListItr l_itTSourceRegistryListItr = m_mPersistRegistry.find(f_crequesterappname);
649
650   if (m_mPersistRegistry.end() != l_itTSourceRegistryListItr) {
651     // source available, check for file if registered
652     TTagRegistryListItr l_itTRegistryList = (l_itTSourceRegistryListItr->second).find(f_ctag);
653
654     if ((l_itTSourceRegistryListItr->second).end() != l_itTRegistryList) {
655       FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "AppName:: %s, Tag:: %s found in registry", f_crequesterappname.c_str(),
656              f_ctag.c_str());
657
658       l_estatus = (l_itTRegistryList->second).SetPersistentCategory(f_epersistcategory);
659     } else {
660       FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__, "Warning: %s didn't register %s", f_crequesterappname.c_str(), f_ctag.c_str());
661       l_estatus = eFrameworkunifiedStatusInvldParam;
662     }
663   } else {
664     // source not registered, cannot persist
665     FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__, "Warning: %s is not registered", f_ctag.c_str());
666     l_estatus = eFrameworkunifiedStatusInvldParam;
667   }
668
669   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
670   return l_estatus;
671 }
672
673 ////////////////////////////////////////////////////////////////////////////////////////////
674 /// ResetPersistFlag
675 /// Resets the persist flag.
676 ////////////////////////////////////////////////////////////////////////////////////////////
677 VOID CPersistence::ResetPersistFlag() {
678   m_bPersist = FALSE;
679
680   m_uiNotificationpersistentservicePersistCategoryFlag = 0;
681 }
682
683 #ifdef NPP_PROFILEINFO_ENABLE
684
685 EFrameworkunifiedStatus CPersistence::GetPersistenceProfilingData(std::string &f_cpersistenceprofileinfo) {
686   EFrameworkunifiedStatus l_estatus = eFrameworkunifiedStatusOK;
687
688   TSourceRegistryListItr l_itrTSourceRegistryListItr;
689   TTagRegistryListItr l_itrTTagRegistryListItr;
690
691   for (l_itrTSourceRegistryListItr =  m_mPersistRegistry.begin();
692        l_itrTSourceRegistryListItr != m_mPersistRegistry.end();
693        l_itrTSourceRegistryListItr++) {
694     for (l_itrTTagRegistryListItr = (l_itrTSourceRegistryListItr->second).begin();
695          l_itrTTagRegistryListItr != (l_itrTSourceRegistryListItr->second).end();
696          l_itrTTagRegistryListItr++) {
697       f_cpersistenceprofileinfo.append("\n");
698       f_cpersistenceprofileinfo.append((*l_itrTSourceRegistryListItr).first);
699       f_cpersistenceprofileinfo.append(",");
700
701       f_cpersistenceprofileinfo.append(l_itrTTagRegistryListItr->first);
702       f_cpersistenceprofileinfo.append(",");
703
704       switch ((l_itrTTagRegistryListItr->second).GetPersistType()) {
705         case ENOTIFICATIONPERSISTENTSERVICEPERSISTFILE: {
706             f_cpersistenceprofileinfo.append("File,");
707           }
708           break;
709
710         case ENOTIFICATIONPERSISTENTSERVICEPERSISTFOLDER: {
711             f_cpersistenceprofileinfo.append("Folder,");
712           }
713           break;
714
715         default: {
716             f_cpersistenceprofileinfo.append(",");
717           }
718           break;
719       }
720
721       if ((l_itrTTagRegistryListItr->second).IsUserPersistence()) {
722         f_cpersistenceprofileinfo.append("Yes,");
723       } else {
724         f_cpersistenceprofileinfo.append("No,");
725       }
726
727       if ((l_itrTTagRegistryListItr->second).IsReleased()) {
728         f_cpersistenceprofileinfo.append("Yes,");
729       } else {
730         f_cpersistenceprofileinfo.append("No,");
731       }
732
733       if ((l_itrTTagRegistryListItr->second).IsPersisted()) {
734         f_cpersistenceprofileinfo.append("Yes");
735       } else {
736         f_cpersistenceprofileinfo.append("No");
737       }
738     }
739   }
740   return l_estatus;
741 }
742
743 #endif