add better error checking and support for symlinks to cpack's use of libarchive
This commit is contained in:
parent
79b8d61ae9
commit
4fd75f959a
@ -170,6 +170,15 @@ int cmCPackArchiveGenerator::InitializeInternal()
|
|||||||
int cmCPackArchiveGenerator::CompressFiles(const char* outFileName,
|
int cmCPackArchiveGenerator::CompressFiles(const char* outFileName,
|
||||||
const char* toplevel, const std::vector<std::string>& files)
|
const char* toplevel, const std::vector<std::string>& files)
|
||||||
{
|
{
|
||||||
|
int res = ARCHIVE_OK;
|
||||||
|
#define CHECK_ARCHIVE_ERROR(res, msg) \
|
||||||
|
if(res != ARCHIVE_OK)\
|
||||||
|
{\
|
||||||
|
cmCPackLogger(cmCPackLog::LOG_ERROR, msg \
|
||||||
|
<< archive_error_string(a) \
|
||||||
|
<< "\n"); \
|
||||||
|
return 0; \
|
||||||
|
}
|
||||||
cmCPackLogger(cmCPackLog::LOG_DEBUG, "Toplevel: "
|
cmCPackLogger(cmCPackLog::LOG_DEBUG, "Toplevel: "
|
||||||
<< (toplevel ? toplevel : "(NULL)") << std::endl);
|
<< (toplevel ? toplevel : "(NULL)") << std::endl);
|
||||||
// create a new archive
|
// create a new archive
|
||||||
@ -181,14 +190,16 @@ int cmCPackArchiveGenerator::CompressFiles(const char* outFileName,
|
|||||||
gf->Open(outFileName, false, true);
|
gf->Open(outFileName, false, true);
|
||||||
StreamData data(gf, this);
|
StreamData data(gf, this);
|
||||||
// pass callbacks to archive_write_open to handle stream
|
// pass callbacks to archive_write_open to handle stream
|
||||||
archive_write_open(a,
|
res = archive_write_open(a,
|
||||||
&data,
|
&data,
|
||||||
OpenArchive,
|
OpenArchive,
|
||||||
WriteArchive,
|
WriteArchive,
|
||||||
CloseArchive);
|
CloseArchive);
|
||||||
|
CHECK_ARCHIVE_ERROR(res, "archive_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);
|
res = archive_read_disk_set_standard_lookup(disk);
|
||||||
|
CHECK_ARCHIVE_ERROR(res, "archive_read_disk_set_standard_lookup:");
|
||||||
std::vector<std::string>::const_iterator fileIt;
|
std::vector<std::string>::const_iterator fileIt;
|
||||||
for ( fileIt = files.begin(); fileIt != files.end(); ++ fileIt )
|
for ( fileIt = files.begin(); fileIt != files.end(); ++ fileIt )
|
||||||
{
|
{
|
||||||
@ -198,12 +209,14 @@ int cmCPackArchiveGenerator::CompressFiles(const char* outFileName,
|
|||||||
std::string rp = cmSystemTools::RelativePath(toplevel, fileIt->c_str());
|
std::string rp = cmSystemTools::RelativePath(toplevel, fileIt->c_str());
|
||||||
// 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, "archive_read_disk_entry_from_file:");
|
||||||
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, "archive_write_header:");
|
||||||
|
// the entry size can be 0 if it is a symlink
|
||||||
|
if(archive_entry_size(entry) > 0)
|
||||||
|
{
|
||||||
// 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)
|
||||||
@ -215,14 +228,22 @@ int cmCPackArchiveGenerator::CompressFiles(const char* outFileName,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
char buff[cmCPackTGZ_Data_BlockSize];
|
char buff[cmCPackTGZ_Data_BlockSize];
|
||||||
int len = fread(buff, 1, sizeof(buff), file);
|
size_t len = fread(buff, 1, sizeof(buff), file);
|
||||||
while (len > 0)
|
while (len > 0)
|
||||||
{
|
{
|
||||||
archive_write_data(a, buff, len);
|
size_t wlen = archive_write_data(a, buff, len);
|
||||||
|
if(wlen != len)
|
||||||
|
{
|
||||||
|
cmCPackLogger(cmCPackLog::LOG_ERROR, "archive_write_data(): "
|
||||||
|
<< "tried to write " << len << "\n"
|
||||||
|
<< "write " << wlen << "\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
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
|
||||||
fclose(file);
|
fclose(file);
|
||||||
|
}
|
||||||
archive_entry_free(entry);
|
archive_entry_free(entry);
|
||||||
}
|
}
|
||||||
// close the archive and finish the write
|
// close the archive and finish the write
|
||||||
|
Loading…
x
Reference in New Issue
Block a user