Reputation: 430
I have an issue when using libzip. I am on linux and have installed the library using sudo apt-get install libzip2 libzip-dev
(so it is not the latest version).
Here is my code :
#include <iostream>
#include <zip.h>
#include <unistd.h>
#include <sys/stat.h>
#define ZIP_ERROR 2
using namespace std;
bool isFilePresent(string const& path)
{
struct stat *buf;
return(stat(path.c_str(), buf)==0);
}
int main(void)
{
struct zip *zip;
struct zip_source *zip_source;
int err(0);
string zipFile("filesZip/zipTest");
string fileToZip("filesToZip/test1");
string fileToZip2("filesToZip/test2");
char tmp[] = "filesZip/zipTest\0";
// Test if the file is present
if(isFilePresent(zipFile))
{
// if(remove(tmp) != 0)
if(remove(zipFile.c_str()) != 0)
{
return ZIP_ERROR;
}
}
// Open a zip archive
zip = zip_open(zipFile.c_str(), ZIP_CREATE, &err);
// if there is an error on the opening
if(err != ZIP_ER_OK)
{
cout << "error when opening" << endl;
return ZIP_ERROR;
}
// If the zip file is not open
if(zip == NULL)
{
zip_close(zip);
cout << "error when zip opens" << endl;
return ZIP_ERROR;
}
// zip_source_file zip a file so that it can be added to the zip
if((zip_source = zip_source_file(zip, fileToZip.c_str(), (off_t)0, (off_t)0))== NULL)
{
zip_close(zip);
zip_source_free(zip_source);
cout << "pb when zipping file1" << endl;
return ZIP_ERROR;
}
// Add the zipped file to the zip
if(zip_add(zip, fileToZip.c_str(), zip_source)==-1)
{
zip_close(zip);
zip_source_free(zip_source);
cout << "pb when adding file1" << endl;
return ZIP_ERROR;
}
// zip_source_file zip a file so that it can be added to the zip
if((zip_source = zip_source_file(zip, fileToZip2.c_str(), (off_t)0, (off_t)0))== NULL)
{
zip_close(zip);
zip_source_free(zip_source);
cout << "pb when zipping file2" << endl;
return ZIP_ERROR;
}
if(zip_add(zip, fileToZip2.c_str(), zip_source)==-1)
{
zip_close(zip);
zip_source_free(zip_source);
cout << "pb when adding file2" << endl;
return ZIP_ERROR;
}
// sleep(180);
// Closing the archive
zip_close(zip);
return 0;
}
This code is supposed to take the two files in the filesToZip folder and compress them in a zipTest file in the filesZip folder.
To do so, first it checks if the zipTest file already exists. If so, then it deletes it. And then it opens a zip archive, zip the files to add and add them to the archive before closing the archive.
So my problem is :
when the zip archive filesZip/zipTest doesnt exist then it works just fine. when the zip archive filesZip/zipTest do exist, then i got a core dumped.
What i have tried so far :
Does anyone have any idea what could be my problem ?
Upvotes: 3
Views: 775
Reputation: 48615
This is dangerous:
bool isFilePresent(string const& path)
{
struct stat *buf;
return(stat(path.c_str(), buf)==0);
}
You don't allocate any memory for your struct stat*
so random memory gets written to when the function is called - likely causing a crash.
Try this:
bool isFilePresent(string const& path)
{
struct stat buf; // memory is allocated on the stack for this object
return(stat(path.c_str(), &buf)==0); // pass its address to the function
}
It creates a local struct stat
object and passes its address to the function.
Upvotes: 4