Thunder
Thunder

Reputation: 631

Zlib returns error code 2 after calling gzopen and gzclose

I have a compress function that looks similar to the code below. It basically takes logs files from my app and compresses them using Zlib. One user reports an error like this: Failed compressing file: 'C:/ProgramData/AppName/logs/cef.log'; error number 2. I really hate C and it's error codes. Can someone point me to the cause of the problem?

string contents;

if ( ! fileContents(source, contents))
    throw FailedCompressionError{"Failed to get the file contents of '" + source + "'."};

gzFile targetFile{gzopen(target.c_str(), "wb")};

const auto result{gzclose(targetFile)};
if (result != Z_OK)
    Logger::Error(
        "Failed closing file: '{}'; error number {}; result {}.",
        target,
        errno,
        result
    );
    
if (targetFile == Z_NULL)
    throw FailedCompressionError{
        "Failed compressing file: '" + source + "'; error number " + to_string(errno) + '.'
    };

const auto contentsLength{static_cast<unsigned>(strlen(contents.c_str()))};
auto result{gzwrite(targetFile, contents.c_str(), contentsLength)};

(...)

Upvotes: 0

Views: 448

Answers (1)

Mark Adler
Mark Adler

Reputation: 112422

That's not a zlib error code. It is a stdio errno code. 2 means file not found.

Upvotes: 1

Related Questions