add much better error checking on libarchive calls.
This commit is contained in:
parent
a73acfbeb9
commit
3d1afdee4a
|
@ -1706,7 +1706,18 @@ bool cmSystemTools::CreateTar(const char* outFileName,
|
||||||
const std::vector<cmStdString>& files,
|
const std::vector<cmStdString>& files,
|
||||||
bool gzip, bool bzip2, bool verbose)
|
bool gzip, bool bzip2, bool verbose)
|
||||||
{
|
{
|
||||||
#if defined(CMAKE_BUILD_WITH_CMAKE)
|
#if defined(CMAKE_BUILD_WITH_CMAKE)
|
||||||
|
|
||||||
|
// Create a macro to handle return from libarchive
|
||||||
|
// functions
|
||||||
|
#define CHECK_ARCHIVE_ERROR(res, msg)\
|
||||||
|
if(res != ARCHIVE_OK)\
|
||||||
|
{\
|
||||||
|
cmSystemTools::Error(msg, \
|
||||||
|
archive_error_string(a));\
|
||||||
|
return false;\
|
||||||
|
}
|
||||||
|
|
||||||
std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
|
std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
|
||||||
// recursively expand all directories in files so that we have a list
|
// recursively expand all directories in files so that we have a list
|
||||||
// of files
|
// of files
|
||||||
|
@ -1747,28 +1758,31 @@ bool cmSystemTools::CreateTar(const char* outFileName,
|
||||||
int res;
|
int res;
|
||||||
// create a new archive
|
// create a new archive
|
||||||
struct archive* a = archive_write_new();
|
struct archive* a = archive_write_new();
|
||||||
|
if(!a)
|
||||||
|
{
|
||||||
|
cmSystemTools::Error("Unable to use create archive");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if(gzip)
|
if(gzip)
|
||||||
{
|
{
|
||||||
res = archive_write_set_compression_gzip(a);
|
res = archive_write_set_compression_gzip(a);
|
||||||
if(res != ARCHIVE_OK)
|
CHECK_ARCHIVE_ERROR(res, "Can not set gzip:");
|
||||||
{
|
|
||||||
cmSystemTools::Error("Unable to use gzip in libarchive");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if(bzip2)
|
if(bzip2)
|
||||||
{
|
{
|
||||||
res = archive_write_set_compression_bzip2(a);
|
res = archive_write_set_compression_bzip2(a);
|
||||||
if(res != ARCHIVE_OK)
|
CHECK_ARCHIVE_ERROR(res, "Can not set bzip2:");
|
||||||
{
|
|
||||||
cmSystemTools::Error("Unable to use bzip2 in libarchive");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
res = archive_write_set_format_ustar(a);
|
|
||||||
if(res != ARCHIVE_OK)
|
|
||||||
{
|
|
||||||
cmSystemTools::Error("Unable to use tar libarchive");
|
|
||||||
}
|
}
|
||||||
archive_write_open_file(a, outFileName);
|
if(!bzip2 && !gzip)
|
||||||
|
{
|
||||||
|
res = archive_write_set_compression_none(a);
|
||||||
|
CHECK_ARCHIVE_ERROR(res, "Can not set none:");
|
||||||
|
}
|
||||||
|
res = archive_write_set_format_ustar(a);
|
||||||
|
CHECK_ARCHIVE_ERROR(res, "Can not set tar format:");
|
||||||
|
res = archive_write_open_file(a, outFileName);
|
||||||
|
CHECK_ARCHIVE_ERROR(res, "write open:");
|
||||||
// create a new disk struct
|
// create a new disk struct
|
||||||
struct archive* disk = archive_read_disk_new();
|
struct archive* disk = archive_read_disk_new();
|
||||||
archive_read_disk_set_standard_lookup(disk);
|
archive_read_disk_set_standard_lookup(disk);
|
||||||
|
@ -1787,12 +1801,13 @@ bool cmSystemTools::CreateTar(const char* outFileName,
|
||||||
}
|
}
|
||||||
// Set the name of the entry to the file name
|
// Set the name of the entry to the file name
|
||||||
archive_entry_set_pathname(entry, rp.c_str());
|
archive_entry_set_pathname(entry, rp.c_str());
|
||||||
// get the information about the file from stat
|
res = archive_read_disk_entry_from_file(disk, entry, -1, 0);
|
||||||
struct stat s;
|
CHECK_ARCHIVE_ERROR(res, "read disk entry:");
|
||||||
stat(fileIt->c_str(), &s);
|
|
||||||
archive_read_disk_entry_from_file(disk, entry, -1, &s);
|
|
||||||
// write entry header
|
// write entry header
|
||||||
archive_write_header(a, entry);
|
res = archive_write_header(a, entry);
|
||||||
|
CHECK_ARCHIVE_ERROR(res, "write header: ");
|
||||||
|
|
||||||
// now copy contents of file into archive a
|
// now copy contents of file into archive a
|
||||||
FILE* file = fopen(fileIt->c_str(), "rb");
|
FILE* file = fopen(fileIt->c_str(), "rb");
|
||||||
if(!file)
|
if(!file)
|
||||||
|
@ -1805,7 +1820,14 @@ bool cmSystemTools::CreateTar(const char* outFileName,
|
||||||
int len = fread(buff, 1, sizeof(buff), file);
|
int len = fread(buff, 1, sizeof(buff), file);
|
||||||
while (len > 0)
|
while (len > 0)
|
||||||
{
|
{
|
||||||
archive_write_data(a, buff, len);
|
int wlen = archive_write_data(a, buff, len);
|
||||||
|
if(wlen != len)
|
||||||
|
{
|
||||||
|
cmSystemTools::Error("Problem with archive_write_data:",
|
||||||
|
archive_error_string(a)
|
||||||
|
);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
len = fread(buff, 1, sizeof(buff), file);
|
len = fread(buff, 1, sizeof(buff), file);
|
||||||
}
|
}
|
||||||
// close the file and free the entry
|
// close the file and free the entry
|
||||||
|
|
Loading…
Reference in New Issue