Skip to content

Commit

Permalink
[Test] Avoid opening the archive file in the OpenCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
rikyoz committed Dec 9, 2023
1 parent 5ad9678 commit facc53f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 43 deletions.
4 changes: 2 additions & 2 deletions src/bitinputarchive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,14 @@ BitInputArchive::BitInputArchive( const BitAbstractArchiveHandler& handler, cons
: mDetectedFormat{ &handler.format() }, // if auto, detect the format from content, otherwise try the passed format.
mArchiveHandler{ handler } {
auto bufStream = bit7z::make_com< CBufferInStream, IInStream >( inBuffer );
mInArchive = openArchiveStream( BIT7Z_STRING( "." ), bufStream );
mInArchive = openArchiveStream( fs::path{}, bufStream );
}

BitInputArchive::BitInputArchive( const BitAbstractArchiveHandler& handler, std::istream& inStream )
: mDetectedFormat{ &handler.format() }, // if auto, detect the format from content, otherwise try the passed format.
mArchiveHandler{ handler } {
auto stdStream = bit7z::make_com< CStdInStream, IInStream >( inStream );
mInArchive = openArchiveStream( BIT7Z_STRING( "." ), stdStream );
mInArchive = openArchiveStream( fs::path{}, stdStream );
}

auto BitInputArchive::archiveProperty( BitProperty property ) const -> BitPropVariant {
Expand Down
49 changes: 11 additions & 38 deletions src/internal/opencallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@
#include "internal/stringutil.hpp"
#include "internal/util.hpp"

#include <utility>

namespace bit7z {

OpenCallback::OpenCallback( const BitAbstractArchiveHandler& handler, const fs::path& filename )
: Callback( handler ), mSubArchiveMode( false ), mFileItem( filename ), mPasswordWasAsked{ false } {}
OpenCallback::OpenCallback( const BitAbstractArchiveHandler& handler, fs::path archivePath )
: Callback( handler ),
mSubArchiveMode( false ),
mArchivePath{ std::move( archivePath ) },
mPasswordWasAsked{ false } {}

COM_DECLSPEC_NOTHROW
STDMETHODIMP OpenCallback::SetTotal( const UInt64* /* files */, const UInt64* /* bytes */ ) noexcept {
Expand All @@ -38,40 +43,11 @@ STDMETHODIMP OpenCallback::SetCompleted( const UInt64* /* files */, const UInt64
COM_DECLSPEC_NOTHROW
STDMETHODIMP OpenCallback::GetProperty( PROPID property, PROPVARIANT* value ) noexcept try {
BitPropVariant prop;
if ( mSubArchiveMode ) {
if ( property == kpidName ) {
prop = mSubArchiveName;
// case kpidSize: prop = _subArchiveSize; break; // we don't use it for now.
}
} else {
switch ( property ) {
case kpidName:
prop = path_to_wide_string( mFileItem.filesystemName() );
break;
case kpidIsDir:
prop = mFileItem.isDir();
break;
case kpidSize:
prop = mFileItem.size();
break;
case kpidAttrib:
prop = mFileItem.attributes();
break;
case kpidCTime:
prop = mFileItem.creationTime();
break;
case kpidATime:
prop = mFileItem.lastAccessTime();
break;
case kpidMTime:
prop = mFileItem.lastWriteTime();
break;
default: //prop is empty
break;
}
if ( property == kpidName ) {
prop = mSubArchiveMode ? mSubArchiveName : path_to_wide_string( mArchivePath.filename() );
}
*value = prop;
prop.bstrVal = nullptr;
prop.bstrVal = nullptr; // NOLINT(*-pro-type-union-access)
return S_OK;
} catch ( const BitException& ex ) {
return ex.hresultCode();
Expand All @@ -84,10 +60,7 @@ STDMETHODIMP OpenCallback::GetStream( const wchar_t* name, IInStream** inStream
if ( mSubArchiveMode ) {
return S_FALSE;
}
if ( mFileItem.isDir() ) {
return S_FALSE;
}
fs::path streamPath = mFileItem.filesystemPath();
fs::path streamPath = mArchivePath;
if ( name != nullptr ) {
streamPath = streamPath.parent_path();
streamPath.append( name );
Expand Down
5 changes: 2 additions & 3 deletions src/internal/opencallback.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ class OpenCallback final : public IArchiveOpenCallback,
public ICryptoGetTextPassword,
public Callback {
public:
explicit OpenCallback( const BitAbstractArchiveHandler& handler,
const fs::path& filename = BIT7Z_STRING( "." ) );
explicit OpenCallback( const BitAbstractArchiveHandler& handler, fs::path archivePath = fs::path{} );

OpenCallback( const OpenCallback& ) = delete;

Expand Down Expand Up @@ -72,7 +71,7 @@ class OpenCallback final : public IArchiveOpenCallback,
private:
bool mSubArchiveMode;
std::wstring mSubArchiveName;
FilesystemItem mFileItem;
fs::path mArchivePath;
bool mPasswordWasAsked;
};

Expand Down

0 comments on commit facc53f

Please sign in to comment.