Remixt
Remixt

Reputation: 597

Libzip - Archive error always returns ZIP_ER_OK, even if archive fails to be created

I'm using a switch statement to check the value of the error code in the libzip zip archive that I'm creating. However, it never seems to return anything other than 0, even when I cause it to fail on purpose. (I'm causing it to fail by trying to copy files to a usb drive that is 100% full).

How is my usage incorrect here?

                int error = 0;
                zip_t * zip = zip_open(externalDevicePath.c_str(), ZIP_CREATE, &error);
                zip_add_dir(zip,(institutionId + DIR_SLASH + userId).c_str());

                string instAndUserPath = basePath + DIR_SLASH + institutionId + DIR_SLASH + userId;
                string stbackupPath = basePath + DIR_SLASH + "STBackups";

                ESFileUtilsCommon::addDirectoryToZip(instAndUserPath,zip,(institutionId + DIR_SLASH + userId).c_str());
                ESFileUtilsCommon::addDirectoryToZip(stbackupPath,zip,"STBackups");
                ESFileUtilsCommon::addDirectoryContentsToZip(basePath, zip);

                zip_close(zip);

                switch (error){
                case ZIP_ER_OK:         
                    mapper.setResult(RESULT_SUCCESS);
                    retval = CefV8Value::CreateString(mapper.getMappedJsonResponse());
                    return true;
                    break;
                default:
                    mapper.setResult(RESULT_FAILURE);
                    retval = CefV8Value::CreateString(mapper.getMappedJsonResponse());
                    return false;
                    break;
                }

Upvotes: 0

Views: 535

Answers (1)

llllllllll
llllllllll

Reputation: 16404

You haven't provided a Minimal, Complete, and Verifiable example, so we can't test your code. However, from the fact that you are checking error long after zip_open, I think you are confused by the how error works in libzip.

The int error you pass to zip_open is only responsible to zip_open, not any potential errors afterwards. If you want to know whether zip_dir_add has an error, you need to check it's own return value, see its documentation: https://libzip.org/documentation/zip_dir_add.html. You can also use the zip_get_error() function to check the latest error, see documentation: https://libzip.org/documentation/zip_get_error.html.

Upvotes: 1

Related Questions