MLBookProc 2.1
Loading...
Searching...
No Matches
LibArchive Class Reference

The LibArchive class. More...

#include <LibArchive.h>

Inheritance diagram for LibArchive:

Public Member Functions

 LibArchive (const std::shared_ptr< MLBookProc > &mlbp)
 LibArchive constructor.
void listFilesInArchive (const std::filesystem::path &archive_path, std::vector< std::tuple< std::string, uint64_t, uint64_t > > &result)
void listFilesInArchiveBuffer (const std::string &buffer, std::vector< std::tuple< std::string, uint64_t, uint64_t > > &result)
void listFilesInZip (const std::filesystem::path &archive_path, std::vector< std::tuple< std::string, uint64_t, uint64_t > > &result)
void listFilesInZipBuffer (const std::string &buffer, std::vector< std::tuple< std::string, uint64_t, uint64_t > > &result)
std::filesystem::path unpackFileToDirectory (const std::filesystem::path &archive_path, const std::string &filename, const std::filesystem::path &directory, const size_t &offset=size_t(0))
std::filesystem::path unpackBufferFileToDirectory (const std::string &buffer, const std::string &filename, const std::filesystem::path &directory, const size_t &offset=size_t(0))
std::filesystem::path unpackZipFileToDirectory (const std::filesystem::path &archive_path, const std::string &filename, const std::filesystem::path &directory)
std::filesystem::path unpackZipBufferFileToDirectory (const std::string &buffer, const std::string &filename, const std::filesystem::path &directory)
std::string unpackFileToBuffer (const std::filesystem::path &archive_path, const std::string &filename, const size_t &offset=size_t(0))
std::string unpackBufferFileToBuffer (const std::string &buffer, const std::string &filename, const size_t &offset=size_t(0))
std::string unpackZipFileToBuffer (const std::filesystem::path &archive_path, const std::string &filename)
std::string unpackZipBufferFileToBuffer (const std::string &buffer, const std::string &filename)
void writeToArchive (const std::filesystem::path &source_object, const std::filesystem::path &archive_path, std::string name_in_archive, const std::filesystem::perms &perms=std::filesystem::perms::none, const bool &overwrite_existing=true)
void writeBufferObjectToArchive (const std::string &buffer, const std::filesystem::path &archive_path, std::string name_in_archive, const std::filesystem::perms &perms, const bool &overwrite_existing)

Protected Member Functions

std::shared_ptr< archive > initForReading (const std::shared_ptr< LibArchiveFileData > &fd)
std::shared_ptr< archive > initForWriting (const std::shared_ptr< LibArchiveFileData > &fd)
void archiveError (const std::shared_ptr< archive > &a, const std::string &message)
std::filesystem::path unpackToDirectory (std::shared_ptr< LibArchiveFileData > fd, const std::string &filename, const std::filesystem::path &directory)
void unpackToBuffer (std::shared_ptr< LibArchiveFileData > fd, const std::string &filename, std::string &result)
void unpackEntryToDirectory (std::shared_ptr< archive > a, std::shared_ptr< archive_entry > e, const std::filesystem::path &file_path)
std::string unpackEntryToBuffer (std::shared_ptr< archive > a, std::shared_ptr< archive_entry > e)
void writeFile (std::shared_ptr< archive > a, const std::filesystem::path &path, std::string name_in_archive, const std::filesystem::perms &perms)
void writeBufferToArchive (std::shared_ptr< archive > a, std::shared_ptr< archive_entry > e, const std::string &buf)
std::filesystem::perms getPermissionsFromEntry (const std::shared_ptr< archive_entry > &e)
void setUsernameGroupname (std::shared_ptr< archive_entry > e)

Static Protected Member Functions

static int openCallBack (archive *a, void *client_data)
static la_ssize_t readCallBack (archive *a, void *client_data, const void **buffer)
static la_int64_t skipCallback (archive *a, void *client_data, la_int64_t request)
static int closeCallback (archive *a, void *client_data)
static la_int64_t seekCallback (archive *a, void *client_data, la_int64_t offset, int whence)
static la_ssize_t writeCallback (archive *a, void *client_data, const void *buffer, size_t length)

Protected Attributes

std::shared_ptr< MLBookProc > mlbp

Detailed Description

The LibArchive class.

This class contains methods to work with archives.

Constructor & Destructor Documentation

◆ LibArchive()

LibArchive::LibArchive ( const std::shared_ptr< MLBookProc > & mlbp)

LibArchive constructor.

Parameters
mlbpSmart pointer to MLBookProc object.

Member Function Documentation

◆ archiveError()

void LibArchive::archiveError ( const std::shared_ptr< archive > & a,
const std::string & message )
protected

Handles libarchive errors.

This method throws std::exception in all cases.

Parameters
aSmart pointer to archive object (see libarchive documentation for details).
messageMessage to be prepended to libarchive error message.

◆ closeCallback()

int LibArchive::closeCallback ( archive * a,
void * client_data )
staticprotected

Close callback function for libarchive.

Parameters
aPointer to archive object (see libarchive documentation for details).
client_dataPointer to client data.
Returns
ARCHIVE_OK in case of success, ARCHIVE_FATAL otherwise.

◆ getPermissionsFromEntry()

std::filesystem::perms LibArchive::getPermissionsFromEntry ( const std::shared_ptr< archive_entry > & e)
protected

Returns permissions read from entry.

Parameters
eSmart pointer to archive_entry object (see libarchive documentation for details).
Returns

◆ initForReading()

std::shared_ptr< archive > LibArchive::initForReading ( const std::shared_ptr< LibArchiveFileData > & fd)
protected

Initializes archive for reading.

Note
This method can throw std::exception in case of errors.
Parameters
fdSmart pointer to LibArchiveFileData object.
Returns
Smart pointer to archive object (see libarchive documentation for details).

◆ initForWriting()

std::shared_ptr< archive > LibArchive::initForWriting ( const std::shared_ptr< LibArchiveFileData > & fd)
protected

Initializes archive for writing.

Note
This method can throw std::exception in case of errors.
Parameters
fdSmart pointer to LibArchiveFileData object.
Returns
Smart pointer to archive object (see libarchive documentation for details).

◆ listFilesInArchive()

void LibArchive::listFilesInArchive ( const std::filesystem::path & archive_path,
std::vector< std::tuple< std::string, uint64_t, uint64_t > > & result )

Obtains entries from archive.

Note
This method can throw std::exception in case of errors.
Parameters
archive_pathPath to archive.
resultVector of obtained entries. First element of tuple - name of object in archive, second - size of unpacked object, third - offset of entry in archive file (this method sets to 0 in all cases).

◆ listFilesInArchiveBuffer()

void LibArchive::listFilesInArchiveBuffer ( const std::string & buffer,
std::vector< std::tuple< std::string, uint64_t, uint64_t > > & result )

Same as listFilesInArchive(), but obtains entiries from archive placed in buffer.

Note
This method can throw std::exception in case of errors.
Parameters
bufferArchive file content.
resultVector of obtained entries. First element of tuple - name of object in archive, second - size of unpacked object, third - offset of entry in archive file (this method sets to 0 in all cases).

◆ listFilesInZip()

void LibArchive::listFilesInZip ( const std::filesystem::path & archive_path,
std::vector< std::tuple< std::string, uint64_t, uint64_t > > & result )

Same as listFilesInArchive(), but designed specially for zip archives. This method can be used to other archives also, but it uses listFilesInArchive() in those cases.

Note
This method can throw std::exception in case of errors.
Parameters
archive_pathPath to archive.
resultVector of obtained entries. First element of tuple - name of object in archive, second - size of unpacked object, third - offset of entry in archive file (will be set to 0 in case of non zip archives).

◆ listFilesInZipBuffer()

void LibArchive::listFilesInZipBuffer ( const std::string & buffer,
std::vector< std::tuple< std::string, uint64_t, uint64_t > > & result )

Same as listFilesInZip(), but but obtains entiries from archive placed in buffer.

Note
This method can throw std::exception in case of errors.
Parameters
bufferArchive file content.
resultVector of obtained entries. First element of tuple - name of object in archive, second - size of unpacked object, third - offset of entry in archive file (will be set to 0 in case of non zip archives).

◆ openCallBack()

int LibArchive::openCallBack ( archive * a,
void * client_data )
staticprotected

Open callback function for libarchive.

Parameters
aPointer to archive object (see libarchive documentation for details).
client_dataPointer to client data.
Returns
ARCHIVE_OK if the underlying file or data source is successfully opened. If the open fails, returns ARCHIVE_FATAL.

◆ readCallBack()

la_ssize_t LibArchive::readCallBack ( archive * a,
void * client_data,
const void ** buffer )
staticprotected

Read callback function for libarchive.

Parameters
aPointer to archive object (see libarchive documentation for details).
client_dataPointer to client data.
bufferPointer to buffer.
Returns
Number of actually read bytes.

◆ seekCallback()

la_int64_t LibArchive::seekCallback ( archive * a,
void * client_data,
la_int64_t offset,
int whence )
staticprotected

Seek callback function for libarchive.

Parameters
aPointer to archive object (see libarchive documentation for details).
client_dataPointer to client data.
offsetRequested offset.
whenceThe possibilities for the third argument to fseek.
Returns
Actual position in file or ARCHIVE_FATAL in case of errors.

◆ setUsernameGroupname()

void LibArchive::setUsernameGroupname ( std::shared_ptr< archive_entry > e)
protected

Sets user id, group id (only Linux), user name and group name to entry.

Parameters
eSmart pointer to archive_entry object (see libarchive documentation for details).

◆ skipCallback()

la_int64_t LibArchive::skipCallback ( archive * a,
void * client_data,
la_int64_t request )
staticprotected

Skip callback function for libarchive.

Parameters
aPointer to archive object (see libarchive documentation for details).
client_dataPointer to client data.
requestNumber of bytes to skip.
Returns
Number of actually skipped bytes.

◆ unpackBufferFileToBuffer()

std::string LibArchive::unpackBufferFileToBuffer ( const std::string & buffer,
const std::string & filename,
const size_t & offset = size_t(0) )

Same as unpackFileToBuffer(), but uses buffered archive as source.

Note
This method can throw std::exception in case of errors.
Parameters
bufferArchive file content.
filenameName of file in archive.
offsetOffset of file entry in archive (should be set only in case of zip archives).
Returns
Buffer containing unpacked file.

◆ unpackBufferFileToDirectory()

std::filesystem::path LibArchive::unpackBufferFileToDirectory ( const std::string & buffer,
const std::string & filename,
const std::filesystem::path & directory,
const size_t & offset = size_t(0) )

Same as unpackFileToDirectory(), but uses buffered archive.

Note
This method can throw std::exception in case of errors.
Parameters
bufferArchive file content.
filenameName of file in archive.
directoryPath to directory file should be unpacked to.
offsetOffset of file entry in archive (should be set only in case of zip archives).
Returns
Absolute path to unpacked file.

◆ unpackEntryToBuffer()

std::string LibArchive::unpackEntryToBuffer ( std::shared_ptr< archive > a,
std::shared_ptr< archive_entry > e )
protected

Unpacks single entry to buffer.

Parameters
aSmart pointer to archive object (see libarchive documentation for details).
eSmart pointer to archive_entry object (see libarchive documentation for details).
Returns
Buffer containing result (empty if entry is not a file).

◆ unpackEntryToDirectory()

void LibArchive::unpackEntryToDirectory ( std::shared_ptr< archive > a,
std::shared_ptr< archive_entry > e,
const std::filesystem::path & file_path )
protected

Unpacks single entry to directory.

Parameters
aSmart pointer to archive object (see libarchive documentation for details).
eSmart pointer to archive_entry object (see libarchive documentation for details).
file_pathPath entry should be unpacked to.

◆ unpackFileToBuffer()

std::string LibArchive::unpackFileToBuffer ( const std::filesystem::path & archive_path,
const std::string & filename,
const size_t & offset = size_t(0) )

Unpacks file to buffer.

Note
This method can throw std::exception in case of errors.
Parameters
archive_pathPath to archive.
filenameName of file in archive.
offsetOffset of file entry in archive (should be set only in case of zip archives).
Returns
Buffer containing unpacked file.

◆ unpackFileToDirectory()

std::filesystem::path LibArchive::unpackFileToDirectory ( const std::filesystem::path & archive_path,
const std::string & filename,
const std::filesystem::path & directory,
const size_t & offset = size_t(0) )

Unpacks single file from archive to given directory.

Note
This method can throw std::exception in case of errors.
Parameters
archive_pathPath to archive.
filenameName of file in archive.
directoryPath to directory file should be unpacked to.
offsetOffset of file entry in archive (should be set only in case of zip archives).
Returns
Absolute path to unpacked file.

◆ unpackToBuffer()

void LibArchive::unpackToBuffer ( std::shared_ptr< LibArchiveFileData > fd,
const std::string & filename,
std::string & result )
protected

Unpacks file to buffer.

Note
This method can throw std::exception in case of errors.
Parameters
fdSmart pointer to LibArchiveFileData object.
filenameFile to be unpacked.
resultBuffer file to be unpacked to.

◆ unpackToDirectory()

std::filesystem::path LibArchive::unpackToDirectory ( std::shared_ptr< LibArchiveFileData > fd,
const std::string & filename,
const std::filesystem::path & directory )
protected

Unpacks file to directory.

Note
This method can throw std::exception in case of errors.
Parameters
fdSmart pointer to LibArchiveFileData object.
filenameFile to be unpacked.
directoryDirectory file to be unpacked to.
Returns
Absolute path to unpacked file.

◆ unpackZipBufferFileToBuffer()

std::string LibArchive::unpackZipBufferFileToBuffer ( const std::string & buffer,
const std::string & filename )

Same as unpackZipFileToBuffer(), but uses buffered archive instead.

Note
This method can throw std::exception in case of errors.
Parameters
bufferArchive file content.
filenameName of file in archive.
Returns
Buffer containing unpacked file.

◆ unpackZipBufferFileToDirectory()

std::filesystem::path LibArchive::unpackZipBufferFileToDirectory ( const std::string & buffer,
const std::string & filename,
const std::filesystem::path & directory )

Same as unpackZipFileToDirectory(), but uses buffered archive as source.

Note
This method can throw std::exception in case of errors.
Parameters
bufferArchive file content.
filenameName of file in archive.
directoryPath to directory file should be unpacked to.
Returns
Absolute path to unpacked file.

◆ unpackZipFileToBuffer()

std::string LibArchive::unpackZipFileToBuffer ( const std::filesystem::path & archive_path,
const std::string & filename )

Same as unpackFileToBuffer(), but designed for zip archives specially. This method can be used for other types of archives, but it uses unpackFileToBuffer() in those cases.

Note
This method can throw std::exception in case of errors.
Parameters
archive_pathPath to archive.
filenameName of file in archive.
Returns
Buffer containing unpacked file.

◆ unpackZipFileToDirectory()

std::filesystem::path LibArchive::unpackZipFileToDirectory ( const std::filesystem::path & archive_path,
const std::string & filename,
const std::filesystem::path & directory )

Same as unpackFileToDirectory(), but designed for zip archives specially. This method can be used for other types of archives, but it uses unpackFileToDirectory() in those cases.

Note
This method can throw std::exception in case of errors.
Parameters
archive_pathPath to archive.
filenameName of file in archive.
directoryPath to directory file should be unpacked to.
Returns
Absolute path to unpacked file.

◆ writeBufferObjectToArchive()

void LibArchive::writeBufferObjectToArchive ( const std::string & buffer,
const std::filesystem::path & archive_path,
std::string name_in_archive,
const std::filesystem::perms & perms,
const bool & overwrite_existing )

Same as writeToArchive(), but uses buffer as source.

Note
This method can throw std::exception in case of errors.
Parameters
bufferBuffer to be packed to archive.
archive_pathPath to archive.
name_in_archiveName of file in archive.
permsFile permissions.
overwrite_existingIf true archive will be overwritten. If false archive will be repacked and file will be appended to new archive.

◆ writeBufferToArchive()

void LibArchive::writeBufferToArchive ( std::shared_ptr< archive > a,
std::shared_ptr< archive_entry > e,
const std::string & buf )
protected

Writes buffer to archive.

Parameters
aSmart pointer to archive object (see libarchive documentation for details).
eSmart pointer to archive_entry object (see libarchive documentation for details).
bufBuffer to be packed.

◆ writeCallback()

la_ssize_t LibArchive::writeCallback ( archive * a,
void * client_data,
const void * buffer,
size_t length )
staticprotected

Write callback function for libarchive.

Parameters
aPointer to archive object (see libarchive documentation for details).
client_dataPointer to client data.
bufferBuffer to be written.
lengthSize of given buffer.
Returns
Number of actually written bytes.

◆ writeFile()

void LibArchive::writeFile ( std::shared_ptr< archive > a,
const std::filesystem::path & path,
std::string name_in_archive,
const std::filesystem::perms & perms )
protected

Writes file to archive.

Parameters
aSmart pointer to archive object (see libarchive documentation for details).
pathPath to file to be packed.
name_in_archiveName of file in archive.
permsFile permissions.

◆ writeToArchive()

void LibArchive::writeToArchive ( const std::filesystem::path & source_object,
const std::filesystem::path & archive_path,
std::string name_in_archive,
const std::filesystem::perms & perms = std::filesystem::perms::none,
const bool & overwrite_existing = true )

Writes file to archive.

Note
This method can throw std::exception in case of errors.
Parameters
source_objectFile to be written to archive.
archive_pathPath to archive.
name_in_archiveName of file in archive.
permsFile permissions. In case of std::filesystem::perms::none, source_object permissions will be used.
overwrite_existingIf true archive will be overwritten. If false archive will be repacked and file will be appended to new archive.

Member Data Documentation

◆ mlbp

std::shared_ptr<MLBookProc> LibArchive::mlbp
protected

Smart pointer to MLBookProc object.

Warning
Do not set or modify this object yourself.