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 contains the implementation for archiving and extracting files and folders.
27 ////////////////////////////////////////////////////////////////////////////////////////////////////
29 #include <other_service/strlcpy.h>
39 #include <sys/types.h>
41 #include <libtar_listhash.h>
46 #include "ns_npp_notificationpersistentservicelog.h"
47 #include "ns_npp_fs_directory.h"
48 #include "ns_npp_copy_worker.h"
51 tartype_t g_gztype = { (openfunc_t) OpenArchive, (closefunc_t) CloseArchive,
52 (readfunc_t) ReadArchive, (writefunc_t) WriteArchive
58 // Buffer size for feeding data to and pulling data from the zlib routines
59 const size_t kChunkSize = 1U << 18;
61 const size_t kBufSize = 1U << 15;
63 ////////////////////////////////////////////////////////////////////////////////////////////////////
65 /// Constructor of CArchive class
66 ////////////////////////////////////////////////////////////////////////////////////////////////////
67 CCopyWorker::CArchive::CArchive() {
70 ////////////////////////////////////////////////////////////////////////////////////////////////////
72 /// Destructor of CArchive class
73 ////////////////////////////////////////////////////////////////////////////////////////////////////
74 CCopyWorker::CArchive::~CArchive() {
77 ////////////////////////////////////////////////////////////////////////////////////////////
79 /// Method to perform archive or extract operations
80 ////////////////////////////////////////////////////////////////////////////////////////////
81 EFrameworkunifiedStatus CCopyWorker::CArchive::Archive(NSP_CopyInfoCmd &f_tarchiveinfocmd,
82 NSP_CopyStatusResponse &f_tarchivestatusresponse) {
83 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
84 EFrameworkunifiedStatus l_estatus = eFrameworkunifiedStatusOK;
85 std::string l_pSourcePath = f_tarchiveinfocmd.m_csourcepath;
86 std::string l_pDestPath = f_tarchiveinfocmd.m_cdestinationpath;
88 if (!l_pSourcePath.empty() || !l_pDestPath.empty()) { // LCOV_EXCL_BR_LINE 6: double check, l_pSourcePath and l_pDestPath can't be empty // NOLINT[whitespace/line_length]
89 size_t l_ifound = l_pDestPath.rfind("/");
90 // get string from start to last occurrence of '/'. Which will return directory path
91 std::string l_cOutDirPath = l_pDestPath.substr(0, l_ifound);
92 // check for load file or release file
93 if (LOADTYPE_RELEASE == f_tarchiveinfocmd.m_eloadtype) {
94 if (CFSDirectory::DoesDirecotryExist(l_pSourcePath)) {
95 // check if destination directory exist
96 if (!CFSDirectory::DoesDirecotryExist(l_cOutDirPath)) {
97 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __PRETTY_FUNCTION__, "%s DOESN'T exist, Creating...",
98 l_cOutDirPath.c_str());
99 l_estatus = CFSDirectory::CreateDirectory(l_cOutDirPath);
102 if (eFrameworkunifiedStatusOK == (l_estatus = CreateTar(l_pSourcePath, l_pDestPath, // LCOV_EXCL_BR_LINE 6: CreateTar always return eFrameworkunifiedStatusOK // NOLINT[whitespace/line_length]
103 f_tarchivestatusresponse))) {
104 // sync tar file to persistent memory
105 SI_32 l_iTarHandle = LIBTARFAIL;
106 if (LIBTARFAIL != (l_iTarHandle = open(l_pDestPath.c_str() , O_RDONLY))) {
107 struct stat l_objStat;
108 if (-1 != fstat(l_iTarHandle , &l_objStat)) { // LCOV_EXCL_BR_LINE 6: fstat always return ok
109 FRAMEWORKUNIFIEDLOG(ZONE_PRD_INFO3, __FUNCTION__, "Bytes written in NAND memory, %ld, file=%s", l_objStat.st_size,
110 l_pDestPath.c_str());
115 l_estatus = eFrameworkunifiedStatusFail;
118 // LCOV_EXCL_START 6: CreateTar always return eFrameworkunifiedStatusOK
119 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
120 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Create tar Failed.");
121 l_estatus = eFrameworkunifiedStatusFail;
125 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __PRETTY_FUNCTION__, "CP_WRK_FAILURE_SRC_NOT_FND for %s ",
126 l_pSourcePath.c_str());
127 l_estatus = eFrameworkunifiedStatusFail;
130 // check if destination directory exist
131 if (!CFSDirectory::DoesDirecotryExist(l_pDestPath)) {
132 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __PRETTY_FUNCTION__, "%s DOESN'T exist, Creating...",
133 l_pDestPath.c_str());
134 l_estatus = CFSDirectory::CreateDirectory(l_pDestPath);
136 if (eFrameworkunifiedStatusOK == l_estatus) { // LCOV_EXCL_BR_LINE 6: l_estatus will be always eFrameworkunifiedStatusOK
138 l_estatus = ExtractTar(l_pSourcePath, l_pDestPath, f_tarchivestatusresponse);
142 // LCOV_EXCL_START 6: double check, l_pSourcePath and l_pDestPath can't be empty
143 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
144 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Source path or Dest Path empty.");
145 l_estatus = eFrameworkunifiedStatusFail;
148 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
153 ////////////////////////////////////////////////////////////////////////////////////////////
155 /// Method to create tar
156 ////////////////////////////////////////////////////////////////////////////////////////////
157 EFrameworkunifiedStatus CCopyWorker::CArchive::CreateTar(const std::string &f_csourepath,
158 const std::string &f_cdestpath,
159 NSP_CopyStatusResponse &f_tarchivestatusresponse) {
160 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
161 EFrameworkunifiedStatus l_estatus = eFrameworkunifiedStatusOK;
162 std::string l_cDestPath = f_cdestpath;
163 std::string l_pcRootDirPath = f_csourepath;
165 libtar_list_t *l_pLibTarList = NULL;
166 l_pLibTarList = libtar_list_new(LIST_QUEUE, NULL);
167 libtar_list_add(l_pLibTarList, (void *)l_pcRootDirPath.c_str()); // NOLINT (readability/casting)
169 if (!l_cDestPath.empty() || !l_pcRootDirPath.empty()) { // LCOV_EXCL_BR_LINE 6: double check, l_cDestPath and l_pcRootDirPath cannot be empty // NOLINT[whitespace/line_length]
170 if (eFrameworkunifiedStatusOK != Create(l_cDestPath, l_pcRootDirPath, l_pLibTarList)) { // LCOV_EXCL_BR_LINE 6: Create always return eFrameworkunifiedStatusOK // NOLINT[whitespace/line_length]
171 // LCOV_EXCL_START 6: Create always return eFrameworkunifiedStatusOK
172 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
173 // not copied properly so remove temporary file
174 if (TRUE == CFSDirectory::RemoveDirectory(l_cDestPath)) {
175 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "Delete persistent directory successful.");
177 FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__, "Delete persistent directory unsuccessful.");
179 l_estatus = eFrameworkunifiedStatusFail;
180 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "Archiving fails for %s to %s", l_pcRootDirPath.c_str(),
181 l_cDestPath.c_str());
184 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __PRETTY_FUNCTION__, "Folder archived %s ", l_cDestPath.c_str());
185 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "Source Path %s", l_pcRootDirPath.c_str());
187 if (TRUE == CFSDirectory::RemoveDirectory(l_pcRootDirPath)) { // LCOV_EXCL_BR_LINE 6: always return true
188 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "Delete temporary directory successful.");
190 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
191 FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__, "Delete temporary directory unsuccessful."); // LCOV_EXCL_LINE 6: always return true // NOLINT[whitespace/line_length]
193 l_estatus = eFrameworkunifiedStatusOK;
196 // LCOV_EXCL_START 6: double check, l_cDestPath and l_pcRootDirPath cannot be empty
197 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
198 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Source path or Dest Path empty.");
199 l_estatus = eFrameworkunifiedStatusFail;
202 libtar_list_free(l_pLibTarList, NULL);
203 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
207 ////////////////////////////////////////////////////////////////////////////////////////////
209 /// Method to extract tar
210 ////////////////////////////////////////////////////////////////////////////////////////////
211 EFrameworkunifiedStatus CCopyWorker::CArchive::ExtractTar(const std::string &f_csourepath,
212 const std::string &f_cdestpath,
213 NSP_CopyStatusResponse &f_tarchivestatusresponse) {
214 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
215 EFrameworkunifiedStatus l_estatus = eFrameworkunifiedStatusOK;
216 std::string l_pcTarFileDestPath = f_cdestpath;
217 std::string l_pcRootDirPath = f_csourepath;
219 if (!l_pcTarFileDestPath.empty() || !l_pcRootDirPath.empty()) { // LCOV_EXCL_BR_LINE 6: double check, l_pcTarFileDestPath and l_pcRootDirPath can't be empty // NOLINT[whitespace/line_length]
220 if (eFrameworkunifiedStatusOK != Extract(l_pcRootDirPath, l_pcTarFileDestPath)) {
221 // not copied properly so remove temporary file
222 if (TRUE == CFSDirectory::RemoveDirectory(l_pcTarFileDestPath)) { // LCOV_EXCL_BR_LINE 6: RemoveDirectory always return true // NOLINT[whitespace/line_length]
223 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "Delete retrieved directory successful.");
225 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
226 FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__, "Delete retrieved directory unsuccessful."); // LCOV_EXCL_LINE 6: RemoveDirectory always return true // NOLINT[whitespace/line_length]
229 l_estatus = eFrameworkunifiedStatusFail;
230 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __PRETTY_FUNCTION__, "ABORT WAS CALLED!");
231 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "Extraction fails for %s to %s",
232 l_pcTarFileDestPath.c_str(), l_pcTarFileDestPath.c_str());
234 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __PRETTY_FUNCTION__, "Folder extracted %s ", l_pcTarFileDestPath.c_str());
235 l_estatus = eFrameworkunifiedStatusOK;
238 // LCOV_EXCL_START 6: double check, l_pcTarFileDestPath and l_pcRootDirPath can't be empty
239 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
240 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Source path or Dest Path empty.");
241 l_estatus = eFrameworkunifiedStatusFail;
244 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
248 ////////////////////////////////////////////////////////////////////////////////////////////
250 /// Method to create tar
251 ////////////////////////////////////////////////////////////////////////////////////////////
252 EFrameworkunifiedStatus CCopyWorker::CArchive::Create(std::string &f_pctarfiledestpath,
253 std::string &f_pcrootdirpath,
254 libtar_list_t *f_plibtarlist) {
255 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
256 EFrameworkunifiedStatus l_estatus = eFrameworkunifiedStatusOK;
258 if (FILEERROR == access(f_pcrootdirpath.c_str(), R_OK)) { // LCOV_EXCL_BR_LINE 6: access always return ok
259 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
260 return eFrameworkunifiedStatusFail; // LCOV_EXCL_LINE 6: access always return ok
263 TAR *l_pTarInfo = NULL;
264 CHAR l_cBuffer[MAX_PATH_LENGTH];
265 libtar_listptr_t l_pTarListPtr;
267 PCHAR l_pcTarFileDestPath = const_cast<PCHAR >(f_pctarfiledestpath.c_str());
268 PCHAR l_pcRootDirPath = const_cast<PCHAR >(f_pcrootdirpath.c_str());
272 if (l_pcTarFileDestPath || l_pcRootDirPath) { // LCOV_EXCL_BR_LINE 6: double check, l_pcTarFileDestPath and l_pcRootDirPath can't be NULL // NOLINT[whitespace/line_length]
274 if (LIBTARFAIL == (tar_open(&l_pTarInfo, l_pcTarFileDestPath, &g_gztype, O_WRONLY | O_CREAT, // LCOV_EXCL_BR_LINE 6: tar_open always return ok // NOLINT[whitespace/line_length]
275 TARMODE, TAR_GNU))) {
276 // LCOV_EXCL_START 6: tar_open always return ok
277 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
278 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __PRETTY_FUNCTION__, "tar_open(): %s", std::strerror(errno));
279 l_estatus = eFrameworkunifiedStatusFail;
283 libtar_listptr_reset(&l_pTarListPtr);
285 while (libtar_list_next(f_plibtarlist, &l_pTarListPtr) != LIBTARSUCCESS) {
286 if (l_pcRootDirPath[0] != '/') { // LCOV_EXCL_BR_LINE 6: l_pcRootDirPath must begin with '/'
287 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
288 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __PRETTY_FUNCTION__, "%s", l_pcRootDirPath); // LCOV_EXCL_LINE 6: l_pcRootDirPath must begin with '/' // NOLINT[whitespace/line_length]
290 #ifdef AGL_PosixBasedOS001LEGACY_USED
291 strlcpy(l_cBuffer, l_pcRootDirPath, sizeof(l_cBuffer));
295 if (tar_append_tree(l_pTarInfo, l_cBuffer, l_temp) != LIBTARSUCCESS) { // LCOV_EXCL_BR_LINE 6: tar_append_tree always return ok // NOLINT[whitespace/line_length]
296 // LCOV_EXCL_START 6: tar_append_tree always return ok
297 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
298 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __PRETTY_FUNCTION__,
299 "tar_append_tree(\"%s\", \"%s\"): %s", l_cBuffer,
300 l_temp, std::strerror(errno));
301 tar_close(l_pTarInfo);
302 return eFrameworkunifiedStatusFail;
306 if (tar_append_eof(l_pTarInfo) != LIBTARSUCCESS) { // LCOV_EXCL_BR_LINE 6: tar_append_eof always return ok
307 // LCOV_EXCL_START 6: tar_append_eof always return ok
308 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
309 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __PRETTY_FUNCTION__, "tar_append_eof(): %s", std::strerror(errno));
310 tar_close(l_pTarInfo);
311 return eFrameworkunifiedStatusFail;
315 if (tar_close(l_pTarInfo) != LIBTARSUCCESS) { // LCOV_EXCL_BR_LINE 6: tar_close always return ok
316 // LCOV_EXCL_START 6: tar_close always return ok
317 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
318 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __PRETTY_FUNCTION__, "tar_close(): %s", std::strerror(errno));
319 l_estatus = eFrameworkunifiedStatusFail;
323 // LCOV_EXCL_START 6: double check, l_pcTarFileDestPath and l_pcRootDirPath can't be NULL
324 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
325 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Source path or Dest Path empty.");
326 l_estatus = eFrameworkunifiedStatusFail;
329 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
333 ////////////////////////////////////////////////////////////////////////////////////////////
335 /// Method to extract tar
336 ////////////////////////////////////////////////////////////////////////////////////////////
337 EFrameworkunifiedStatus CCopyWorker::CArchive::Extract(std::string &f_pcrootdirpath, std::string &f_pctarfiledestpath) {
338 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
339 EFrameworkunifiedStatus l_estatus = eFrameworkunifiedStatusOK;
341 if (FILEERROR == access(f_pcrootdirpath.c_str(), R_OK)) { // LCOV_EXCL_BR_LINE 6: access always return ok
342 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
343 return eFrameworkunifiedStatusFail; // LCOV_EXCL_LINE 6: access always return ok
346 PCHAR l_pcRootDirPath = const_cast<PCHAR >(f_pcrootdirpath.c_str());
347 PCHAR l_pcTarFileDestPath = const_cast<PCHAR >(f_pctarfiledestpath.c_str());
349 TAR *l_pTarInfo = NULL;
351 if (l_pcRootDirPath || l_pcTarFileDestPath) { // LCOV_EXCL_BR_LINE 6: double check, l_pcRootDirPath and l_pcTarFileDestPath can't be NULL // NOLINT[whitespace/line_length]
352 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "Extraction open for %s to ", l_pcRootDirPath);
354 if (LIBTARFAIL == (tar_open(&l_pTarInfo, l_pcRootDirPath, &g_gztype, O_RDONLY, LIBTARSUCCESS, TAR_GNU))) { // LCOV_EXCL_BR_LINE 6: tar_open always return ok // NOLINT[whitespace/line_length]
355 // LCOV_EXCL_START 6: tar_open always return ok
356 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
357 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __PRETTY_FUNCTION__, "tar_open(): %s", std::strerror(errno));
358 l_estatus = eFrameworkunifiedStatusFail;
362 if (LIBTARSUCCESS != tar_extract_all(l_pTarInfo, l_pcTarFileDestPath)) {
363 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __PRETTY_FUNCTION__, "tar_extract_all(): %s", std::strerror(errno));
364 l_estatus = eFrameworkunifiedStatusFail;
367 if (LIBTARSUCCESS != tar_close(l_pTarInfo)) { // LCOV_EXCL_BR_LINE 6: tar_close always return ok
368 // LCOV_EXCL_START 6: tar_close always return ok
369 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
370 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __PRETTY_FUNCTION__, "tar_close(): %s", std::strerror(errno));
371 l_estatus = eFrameworkunifiedStatusFail;
375 // LCOV_EXCL_START 6: double check, l_pcRootDirPath and l_pcTarFileDestPath can't be NULL
376 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
377 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Source path or Dest Path empty.");
378 l_estatus = eFrameworkunifiedStatusFail;
381 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
385 SI_32 OpenArchive(PCHAR f_pcpathname, SI_32 f_sioflags, SI_32 f_simode) {
386 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
387 gzFile l_gzfile = Z_NULL;
388 SI_32 l_sifiledescriptor = LIBTARFAIL;
389 if (f_pcpathname) { // LCOV_EXCL_BR_LINE 6: double check, f_pcpathname can't be NULL
392 switch (f_sioflags & O_ACCMODE) {
394 l_pcgzoflags = (PCHAR)"wb";
398 l_pcgzoflags = (PCHAR)"rb";
403 // LCOV_EXCL_START 6: must be O_WRONLY or O_RDONLY
404 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
411 l_sifiledescriptor = open(f_pcpathname, f_sioflags, f_simode);
413 if (LIBTARFAIL == l_sifiledescriptor) { // LCOV_EXCL_BR_LINE 6: l_sifiledescriptor always be ok
414 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
415 return LIBTARFAIL; // LCOV_EXCL_LINE 6: l_sifiledescriptor always be ok
418 if (((f_sioflags & O_CREAT) && fchmod(l_sifiledescriptor, f_simode))) { // LCOV_EXCL_BR_LINE 6: always be ok
419 // LCOV_EXCL_START 6: always be ok
420 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
421 close(l_sifiledescriptor);
426 l_gzfile = gzdopen(l_sifiledescriptor, l_pcgzoflags);
427 if (!l_gzfile) { // LCOV_EXCL_BR_LINE 6: l_gzfile always be not null
428 // LCOV_EXCL_START 6: l_gzfile always be not null
429 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
430 close(l_sifiledescriptor);
436 g_gzfiles.insert(std::make_pair(l_sifiledescriptor, l_gzfile));
439 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
440 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Source Path empty."); // LCOV_EXCL_LINE 6: double check, f_pcpathname can't be NULL // NOLINT[whitespace/line_length]
442 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
443 return l_sifiledescriptor;
446 int CloseArchive(int fd) {
448 gzFile gzfile = Z_NULL;
451 if (g_gzfiles.empty()) { // LCOV_EXCL_BR_LINE 6: g_gzfiles always be not empty
452 // LCOV_EXCL_START 6: g_gzfiles always be not empty
453 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
459 GZFiles::iterator gzfiles_it = g_gzfiles.find(fd);
460 if (gzfiles_it == g_gzfiles.end()) { // LCOV_EXCL_BR_LINE 6: fd will always be found
461 // LCOV_EXCL_START 6: fd will always be found
462 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
469 gzfile = gzfiles_it->second;
470 ret = gzclose(gzfile);
472 /* remove gzFile* association from our list */
473 g_gzfiles.erase(gzfiles_it);
478 ssize_t ReadArchive(int fd, void *buf, size_t count) {
479 gzFile gzfile = Z_NULL;
481 if (g_gzfiles.empty()) { // LCOV_EXCL_BR_LINE 6: g_gzfiles always be not empty
482 // LCOV_EXCL_START 6: g_gzfiles always be not empty
483 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
489 GZFiles::iterator gzfiles_it = g_gzfiles.find(fd);
490 if (gzfiles_it == g_gzfiles.end()) { // LCOV_EXCL_BR_LINE 6: fd will always be found
491 // LCOV_EXCL_START 6: fd will always be found
492 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
498 gzfile = gzfiles_it->second;
499 return gzread(gzfile, buf, static_cast<UI_32>(count));
502 ssize_t WriteArchive(int fd, const void *buf, size_t count) {
503 gzFile gzfile = Z_NULL;
505 if (g_gzfiles.empty()) { // LCOV_EXCL_BR_LINE 6: g_gzfiles always be not empty
506 // LCOV_EXCL_START 6: g_gzfiles always be not empty
507 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
513 GZFiles::iterator gzfiles_it = g_gzfiles.find(fd);
514 if (gzfiles_it == g_gzfiles.end()) { // LCOV_EXCL_BR_LINE 6: fd will always be found
515 // LCOV_EXCL_START 6: fd will always be found
516 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
522 gzfile = gzfiles_it->second;
523 return gzwrite(gzfile, buf, static_cast<UI_32>(count));
526 // LCOV_EXCL_START 6: unused code
527 ////////////////////////////////////////////////////////////////////////////////////////////
528 /// FileOperationUsingLibz
529 /// Method to determine whether to compress/decompress file using libz
530 ////////////////////////////////////////////////////////////////////////////////////////////
531 EFrameworkunifiedStatus CCopyWorker::CArchive::FileOperationUsingLibz(NSP_CopyInfoCmd &f_toperainfocmd,
532 NSP_CopyStatusResponse &f_toperarespstatus) {
533 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
534 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
535 EFrameworkunifiedStatus l_estatus = eFrameworkunifiedStatusOK;
536 std::string l_pSourcePath = f_toperainfocmd.m_csourcepath;
537 std::string l_pDestPath = f_toperainfocmd.m_cdestinationpath;
539 if (!l_pSourcePath.empty() || !l_pDestPath.empty()) {
540 size_t l_ifound = l_pDestPath.rfind("/");
541 // get string from start to last occurrence of '/'. Which will return directory path
542 std::string l_cOutDirPath = l_pDestPath.substr(0, l_ifound);
544 // check if destination directory exist
545 if (!CFSDirectory::DoesDirecotryExist(l_cOutDirPath)) {
546 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __PRETTY_FUNCTION__, "%s DOESN'T exist, Creating...",
547 l_cOutDirPath.c_str());
548 l_estatus = CFSDirectory::CreateDirectory(l_cOutDirPath);
551 // check for load file or release file
552 if (LOADTYPE_RELEASE == f_toperainfocmd.m_eloadtype) {
554 if (eFrameworkunifiedStatusOK == (l_estatus = CompressUsingZlib(l_pSourcePath, l_pDestPath, Z_BEST_COMPRESSION))) {
555 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __PRETTY_FUNCTION__, "File Compression successful.");
557 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "File Compression failed.");
558 f_toperarespstatus.m_bpersistencechk = FALSE;
559 l_estatus = eFrameworkunifiedStatusFail;
561 } else if (LOADTYPE_LOAD == f_toperainfocmd.m_eloadtype) {
563 if (eFrameworkunifiedStatusOK == (l_estatus = DeCompressUsingZlib(l_pSourcePath, l_pDestPath))) {
564 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __PRETTY_FUNCTION__, "File DeCompression successful.");
566 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "File DeCompression failed.");
567 f_toperarespstatus.m_bpersistencechk = FALSE;
568 l_estatus = eFrameworkunifiedStatusFail;
571 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __PRETTY_FUNCTION__, "Wrong Request or No request for compression or decompression!");
572 l_estatus = eFrameworkunifiedStatusFail;
575 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Source Path or Destination Path Empty.");
576 l_estatus = eFrameworkunifiedStatusFail;
578 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
583 // LCOV_EXCL_START 6: unused code
584 ////////////////////////////////////////////////////////////////////////////////////////////
585 /// CompressUsingZlib
586 /// Method to compress file using libz
587 ////////////////////////////////////////////////////////////////////////////////////////////
588 EFrameworkunifiedStatus CCopyWorker::CArchive::CompressUsingZlib(const std::string &f_csourepath,
589 const std::string &f_cdestpath,
591 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
592 EFrameworkunifiedStatus l_estatus = eFrameworkunifiedStatusOK;
594 if (!f_csourepath.empty() || !f_cdestpath.empty()) {
595 std::string l_cSourePath = f_csourepath;
596 std::string l_cDestPath = f_cdestpath;
598 std::string l_TempFile = "";
599 l_TempFile.append(l_cDestPath.c_str());
600 l_TempFile.append(".nps_tmp");
602 FILE *l_pSrcFilePtr = fopen(l_cSourePath.c_str(), "rb");
604 if (NULL != l_pSrcFilePtr) {
605 FILE *l_pDestFilePtr = fopen(l_TempFile.c_str(), "wb");
606 if (NULL != l_pDestFilePtr) {
607 setvbuf(l_pSrcFilePtr, NULL, _IOFBF, kBufSize);
608 setvbuf(l_pDestFilePtr, NULL, _IOFBF, kBufSize);
610 // structure used to pass information to and from the zlib routines
611 z_stream l_tZstreamInfo = {};
613 // Allocate deflate state
614 l_tZstreamInfo.zalloc = Z_NULL;
615 l_tZstreamInfo.zfree = Z_NULL;
616 l_tZstreamInfo.opaque = Z_NULL;
618 // Initialize deflates
619 SI_32 l_iDeflateStatus = deflateInit(&l_tZstreamInfo, f_iziplevel);
621 // Deflate Flush Status to determine end of input file
622 SI_32 l_iDeflateFlushState = Z_FINISH;
624 std::vector< UI_8 > l_vInBuffer(kChunkSize);
625 std::vector< UI_8 > l_vOutBuffer(kChunkSize);
627 // pointer to the read bytes from the input file
628 UI_8 *l_pReadBuffer = &l_vInBuffer[0];
629 // pointer to the next available byte space to write to output file
630 UI_8 *l_pWriteBuffer = &l_vOutBuffer[0];
632 size_t l_uiDeflateRetData = 0;
634 if (Z_OK == l_iDeflateStatus) {
636 // bytes read from input file
637 l_tZstreamInfo.avail_in = static_cast<uInt>(fread(l_pReadBuffer, 1, kChunkSize, l_pSrcFilePtr));
639 if (0 == ferror(l_pSrcFilePtr)) {
640 // if the eof input file reached set the flush state to Z_FINISH
641 // Z_NO_FLUSH to indicate that we still have uncompressed data
642 l_iDeflateFlushState = feof(l_pSrcFilePtr) ? Z_FINISH : Z_NO_FLUSH;
643 l_tZstreamInfo.next_in = l_pReadBuffer;
645 // run deflate() on input until output buffer not full, finish
646 // compression if all of source has been read in
648 l_tZstreamInfo.avail_out = kChunkSize;
649 l_tZstreamInfo.next_out = l_pWriteBuffer;
650 // no bad return value
651 l_iDeflateStatus = deflate(&l_tZstreamInfo, l_iDeflateFlushState);
652 // state not clobbered
653 if (Z_STREAM_ERROR == l_iDeflateStatus) {
654 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "zlib stream error");
656 l_uiDeflateRetData = kChunkSize - l_tZstreamInfo.avail_out;
657 if ((fwrite(l_pWriteBuffer, 1, l_uiDeflateRetData, l_pDestFilePtr) != l_uiDeflateRetData) ||
658 // all data not written properly
659 (0 != ferror(l_pDestFilePtr))) { // file error in writing
660 deflateEnd(&l_tZstreamInfo);
661 l_estatus = eFrameworkunifiedStatusFail;
662 l_tZstreamInfo.avail_out = 0;
663 l_iDeflateFlushState = Z_FINISH;
666 } while (0 == l_tZstreamInfo.avail_out); //<< check if deflate() has no more output
667 } else { // by seeing that it did not fill the output buffer
668 deflateEnd(&l_tZstreamInfo); // leaving avail_out greater than zero >>//
669 l_estatus = eFrameworkunifiedStatusFail;
672 // all input will be used
673 if (0 != l_tZstreamInfo.avail_in) {
674 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "All inputs in z-stream avail_in not used");
675 l_estatus = eFrameworkunifiedStatusFail;
677 // done when last data in file processed
678 } while (Z_FINISH != l_iDeflateFlushState);
679 // stream will be complete
680 if (Z_STREAM_END != l_iDeflateStatus) {
681 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "z-stream not completed");
682 l_estatus = eFrameworkunifiedStatusFail;
685 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "Compressed %lu bytes down to %lu bytes",
686 l_tZstreamInfo.total_in, l_tZstreamInfo.total_out);
688 FRAMEWORKUNIFIEDLOG(ZONE_PRD_INFO3, __FUNCTION__, "Bytes written in NAND memory, %lu, file=%s", l_tZstreamInfo.total_out,
689 f_cdestpath.c_str());
691 // clean up and return
692 deflateEnd(&l_tZstreamInfo);
694 l_estatus = eFrameworkunifiedStatusFail;
695 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __PRETTY_FUNCTION__, "deflateInit Failed");
698 fclose(l_pDestFilePtr);
700 if (0 != rename(l_TempFile.c_str(), l_cDestPath.c_str())) {
701 l_estatus = eFrameworkunifiedStatusFail;
702 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __PRETTY_FUNCTION__, "Error renaming file %s to %s",
703 l_TempFile.c_str(), l_cDestPath.c_str());
706 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __PRETTY_FUNCTION__, "Destination File Opening Error");
707 l_estatus = eFrameworkunifiedStatusFail;
709 fclose(l_pSrcFilePtr);
711 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __PRETTY_FUNCTION__, "Source File Opening Error");
712 l_estatus = eFrameworkunifiedStatusFail;
715 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Source Path or Destination Path Empty.");
716 l_estatus = eFrameworkunifiedStatusFail;
719 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
724 // LCOV_EXCL_START 6: unused code
725 ////////////////////////////////////////////////////////////////////////////////////////////
726 /// DeCompressUsingZlib
727 /// Method to decompress file using libz
728 ////////////////////////////////////////////////////////////////////////////////////////////
729 EFrameworkunifiedStatus CCopyWorker::CArchive::DeCompressUsingZlib(const std::string &f_csourepath,
730 const std::string &f_cdestpath) {
731 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
732 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
733 EFrameworkunifiedStatus l_estatus = eFrameworkunifiedStatusOK;
735 if (!f_csourepath.empty() || !f_cdestpath.empty()) {
736 std::string l_cSourePath = f_csourepath;
737 std::string l_cDestPath = f_cdestpath;
739 std::string l_TempFile = "";
740 l_TempFile.append(l_cDestPath.c_str());
741 l_TempFile.append(".nps_tmp");
743 FILE *l_pSrcFilePtr = fopen(l_cSourePath.c_str(), "rb");
745 if (NULL != l_pSrcFilePtr) {
746 FILE *l_pDestFilePtr = fopen(l_TempFile.c_str(), "wb");
747 if (NULL != l_pDestFilePtr) {
748 setvbuf(l_pSrcFilePtr, NULL, _IOFBF, kBufSize);
749 setvbuf(l_pDestFilePtr, NULL, _IOFBF, kBufSize);
751 z_stream l_tZstreamInfo = {};
753 // allocate inflate state
754 l_tZstreamInfo.zalloc = Z_NULL;
755 l_tZstreamInfo.zfree = Z_NULL;
756 l_tZstreamInfo.opaque = Z_NULL;
757 l_tZstreamInfo.avail_in = 0;
758 l_tZstreamInfo.next_in = Z_NULL;
760 SI_32 l_iInflateStatus = inflateInit(&l_tZstreamInfo);
762 if (Z_OK == l_iInflateStatus) {
763 std::vector< UI_8 > l_vInBuffer(kChunkSize);
764 std::vector< UI_8 > l_vOutBuffer(kChunkSize);
766 // pointer to the read bytes from the input file
767 UI_8 *l_pReadBuffer = &l_vInBuffer[0];
768 // pointer to the next available byte space to write to output file
769 UI_8 *l_pWriteBuffer = &l_vOutBuffer[0];
771 size_t l_uiDeflateRetData = 0;
774 l_tZstreamInfo.avail_in = static_cast<uInt>(fread(l_pReadBuffer, 1, kChunkSize, l_pSrcFilePtr));
775 if (0 == ferror(l_pSrcFilePtr)) {
776 if (0 == l_tZstreamInfo.avail_in) {
779 l_tZstreamInfo.next_in = l_pReadBuffer;
780 // run inflate() on input until output buffer not full
782 l_tZstreamInfo.avail_out = kChunkSize;
783 l_tZstreamInfo.next_out = l_pWriteBuffer;
784 l_iInflateStatus = inflate(&l_tZstreamInfo, Z_NO_FLUSH);
787 if (Z_STREAM_ERROR == l_iInflateStatus) {
788 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "zlib stream error");
789 l_estatus = eFrameworkunifiedStatusFail;
790 } else if (Z_NEED_DICT == l_iInflateStatus) {
791 l_iInflateStatus = Z_DATA_ERROR;
792 l_estatus = eFrameworkunifiedStatusFail;
793 } else if (Z_MEM_ERROR == l_iInflateStatus) {
794 inflateEnd(&l_tZstreamInfo);
795 l_estatus = eFrameworkunifiedStatusFail;
796 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Out of Memory");
798 l_uiDeflateRetData = kChunkSize - l_tZstreamInfo.avail_out;
799 // all data not written
800 if ((fwrite(l_pWriteBuffer, 1, l_uiDeflateRetData, l_pDestFilePtr) != l_uiDeflateRetData) ||
801 (0 != ferror(l_pDestFilePtr))) { // or file writing error
802 inflateEnd(&l_tZstreamInfo);
803 l_estatus = eFrameworkunifiedStatusFail;
806 } while (0 == l_tZstreamInfo.avail_out);
808 inflateEnd(&l_tZstreamInfo);
809 l_estatus = eFrameworkunifiedStatusFail;
812 // done when inflate() says it's done
813 } while (Z_STREAM_END != l_iInflateStatus);
815 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, " De-compressed %lu bytes to %lu bytes",
816 l_tZstreamInfo.total_in, l_tZstreamInfo.total_out);
817 // clean up and return
818 inflateEnd(&l_tZstreamInfo);
820 l_estatus = eFrameworkunifiedStatusFail;
823 fclose(l_pDestFilePtr);
825 if (0 != rename(l_TempFile.c_str(), l_cDestPath.c_str())) {
826 l_estatus = eFrameworkunifiedStatusFail;
827 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __PRETTY_FUNCTION__, "Error renaming file %s to %s",
828 l_TempFile.c_str(), l_cDestPath.c_str());
831 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __PRETTY_FUNCTION__, "Destination File Opening Error");
832 l_estatus = eFrameworkunifiedStatusFail;
834 fclose(l_pSrcFilePtr);
836 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __PRETTY_FUNCTION__, "Source File Opening Error");
837 l_estatus = eFrameworkunifiedStatusFail;
840 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Source Path or Destination Path Empty.");
841 l_estatus = eFrameworkunifiedStatusFail;
843 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");