From: Pawel Andruszkiewicz Date: Fri, 27 Nov 2015 07:31:44 +0000 (+0100) Subject: [Archive] Log refactoring. X-Git-Tag: submit/tizen_mobile/20151215.080542^2~68^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=317031644dce33a95a1218d81be8f90dd22da88c;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [Archive] Log refactoring. [Verification] TCT pass rate (r20): 100% (106/106/0/0/0) Change-Id: Ie1cdd9bc3cb523d30744f268a473cb36b6547f5c Signed-off-by: Pawel Andruszkiewicz --- diff --git a/src/archive/archive_callback_data.cc b/src/archive/archive_callback_data.cc index 81bb8211..b4d296a7 100755 --- a/src/archive/archive_callback_data.cc +++ b/src/archive/archive_callback_data.cc @@ -178,13 +178,11 @@ PlatformResult OpenCallbackData::executeOperation(ArchiveFilePtr archive_file_pt filesystem::FilePtr file = archive_file_ptr->getFile(); if (!file) { - LoggerE("File is null"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "File is null"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "File is null"); } filesystem::NodePtr node = file->getNode(); if(!node) { - LoggerE("Node is null"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Node is null"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Node is null"); } const FileMode fm = archive_file_ptr->m_file_mode; unsigned long long size = 0; @@ -211,8 +209,7 @@ PlatformResult OpenCallbackData::executeOperation(ArchiveFilePtr archive_file_pt archive_file_ptr->setIsOpen(true); } else { - LoggerE("The file is empty throwing: InvalidValuesException - Invalid ZIP archive"); - return PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Invalid ZIP archive"); + return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, "Invalid ZIP archive", ("The file is empty")); } } else { @@ -226,8 +223,7 @@ PlatformResult OpenCallbackData::executeOperation(ArchiveFilePtr archive_file_pt guint id = g_idle_add(ArchiveFile::openTaskCompleteCB, this); if (!id) { - LoggerE("g_idle_add fails"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "g_idle_add fails"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "g_idle_add fails"); } return PlatformResult(ErrorCode::NO_ERROR); } @@ -267,8 +263,7 @@ PlatformResult GetEntriesCallbackData::executeOperation(ArchiveFilePtr archive_f guint id = g_idle_add(ArchiveFile::getEntriesTaskCompleteCB, this); if (!id) { - LoggerE("g_idle_add fails"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "g_idle_add fails"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "g_idle_add fails"); } return PlatformResult(ErrorCode::NO_ERROR); } @@ -329,17 +324,15 @@ PlatformResult GetEntryByNameCallbackData::executeOperation(ArchiveFilePtr archi } if (it == entries->end()) { - LoggerE("GetEntryByName Entry with name: [%s] not found", getName().c_str()); - LoggerE("Throwing NotFoundException - Entry not found"); - return PlatformResult(ErrorCode::NOT_FOUND_ERR, "Entry not found"); + SLoggerE("GetEntryByName Entry with name: [%s] not found", getName().c_str()); + return LogAndCreateResult(ErrorCode::NOT_FOUND_ERR, "Entry not found"); } setFileEntry(it->second); guint id = g_idle_add(ArchiveFile::getEntryByNameTaskCompleteCB, this); if (!id) { - LoggerE("g_idle_add fails"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "g_idle_add fails"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "g_idle_add fails"); } return PlatformResult(ErrorCode::NO_ERROR); } @@ -564,13 +557,11 @@ PlatformResult AddProgressCallback::executeOperation(ArchiveFilePtr archive_file LoggerD("Entered"); if(!m_file_entry) { - LoggerE("ArchiveFileEntry is not set in callback"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Could not add file to archive"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Could not add file to archive", ("ArchiveFileEntry is not set in callback")); } if(!archive_file_ptr) { - LoggerE("archive_file_ptr is NULL"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Could not extract archive file entry"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Could not extract archive file entry", ("archive_file_ptr is NULL")); } AddProgressCallback* callback = this; @@ -806,13 +797,11 @@ PlatformResult ExtractEntryProgressCallback::executeOperation(ArchiveFilePtr arc LoggerD("Entered"); if(!m_archive_file_entry) { - LoggerE("ArchiveFileEntry is not set in callback"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Could not extract archive file entry"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Could not extract archive file entry", ("ArchiveFileEntry is not set in callback")); } if(!archive_file_ptr) { - LoggerE("archive_file_ptr is NULL"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Could not extract archive file entry"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Could not extract archive file entry", ("archive_file_ptr is NULL")); } UnZipPtr unzip; diff --git a/src/archive/archive_file.cc b/src/archive/archive_file.cc index 990d6c58..ef341fe7 100755 --- a/src/archive/archive_file.cc +++ b/src/archive/archive_file.cc @@ -253,8 +253,7 @@ PlatformResult ArchiveFile::addOperation(OperationCallbackData* callback) if(1 == size) { ArchiveFileHolder* holder = new(std::nothrow) ArchiveFileHolder(); if(!holder) { - LoggerE("Memory allocation error"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Memory allocation error"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Memory allocation error"); } holder->ptr = shared_from_this(); @@ -262,10 +261,9 @@ PlatformResult ArchiveFile::addOperation(OperationCallbackData* callback) // (no risk of parallel operations on file) if (!g_thread_pool_push(ArchiveManager::getInstance().getThreadPool(), static_cast(holder), NULL)) { - LoggerE("Thread creation failed"); delete holder; holder = NULL; - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Thread creation failed"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Thread creation failed"); } } return PlatformResult(ErrorCode::NO_ERROR); @@ -277,22 +275,18 @@ PlatformResult ArchiveFile::extractAllTask(ExtractAllProgressCallback* callback) filesystem::FilePtr directory = callback->getDirectory(); if(!directory) { - LoggerE("Directory is null"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Directory is null"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Directory is null"); } else { if(!directory->getNode()){ - LoggerE("Node in directory is null"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Node is null"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Node in directory is null"); } } if(!m_file) { - LoggerE("File is null"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "File is null"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "File is null"); } else { if(!m_file->getNode()){ - LoggerE("Node in file is null"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Node in file is null"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Node in file is null"); } } @@ -315,9 +309,7 @@ PlatformResult ArchiveFile::extractAllTask(ExtractAllProgressCallback* callback) return PlatformResult(ErrorCode::NO_ERROR); } else { - LoggerW("m_created_as_new_empty_archive is false"); - LoggerE("Throwing InvalidStateException: File is not valid ZIP archive"); - return PlatformResult(ErrorCode::INVALID_STATE_ERR, "File is not valid ZIP archive"); + return LogAndCreateResult(ErrorCode::INVALID_STATE_ERR, "File is not valid ZIP archive", ("m_created_as_new_empty_archive is false")); } } @@ -335,13 +327,11 @@ PlatformResult ArchiveFile::getEntries(GetEntriesCallbackData* callback) { LoggerD("Entered"); if(!callback) { - LoggerE("callback is NULL"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Could not get list of files in archive"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Could not get list of files in archive", ("callback is NULL")); } if(!m_is_open){ - LoggerE("ArchiveFile closed - operation not permitted"); - return PlatformResult(ErrorCode::INVALID_STATE_ERR, "ArchiveFile closed - operation not permitted"); + return LogAndCreateResult(ErrorCode::INVALID_STATE_ERR, "ArchiveFile closed - operation not permitted"); } return addOperation(callback); @@ -408,13 +398,11 @@ PlatformResult ArchiveFile::extractAll(ExtractAllProgressCallback *callback) { LoggerD("Entered"); if(!callback) { - LoggerE("callback is NULL"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Could not extract all files from archive"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Could not extract all files from archive", ("callback is NULL")); } if(!m_is_open){ - LoggerE("ArchiveFile closed - operation not permitted"); - return PlatformResult(ErrorCode::INVALID_STATE_ERR, "ArchiveFile closed - operation not permitted"); + return LogAndCreateResult(ErrorCode::INVALID_STATE_ERR, "ArchiveFile closed - operation not permitted"); } return addOperation(callback); @@ -424,8 +412,7 @@ PlatformResult ArchiveFile::extractEntryTo(ExtractEntryProgressCallback* callbac { LoggerD("Entered"); if(!callback) { - LoggerE("callback is NULL"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Could not extract archive file entry"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Could not extract archive file entry", ("callback is NULL")); } // FIXME according to documentation: @@ -434,8 +421,7 @@ PlatformResult ArchiveFile::extractEntryTo(ExtractEntryProgressCallback* callbac // uncomment in case when this method have permission to throwing InvalidStateError if(!m_is_open) { - LoggerE("Archive is not opened"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Archive is not opened"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Archive is not opened"); } return addOperation(callback); @@ -446,17 +432,14 @@ PlatformResult ArchiveFile::add(AddProgressCallback *callback) { LoggerD("Entered"); if(!callback) { - LoggerE("callback is NULL"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Could not add file to archive"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Could not add file to archive", ("callback is NULL")); } if(FileMode::READ == m_file_mode) { - LoggerE("Trying to add file when READ access mode selected"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Add not allowed for \"r\" access mode"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Add not allowed for \"r\" access mode", ("Trying to add file when READ access mode selected")); } if(!m_is_open){ - LoggerE("ArchiveFile closed - operation not permitted"); - return PlatformResult(ErrorCode::INVALID_STATE_ERR, "ArchiveFile closed - operation not permitted"); + return LogAndCreateResult(ErrorCode::INVALID_STATE_ERR, "ArchiveFile closed - operation not permitted"); } return addOperation(callback); @@ -478,13 +461,11 @@ PlatformResult ArchiveFile::getEntryByName(GetEntryByNameCallbackData* callback) { LoggerD("Entered"); if(!callback) { - LoggerE("callback is NULL"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Could not get archive file entries by name"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Could not get archive file entries by name", ("callback is NULL")); } if(!m_is_open){ - LoggerE("ArchiveFile closed - operation not permitted"); - return PlatformResult(ErrorCode::INVALID_STATE_ERR, "ArchiveFile closed - operation not permitted"); + return LogAndCreateResult(ErrorCode::INVALID_STATE_ERR, "ArchiveFile closed - operation not permitted"); } return addOperation(callback); @@ -618,19 +599,16 @@ PlatformResult ArchiveFile::createUnZipObject(UnZipPtr* unzip) { LoggerD("Entered"); if(!m_is_open) { - LoggerE("File is not opened"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "File is not opened"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "File is not opened"); } if (!m_file) { - LoggerE("m_file is null"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "File is null"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "File is null", ("m_file is null")); } filesystem::NodePtr node = m_file->getNode(); if(!node) { - LoggerE("Node is null"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "File is null"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "File is null", ("Node is null")); } return UnZip::open(m_file->getNode()->getPath()->getFullPath(), unzip); @@ -640,19 +618,16 @@ PlatformResult ArchiveFile::createZipObject(ZipPtr* zip) { LoggerD("Entered"); if(!m_is_open) { - LoggerE("File is not opened"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "File is not opened"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "File is not opened"); } if (!m_file) { - LoggerE("m_file is null"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "File is null"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "File is null", ("m_file is null")); } filesystem::NodePtr node = m_file->getNode(); if(!node) { - LoggerE("Node is null"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Node is null"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Node is null"); } return Zip::open(m_file->getNode()->getPath()->getFullPath(), zip); @@ -713,9 +688,7 @@ PlatformResult ArchiveFile::updateListOfEntries() return PlatformResult(ErrorCode::NO_ERROR); } else { - LoggerW("m_created_as_new_empty_archive is false"); - LoggerE("Throwing InvalidStateException: File is not valid ZIP archive"); - return PlatformResult(ErrorCode::INVALID_STATE_ERR, "File is not valid ZIP archive"); + return LogAndCreateResult(ErrorCode::INVALID_STATE_ERR, "File is not valid ZIP archive", ("m_created_as_new_empty_archive is false")); } } diff --git a/src/archive/archive_file_entry.cc b/src/archive/archive_file_entry.cc index 6ccc13d9..cf6dcd73 100755 --- a/src/archive/archive_file_entry.cc +++ b/src/archive/archive_file_entry.cc @@ -140,8 +140,7 @@ ArchiveFile* ArchiveFileEntry::getArchiveFileNonProtectPtr() PlatformResult ArchiveFileEntry::extractTo(ExtractEntryProgressCallback* callback) { if(!m_archive) { - LOGE("m_archive is NULL"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Could not extract archive file entry"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Could not extract archive file entry", ("m_archive is NULL")); } //Callback should be associated with this instance of ArchiveFileEntry diff --git a/src/archive/archive_instance.cc b/src/archive/archive_instance.cc index 1863e6fc..42ed0266 100755 --- a/src/archive/archive_instance.cc +++ b/src/archive/archive_instance.cc @@ -81,6 +81,10 @@ ArchiveInstance::~ArchiveInstance() { } void ArchiveInstance::PostError(const PlatformResult& e, double callback_id) { + LoggerD("Entered"); + + LoggerE("Posting an error: %d, message: %s", e.error_code(), e.message().c_str()); + picojson::value val = picojson::value(picojson::object()); picojson::object& obj = val.get(); obj[JSON_CALLBACK_ID] = picojson::value(callback_id); @@ -157,8 +161,8 @@ void ArchiveInstance::Open(const picojson::value& args, picojson::object& out) { } file_ptr.reset(); //We need to create new empty file } else if (FileMode::WRITE == fm) { - LoggerE("open: %s with mode: \"w\" file exists and overwrite is FALSE!" - " throwing InvalidModificationException", location_full_path.c_str()); + SLoggerE("open: %s with mode: \"w\" file exists and overwrite is FALSE!", + location_full_path.c_str()); PostError(PlatformResult(ErrorCode::INVALID_MODIFICATION_ERR, "Zip archive already exists"), callbackId); delete callback; @@ -204,7 +208,6 @@ void ArchiveInstance::Open(const picojson::value& args, picojson::object& out) { } } else { LoggerE("Archive file not found"); - LoggerE("Filesystem exception - calling error callback"); PostError(PlatformResult(ErrorCode::NOT_FOUND_ERR, "Archive file not found"), callbackId); delete callback; callback = NULL; @@ -223,7 +226,7 @@ void ArchiveInstance::Open(const picojson::value& args, picojson::object& out) { if (result) { ReportSuccess(out); } else { - ReportError(result, &out); + LogAndReportError(result, &out, ("Failed to open archive.")); } } diff --git a/src/archive/archive_manager.cc b/src/archive/archive_manager.cc index ebe59641..bed9febd 100755 --- a/src/archive/archive_manager.cc +++ b/src/archive/archive_manager.cc @@ -98,8 +98,7 @@ PlatformResult ArchiveManager::getPrivData(long handle, ArchiveFilePtr* archive_ *archive_file = it->second; return PlatformResult(ErrorCode::NO_ERROR); } - LoggerE("Failed: Priv is null"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Priv is null"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Priv is null"); } PlatformResult ArchiveManager::open(OpenCallbackData* callback) diff --git a/src/archive/archive_utils.cc b/src/archive/archive_utils.cc index b0414d55..39611493 100755 --- a/src/archive/archive_utils.cc +++ b/src/archive/archive_utils.cc @@ -62,8 +62,7 @@ PlatformResult fileModeToString(FileMode fm, std::string* fm_str) *fm_str = "a"; break; default: - LoggerE("Unknown file mode"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Unknown file mode"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Unknown file mode"); } return PlatformResult(ErrorCode::NO_ERROR); } @@ -89,8 +88,7 @@ PlatformResult stringToFileMode(std::string fmString, FileMode* fm) } // In widl it's "TypeMismatchError" so this exception used // instead of InvalidValues - LoggerE("Invalid FileMode"); - return PlatformResult(ErrorCode::TYPE_MISMATCH_ERR, "Invalid FileMode"); + return LogAndCreateResult(ErrorCode::TYPE_MISMATCH_ERR, "Invalid FileMode"); } void getBasePathAndName(const std::string& filepath, diff --git a/src/archive/filesystem_node.cc b/src/archive/filesystem_node.cc index 918b0c17..b12708be 100644 --- a/src/archive/filesystem_node.cc +++ b/src/archive/filesystem_node.cc @@ -49,13 +49,11 @@ PlatformResult Node::checkPermission(const PathPtr &path, const std::string &mod DIR* dir = opendir(path->getFullPath().c_str()); if (!dir) { - LoggerE("throw InvalidValuesException"); - return PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Node has been deleted from platform."); + return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, "Node has been deleted from platform."); } if (closedir(dir) != 0) { - LoggerE("throw InvalidValuesException"); - return PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Could not close platform node."); + return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, "Could not close platform node."); } if (mode == "r") { @@ -84,8 +82,8 @@ PlatformResult Node::checkPermission(const PathPtr &path, const std::string &mod *granted = true; return PlatformResult(ErrorCode::NO_ERROR); } - LoggerE("throw InvalidValuesException"); - return PlatformResult(ErrorCode::INVALID_VALUES_ERR, "invalid mode"); + + return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, "invalid mode"); } break; case NT_FILE: @@ -103,8 +101,7 @@ PlatformResult Node::checkPermission(const PathPtr &path, const std::string &mod } else { - LoggerE("throw InvalidValuesException"); - return PlatformResult(ErrorCode::INVALID_VALUES_ERR, "invalid mode"); + return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, "invalid mode"); } stream.open(path->getFullPath().c_str(), modeBit); @@ -134,22 +131,22 @@ PlatformResult Node::resolve(const PathPtr& path, NodePtr* node) switch (errno) { case EACCES: - LoggerE("throw InvalidValuesException for file:[%s]", path->getFullPath().c_str()); - return PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Node access denied"); + SLoggerE("File: [%s]", path->getFullPath().c_str()); + return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, "Node access denied"); break; case ENOENT: - LoggerE("throw NotFoundException for file:[%s]", path->getFullPath().c_str()); - return PlatformResult(ErrorCode::NOT_FOUND_ERR, "NotFoundError"); + SLoggerE("File: [%s]", path->getFullPath().c_str()); + return LogAndCreateResult(ErrorCode::NOT_FOUND_ERR, "NotFoundError"); break; default: - LoggerE("throw IOException for file:[%s]", path->getFullPath().c_str()); - return PlatformResult(ErrorCode::IO_ERR, "Platform exception fail"); + SLoggerE("File: [%s]", path->getFullPath().c_str()); + return LogAndCreateResult(ErrorCode::IO_ERR, "Platform exception fail"); } } if ((!S_ISDIR(info.st_mode)) & (!S_ISREG(info.st_mode)) && !S_ISLNK(info.st_mode)) { - LoggerE("throw IOException for file:[%s]", path->getFullPath().c_str()); - return PlatformResult(ErrorCode::IO_ERR, "Platform node is of unsupported type."); + SLoggerE("File: [%s]", path->getFullPath().c_str()); + return LogAndCreateResult(ErrorCode::IO_ERR, "Platform node is of unsupported type."); } NodeType type = S_ISDIR(info.st_mode) ? NT_DIRECTORY : NT_FILE; @@ -182,8 +179,7 @@ PlatformResult Node::getChild(const PathPtr& path, NodePtr* node) { LoggerD("Enter"); if (m_type != NT_DIRECTORY) { - LoggerW("throw IOException"); - return PlatformResult(ErrorCode::IO_ERR, "Not a directory."); + return LogAndCreateResult(ErrorCode::IO_ERR, "Not a directory."); } return Node::resolve(*m_path + *path, node); } @@ -207,19 +203,16 @@ PlatformResult Node::getChildNames(Node::NameList* out_name_list) const { LoggerD("Enter"); if (m_type != NT_DIRECTORY) { - LoggerE("throw IOException"); - return PlatformResult(ErrorCode::IO_ERR, "Node is not directory."); + return LogAndCreateResult(ErrorCode::IO_ERR, "Node is not directory."); } if ((m_perms & PERM_READ) == 0) { - LoggerE("throw InvalidValuesException"); - return PlatformResult(ErrorCode::INVALID_VALUES_ERR, "No permission."); + return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, "No permission."); } DIR* dir = opendir(m_path->getFullPath().c_str()); if (!dir) { - LoggerE("throw IOException"); - return PlatformResult(ErrorCode::IO_ERR, "Node has been deleted from platform."); + return LogAndCreateResult(ErrorCode::IO_ERR, "Node has been deleted from platform."); } int err = 0; @@ -234,13 +227,11 @@ PlatformResult Node::getChildNames(Node::NameList* out_name_list) const } if (closedir(dir) != 0) { - LoggerE("throw IOException"); - return PlatformResult(ErrorCode::IO_ERR, "Could not close platform node."); + return LogAndCreateResult(ErrorCode::IO_ERR, "Could not close platform node."); } if (0 != err) { - LoggerE("throw IOException"); - return PlatformResult(ErrorCode::IO_ERR, "Error while reading directory."); + return LogAndCreateResult(ErrorCode::IO_ERR, "Error while reading directory."); } *out_name_list = name_list; @@ -252,23 +243,19 @@ PlatformResult Node::getChildNodes(NodeList* out_node_list) const LoggerD("Enter"); if (m_type != NT_DIRECTORY) { - LoggerE("Path %s Perm %d", m_path->getFullPath().c_str(), m_perms); - LoggerE("throw IOException"); - LoggerE("Path %s Perm %d", m_path->getFullPath().c_str(), m_perms); - return PlatformResult(ErrorCode::IO_ERR, "Node is not directory."); + SLoggerE("Path %s Perm %d", m_path->getFullPath().c_str(), m_perms); + return LogAndCreateResult(ErrorCode::IO_ERR, "Node is not directory."); } if ((m_perms & PERM_READ) == 0) { - LoggerE("Path %s Perm %d", m_path->getFullPath().c_str(), m_perms); - LoggerE("throw InvalidValuesException"); - return PlatformResult(ErrorCode::INVALID_VALUES_ERR, "No permission."); + SLoggerE("Path %s Perm %d", m_path->getFullPath().c_str(), m_perms); + return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, "No permission."); } DIR* dir = opendir(m_path->getFullPath().c_str()); if (!dir) { - LoggerE("Path %s Perm %d", m_path->getFullPath().c_str(), m_perms); - LoggerE("throw IOException"); - return PlatformResult(ErrorCode::IO_ERR, "Node has been deleted from platform."); + SLoggerE("Path %s Perm %d", m_path->getFullPath().c_str(), m_perms); + return LogAndCreateResult(ErrorCode::IO_ERR, "Node has been deleted from platform."); } int err = 0; @@ -287,15 +274,13 @@ PlatformResult Node::getChildNodes(NodeList* out_node_list) const } if (closedir(dir) != 0) { - LoggerE("Path %s Perm %d", m_path->getFullPath().c_str(), m_perms); - LoggerE("throw IOException"); - return PlatformResult(ErrorCode::IO_ERR, "Could not close platform node."); + SLoggerE("Path %s Perm %d", m_path->getFullPath().c_str(), m_perms); + return LogAndCreateResult(ErrorCode::IO_ERR, "Could not close platform node."); } if (0 != err) { - LoggerE("Path %s Perm %d", m_path->getFullPath().c_str(), m_perms); - LoggerE("throw IOException"); - return PlatformResult(ErrorCode::IO_ERR, "Error while reading directory."); + SLoggerE("Path %s Perm %d", m_path->getFullPath().c_str(), m_perms); + return LogAndCreateResult(ErrorCode::IO_ERR, "Error while reading directory."); } *out_node_list = node_list; @@ -311,13 +296,11 @@ PlatformResult Node::createChild( { LoggerD("Enter"); if (m_type != NT_DIRECTORY) { - LoggerE("throw IOException"); - return PlatformResult(ErrorCode::IO_ERR, "Parent node is not a directory."); + return LogAndCreateResult(ErrorCode::IO_ERR, "Parent node is not a directory."); } if ((m_perms & PERM_WRITE) == 0) { - LoggerE("throw InvalidValuesException"); - return PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Not enough permissions."); + return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, "Not enough permissions."); } PathPtr childPath = *m_path + *path; @@ -328,8 +311,7 @@ PlatformResult Node::createChild( return result; } if (existed) { - LoggerE("throw IOException"); - return PlatformResult(ErrorCode::IO_ERR, "Node already exists."); + return LogAndCreateResult(ErrorCode::IO_ERR, "Node already exists."); } switch (type) { @@ -340,8 +322,7 @@ PlatformResult Node::createChild( result = createAsDirectory(childPath, node, options); break; default: - LoggerE("throw IOException"); - return PlatformResult(ErrorCode::IO_ERR, "Unsupported node type."); + return LogAndCreateResult(ErrorCode::IO_ERR, "Unsupported node type."); } if (result.error_code() != ErrorCode::NO_ERROR) { LoggerE("Fail CreateAs...()"); @@ -351,8 +332,7 @@ PlatformResult Node::createChild( if (!!(*node)) { (*node)->m_perms = m_perms; } else { - LoggerE("throw IOException"); - return PlatformResult(ErrorCode::IO_ERR, "Node creation error"); + return LogAndCreateResult(ErrorCode::IO_ERR, "Node creation error"); } return PlatformResult(ErrorCode::NO_ERROR); @@ -370,8 +350,7 @@ PlatformResult Node::remove(int options) result = removeAsDirectory(m_path, (options & OPT_RECURSIVE)); break; default: - LoggerE("throw UnknownError"); - result = PlatformResult(ErrorCode::UNKNOWN_ERR, "Not supported value of m_type"); + result = LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Not supported value of m_type"); } return result; } @@ -380,9 +359,7 @@ PlatformResult Node::getSize(unsigned long long* size) const { LoggerD("Enter"); if (m_type == NT_DIRECTORY) { - LoggerE("Getting size for directories is not supported."); - LoggerE("throw IOException"); - return PlatformResult(ErrorCode::IO_ERR, "Getting size for directories is not supported."); + return LogAndCreateResult(ErrorCode::IO_ERR, "Getting size for directories is not supported."); } struct stat info; @@ -393,9 +370,7 @@ PlatformResult Node::getSize(unsigned long long* size) const } if (!S_ISREG(info.st_mode)) { - LoggerE("Specified node is not a regular file."); - LoggerE("throw IOException"); - return PlatformResult(ErrorCode::IO_ERR, "Specified node is not a regular file."); + return LogAndCreateResult(ErrorCode::IO_ERR, "Specified node is not a regular file."); } *size = info.st_size; @@ -485,8 +460,7 @@ PlatformResult Node::exists(const PathPtr& path, bool* existed) } else if (ENAMETOOLONG == errno) { - LoggerE("throw IOException"); - return PlatformResult(ErrorCode::IO_ERR, "file name is too long"); + return LogAndCreateResult(ErrorCode::IO_ERR, "file name is too long"); } else if (errno != ENOENT) { @@ -504,9 +478,8 @@ PlatformResult Node::stat(const PathPtr& path, struct stat* out_info) if (::stat(path->getFullPath().c_str(), &info) != 0) { - LoggerE("File: %s", path->getFullPath().c_str()); - LoggerE("throw IOException"); - return PlatformResult(ErrorCode::IO_ERR, "Node does not exist or no access"); + SLoggerE("File: %s", path->getFullPath().c_str()); + return LogAndCreateResult(ErrorCode::IO_ERR, "Node does not exist or no access"); } *out_info = info; return PlatformResult(ErrorCode::NO_ERROR); @@ -537,9 +510,9 @@ PlatformResult Node::createAsFileInternal(const PathPtr& path) FILE* file = std::fopen(path->getFullPath().c_str(), "wb"); if (!file) { - LoggerE("fopen fails IOException throw for path [%s]", - path->getFullPath().c_str()); - return PlatformResult(ErrorCode::IO_ERR, "Platform node could not be created."); + SLoggerE("fopen fails IOException throw for path [%s]", + path->getFullPath().c_str()); + return LogAndCreateResult(ErrorCode::IO_ERR, "Platform node could not be created."); } std::fclose(file); return PlatformResult(ErrorCode::NO_ERROR); @@ -568,8 +541,7 @@ PlatformResult Node::createAsDirectoryInternal(const PathPtr& path) if (mkdir(path->getFullPath().c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) { - LoggerE("throw IOException"); - return PlatformResult(ErrorCode::IO_ERR, "Platform node could not be created."); + return LogAndCreateResult(ErrorCode::IO_ERR, "Platform node could not be created."); } return PlatformResult(ErrorCode::NO_ERROR); } @@ -580,9 +552,8 @@ PlatformResult Node::removeAsFile(const PathPtr& path) auto fullPath = path->getFullPath(); if (unlink(fullPath.c_str()) != 0) { - LoggerE("remove [%s]", fullPath.c_str()); - LoggerE("throw IOException"); - return PlatformResult(ErrorCode::IO_ERR, "Error while removing platform node."); + SLoggerE("remove [%s]", fullPath.c_str()); + return LogAndCreateResult(ErrorCode::IO_ERR, "Error while removing platform node."); } return PlatformResult(ErrorCode::NO_ERROR); } @@ -594,9 +565,8 @@ PlatformResult Node::removeAsDirectory(const PathPtr& path, bool recursive) if (recursive) { DIR* dir = opendir(path->getFullPath().c_str()); if (!dir) { - LoggerE("File %s", path->getFullPath().c_str()); - LoggerE("throw IOException"); - return PlatformResult(ErrorCode::IO_ERR, "Node does not exist or access denied."); + SLoggerE("File %s", path->getFullPath().c_str()); + return LogAndCreateResult(ErrorCode::IO_ERR, "Node does not exist or access denied."); } struct dirent entry = {0}; struct dirent* result = nullptr; @@ -627,11 +597,9 @@ PlatformResult Node::removeAsDirectory(const PathPtr& path, bool recursive) errno = 0; if (rmdir(path->getFullPath().c_str()) != 0) { if (errno == EEXIST) { - LoggerE("throw IOException"); - return PlatformResult(ErrorCode::IO_ERR, "Node has child nodes."); + return LogAndCreateResult(ErrorCode::IO_ERR, "Node has child nodes."); } - LoggerE("throw IOException"); - return PlatformResult(ErrorCode::IO_ERR, "Error while removing platform node."); + return LogAndCreateResult(ErrorCode::IO_ERR, "Error while removing platform node."); } return PlatformResult(ErrorCode::NO_ERROR); diff --git a/src/archive/un_zip.cc b/src/archive/un_zip.cc index 579958ed..757992e7 100644 --- a/src/archive/un_zip.cc +++ b/src/archive/un_zip.cc @@ -74,8 +74,7 @@ PlatformResult UnZip::close() m_unzip = NULL; if (errclose != UNZ_OK) { - LoggerE("ret: %d",errclose); - return PlatformResult(ErrorCode::UNKNOWN_ERR, getArchiveLogMessage(errclose, "unzClose()")); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, getArchiveLogMessage(errclose, "unzClose()"), ("ret: %d", errclose)); } m_is_open = false; return PlatformResult(ErrorCode::NO_ERROR); @@ -87,8 +86,7 @@ PlatformResult UnZip::open(const std::string& filename, UnZipPtr* out_unzip) UnZipPtr unzip = UnZipPtr(new UnZip(filename)); if(!unzip->m_unzip) { - LoggerE("unzOpen returned NULL : It means the file is invalid."); - return PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Failed to open zip file"); + return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, "Failed to open zip file", ("unzOpen returned NULL : It means the file is invalid.")); } unzip->m_is_open = true; *out_unzip = unzip; @@ -99,14 +97,12 @@ PlatformResult UnZip::listEntries(unsigned long *decompressedSize, ArchiveFileEn { LoggerD("Enter"); if(!m_is_open) { - LoggerE("Failed to get list of entries - UnZip closed"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Failed to get list of files in zip archive"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Failed to get list of files in zip archive"); } unz_global_info gi; int err = unzGetGlobalInfo (m_unzip, &gi); if (UNZ_OK != err) { - LoggerE("ret: %d",err); - return PlatformResult(ErrorCode::UNKNOWN_ERR, getArchiveLogMessage(err, "unzGetGlobalInfo()")); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, getArchiveLogMessage(err, "unzGetGlobalInfo()"), ("ret: %d", err)); } char filename_inzip[512]; @@ -126,8 +122,7 @@ PlatformResult UnZip::listEntries(unsigned long *decompressedSize, ArchiveFileEn err = unzGetCurrentFileInfo(m_unzip, &file_info, filename_inzip, sizeof(filename_inzip), NULL, 0, NULL, 0); if (err != UNZ_OK) { - LoggerE("ret: %d",err); - return PlatformResult(ErrorCode::UNKNOWN_ERR, getArchiveLogMessage(err, "unzGetCurrentFileInfo()")); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, getArchiveLogMessage(err, "unzGetCurrentFileInfo()"), ("ret: %d", err)); } LoggerD("file: %s | unc size: %d | comp size: %d", filename_inzip, @@ -159,8 +154,7 @@ PlatformResult UnZip::listEntries(unsigned long *decompressedSize, ArchiveFileEn err = unzGoToNextFile(m_unzip); if (UNZ_OK != err) { - LoggerE("ret: %d",err); - return PlatformResult(ErrorCode::UNKNOWN_ERR, getArchiveLogMessage(err, "unzGoToNextFile()")); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, getArchiveLogMessage(err, "unzGoToNextFile()"), ("ret: %d", err)); } } } @@ -177,8 +171,7 @@ PlatformResult UnZip::extractAllFilesTo(const std::string& extract_path, { LoggerD("Enter"); if(!m_is_open) { - LoggerE("Failed to extract files - UnZip closed"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Failed to extract zip archive"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Failed to extract zip archive"); } // @@ -202,8 +195,7 @@ PlatformResult UnZip::extractAllFilesTo(const std::string& extract_path, for (uLong i = 0; i < gi.number_entry; i++) { if (callback->isCanceled()) { - LoggerD("Operation cancelled"); - return PlatformResult(ErrorCode::OPERATION_CANCELED_ERR); + return LogAndCreateResult(ErrorCode::OPERATION_CANCELED_ERR, "Operation canceled"); } result = extractCurrentFile(extract_path, std::string(), callback); @@ -215,8 +207,7 @@ PlatformResult UnZip::extractAllFilesTo(const std::string& extract_path, if ((i + 1) < gi.number_entry) { err = unzGoToNextFile(m_unzip); if (UNZ_OK != err) { - LoggerE("ret: %d",err); - return PlatformResult(ErrorCode::UNKNOWN_ERR, getArchiveLogMessage(err, "unzGoToNextFile()")); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, getArchiveLogMessage(err, "unzGoToNextFile()"), ("ret: %d", err)); } } } @@ -237,36 +228,30 @@ PlatformResult UnZip::extractTo(ExtractEntryProgressCallback* callback) { LoggerD("Enter"); if(!m_is_open) { - LoggerE("Extract archive file entry failed - UnZip closed"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Extract archive file entry failed"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Extract archive file entry failed"); } if(!callback) { - LoggerE("callback is NULL"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Extract archive file entry failed"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Extract archive file entry failed", ("callback is NULL")); } if(!callback->getArchiveFileEntry()) { - LoggerE("callback->getArchiveFileEntry() is NULL"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Extract archive file entry failed"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Extract archive file entry failed", ("callback->getArchiveFileEntry() is NULL")); } filesystem::FilePtr out_dir = callback->getDirectory(); if(!out_dir) { - LoggerE("Output directory is not valid"); - return PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Output directory is not correct"); + return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, "Output directory is not correct"); } NodePtr out_node = out_dir->getNode(); if(!out_node) { - LoggerE("Output directory is not valid"); - return PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Output directory is not correct"); + return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, "Output directory is not correct"); } PathPtr out_path = out_node->getPath(); if(!out_path) { - LoggerE("Output directory is not valid"); - return PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Output directory is not correct"); + return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, "Output directory is not correct"); } auto entry_name_in_zip = callback->getArchiveFileEntry()->getName(); @@ -316,8 +301,7 @@ PlatformResult UnZip::extractItFunction(const std::string& file_name, unz_file_i LoggerD("Enter"); ExtractDataHolder* h = static_cast(user_data); if(!h) { - LoggerE("ExtractDataHolder is NULL!"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Could not list content of zip archive"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Could not list content of zip archive", ("ExtractDataHolder is NULL!")); } PlatformResult result = h->unzip->extractCurrentFile(h->root_output_path, @@ -352,15 +336,13 @@ PlatformResult UnZip::IterateFilesInZip(unz_global_info& gi, for (uLong i = 0; i < gi.number_entry; i++) { if (callback->isCanceled()) { - LoggerD("Operation cancelled"); - return PlatformResult(ErrorCode::OPERATION_CANCELED_ERR); + return LogAndCreateResult(ErrorCode::OPERATION_CANCELED_ERR, "Operation canceled"); } err = unzGetCurrentFileInfo(m_unzip, &cur_file_info, tmp_fname, sizeof(tmp_fname), NULL, 0, NULL, 0); if (UNZ_OK != err) { - LoggerE("ret: %d",err); - return PlatformResult(ErrorCode::UNKNOWN_ERR, getArchiveLogMessage(err, "unzGetCurrentFileInfo()")); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, getArchiveLogMessage(err, "unzGetCurrentFileInfo()"), ("ret: %d", err)); } const std::string cur_filename_in_zip(tmp_fname); @@ -389,8 +371,7 @@ PlatformResult UnZip::IterateFilesInZip(unz_global_info& gi, if ((i + 1) < gi.number_entry) { err = unzGoToNextFile(m_unzip); if (UNZ_OK != err) { - LoggerE("ret: %d",err); - return PlatformResult(ErrorCode::UNKNOWN_ERR, getArchiveLogMessage(err, "unzGoToNextFile()")); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, getArchiveLogMessage(err, "unzGoToNextFile()"), ("ret: %d", err)); } } } @@ -405,8 +386,7 @@ PlatformResult UnZip::extractCurrentFile(const std::string& extract_path, LoggerD("Entered"); if (callback->isCanceled()) { - LoggerD("Operation cancelled"); - return PlatformResult(ErrorCode::OPERATION_CANCELED_ERR); + return LogAndCreateResult(ErrorCode::OPERATION_CANCELED_ERR, "Operation canceled"); } LoggerD("extract_path: [%s] base_strip_path: [%s] ", extract_path.c_str(), @@ -450,8 +430,7 @@ PlatformResult UnZip::updateCallbackWithArchiveStatistics(ExtractAllProgressCall LoggerD("Enter"); int err = unzGetGlobalInfo(m_unzip, &out_global_info); if (UNZ_OK != err) { - LoggerE("ret: %d",err); - return PlatformResult(ErrorCode::UNKNOWN_ERR, getArchiveLogMessage(err, "unzGetGlobalInfo()")); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, getArchiveLogMessage(err, "unzGetGlobalInfo()"), ("ret: %d", err)); } ArchiveStatistics astats; @@ -464,10 +443,9 @@ PlatformResult UnZip::updateCallbackWithArchiveStatistics(ExtractAllProgressCall return result; } if(0 == num_matched) { - LoggerE("No matching file/directory: [%s] has been found in zip archive", - optional_filter.c_str()); - LoggerE("Throwing NotFoundException - Could not extract file from archive"); - return PlatformResult(ErrorCode::NOT_FOUND_ERR, "Could not extract file from archive"); + SLoggerE("No matching file/directory: [%s] has been found in zip archive", + optional_filter.c_str()); + return LogAndCreateResult(ErrorCode::NOT_FOUND_ERR, "Could not extract file from archive"); } callback->setExpectedDecompressedSize(astats.uncompressed_size); diff --git a/src/archive/un_zip_extract_request.cc b/src/archive/un_zip_extract_request.cc index 197d6b5d..dcc16dcd 100644 --- a/src/archive/un_zip_extract_request.cc +++ b/src/archive/un_zip_extract_request.cc @@ -137,8 +137,7 @@ PlatformResult UnZipExtractRequest::execute(UnZip& owner, const std::string& ext LoggerD("Enter"); UnZipExtractRequest req(owner, extract_path, base_strip_path, callback); if(!req.m_callback){ - LoggerE("Callback is null"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Problem with callback functionality"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Problem with callback functionality", ("Callback is null")); } return req.run(); } @@ -218,8 +217,7 @@ PlatformResult UnZipExtractRequest::getCurrentFileInfo() int err = unzGetCurrentFileInfo(m_owner.m_unzip, &m_file_info, m_filename_inzip, sizeof(m_filename_inzip), NULL, 0, NULL, 0); if (err != UNZ_OK) { - LoggerE("ret: %d", err); - return PlatformResult(ErrorCode::UNKNOWN_ERR, getArchiveLogMessage(err, "unzGetCurrentFileInfo()")); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, getArchiveLogMessage(err, "unzGetCurrentFileInfo()"), ("ret: %d", err)); } LoggerD("Input from ZIP: m_filename_inzip: [%s]", m_filename_inzip); @@ -291,23 +289,21 @@ PlatformResult UnZipExtractRequest::handleDirectoryEntry() if(m_callback->getOverwrite()) { //Is a file & overwrite is set: std::string fn = removeTrailingDirectorySlashFromPath(m_new_dir_path); if(std::remove(fn.c_str()) != 0) { - LoggerE("std::remove(\"%s\") failed with errno: %s", - m_new_dir_path.c_str(), GetErrorString(errno).c_str()); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Could not overwrite file in output directory"); + SLoggerE("std::remove(\"%s\") failed with errno: %s", + m_new_dir_path.c_str(), GetErrorString(errno).c_str()); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Could not overwrite file in output directory"); } } else { //Is a file & overwrite is not set: - LoggerE("Failed to extract directory, " - "file with the same name exists in output directory"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Failed to extract directory, " - "file with the same name exists in output directory"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Failed to extract directory, " + "file with the same name exists in output directory"); } } //Try to create new directory in output directory if(mkdir(m_new_dir_path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == -1) { - LoggerE("Couldn't create new directory: %s errno: %s", - m_new_dir_path.c_str(), GetErrorString(errno).c_str()); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Could not create new directory in extract output directory"); + SLoggerE("Couldn't create new directory: %s errno: %s", + m_new_dir_path.c_str(), GetErrorString(errno).c_str()); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Could not create new directory in extract output directory"); } } @@ -335,10 +331,10 @@ PlatformResult UnZipExtractRequest::prepareOutputSubdirectory() //This zip entry points to file - verify that parent directory in output dir exists if(FPS_DIRECTORY != m_new_dir_status) { if(FPS_FILE == m_new_dir_status) { - LoggerE("Path: %s is pointing to file not directory!", - m_new_dir_path.c_str()); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Failed to extract file from zip archive, " - "output path is invalid"); + SLoggerE("Path: %s is pointing to file not directory!", + m_new_dir_path.c_str()); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Failed to extract file from zip archive, " + "output path is invalid"); } // permissions would be changed after extract process would be finished, @@ -350,17 +346,16 @@ PlatformResult UnZipExtractRequest::prepareOutputSubdirectory() } if(m_callback->isCanceled()) { - LoggerD("Operation cancelled"); - return PlatformResult(ErrorCode::OPERATION_CANCELED_ERR); + return LogAndCreateResult(ErrorCode::OPERATION_CANCELED_ERR, "Operation canceled"); } const FilePathStatus output_fstatus = getPathStatus(m_output_filepath); if(FPS_NOT_EXIST != output_fstatus) { if(!m_callback->getOverwrite()) { - LoggerW("%s exists at output path: [%s], overwrite is set to FALSE", - (FPS_DIRECTORY == output_fstatus ? "Directory" : "File"), - m_output_filepath.c_str()); - return PlatformResult(ErrorCode::INVALID_MODIFICATION_ERR, "file already exists."); + SLoggerE("%s exists at output path: [%s], overwrite is set to FALSE", + (FPS_DIRECTORY == output_fstatus ? "Directory" : "File"), + m_output_filepath.c_str()); + return LogAndCreateResult(ErrorCode::INVALID_MODIFICATION_ERR, "File already exists."); } else { if(FPS_DIRECTORY == output_fstatus) { filesystem::PathPtr path = filesystem::Path::create(m_output_filepath); @@ -395,8 +390,7 @@ PlatformResult UnZipExtractRequest::handleFileEntry() int err = unzOpenCurrentFilePassword(m_owner.m_unzip, NULL); //password is not supported yet therefore passing NULL if (UNZ_OK != err) { - LoggerE("ret: %d", err); - return PlatformResult(ErrorCode::UNKNOWN_ERR, getArchiveLogMessage(err, "unzOpenCurrentFilePassword()")); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, getArchiveLogMessage(err, "unzOpenCurrentFilePassword()"), ("ret: %d", err)); } //We have successfully opened curent file, therefore we should close it later @@ -405,15 +399,14 @@ PlatformResult UnZipExtractRequest::handleFileEntry() const size_t buffer_size = m_owner.m_default_buffer_size; m_buffer = new(std::nothrow) char[buffer_size]; if(!m_buffer) { - LoggerE("Couldn't allocate buffer with size: %s", - bytesToReadableString(buffer_size).c_str()); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Memory allocation failed"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Memory allocation failed", ("Couldn't allocate buffer with size: %s", + bytesToReadableString(buffer_size).c_str())); } m_output_file = fopen(m_output_filepath.c_str(), "wb"); if(!m_output_file) { - LoggerE("Couldn't open output file: %s", m_output_filepath.c_str()); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Could not create extracted file"); + SLoggerE("Couldn't open output file: %s", m_output_filepath.c_str()); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Could not create extracted file"); } m_delete_output_file = true; @@ -433,21 +426,19 @@ PlatformResult UnZipExtractRequest::handleFileEntry() ArchiveFileEntryPtrMapPtr entries = m_callback->getArchiveFile()->getEntryMap(); auto it = entries->find(m_filename_inzip); if (it == entries->end()) { - LoggerE("Entry not found"); - return PlatformResult(ErrorCode::NOT_FOUND_ERR, "Entry not found"); + return LogAndCreateResult(ErrorCode::NOT_FOUND_ERR, "Entry not found"); } while(true) { if(m_callback->isCanceled()) { - LoggerD("Operation cancelled"); - return PlatformResult(ErrorCode::OPERATION_CANCELED_ERR); + return LogAndCreateResult(ErrorCode::OPERATION_CANCELED_ERR, "Operation canceled"); } int read_size = unzReadCurrentFile(m_owner.m_unzip, m_buffer, buffer_size); if (read_size < 0) { - LoggerE("unzReadCurrentFile failed with error code:%d for file:%s", read_size, - m_filename_inzip); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Failed to extract file from zip archive"); + SLoggerE("unzReadCurrentFile failed with error code: %d for file: %s", read_size, + m_filename_inzip); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Failed to extract file from zip archive"); } else if(0 == read_size) { @@ -471,9 +462,7 @@ PlatformResult UnZipExtractRequest::handleFileEntry() } if (fwrite(m_buffer, read_size, 1, m_output_file) != 1) { - LoggerE("Couldn't write extracted data to output file:%s", - m_output_filepath.c_str()); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Could not write extract file into output file"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Could not write extract file into output file"); } if(extract_callback) { diff --git a/src/archive/zip.cc b/src/archive/zip.cc index b5d18a15..3517beb2 100755 --- a/src/archive/zip.cc +++ b/src/archive/zip.cc @@ -104,8 +104,7 @@ PlatformResult Zip::close() m_zip = NULL; if (errclose != ZIP_OK) { - LoggerE("ret: %d", errclose); - return PlatformResult(ErrorCode::UNKNOWN_ERR, getArchiveLogMessage(errclose, "zipClose()")); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, getArchiveLogMessage(errclose, "zipClose()"), ("ret: %d", errclose)); } m_is_open = false; return PlatformResult(ErrorCode::NO_ERROR); @@ -116,8 +115,7 @@ PlatformResult Zip::createNew(const std::string& filename, ZipPtr* out_zip) LoggerD("Entered"); ZipPtr zip = ZipPtr(new Zip(filename, ZOM_CREATE)); if(!zip->m_zip) { - LoggerE("zipOpen returned NULL!"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Opening/creating zip file failed"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Opening/creating zip file failed", ("zipOpen returned NULL!")); } zip->m_is_open = true; *out_zip = zip; @@ -129,8 +127,7 @@ PlatformResult Zip::open(const std::string& filename, ZipPtr* out_zip) LoggerD("Entered"); ZipPtr zip = ZipPtr(new Zip(filename, ZOM_ADDINZIP)); if(!zip->m_zip) { - LoggerE("zipOpen returned NULL!"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Opening/creating zip file failed"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Opening/creating zip file failed", ("zipOpen returned NULL!")); } zip->m_is_open = true; *out_zip = zip; @@ -141,12 +138,10 @@ PlatformResult Zip::addFile(AddProgressCallback*& callback) { LoggerD("Entered"); if(!callback) { - LoggerE("callback is NULL!"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Could not add file(-s) to archive"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Could not add file(-s) to archive", ("callback is NULL!")); } if(!m_is_open) { - LoggerE("Zip file not opened - exiting"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Could not add file(-s) to archive - zip file closed"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Could not add file(-s) to archive - zip file closed"); } return ZipAddRequest::execute(*this, callback); diff --git a/src/archive/zip_add_request.cc b/src/archive/zip_add_request.cc index 6a5e570f..969b0fbb 100755 --- a/src/archive/zip_add_request.cc +++ b/src/archive/zip_add_request.cc @@ -73,18 +73,15 @@ PlatformResult ZipAddRequest::run() { LoggerD("Enter"); if(!m_callback) { - LoggerE("m_callback is NULL"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Could not add file(-s) to archive"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Could not add file(-s) to archive", ("m_callback is NULL")); } if(!m_callback->getFileEntry()) { - LoggerE("m_callback->getFileEntry() is NULL"); - return PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Provided ArchiveFileEntry is not correct"); + return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, "Provided ArchiveFileEntry is not correct", ("m_callback->getFileEntry() is NULL")); } if(m_callback->isCanceled()) { - LoggerD("Operation cancelled"); - return PlatformResult(ErrorCode::OPERATION_CANCELED_ERR); + return LogAndCreateResult(ErrorCode::OPERATION_CANCELED_ERR, "Operation canceled"); } m_compression_level = m_callback->getFileEntry()->getCompressionLevel(); @@ -136,8 +133,7 @@ PlatformResult ZipAddRequest::run() addNodeAndSubdirsToList(m_root_src_file_node, all_sub_nodes); if(m_callback->isCanceled()) { - LoggerD("Operation cancelled"); - return PlatformResult(ErrorCode::OPERATION_CANCELED_ERR); + return LogAndCreateResult(ErrorCode::OPERATION_CANCELED_ERR, "Operation canceled"); } // Calculate total size to be compressed @@ -168,8 +164,7 @@ PlatformResult ZipAddRequest::run() bytesToReadableString(m_bytes_to_compress).c_str()); if(m_callback->isCanceled()) { - LoggerD("Operation cancelled"); - return PlatformResult(ErrorCode::OPERATION_CANCELED_ERR); + return LogAndCreateResult(ErrorCode::OPERATION_CANCELED_ERR, "Operation canceled"); } // Begin files compression @@ -232,10 +227,8 @@ PlatformResult ZipAddRequest::addEmptyDirectoryToZipArchive(std::string name_in_ &is_directory, &conflicting_name)) { if(!is_directory) { - LoggerE("Entry: [%s] exists and is NOT directory!", conflicting_name.c_str()); - - LoggerE("Throwing InvalidValuesException - File with the same name exists"); - return PlatformResult(ErrorCode::INVALID_VALUES_ERR, "File with the same name exists"); + SLoggerE("Entry: [%s] exists and is NOT directory!", conflicting_name.c_str()); + return LogAndCreateResult(ErrorCode::INVALID_VALUES_ERR, "File with the same name exists"); } LoggerD("Directory: [%s] already exists -> nothing to do", name_in_zip.c_str()); @@ -243,8 +236,7 @@ PlatformResult ZipAddRequest::addEmptyDirectoryToZipArchive(std::string name_in_ } if(m_callback->isCanceled()) { - LoggerD("Operation cancelled"); - return PlatformResult(ErrorCode::OPERATION_CANCELED_ERR); + return LogAndCreateResult(ErrorCode::OPERATION_CANCELED_ERR, "Operation canceled"); } zip_fileinfo new_dir_info; @@ -273,8 +265,7 @@ PlatformResult ZipAddRequest::addEmptyDirectoryToZipArchive(std::string name_in_ NULL, 0); if (err != ZIP_OK) { - LoggerE("ret: %d", err); - return PlatformResult(ErrorCode::UNKNOWN_ERR, getArchiveLogMessage(err, "zipOpenNewFileInZip3()")); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, getArchiveLogMessage(err, "zipOpenNewFileInZip3()"), ("ret: %d", err)); } m_new_file_in_zip_opened = true; @@ -312,20 +303,18 @@ PlatformResult ZipAddRequest::addToZipArchive(filesystem::NodePtr src_file_node) } if(m_callback->isCanceled()) { - LoggerD("Operation cancelled"); - return PlatformResult(ErrorCode::OPERATION_CANCELED_ERR); + return LogAndCreateResult(ErrorCode::OPERATION_CANCELED_ERR, "Operation canceled"); } std::string conflicting_name; if(m_callback->getArchiveFile()->isEntryWithNameInArchive(name_in_zip, NULL, &conflicting_name)) { - LoggerE("Cannot add new entry with name name: [%s] " - "it would conflict with existing entry: [%s]", - name_in_zip.c_str(), conflicting_name.c_str()); + SLoggerE("Cannot add new entry with name name: [%s] " + "it would conflict with existing entry: [%s]", + name_in_zip.c_str(), conflicting_name.c_str()); - LoggerE("Throwing InvalidModificationException - Archive entry name conflicts"); - return PlatformResult(ErrorCode::INVALID_MODIFICATION_ERR, "Archive entry name conflicts"); + return LogAndCreateResult(ErrorCode::INVALID_MODIFICATION_ERR, "Archive entry name conflicts"); } int err = zipOpenNewFileInZip3(m_owner.m_zip, name_in_zip.c_str(), &new_file_info, @@ -336,8 +325,8 @@ PlatformResult ZipAddRequest::addToZipArchive(filesystem::NodePtr src_file_node) NULL, 0); if (err != ZIP_OK) { - LoggerE("Error opening new file: [%s] in zipfile", name_in_zip.c_str()); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Could not add new file to zip archive"); + SLoggerE("Error opening new file: [%s] in zipfile", name_in_zip.c_str()); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Could not add new file to zip archive"); } m_new_file_in_zip_opened = true; @@ -375,8 +364,8 @@ PlatformResult ZipAddRequest::addToZipArchive(filesystem::NodePtr src_file_node) if(src_file_node->getType() == filesystem::NT_FILE) { m_input_file = fopen(src_file_path.c_str(), "rb"); if (!m_input_file) { - LoggerE("Error opening source file:%s", src_file_path.c_str()); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Could not open file to be added"); + SLoggerE("Error opening source file: %s", src_file_path.c_str()); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Could not open file to be added"); } //Get file length @@ -392,8 +381,7 @@ PlatformResult ZipAddRequest::addToZipArchive(filesystem::NodePtr src_file_node) if(!m_buffer) { m_buffer = new(std::nothrow) char[m_buffer_size]; if(!m_buffer) { - LoggerE("Couldn't allocate m_buffer"); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Memory allocation error"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Memory allocation error"); } } @@ -404,8 +392,8 @@ PlatformResult ZipAddRequest::addToZipArchive(filesystem::NodePtr src_file_node) size_read = fread(m_buffer, 1, m_buffer_size, m_input_file); if (size_read < m_buffer_size && feof(m_input_file) == 0) { - LoggerE("Error reading source file: %s\n", src_file_path.c_str()); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "New file addition failed"); + SLoggerE("Error reading source file: %s", src_file_path.c_str()); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "New file addition failed"); } LoggerD("Read: %d bytes from input file:[%s]", size_read, @@ -416,9 +404,9 @@ PlatformResult ZipAddRequest::addToZipArchive(filesystem::NodePtr src_file_node) if (size_read > 0) { err = zipWriteInFileInZip (m_owner.m_zip, m_buffer, size_read); if (err < 0) { - LoggerE("Error during adding file: %s into zip archive", - src_file_path.c_str()); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "New file addition failed"); + SLoggerE("Error during adding file: %s into zip archive", + src_file_path.c_str()); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "New file addition failed"); } } @@ -452,9 +440,8 @@ PlatformResult ZipAddRequest::addToZipArchive(filesystem::NodePtr src_file_node) } while (size_read > 0 && total_bytes_read < in_file_size); if(in_file_size != total_bytes_read) { - LoggerE("in_file_size(%d) != total_bytes_read(%d)", in_file_size, - total_bytes_read); - return PlatformResult(ErrorCode::UNKNOWN_ERR, "Could not add file to archive"); + return LogAndCreateResult(ErrorCode::UNKNOWN_ERR, "Could not add file to archive", ("in_file_size(%d) != total_bytes_read(%d)", in_file_size, + total_bytes_read)); } fclose(m_input_file);