common_library: gettid is multiple declaration in cl_error
[staging/basesystem.git] / video_in_hal / nsframework / notification_persistent_service / server / include / ns_npp_copy_worker.h
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 contain declaration of class CCopyWorker and CArchive.
26 ///       Class CCopyWorker holds the implementation for worker thread.
27 ///       Class CArchive holds the implementation for archive.
28 ///
29 ////////////////////////////////////////////////////////////////////////////////////////////////////
30
31 #ifndef NOTIFICATION_PERSISTENT_SERVICE_SERVER_INCLUDE_NS_NPP_COPY_WORKER_H_
32 #define NOTIFICATION_PERSISTENT_SERVICE_SERVER_INCLUDE_NS_NPP_COPY_WORKER_H_
33
34 #define LIBTARFAIL   -1
35 #define LIBTARSUCCESS 0
36 #define TARMODE       0644
37 #define FILEERROR   -1
38
39 #include <pthread.h>
40 #include <libtar.h>
41 #include <stdio.h>
42 #include <zlib.h>
43
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>
48
49 #include <map>
50 #include <string>
51
52 #include "ns_npp_types.h"
53
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;
57
58 ////////////////////////////////////////////////////////////////////////////////////////////////
59 /// NSPCopyWorkerOnStart
60 /// Callback method on start of worker thread.
61 ///
62 /// \param [IN] f_hthread
63 ///      HANDLE - Thread Handle
64 ///
65 /// \return EFrameworkunifiedStatus
66 //      EFrameworkunifiedStatus - success or failure status
67 ///
68 ////////////////////////////////////////////////////////////////////////////////////////////////
69 EFrameworkunifiedStatus NSPCopyWorkerOnStart(HANDLE f_hthread);
70
71 ////////////////////////////////////////////////////////////////////////////////////////////////
72 /// NSPCopyWorkerOnStop
73 /// Callback method on stopping of worker thread.
74 ///
75 /// \param [IN] f_hthread
76 ///      HANDLE - Thread Handle
77 ///
78 /// \return EFrameworkunifiedStatus
79 //      EFrameworkunifiedStatus - success or failure status
80 ///
81 ////////////////////////////////////////////////////////////////////////////////////////////////
82 EFrameworkunifiedStatus NSPCopyWorkerOnStop(HANDLE f_hthread);
83
84 ////////////////////////////////////////////////////////////////////////////////////////////////
85 /// gzopen_frontend
86 /// Method to set archiving options.
87 ///
88 /// \param [IN] f_pcpathname
89 ///      PCHAR - Thread Handle
90 ///
91 /// \param [IN] f_sioflags
92 ///      SI_32 - Open file flags
93 ///
94 /// \param [IN] f_simode
95 ///      SI_32 - File Mode
96 ///
97 /// \return EFrameworkunifiedStatus
98 //      EFrameworkunifiedStatus - success or failure status
99 ///
100 ////////////////////////////////////////////////////////////////////////////////////////////////
101 SI_32 OpenArchive(PCHAR f_pcpathname, SI_32 f_sioflags, SI_32 f_simode);
102
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);
106
107 ////////////////////////////////////////////////////////////////////////////////////////////
108 /// NPServiceOnDeleteOldDataCmd
109 /// This callback is used to delete the data which was requested to be deleted during
110 /// previous shutdown.
111 ///
112 /// \param [in] f_happ
113 ///         HANDLE - Handle to notificationpersistentservice_application Framework.
114 ///
115 /// \return status
116 ///         EFrameworkunifiedStatus - success or error
117 ////////////////////////////////////////////////////////////////////////////////////////////
118 EFrameworkunifiedStatus NPServiceOnDeleteOldDataCmd(HANDLE f_happ);
119
120 ////////////////////////////////////////////////////////////////////////////////////////////
121 /// NSPDataResetThreadCallback
122 /// Callback of data reset thread.
123 /// This callback delete all the data which was requested for reset during previous shutdown.
124 ///
125 /// \param [in] f_parg
126 ///         PVOID - Thread argument
127 ///
128 /// \return PVOID
129 ///         PVOID - NULL
130 ////////////////////////////////////////////////////////////////////////////////////////////
131 PVOID NSPDataResetThreadCallback(PVOID f_parg);
132
133 class CCopyWorker {
134  public:
135   static TMServiceTagCRC g_mservicetagcrc;
136   static CMutex g_objmtxservicetag;
137
138   ////////////////////////////////////////////////////////////////////////////////////////////////
139   /// CCopyWorker
140   /// Constructor of class CCopyWorker
141   ///
142   ////////////////////////////////////////////////////////////////////////////////////////////////
143   CCopyWorker();
144
145   ////////////////////////////////////////////////////////////////////////////////////////////////
146   /// ~CCopyWorker
147   /// Destructor of class CCopyWorker
148   ///
149   ////////////////////////////////////////////////////////////////////////////////////////////////
150   ~CCopyWorker();
151
152   ////////////////////////////////////////////////////////////////////////////////////////////////
153   /// OnWrkCmdCopy
154   /// Callback method for start copy command.
155   ///
156   /// \param [IN] f_hthread
157   ///      HANDLE - Thread Handle
158   ///
159   /// \return EFrameworkunifiedStatus
160   //      EFrameworkunifiedStatus - success or failure status
161   ///
162   ////////////////////////////////////////////////////////////////////////////////////////////////
163   EFrameworkunifiedStatus OnWrkCmdCopy(HANDLE hthread);
164
165   ////////////////////////////////////////////////////////////////////////////////////////////////
166   /// OnWrkCmdResume
167   /// Callback method for resuming worker thread.
168   ///
169   /// \param [IN] f_hthread
170   ///      HANDLE - Thread Handle
171   ///
172   /// \return EFrameworkunifiedStatus
173   //      EFrameworkunifiedStatus - success or failure status
174   ///
175   ////////////////////////////////////////////////////////////////////////////////////////////////
176   EFrameworkunifiedStatus OnWrkCmdResume(HANDLE hthread);
177
178   ////////////////////////////////////////////////////////////////////////////////////////////////
179   /// OnWrkCmdStart
180   /// Callback method for starting worker thread.
181   ///
182   /// \param [IN] f_hthread
183   ///      HANDLE - Thread Handle
184   ///
185   /// \return EFrameworkunifiedStatus
186   //      EFrameworkunifiedStatus - success or failure status
187   ///
188   ////////////////////////////////////////////////////////////////////////////////////////////////
189   EFrameworkunifiedStatus OnWrkCmdStart(HANDLE hthread);
190
191   ////////////////////////////////////////////////////////////////////////////////////////////////
192   /// OnWrkCmdArchive
193   /// Callback method to start archiving/unarchiving for file or folder
194   ///
195   /// \param [IN] f_hthread
196   ///      HANDLE - Thread Handle
197   ///
198   /// \return EFrameworkunifiedStatus
199   //      EFrameworkunifiedStatus - success or failure status
200   ///
201   ////////////////////////////////////////////////////////////////////////////////////////////////
202   EFrameworkunifiedStatus OnWrkCmdArchive(HANDLE f_hthread);
203
204   ////////////////////////////////////////////////////////////////////////////////////////////////
205   /// OnWrkCmdStop
206   /// Callback method for stopping worker thread.
207   ///
208   /// \param [IN] f_hthread
209   ///      HANDLE - Thread Handle
210   ///
211   /// \return EFrameworkunifiedStatus
212   //      EFrameworkunifiedStatus - success or failure status
213   ///
214   ////////////////////////////////////////////////////////////////////////////////////////////////
215   EFrameworkunifiedStatus OnWrkCmdStop(HANDLE hthread);
216
217   ////////////////////////////////////////////////////////////////////////////////////////////////
218   /// OnCmdShutdownRequest
219   /// Callback method on shutdown request.
220   ///
221   /// \param [IN] f_hthread
222   ///      HANDLE - Thread Handle
223   ///
224   /// \return EFrameworkunifiedStatus
225   //      EFrameworkunifiedStatus - success or failure status
226   ///
227   ////////////////////////////////////////////////////////////////////////////////////////////////
228   EFrameworkunifiedStatus OnCmdShutdownRequest(HANDLE hthread);
229
230   ////////////////////////////////////////////////////////////////////////////////////////////////
231   /// Abort
232   /// Method to abort worker thread tasks.
233   ///
234   ///
235   /// \return VOID
236   //
237   ////////////////////////////////////////////////////////////////////////////////////////////////
238   static VOID Abort();
239
240   ////////////////////////////////////////////////////////////////////////////////////////////////
241   /// SetCopyStatusToFailure
242   /// Method to set the copy status to failure.
243   ///
244   /// \param [IN] f_tcopystatusresponse
245   ///      NSP_CopyStatusResponse - Response status struct
246   ///
247   /// \param [IN] f_eworkerfailcmd
248   ///      ENPS_CopyWorkerFailures - Worker failure Commands
249   ///
250   /// \return EFrameworkunifiedStatus
251   //      EFrameworkunifiedStatus - success or failure status
252   ///
253   ////////////////////////////////////////////////////////////////////////////////////////////////
254   EFrameworkunifiedStatus SetCopyStatusToFailure(NSP_CopyStatusResponse &f_tcopystatusresponse, // NOLINT (runtime/references)
255                                     const ENPS_CopyWorkerFailures f_eworkerfailcmd);
256
257   ////////////////////////////////////////////////////////////////////////////////////////////////
258   /// InAbortState
259   /// Method to check if the thread is in abort state
260   ///
261   /// \param [IN] f_hthread
262   ///      HANDLE - Thread Handle
263   ///
264   /// \return BOOL
265   //      BOOL - true if in abort state or false.
266   ///
267   ////////////////////////////////////////////////////////////////////////////////////////////////
268   BOOL InAbortState() const;
269
270   ////////////////////////////////////////////////////////////////////////////////////////////////
271   /// SendAck
272   /// Method to send ack to the parent thread
273   ///
274   /// \param [IN] f_hthread
275   ///      HANDLE - Thread Handle
276   ///
277   /// \param [IN] f_eworkercmd
278   ///      ENSP_CopyWorkerProtocol - Worker protocol command
279   ///
280   /// \return EFrameworkunifiedStatus
281   //      EFrameworkunifiedStatus - success or failure status
282   ///
283   ////////////////////////////////////////////////////////////////////////////////////////////////
284   EFrameworkunifiedStatus SendAck(HANDLE hthread, const ENSP_CopyWorkerProtocol &f_eworkercmd);
285
286  private:
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
289
290   ////////////////////////////////////////////////////////////////////////////////////////////////
291   /// CopyFile
292   /// Copy the content of source file f_csrcpath to destination file f_cdestpath.
293   /// Creates the destination file path if does not exists.
294   ///
295   /// \param [IN] f_csrcpath
296   ///        PCSTR - source file path
297   ///
298   /// \param [IN] f_cdestpath
299   ///        PCSTR - dest file path
300   ///
301   /// \param [OUT] f_uiwritesize
302   ///        UI_32 - number of bytes written to the destination file
303   ///
304   /// \param [IN] f_btmpfile
305   ///        BOOL - TRUE:RELEASE-temp file use /FALSE:LOAD-temp file not use
306   ///
307   /// \return EFrameworkunifiedStatus
308   ///          EFrameworkunifiedStatus - eFrameworkunifiedStatusOK on success else eFrameworkunifiedStatusFail
309   ///
310   ////////////////////////////////////////////////////////////////////////////////////////////////
311   EFrameworkunifiedStatus CopyFile(PCSTR f_csrcpath, PCSTR f_cdestpath, UI_32 &f_uiwritesize, BOOL btmpfile = FALSE); // NOLINT (runtime/references)
312
313   ////////////////////////////////////////////////////////////////////////////////////////////////
314   /// GetCopyBufSize
315   /// Get the buffer size appropriate for file copy
316   ///
317   /// \param [IN] f_si32openfd
318   ///        SI_32 - File descriptor of a file opened with open() api
319   ///
320   /// \param [OUT] f_ui32filesize
321   ///        UI_32 - Get the size of file
322   ///
323   /// \return SI_32
324   ///          SI_32 - Returns the buffer size appropriate for file copy
325   ///
326   ////////////////////////////////////////////////////////////////////////////////////////////////
327   SI_32 GetCopyBufSize(SI_32 f_si32openfd, UI_32 &f_ui32filesize); // NOLINT (runtime/references)
328
329   ////////////////////////////////////////////////////////////////////////////////////////////////
330   /// VerifyWithStoredCRC
331   /// Verify CRC stored internally for the tag matches with the CRC of a file (file passed as a argument)
332   ///
333   /// \param [IN] f_crequesterappname
334   ///        PCSTR - Name of the requester. This is to find the corresponding stored CRC
335   ///
336   /// \param [IN] f_cpersistenttag
337   ///        PCSTR - Tag. This is to find the corresponding stored CRC
338   ///
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.
342   ///
343   /// \return BOOL
344   ///          BOOL - Returns TRUE if f_cFile's CRC matches with CRC stored internally for the corresponding tag
345   ///         else FALSE
346   ///
347   ////////////////////////////////////////////////////////////////////////////////////////////////
348   BOOL VerifyWithStoredCRC(PCSTR f_crequesterappname, PCSTR f_cpersistenttag, PCSTR f_cFile, UI_32 &f_ui32crc); // NOLINT (runtime/references)
349
350   ////////////////////////////////////////////////////////////////////////////////////////////////
351   /// UpdateTagCRC
352   /// Update the passed CRC in the internal map for the corresponding requester and tag.
353   ///
354   /// \param [IN] f_crequesterappname
355   ///        PCSTR - Name of the requester.
356   ///
357   /// \param [IN] f_cpersistenttag
358   ///        PCSTR - File Tag.
359   ///
360   /// \param [IN] f_ui32crc
361   ///        UI_32 - New CRC.
362   ///
363   /// \return None
364   ///
365   ////////////////////////////////////////////////////////////////////////////////////////////////
366   VOID UpdateTagCRC(PCSTR f_crequesterappname, PCSTR f_cpersistenttag, UI_32 f_ui32crc);
367
368   ////////////////////////////////////////////////////////////////////////////////////////////////
369   /// TryFileCopyWithCRCCheck
370   /// Try to copy file from source to destination until src and dest CRC matches or max f_ui32checkcount
371   /// number of times
372   ///
373   /// \param [IN] f_csource
374   ///        PCSTR - Source file
375   ///
376   /// \param [IN] f_cdest
377   ///        PCSTR - Destination file
378   ///
379   /// \param [IN] f_ui32checkcount
380   ///        UI_32 - Maximum number of times to perform file copy
381   ///
382   /// \return EFrameworkunifiedStatus
383   ///          EFrameworkunifiedStatus - eFrameworkunifiedStatusOK - If file copy done successfully and CRC matches.
384   ///             eFrameworkunifiedStatusFail - otherwise
385   ///
386   ////////////////////////////////////////////////////////////////////////////////////////////////
387   EFrameworkunifiedStatus TryFileCopyWithCRCCheck(PCSTR f_csource, PCSTR f_cdest, UI_32 f_ui32checkcount);
388
389   class CArchive {
390    public:
391     ////////////////////////////////////////////////////////////////////////////////////////////////
392     /// CArchive
393     /// Constructor of CArchive class
394     ///
395     /// \param
396     ///
397     /// \return
398     ///
399     ////////////////////////////////////////////////////////////////////////////////////////////////
400     CArchive();
401
402     ////////////////////////////////////////////////////////////////////////////////////////////////
403     /// CArchive
404     /// Destructor of CArchive class
405     ///
406     /// \param
407     ///
408     /// \return
409     ///
410     ////////////////////////////////////////////////////////////////////////////////////////////////
411     virtual ~CArchive();
412
413     ////////////////////////////////////////////////////////////////////////////////////////////////
414     /// Archive
415     ///  Method for performing folder archive or extraction operation.
416     ///
417     /// \param [IN] f_tarchiveinfocmd
418     ///      NSP_CopyInfoCmd - Archive Command Info Structure
419     ///
420     /// \param [IN] f_tarchivestatusresponse
421     ///      NSP_CopyStatusResponse - Archive Command Response Structure
422     ///
423     /// \return EFrameworkunifiedStatus
424     //      EFrameworkunifiedStatus - success or failure status
425     ///
426     ////////////////////////////////////////////////////////////////////////////////////////////////
427     EFrameworkunifiedStatus Archive(NSP_CopyInfoCmd &f_tarchiveinfocmd, NSP_CopyStatusResponse &f_tarchivestatusresponse); // NOLINT (runtime/references)
428
429     ////////////////////////////////////////////////////////////////////////////////////////////////
430     ///  FileOperationUsingLibz
431     ///  Method to determine whether to compress or decompres file using libz.
432     ///
433     /// \param [IN] f_toperainfocmd
434     ///      NSP_CopyInfoCmd - File  Operation Command Info Structure
435     ///
436     /// \param [IN] f_toperarespstatus
437     ///      NSP_CopyStatusResponse - File Operation Command Response Structure
438     ///
439     /// \return EFrameworkunifiedStatus
440     //      EFrameworkunifiedStatus - success or failure status
441     ///
442     ////////////////////////////////////////////////////////////////////////////////////////////////
443     EFrameworkunifiedStatus FileOperationUsingLibz(NSP_CopyInfoCmd &f_toperainfocmd, NSP_CopyStatusResponse &f_toperarespstatus); // NOLINT (runtime/references)
444
445    private:
446     ////////////////////////////////////////////////////////////////////////////////////////////////
447     /// CreateTar
448     /// Method to Create tar archive.
449     ///
450     /// \param [IN] f_csourepath
451     ///      std::string - Source path of the folder to be archived
452     ///
453     /// \param [IN] f_cdestpath
454     ///      std::string - Destination path for the archived folder.
455     ///
456     /// \param [IN] f_tarchivestatusresponse
457     ///      NSP_CopyStatusResponse - Archive Command Response Structure
458     ///
459     /// \return EFrameworkunifiedStatus
460     //      EFrameworkunifiedStatus - success or failure status
461     ///
462     ////////////////////////////////////////////////////////////////////////////////////////////////
463     EFrameworkunifiedStatus CreateTar(const std::string &f_csourepath, const std::string &f_cdestpath,
464                          NSP_CopyStatusResponse &f_tarchivestatusresponse); // NOLINT (runtime/references)
465
466     ////////////////////////////////////////////////////////////////////////////////////////////////
467     /// ExtractTar
468     /// Method to extract tar archive.
469     ///
470     /// \param [IN] f_csourepath
471     ///      std::string - Source path of the folder to be extracted
472     ///
473     /// \param [IN] f_csourcedest
474     ///      std::string - Destination path for the extracted folder.
475     ///
476     /// \param [IN] f_tarchivestatusresponse
477     ///      NSP_CopyStatusResponse - Archive Command Response Structure
478     ///
479     /// \return EFrameworkunifiedStatus
480     //      EFrameworkunifiedStatus - success or failure status
481     ///
482     ////////////////////////////////////////////////////////////////////////////////////////////////
483     EFrameworkunifiedStatus ExtractTar(const std::string &f_csourepath, const std::string &f_csourcedest,
484                           NSP_CopyStatusResponse &f_tarchivestatusresponse); // NOLINT (runtime/references)
485
486     ////////////////////////////////////////////////////////////////////////////////////////////////
487     /// Create
488     /// Method to Create archive
489     ///
490     /// \param [IN] f_pctarfiledestpath
491     ///      CHAR  - Destination path for the archived folder.
492     ///
493     /// \param [IN] f_pcrootdirpath
494     ///      CHAR  - Source path of the folder to be archived
495     ///
496     /// \param [IN] f_plibtarlist
497     ///      BOOL - TRUE if user file else FALSE
498     ///
499     /// \return SI_32
500     //
501     ///
502     ////////////////////////////////////////////////////////////////////////////////////////////////
503     EFrameworkunifiedStatus Create(std::string &f_pctarfiledestpath, std::string &f_pcrootdirpath, libtar_list_t *f_plibtarlist); // NOLINT (runtime/references)
504
505     ////////////////////////////////////////////////////////////////////////////////////////////////
506     /// Extract
507     ///  Nethod to extract archive.
508     ///
509     /// \param [IN] f_pcrootdirpath
510     ///      CHAR - Source path of the folder to be extracted
511     ///
512     /// \param [IN] f_pctarfiledestpath
513     ///      CHAR - Destination path for extraction.
514     ///
515     /// \return SI_32
516     //
517     ///
518     ////////////////////////////////////////////////////////////////////////////////////////////////
519     EFrameworkunifiedStatus Extract(std::string &f_pcrootdirpath, std::string &f_pctarfiledestpath); // NOLINT (runtime/references)
520
521     ////////////////////////////////////////////////////////////////////////////////////////////////
522     /// CompressUsingZlib
523     /// Method to compress file using libz
524     ///
525     /// \param [IN] f_csourepath
526     ///      std::string - Source path of the file to be compressed
527     ///
528     /// \param [IN] f_cdestpath
529     ///      std::string - Destination path for the compressed file.
530     ///
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.
534     ///
535     /// \return EFrameworkunifiedStatus
536     //      EFrameworkunifiedStatus - success or failure status
537     ///
538     ////////////////////////////////////////////////////////////////////////////////////////////////
539     EFrameworkunifiedStatus CompressUsingZlib(const std::string &f_csourepath, const  std::string &f_cdestpath, SI_32 f_iziplevel);
540
541     ////////////////////////////////////////////////////////////////////////////////////////////////
542     /// DeCompressUsingZlib
543     /// Method to decompress file using libz.
544     ///
545     /// \param [IN] f_csourepath
546     ///      std::string - Source path of the file to be decompressed.
547     ///
548     /// \param [IN] f_csourcedest
549     ///      std::string - Destination path for the decompressed file.
550     ///
551     /// \return EFrameworkunifiedStatus
552     //      EFrameworkunifiedStatus - success or failure status
553     ///
554     ////////////////////////////////////////////////////////////////////////////////////////////////
555     EFrameworkunifiedStatus DeCompressUsingZlib(const std::string &f_csourepath, const std::string &f_csourcedest);
556   };
557 };
558 #endif  // NOTIFICATION_PERSISTENT_SERVICE_SERVER_INCLUDE_NS_NPP_COPY_WORKER_H_