2 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ////////////////////////////////////////////////////////////////////////////////////////////////////
18 /// \defgroup <<Group Tag>> <<Group Name>>
19 /// \ingroup tag_NS_NPPService
21 ////////////////////////////////////////////////////////////////////////////////////////////////////
23 ////////////////////////////////////////////////////////////////////////////////////////////////////
24 /// \ingroup tag_NS_NPPService
25 /// \brief This file contain declaration of class CCopyWorker and CArchive.
26 /// Class CCopyWorker holds the implementation for worker thread.
27 /// Class CArchive holds the implementation for archive.
29 ////////////////////////////////////////////////////////////////////////////////////////////////////
31 #ifndef NOTIFICATION_PERSISTENT_SERVICE_SERVER_INCLUDE_NS_NPP_COPY_WORKER_H_
32 #define NOTIFICATION_PERSISTENT_SERVICE_SERVER_INCLUDE_NS_NPP_COPY_WORKER_H_
35 #define LIBTARSUCCESS 0
44 #include <native_service/frameworkunified_framework_if.h>
45 #include <native_service/ns_utility_sys.hpp>
46 #include <native_service/frameworkunified_multithreading.h>
47 #include <native_service/ns_np_service.h>
52 #include "ns_npp_types.h"
54 typedef std::map<std::string, UI_32> TMTagCRC; // map of tag and corresponding CRC
55 typedef std::map<std::string, TMTagCRC> TMServiceTagCRC; // map of service and tag list
56 typedef std::map<int, gzFile> GZFiles;
58 ////////////////////////////////////////////////////////////////////////////////////////////////
59 /// NSPCopyWorkerOnStart
60 /// Callback method on start of worker thread.
62 /// \param [IN] f_hthread
63 /// HANDLE - Thread Handle
65 /// \return EFrameworkunifiedStatus
66 // EFrameworkunifiedStatus - success or failure status
68 ////////////////////////////////////////////////////////////////////////////////////////////////
69 EFrameworkunifiedStatus NSPCopyWorkerOnStart(HANDLE f_hthread);
71 ////////////////////////////////////////////////////////////////////////////////////////////////
72 /// NSPCopyWorkerOnStop
73 /// Callback method on stopping of worker thread.
75 /// \param [IN] f_hthread
76 /// HANDLE - Thread Handle
78 /// \return EFrameworkunifiedStatus
79 // EFrameworkunifiedStatus - success or failure status
81 ////////////////////////////////////////////////////////////////////////////////////////////////
82 EFrameworkunifiedStatus NSPCopyWorkerOnStop(HANDLE f_hthread);
84 ////////////////////////////////////////////////////////////////////////////////////////////////
86 /// Method to set archiving options.
88 /// \param [IN] f_pcpathname
89 /// PCHAR - Thread Handle
91 /// \param [IN] f_sioflags
92 /// SI_32 - Open file flags
94 /// \param [IN] f_simode
97 /// \return EFrameworkunifiedStatus
98 // EFrameworkunifiedStatus - success or failure status
100 ////////////////////////////////////////////////////////////////////////////////////////////////
101 SI_32 OpenArchive(PCHAR f_pcpathname, SI_32 f_sioflags, SI_32 f_simode);
103 int CloseArchive(int fd);
104 ssize_t ReadArchive(int fd, void *buf, size_t count);
105 ssize_t WriteArchive(int fd, const void *buf, size_t count);
107 ////////////////////////////////////////////////////////////////////////////////////////////
108 /// NPServiceOnDeleteOldDataCmd
109 /// This callback is used to delete the data which was requested to be deleted during
110 /// previous shutdown.
112 /// \param [in] f_happ
113 /// HANDLE - Handle to notificationpersistentservice_application Framework.
116 /// EFrameworkunifiedStatus - success or error
117 ////////////////////////////////////////////////////////////////////////////////////////////
118 EFrameworkunifiedStatus NPServiceOnDeleteOldDataCmd(HANDLE f_happ);
120 ////////////////////////////////////////////////////////////////////////////////////////////
121 /// NSPDataResetThreadCallback
122 /// Callback of data reset thread.
123 /// This callback delete all the data which was requested for reset during previous shutdown.
125 /// \param [in] f_parg
126 /// PVOID - Thread argument
130 ////////////////////////////////////////////////////////////////////////////////////////////
131 PVOID NSPDataResetThreadCallback(PVOID f_parg);
135 static TMServiceTagCRC g_mservicetagcrc;
136 static CMutex g_objmtxservicetag;
138 ////////////////////////////////////////////////////////////////////////////////////////////////
140 /// Constructor of class CCopyWorker
142 ////////////////////////////////////////////////////////////////////////////////////////////////
145 ////////////////////////////////////////////////////////////////////////////////////////////////
147 /// Destructor of class CCopyWorker
149 ////////////////////////////////////////////////////////////////////////////////////////////////
152 ////////////////////////////////////////////////////////////////////////////////////////////////
154 /// Callback method for start copy command.
156 /// \param [IN] f_hthread
157 /// HANDLE - Thread Handle
159 /// \return EFrameworkunifiedStatus
160 // EFrameworkunifiedStatus - success or failure status
162 ////////////////////////////////////////////////////////////////////////////////////////////////
163 EFrameworkunifiedStatus OnWrkCmdCopy(HANDLE hthread);
165 ////////////////////////////////////////////////////////////////////////////////////////////////
167 /// Callback method for resuming worker thread.
169 /// \param [IN] f_hthread
170 /// HANDLE - Thread Handle
172 /// \return EFrameworkunifiedStatus
173 // EFrameworkunifiedStatus - success or failure status
175 ////////////////////////////////////////////////////////////////////////////////////////////////
176 EFrameworkunifiedStatus OnWrkCmdResume(HANDLE hthread);
178 ////////////////////////////////////////////////////////////////////////////////////////////////
180 /// Callback method for starting worker thread.
182 /// \param [IN] f_hthread
183 /// HANDLE - Thread Handle
185 /// \return EFrameworkunifiedStatus
186 // EFrameworkunifiedStatus - success or failure status
188 ////////////////////////////////////////////////////////////////////////////////////////////////
189 EFrameworkunifiedStatus OnWrkCmdStart(HANDLE hthread);
191 ////////////////////////////////////////////////////////////////////////////////////////////////
193 /// Callback method to start archiving/unarchiving for file or folder
195 /// \param [IN] f_hthread
196 /// HANDLE - Thread Handle
198 /// \return EFrameworkunifiedStatus
199 // EFrameworkunifiedStatus - success or failure status
201 ////////////////////////////////////////////////////////////////////////////////////////////////
202 EFrameworkunifiedStatus OnWrkCmdArchive(HANDLE f_hthread);
204 ////////////////////////////////////////////////////////////////////////////////////////////////
206 /// Callback method for stopping worker thread.
208 /// \param [IN] f_hthread
209 /// HANDLE - Thread Handle
211 /// \return EFrameworkunifiedStatus
212 // EFrameworkunifiedStatus - success or failure status
214 ////////////////////////////////////////////////////////////////////////////////////////////////
215 EFrameworkunifiedStatus OnWrkCmdStop(HANDLE hthread);
217 ////////////////////////////////////////////////////////////////////////////////////////////////
218 /// OnCmdShutdownRequest
219 /// Callback method on shutdown request.
221 /// \param [IN] f_hthread
222 /// HANDLE - Thread Handle
224 /// \return EFrameworkunifiedStatus
225 // EFrameworkunifiedStatus - success or failure status
227 ////////////////////////////////////////////////////////////////////////////////////////////////
228 EFrameworkunifiedStatus OnCmdShutdownRequest(HANDLE hthread);
230 ////////////////////////////////////////////////////////////////////////////////////////////////
232 /// Method to abort worker thread tasks.
237 ////////////////////////////////////////////////////////////////////////////////////////////////
240 ////////////////////////////////////////////////////////////////////////////////////////////////
241 /// SetCopyStatusToFailure
242 /// Method to set the copy status to failure.
244 /// \param [IN] f_tcopystatusresponse
245 /// NSP_CopyStatusResponse - Response status struct
247 /// \param [IN] f_eworkerfailcmd
248 /// ENPS_CopyWorkerFailures - Worker failure Commands
250 /// \return EFrameworkunifiedStatus
251 // EFrameworkunifiedStatus - success or failure status
253 ////////////////////////////////////////////////////////////////////////////////////////////////
254 EFrameworkunifiedStatus SetCopyStatusToFailure(NSP_CopyStatusResponse &f_tcopystatusresponse, // NOLINT (runtime/references)
255 const ENPS_CopyWorkerFailures f_eworkerfailcmd);
257 ////////////////////////////////////////////////////////////////////////////////////////////////
259 /// Method to check if the thread is in abort state
261 /// \param [IN] f_hthread
262 /// HANDLE - Thread Handle
265 // BOOL - true if in abort state or false.
267 ////////////////////////////////////////////////////////////////////////////////////////////////
268 BOOL InAbortState() const;
270 ////////////////////////////////////////////////////////////////////////////////////////////////
272 /// Method to send ack to the parent thread
274 /// \param [IN] f_hthread
275 /// HANDLE - Thread Handle
277 /// \param [IN] f_eworkercmd
278 /// ENSP_CopyWorkerProtocol - Worker protocol command
280 /// \return EFrameworkunifiedStatus
281 // EFrameworkunifiedStatus - success or failure status
283 ////////////////////////////////////////////////////////////////////////////////////////////////
284 EFrameworkunifiedStatus SendAck(HANDLE hthread, const ENSP_CopyWorkerProtocol &f_eworkercmd);
287 static pthread_mutex_t m_sAbortMutex; // NOLINT (readability/naming) , Mutex for abort
288 static BOOL m_sbAbortCopy; // NOLINT (readability/naming) , Check for abort state
290 ////////////////////////////////////////////////////////////////////////////////////////////////
292 /// Copy the content of source file f_csrcpath to destination file f_cdestpath.
293 /// Creates the destination file path if does not exists.
295 /// \param [IN] f_csrcpath
296 /// PCSTR - source file path
298 /// \param [IN] f_cdestpath
299 /// PCSTR - dest file path
301 /// \param [OUT] f_uiwritesize
302 /// UI_32 - number of bytes written to the destination file
304 /// \param [IN] f_btmpfile
305 /// BOOL - TRUE:RELEASE-temp file use /FALSE:LOAD-temp file not use
307 /// \return EFrameworkunifiedStatus
308 /// EFrameworkunifiedStatus - eFrameworkunifiedStatusOK on success else eFrameworkunifiedStatusFail
310 ////////////////////////////////////////////////////////////////////////////////////////////////
311 EFrameworkunifiedStatus CopyFile(PCSTR f_csrcpath, PCSTR f_cdestpath, UI_32 &f_uiwritesize, BOOL btmpfile = FALSE); // NOLINT (runtime/references)
313 ////////////////////////////////////////////////////////////////////////////////////////////////
315 /// Get the buffer size appropriate for file copy
317 /// \param [IN] f_si32openfd
318 /// SI_32 - File descriptor of a file opened with open() api
320 /// \param [OUT] f_ui32filesize
321 /// UI_32 - Get the size of file
324 /// SI_32 - Returns the buffer size appropriate for file copy
326 ////////////////////////////////////////////////////////////////////////////////////////////////
327 SI_32 GetCopyBufSize(SI_32 f_si32openfd, UI_32 &f_ui32filesize); // NOLINT (runtime/references)
329 ////////////////////////////////////////////////////////////////////////////////////////////////
330 /// VerifyWithStoredCRC
331 /// Verify CRC stored internally for the tag matches with the CRC of a file (file passed as a argument)
333 /// \param [IN] f_crequesterappname
334 /// PCSTR - Name of the requester. This is to find the corresponding stored CRC
336 /// \param [IN] f_cpersistenttag
337 /// PCSTR - Tag. This is to find the corresponding stored CRC
339 /// \param [IN] f_cFile
340 /// PCSTR - Complete file path on temporary storage.
341 /// Comparison of CRC needs to be done with this temporary file.
344 /// BOOL - Returns TRUE if f_cFile's CRC matches with CRC stored internally for the corresponding tag
347 ////////////////////////////////////////////////////////////////////////////////////////////////
348 BOOL VerifyWithStoredCRC(PCSTR f_crequesterappname, PCSTR f_cpersistenttag, PCSTR f_cFile, UI_32 &f_ui32crc); // NOLINT (runtime/references)
350 ////////////////////////////////////////////////////////////////////////////////////////////////
352 /// Update the passed CRC in the internal map for the corresponding requester and tag.
354 /// \param [IN] f_crequesterappname
355 /// PCSTR - Name of the requester.
357 /// \param [IN] f_cpersistenttag
358 /// PCSTR - File Tag.
360 /// \param [IN] f_ui32crc
365 ////////////////////////////////////////////////////////////////////////////////////////////////
366 VOID UpdateTagCRC(PCSTR f_crequesterappname, PCSTR f_cpersistenttag, UI_32 f_ui32crc);
368 ////////////////////////////////////////////////////////////////////////////////////////////////
369 /// TryFileCopyWithCRCCheck
370 /// Try to copy file from source to destination until src and dest CRC matches or max f_ui32checkcount
373 /// \param [IN] f_csource
374 /// PCSTR - Source file
376 /// \param [IN] f_cdest
377 /// PCSTR - Destination file
379 /// \param [IN] f_ui32checkcount
380 /// UI_32 - Maximum number of times to perform file copy
382 /// \return EFrameworkunifiedStatus
383 /// EFrameworkunifiedStatus - eFrameworkunifiedStatusOK - If file copy done successfully and CRC matches.
384 /// eFrameworkunifiedStatusFail - otherwise
386 ////////////////////////////////////////////////////////////////////////////////////////////////
387 EFrameworkunifiedStatus TryFileCopyWithCRCCheck(PCSTR f_csource, PCSTR f_cdest, UI_32 f_ui32checkcount);
391 ////////////////////////////////////////////////////////////////////////////////////////////////
393 /// Constructor of CArchive class
399 ////////////////////////////////////////////////////////////////////////////////////////////////
402 ////////////////////////////////////////////////////////////////////////////////////////////////
404 /// Destructor of CArchive class
410 ////////////////////////////////////////////////////////////////////////////////////////////////
413 ////////////////////////////////////////////////////////////////////////////////////////////////
415 /// Method for performing folder archive or extraction operation.
417 /// \param [IN] f_tarchiveinfocmd
418 /// NSP_CopyInfoCmd - Archive Command Info Structure
420 /// \param [IN] f_tarchivestatusresponse
421 /// NSP_CopyStatusResponse - Archive Command Response Structure
423 /// \return EFrameworkunifiedStatus
424 // EFrameworkunifiedStatus - success or failure status
426 ////////////////////////////////////////////////////////////////////////////////////////////////
427 EFrameworkunifiedStatus Archive(NSP_CopyInfoCmd &f_tarchiveinfocmd, NSP_CopyStatusResponse &f_tarchivestatusresponse); // NOLINT (runtime/references)
429 ////////////////////////////////////////////////////////////////////////////////////////////////
430 /// FileOperationUsingLibz
431 /// Method to determine whether to compress or decompres file using libz.
433 /// \param [IN] f_toperainfocmd
434 /// NSP_CopyInfoCmd - File Operation Command Info Structure
436 /// \param [IN] f_toperarespstatus
437 /// NSP_CopyStatusResponse - File Operation Command Response Structure
439 /// \return EFrameworkunifiedStatus
440 // EFrameworkunifiedStatus - success or failure status
442 ////////////////////////////////////////////////////////////////////////////////////////////////
443 EFrameworkunifiedStatus FileOperationUsingLibz(NSP_CopyInfoCmd &f_toperainfocmd, NSP_CopyStatusResponse &f_toperarespstatus); // NOLINT (runtime/references)
446 ////////////////////////////////////////////////////////////////////////////////////////////////
448 /// Method to Create tar archive.
450 /// \param [IN] f_csourepath
451 /// std::string - Source path of the folder to be archived
453 /// \param [IN] f_cdestpath
454 /// std::string - Destination path for the archived folder.
456 /// \param [IN] f_tarchivestatusresponse
457 /// NSP_CopyStatusResponse - Archive Command Response Structure
459 /// \return EFrameworkunifiedStatus
460 // EFrameworkunifiedStatus - success or failure status
462 ////////////////////////////////////////////////////////////////////////////////////////////////
463 EFrameworkunifiedStatus CreateTar(const std::string &f_csourepath, const std::string &f_cdestpath,
464 NSP_CopyStatusResponse &f_tarchivestatusresponse); // NOLINT (runtime/references)
466 ////////////////////////////////////////////////////////////////////////////////////////////////
468 /// Method to extract tar archive.
470 /// \param [IN] f_csourepath
471 /// std::string - Source path of the folder to be extracted
473 /// \param [IN] f_csourcedest
474 /// std::string - Destination path for the extracted folder.
476 /// \param [IN] f_tarchivestatusresponse
477 /// NSP_CopyStatusResponse - Archive Command Response Structure
479 /// \return EFrameworkunifiedStatus
480 // EFrameworkunifiedStatus - success or failure status
482 ////////////////////////////////////////////////////////////////////////////////////////////////
483 EFrameworkunifiedStatus ExtractTar(const std::string &f_csourepath, const std::string &f_csourcedest,
484 NSP_CopyStatusResponse &f_tarchivestatusresponse); // NOLINT (runtime/references)
486 ////////////////////////////////////////////////////////////////////////////////////////////////
488 /// Method to Create archive
490 /// \param [IN] f_pctarfiledestpath
491 /// CHAR - Destination path for the archived folder.
493 /// \param [IN] f_pcrootdirpath
494 /// CHAR - Source path of the folder to be archived
496 /// \param [IN] f_plibtarlist
497 /// BOOL - TRUE if user file else FALSE
502 ////////////////////////////////////////////////////////////////////////////////////////////////
503 EFrameworkunifiedStatus Create(std::string &f_pctarfiledestpath, std::string &f_pcrootdirpath, libtar_list_t *f_plibtarlist); // NOLINT (runtime/references)
505 ////////////////////////////////////////////////////////////////////////////////////////////////
507 /// Nethod to extract archive.
509 /// \param [IN] f_pcrootdirpath
510 /// CHAR - Source path of the folder to be extracted
512 /// \param [IN] f_pctarfiledestpath
513 /// CHAR - Destination path for extraction.
518 ////////////////////////////////////////////////////////////////////////////////////////////////
519 EFrameworkunifiedStatus Extract(std::string &f_pcrootdirpath, std::string &f_pctarfiledestpath); // NOLINT (runtime/references)
521 ////////////////////////////////////////////////////////////////////////////////////////////////
522 /// CompressUsingZlib
523 /// Method to compress file using libz
525 /// \param [IN] f_csourepath
526 /// std::string - Source path of the file to be compressed
528 /// \param [IN] f_cdestpath
529 /// std::string - Destination path for the compressed file.
531 /// \param [IN] f_iziplevel
532 /// Level of compression i.e.
533 /// More compression results in more time taken for compression operation and vice versa.
535 /// \return EFrameworkunifiedStatus
536 // EFrameworkunifiedStatus - success or failure status
538 ////////////////////////////////////////////////////////////////////////////////////////////////
539 EFrameworkunifiedStatus CompressUsingZlib(const std::string &f_csourepath, const std::string &f_cdestpath, SI_32 f_iziplevel);
541 ////////////////////////////////////////////////////////////////////////////////////////////////
542 /// DeCompressUsingZlib
543 /// Method to decompress file using libz.
545 /// \param [IN] f_csourepath
546 /// std::string - Source path of the file to be decompressed.
548 /// \param [IN] f_csourcedest
549 /// std::string - Destination path for the decompressed file.
551 /// \return EFrameworkunifiedStatus
552 // EFrameworkunifiedStatus - success or failure status
554 ////////////////////////////////////////////////////////////////////////////////////////////////
555 EFrameworkunifiedStatus DeCompressUsingZlib(const std::string &f_csourepath, const std::string &f_csourcedest);
558 #endif // NOTIFICATION_PERSISTENT_SERVICE_SERVER_INCLUDE_NS_NPP_COPY_WORKER_H_