Skip to content

Commit

Permalink
1) add AUTHORS COPYING file
Browse files Browse the repository at this point in the history
2) add LIB7ZIP_ prefix to error code enum (break the old client, please update your code )
3) add APIs SetLib7ZipLocale/GetLib7ZipLocale, client could use these api to force lib7zip locale, otherwise lib7zip will use current user's locale
4) add list of path to find 7z.so when 7z.so is not in users ld path
  • Loading branch information
stonewell committed Jan 4, 2013
1 parent e25ec4d commit 38723b1
Show file tree
Hide file tree
Showing 8 changed files with 529 additions and 98 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Maintainer: jingnan si <jingnan.si@gmail.com>
373 changes: 373 additions & 0 deletions COPYING

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions Lib7Zip/7zipLibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ bool C7ZipLibrary::OpenArchive(C7ZipInStream * pInStream, C7ZipArchive ** ppArch
const wstring & passwd)
{
if (!m_bInitialized) {
m_LastError = lib7zip::NOT_INITIALIZE;
m_LastError = lib7zip::LIB7ZIP_NOT_INITIALIZE;
return false;
}

Expand All @@ -132,18 +132,18 @@ bool C7ZipLibrary::OpenArchive(C7ZipInStream * pInStream, C7ZipArchive ** ppArch
if (pHandler != NULL && pHandler->OpenArchive(pInStream, NULL, ppArchive, passwd, &hr))
{
if (*ppArchive)
(*ppArchive)->SetArchivePassword(passwd);
(*ppArchive)->SetArchivePassword(passwd);
m_LastError = HResultToErrorCode(hr);
return true;
}

m_LastError = HResultToErrorCode(hr);

if (m_LastError == lib7zip::NEED_PASSWORD)
if (m_LastError == lib7zip::LIB7ZIP_NEED_PASSWORD)
return false;
}

m_LastError = lib7zip::NOT_SUPPORTED_ARCHIVE;
m_LastError = lib7zip::LIB7ZIP_NOT_SUPPORTED_ARCHIVE;
return false;
}

Expand All @@ -156,7 +156,7 @@ bool C7ZipLibrary::OpenMultiVolumeArchive(C7ZipMultiVolumes * pMultiVolumes, C7Z
const wstring & passwd)
{
if (!m_bInitialized) {
m_LastError = lib7zip::NOT_INITIALIZE;
m_LastError = lib7zip::LIB7ZIP_NOT_INITIALIZE;
return false;
}

Expand All @@ -176,11 +176,11 @@ bool C7ZipLibrary::OpenMultiVolumeArchive(C7ZipMultiVolumes * pMultiVolumes, C7Z

m_LastError = HResultToErrorCode(hr);

if (m_LastError == lib7zip::NEED_PASSWORD)
if (m_LastError == lib7zip::LIB7ZIP_NEED_PASSWORD)
return false;
}

m_LastError = lib7zip::NOT_SUPPORTED_ARCHIVE;
m_LastError = lib7zip::LIB7ZIP_NOT_SUPPORTED_ARCHIVE;
return false;
}

Expand All @@ -193,9 +193,9 @@ static lib7zip::ErrorCodeEnum HResultToErrorCode(HRESULT hr)
{
switch(hr){
case S_OK:
return lib7zip::NO_ERROR;
return lib7zip::LIB7ZIP_NO_ERROR;
case E_NEEDPASSWORD:
return lib7zip::NEED_PASSWORD;
return lib7zip::LIB7ZIP_NEED_PASSWORD;
case S_FALSE:
case E_NOTIMPL:
case E_NOINTERFACE:
Expand All @@ -205,6 +205,6 @@ static lib7zip::ErrorCodeEnum HResultToErrorCode(HRESULT hr)
case E_OUTOFMEMORY:
case E_INVALIDARG:
default:
return lib7zip::UNKNOWN_ERROR;
return lib7zip::LIB7ZIP_UNKNOWN_ERROR;
}
}
23 changes: 22 additions & 1 deletion Lib7Zip/HelperFuncs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include "CPP/7zip/IPassword.h"
#include "CPP/7zip/Common/FileStreams.h"

#include <stdlib.h>

#include <locale>
#include <iostream>
#include <string>
Expand All @@ -38,6 +40,8 @@ bool VARIANT_BOOLToBool(VARIANT_BOOL v) { return (v != VARIANT_FALSE); }

#include "HelperFuncs.h"

static const char * g_lib7zip_loc = NULL;

HRESULT ReadProp(
GetHandlerPropertyFunc getProp,
GetHandlerPropertyFunc2 getProp2,
Expand Down Expand Up @@ -279,7 +283,13 @@ HRESULT GetFilePathExt(const wstring & path, wstring & ext)
wstring WidenString( const string& str )
{
std::wostringstream wstm ;
wstm.imbue(std::locale("en_US.utf8"));
const char * loc =
g_lib7zip_loc == NULL ? setlocale(LC_CTYPE, NULL) : g_lib7zip_loc;

if (loc == NULL || strlen(loc) == 0)
loc = "C";
wstm.imbue(std::locale(loc));

const std::ctype<wchar_t>& ctfacet =
std::use_facet< std::ctype<wchar_t> >( wstm.getloc() ) ;
for( size_t i=0 ; i<str.size() ; ++i )
Expand All @@ -297,3 +307,14 @@ string NarrowString( const wstring& str )
stm << ctfacet.narrow( str[i], 0 ) ;
return stm.str() ;
}

//set locale used by lib7zip, if NULL or not set, lib7zip will use user default locale
const char * GetLib7ZipLocale()
{
return g_lib7zip_loc;
}

const char * SetLib7ZipLocale(const char * loc)
{
g_lib7zip_loc = loc;
}
178 changes: 105 additions & 73 deletions Lib7Zip/OSFunctions_UnixLike.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,124 +37,156 @@ static C7ZipLibrary * g_pLibrary = NULL;

bool LoadDllFromFolder(C7ZipDllHandler * pMainHandler, const wstring & wfolder_name, C7ZipObjectPtrArray & handlers)
{
g_pHandlers = &handlers;
g_pLibrary = pMainHandler->GetLibrary();
g_pHandlers = &handlers;
g_pLibrary = pMainHandler->GetLibrary();

string folder_name = NarrowString(wfolder_name);
string mainHandlerPath = NarrowString(pMainHandler->GetHandlerPath());
string folderPath = mainHandlerPath + "/" + folder_name;
string folder_name = NarrowString(wfolder_name);
string mainHandlerPath = NarrowString(pMainHandler->GetHandlerPath());
string folderPath = mainHandlerPath + "/" + folder_name;

char * current_dir = getcwd(NULL, 0);
char * current_dir = getcwd(NULL, 0);

int result = chdir(folderPath.c_str());
int result = chdir(folderPath.c_str());

struct dirent **namelist = NULL;
struct dirent **namelist = NULL;

if (result == 0) {
scandir( ".", &namelist,myselect,alphasort );
}
if (result == 0) {
scandir( ".", &namelist,myselect,alphasort );
}

result = chdir(current_dir);
result = chdir(current_dir);

free(current_dir);
free(current_dir);

g_pHandlers = NULL;
g_pLibrary = NULL;
return true;
g_pHandlers = NULL;
g_pLibrary = NULL;
return true;
}

int myselect(const struct dirent * pDir )
{
if ( NULL == pDir )
return 0;
if ( NULL == pDir )
return 0;

const char * szEntryName = pDir->d_name;
const char * szEntryName = pDir->d_name;

if ( ( strcasecmp( szEntryName,"." ) == 0 ) ||
( strcasecmp( szEntryName,".." ) == 0 ) )
{
return 0;
}
if ( ( strcasecmp( szEntryName,"." ) == 0 ) ||
( strcasecmp( szEntryName,".." ) == 0 ) )
{
return 0;
}

DIR * pTmpDir = NULL;
DIR * pTmpDir = NULL;

if ( NULL == ( pTmpDir = opendir(szEntryName) ) )
if ( NULL == ( pTmpDir = opendir(szEntryName) ) )
{
if ( errno == ENOTDIR )
{
if ( errno == ENOTDIR )
char * current_path = getcwd(NULL, 0);
string path = current_path;
path += "/";
path += szEntryName;
free(current_path);

void * pHandler = dlopen(path.c_str(), RTLD_LAZY | RTLD_GLOBAL);

if (pHandler != NULL)
{
C7ZipDllHandler * p7ZipHandler = new C7ZipDllHandler(g_pLibrary, pHandler);

if (p7ZipHandler->IsInitialized())
{
char * current_path = getcwd(NULL, 0);
string path = current_path;
path += "/";
path += szEntryName;
free(current_path);

void * pHandler = dlopen(path.c_str(), RTLD_LAZY | RTLD_GLOBAL);

if (pHandler != NULL)
{
C7ZipDllHandler * p7ZipHandler = new C7ZipDllHandler(g_pLibrary, pHandler);

if (p7ZipHandler->IsInitialized())
{
g_pHandlers->push_back(p7ZipHandler);
}
else
{
delete p7ZipHandler;
}
}
g_pHandlers->push_back(p7ZipHandler);
}
else
{
delete p7ZipHandler;
}
}
}
else
{
closedir( pTmpDir );
}
else
{
closedir( pTmpDir );

int result = chdir( szEntryName );
int result = chdir( szEntryName );

struct dirent **namelist = NULL;
struct dirent **namelist = NULL;

scandir( ".",&namelist,myselect,alphasort );
scandir( ".",&namelist,myselect,alphasort );

result = chdir( ".." );
}
result = chdir( ".." );
}

return 0;
return 0;
}

wstring GetHandlerPath(void * pHandler)
{
Dl_info info;
Dl_info info;

memset(&info, 0, sizeof(Dl_info));
memset(&info, 0, sizeof(Dl_info));

if (dladdr((void *)pHandler,&info))
if (dladdr((void *)pHandler,&info))
{
if (info.dli_fname != NULL)
{
if (info.dli_fname != NULL)
{
string path = info.dli_fname;
string path = info.dli_fname;

size_t pos = path.rfind("/");
size_t pos = path.rfind("/");

if (pos != string::npos)
{
return WidenString(path.substr(0, pos));
}
}
if (pos != string::npos)
{
return WidenString(path.substr(0, pos));
}
}
}

return L".";
return L".";
}

HMODULE Load7ZLibrary(const wstring & name)
{
string tmpName = NarrowString(name + L".so");
string tmpName = NarrowString(name + L".so");

HMODULE pHandler = dlopen(tmpName.c_str(), RTLD_LAZY | RTLD_GLOBAL);

if (pHandler)
return pHandler;

size_t pos = tmpName.rfind("/");

if (pos != string::npos)
{
tmpName = tmpName.substr(pos + 1);
}

std::vector<const char *> lib_search_pathlist;

lib_search_pathlist.push_back("/usr/local/lib");
lib_search_pathlist.push_back("/usr/lib");
lib_search_pathlist.push_back("/usr/lib/p7zip");
lib_search_pathlist.push_back("/usr/local/lib/p7zip");
lib_search_pathlist.push_back(".");

for(std::vector<const char *>::iterator lib_search_pathlistIt = lib_search_pathlist.begin();
lib_search_pathlistIt != lib_search_pathlist.end();
lib_search_pathlistIt++)
{
string path_prefix = *lib_search_pathlistIt;
path_prefix += "/";
path_prefix += tmpName;

return dlopen(tmpName.c_str(), RTLD_LAZY | RTLD_GLOBAL);
pHandler = dlopen(path_prefix.c_str(), RTLD_LAZY | RTLD_GLOBAL);
if ( pHandler != NULL)
break;
}
return pHandler;
}

void Free7ZLibrary(HMODULE pModule)
{
dlclose(pModule);
dlclose(pModule);
}

#endif
Loading

0 comments on commit 38723b1

Please sign in to comment.