Skip to content

Commit

Permalink
Merge branch 'hotfix/v4.0.6'
Browse files Browse the repository at this point in the history
  • Loading branch information
rikyoz committed Mar 17, 2024
2 parents b602b43 + a88e99e commit d41afbc
Show file tree
Hide file tree
Showing 15 changed files with 115 additions and 46 deletions.
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
cmake_minimum_required( VERSION 3.11 )

project( bit7z
VERSION 4.0.4
VERSION 4.0.6
DESCRIPTION "A C++ static library offering a clean and simple interface to the 7-zip/p7zip shared libraries"
HOMEPAGE_URL "https://github.com/rikyoz/bit7z/" )
set( CMAKE_VERBOSE_MAKEFILE ON CACHE BOOL "ON" )
Expand Down Expand Up @@ -219,6 +219,10 @@ if( WIN32 )
target_link_libraries( ${LIB_TARGET} PUBLIC oleaut32 )
endif()

if( MINGW )
target_link_libraries( ${LIB_TARGET} PUBLIC uuid )
endif()

if( UNIX )
target_compile_definitions( ${LIB_TARGET} PRIVATE _LARGEFILE64_SOURCE _LARGEFILE_SOURCE _REENTRANT
EXTERNAL_CODECS ENV_UNIX BREAK_HANDLER USE_WIN_FILE )
Expand Down
3 changes: 3 additions & 0 deletions cmake/BuildOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ endif()

option( BIT7Z_DISABLE_USE_STD_FILESYSTEM "Disable using the standard filesystem library (always use ghc::filesystem)" )
message( STATUS "Disable using std::filesystem: ${BIT7Z_DISABLE_USE_STD_FILESYSTEM}" )
if( BIT7Z_DISABLE_USE_STD_FILESYSTEM )
target_compile_definitions( ${LIB_TARGET} PUBLIC BIT7Z_DISABLE_USE_STD_FILESYSTEM )
endif()

set( BIT7Z_CUSTOM_7ZIP_PATH "" CACHE STRING "A custom path to the 7-zip source code" )
if( NOT BIT7Z_CUSTOM_7ZIP_PATH STREQUAL "" )
Expand Down
11 changes: 7 additions & 4 deletions include/bit7z/bitdefines.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* and you can't enable them via CMake. */
//#define BIT7Z_AUTO_FORMAT
//#define BIT7Z_AUTO_PREFIX_LONG_PATHS
//#define BIT7Z_DISABLE_USE_STD_FILESYSTEM
//#define BIT7Z_REGEX_MATCHING
//#define BIT7Z_USE_STD_BYTE
//#define BIT7Z_USE_NATIVE_STRING
Expand All @@ -26,11 +27,13 @@
# define BIT7Z_CPP_STANDARD 11
#endif

#if defined( __cpp_lib_filesystem )
# define BIT7Z_USE_STANDARD_FILESYSTEM
#elif BIT7Z_CPP_STANDARD >= 17 && defined( __has_include )
# if __has_include( <filesystem> )
#ifndef BIT7Z_DISABLE_USE_STD_FILESYSTEM
# if defined( __cpp_lib_filesystem )
# define BIT7Z_USE_STANDARD_FILESYSTEM
# elif BIT7Z_CPP_STANDARD >= 17 && defined( __has_include )
# if __has_include( <filesystem> )
# define BIT7Z_USE_STANDARD_FILESYSTEM
# endif
# endif
#endif

Expand Down
4 changes: 2 additions & 2 deletions include/bit7z/bitinputarchive.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,14 +327,14 @@ class BitInputArchive {
*
* @return a reference to the pointed-to element in the archive.
*/
auto operator*() noexcept -> reference;
auto operator*() const noexcept -> reference;

/**
* @brief Accesses the pointed-to element in the archive.
*
* @return a pointer to the pointed-to element in the archive.
*/
auto operator->() noexcept -> pointer;
auto operator->() const noexcept -> pointer;

private:
BitArchiveItemOffset mItemOffset;
Expand Down
4 changes: 2 additions & 2 deletions src/bitinputarchive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,11 +409,11 @@ BitInputArchive::ConstIterator::operator!=( const BitInputArchive::ConstIterator
return !( *this == other );
}

auto BitInputArchive::ConstIterator::operator*() noexcept -> BitInputArchive::ConstIterator::reference {
auto BitInputArchive::ConstIterator::operator*() const noexcept -> BitInputArchive::ConstIterator::reference {
return mItemOffset;
}

auto BitInputArchive::ConstIterator::operator->() noexcept -> BitInputArchive::ConstIterator::pointer {
auto BitInputArchive::ConstIterator::operator->() const noexcept -> BitInputArchive::ConstIterator::pointer {
return &mItemOffset;
}

Expand Down
2 changes: 1 addition & 1 deletion src/bitoutputarchive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ auto BitOutputArchive::initOutArchive() const -> CMyComPtr< IOutArchive > {
if ( mInputArchive == nullptr ) {
newArc = mArchiveCreator.library().initOutArchive( mArchiveCreator.compressionFormat() );
} else {
mInputArchive->initUpdatableArchive( &newArc );
(void)mInputArchive->initUpdatableArchive( &newArc ); // TODO: Handle errors
}
setArchiveProperties( newArc );
return newArc;
Expand Down
5 changes: 5 additions & 0 deletions src/internal/cfileinstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ CFileInStream::CFileInStream( const fs::path& filePath ) : CStdInStream( mFileSt
void CFileInStream::openFile( const fs::path& filePath ) {
mFileStream.open( filePath, std::ios::in | std::ios::binary ); // flawfinder: ignore
if ( mFileStream.fail() ) {
#if defined( __MINGW32__ ) || defined( __MINGW64__ )
std::error_code error{ errno, std::generic_category() };
throw BitException( "Failed to open the archive file", error, path_to_tstring( filePath ) );
#else
throw BitException( "Failed to open the archive file", last_error_code(), path_to_tstring( filePath ) );
#endif
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/internal/cfileoutstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ CFileOutStream::CFileOutStream( fs::path filePath, bool createAlways )
}
mFileStream.open( mFilePath, std::ios::binary | std::ios::trunc ); // flawfinder: ignore
if ( mFileStream.fail() ) {
#if defined( __MINGW32__ ) || defined( __MINGW64__ )
error = std::error_code{ errno, std::generic_category() };
throw BitException( "Failed to open the output file", error, path_to_tstring( mFilePath ) );
#else
throw BitException( "Failed to open the output file", last_error_code(), path_to_tstring( mFilePath ) );
#endif
}

mFileStream.rdbuf()->pubsetbuf( mBuffer.data(), kBufferSize );
Expand Down
9 changes: 8 additions & 1 deletion src/internal/fileextractcallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,16 @@ auto FileExtractCallback::finishOperation( OperationResult operationResult ) ->
return result;
}

if ( mCurrentItem.isModifiedTimeDefined() ) {
#ifdef _WIN32
const auto creationTime = mCurrentItem.hasCreationTime() ? mCurrentItem.creationTime() : FILETIME{};
const auto accessTime = mCurrentItem.hasAccessTime() ? mCurrentItem.accessTime() : FILETIME{};
const auto modifiedTime = mCurrentItem.hasModifiedTime() ? mCurrentItem.modifiedTime() : FILETIME{};
filesystem::fsutil::set_file_time( mFilePathOnDisk, creationTime, accessTime, modifiedTime );
#else
if ( mCurrentItem.hasModifiedTime() ) {
filesystem::fsutil::set_file_modified_time( mFilePathOnDisk, mCurrentItem.modifiedTime() );
}
#endif

if ( mCurrentItem.areAttributesDefined() ) {
filesystem::fsutil::set_file_attributes( mFilePathOnDisk, mCurrentItem.attributes() );
Expand Down
26 changes: 20 additions & 6 deletions src/internal/fsutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,27 +227,41 @@ auto fsutil::set_file_attributes( const fs::path& filePath, DWORD attributes ) n
#endif
}

auto fsutil::set_file_modified_time( const fs::path& filePath, FILETIME ftModified ) noexcept -> bool {
#ifdef _WIN32
auto fsutil::set_file_time( const fs::path& filePath,
FILETIME creation,
FILETIME access,
FILETIME modified ) noexcept -> bool {
if ( filePath.empty() ) {
return false;
}

#ifdef _WIN32
bool res = false;
HANDLE hFile = ::CreateFile( filePath.c_str(), GENERIC_READ | FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ, nullptr,
OPEN_EXISTING, 0, nullptr );
HANDLE hFile = ::CreateFile( filePath.c_str(),
GENERIC_READ | FILE_WRITE_ATTRIBUTES,
FILE_SHARE_READ,
nullptr,
OPEN_EXISTING,
0,
nullptr );
if ( hFile != INVALID_HANDLE_VALUE ) { // NOLINT(cppcoreguidelines-pro-type-cstyle-cast,performance-no-int-to-ptr)
res = ::SetFileTime( hFile, nullptr, nullptr, &ftModified ) != FALSE;
res = ::SetFileTime( hFile, &creation, &access, &modified ) != FALSE;
CloseHandle( hFile );
}
return res;
}
#else
auto fsutil::set_file_modified_time( const fs::path& filePath, FILETIME ftModified ) noexcept -> bool {
if ( filePath.empty() ) {
return false;
}

std::error_code error;
auto fileTime = FILETIME_to_file_time_type( ftModified );
fs::last_write_time( filePath, fileTime, error );
return !error;
#endif
}
#endif

auto fsutil::get_file_attributes_ex( const fs::path& filePath,
SymlinkPolicy symlinkPolicy,
Expand Down
5 changes: 5 additions & 0 deletions src/internal/fsutil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ BIT7Z_NODISCARD auto get_file_attributes_ex( const fs::path& filePath,
SymlinkPolicy symlinkPolicy,
WIN32_FILE_ATTRIBUTE_DATA& fileMetadata ) noexcept -> bool;

#ifdef _WIN32
// TODO: In future, use std::optional instead of empty FILETIME objects.
auto set_file_time( const fs::path& filePath, FILETIME creation, FILETIME access, FILETIME modified ) noexcept -> bool;
#else
auto set_file_modified_time( const fs::path& filePath, FILETIME ftModified ) noexcept -> bool;
#endif

auto set_file_attributes( const fs::path& filePath, DWORD attributes ) noexcept -> bool;

Expand Down
2 changes: 1 addition & 1 deletion src/internal/guids.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
namespace bit7z {

// GUIDs of Interfaces
#ifndef _MSC_VER
#ifndef _WIN32
const GUID IID_IUnknown = {
0x00000000, 0x0000, 0x0000, { 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46 }
};
Expand Down
2 changes: 1 addition & 1 deletion src/internal/guids.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
namespace bit7z {

extern "C" {
#ifndef _MSC_VER
#ifndef _WIN32
extern const GUID IID_IUnknown;
#endif

Expand Down
54 changes: 31 additions & 23 deletions src/internal/processeditem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
namespace bit7z {

ProcessedItem::ProcessedItem()
: mModifiedTime{}, mIsModifiedTimeDefined{ false }, mAttributes{ 0 }, mAreAttributesDefined{ false } {}
: mAttributes{ 0 }, mAreAttributesDefined{ false } {}

void ProcessedItem::loadItemInfo( const BitInputArchive& inputArchive, std::uint32_t itemIndex ) {
loadFilePath( inputArchive, itemIndex );
loadAttributes( inputArchive, itemIndex );
loadModifiedTime( inputArchive, itemIndex );
loadTimeMetadata( inputArchive, itemIndex );
}

auto ProcessedItem::path() const -> fs::path {
Expand All @@ -32,9 +32,31 @@ auto ProcessedItem::attributes() const -> uint32_t {
return mAttributes;
}

auto ProcessedItem::hasModifiedTime() const -> bool {
return mModifiedTime.isFileTime();
}

auto ProcessedItem::modifiedTime() const -> FILETIME {
return mModifiedTime;
return mModifiedTime.getFileTime();
}

#ifdef _WIN32
auto ProcessedItem::hasCreationTime() const -> bool {
return mCreationTime.isFileTime();
}

auto ProcessedItem::creationTime() const -> FILETIME {
return mCreationTime.getFileTime();
}

auto ProcessedItem::hasAccessTime() const -> bool {
return mAccessTime.isFileTime();
}

auto ProcessedItem::accessTime() const -> FILETIME {
return mAccessTime.getFileTime();
}
#endif

void ProcessedItem::loadFilePath( const BitInputArchive& inputArchive, uint32_t itemIndex ) {
const BitPropVariant prop = inputArchive.itemProperty( itemIndex, BitProperty::Path );
Expand Down Expand Up @@ -88,30 +110,16 @@ void ProcessedItem::loadAttributes( const BitInputArchive& inputArchive, uint32_
}
}

void ProcessedItem::loadModifiedTime( const BitInputArchive& inputArchive, uint32_t itemIndex ) {
const BitPropVariant modifiedTime = inputArchive.itemProperty( itemIndex, BitProperty::MTime );

switch ( modifiedTime.type() ) {
case BitPropVariantType::Empty:
mIsModifiedTimeDefined = false;
break;

case BitPropVariantType::FileTime:
mModifiedTime = modifiedTime.getFileTime();
mIsModifiedTimeDefined = true;
break;

default:
throw BitException( "Could not load last modified time of item", make_hresult_code( E_FAIL ) );
}
void ProcessedItem::loadTimeMetadata( const BitInputArchive& inputArchive, uint32_t itemIndex ) {
mModifiedTime = inputArchive.itemProperty( itemIndex, BitProperty::MTime );
#ifdef _WIN32
mCreationTime = inputArchive.itemProperty( itemIndex, BitProperty::CTime );
mAccessTime = inputArchive.itemProperty( itemIndex, BitProperty::ATime );
#endif
}

auto ProcessedItem::areAttributesDefined() const -> bool {
return mAreAttributesDefined;
}

auto ProcessedItem::isModifiedTimeDefined() const -> bool {
return mIsModifiedTimeDefined;
}

} // namespace bit7z
23 changes: 19 additions & 4 deletions src/internal/processeditem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#define PROCESSEDITEM_HPP

#include "bitinputarchive.hpp"
#include "bitdefines.hpp"
#include "bitpropvariant.hpp"
#include "internal/fs.hpp"
#include "internal/windows.hpp"

Expand All @@ -28,15 +30,28 @@ class ProcessedItem final {

BIT7Z_NODISCARD auto areAttributesDefined() const -> bool;

BIT7Z_NODISCARD auto hasModifiedTime() const -> bool;

BIT7Z_NODISCARD auto modifiedTime() const -> FILETIME;

BIT7Z_NODISCARD auto isModifiedTimeDefined() const -> bool;
#ifdef _WIN32
BIT7Z_NODISCARD auto hasCreationTime() const -> bool;

BIT7Z_NODISCARD auto creationTime() const -> FILETIME;

BIT7Z_NODISCARD auto hasAccessTime() const -> bool;

BIT7Z_NODISCARD auto accessTime() const -> FILETIME;
#endif

private:
fs::path mFilePath;

FILETIME mModifiedTime;
bool mIsModifiedTimeDefined;
BitPropVariant mModifiedTime;
#ifdef _WIN32
BitPropVariant mCreationTime;
BitPropVariant mAccessTime;
#endif

uint32_t mAttributes;
bool mAreAttributesDefined;
Expand All @@ -45,7 +60,7 @@ class ProcessedItem final {

void loadAttributes( const BitInputArchive& inputArchive, uint32_t itemIndex );

void loadModifiedTime( const BitInputArchive& inputArchive, uint32_t itemIndex );
void loadTimeMetadata( const BitInputArchive& inputArchive, uint32_t itemIndex );
};

} // namespace bit7z
Expand Down

0 comments on commit d41afbc

Please sign in to comment.