Skip to content

Commit

Permalink
old is dir patch ok
Browse files Browse the repository at this point in the history
  • Loading branch information
sisong committed Jan 3, 2019
1 parent 26cd21b commit f35b2e6
Show file tree
Hide file tree
Showing 13 changed files with 208 additions and 100 deletions.
19 changes: 14 additions & 5 deletions dirDiffPatch/dir_diff/dir_diff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
#include <algorithm> //sort
#include <map>
#include <set>
#include "../file_for_dirDiff.h"
#include "../../file_for_patch.h"
#include "../../libHDiffPatch/HDiff/private_diff/mem_buf.h"
#include "../../libHDiffPatch/HDiff/private_diff/limit_mem_diff/adler_roll.h"
#include "../../libHDiffPatch/HDiff/private_diff/pack_uint.h"
#include "../../libHDiffPatch/HDiff/diff.h"
#include "../../file_for_patch.h"
#include "../file_for_dirDiff.h"
#include "../dir_patch/ref_stream.h"
#include "../dir_patch/dir_patch.h"
using namespace hdiff_private;
Expand Down Expand Up @@ -83,6 +83,15 @@ bool IDirFilter::pathNameIs(const std::string& pathName,const char* testPathName
return (nameSize==testSize) || (pathName[nameSize-testSize-1]==kPatch_dirSeparator);
}

void IDirDiffListener::localePathToUtf8(const std::string& path,std::string& out_utf8){
size_t cStrByteSize=localePath_to_utf8(path.c_str(),0,0);
if (cStrByteSize<=0) throw std::runtime_error("path encoding error!");
out_utf8.resize(cStrByteSize);
cStrByteSize=localePath_to_utf8(path.c_str(),&out_utf8[0],&out_utf8[0]+cStrByteSize);
if (cStrByteSize<=0) throw std::runtime_error("path encoding error!");
out_utf8.resize(cStrByteSize-1); //for C string '\0'
}

struct CFileStreamInput:public TFileStreamInput{
inline CFileStreamInput(){ TFileStreamInput_init(this); }
inline void open(const std::string& fileName){
Expand Down Expand Up @@ -458,7 +467,7 @@ void dir_diff(IDirDiffListener* listener,const std::string& oldPath,const std::s
}else{
_pushv(headData);
}
listener->externDataPosInOutStream(writeToPos);
listener->externDataPosInDiffStream(writeToPos);
_pushv(externData);

//diff data
Expand All @@ -473,7 +482,7 @@ void dir_diff(IDirDiffListener* listener,const std::string& oldPath,const std::s
check(newRefStream.stream->read(newRefStream.stream,0,newData,
newData+newRefStream.stream->streamSize),"read new file error!");
_newRefList.clear();//close files
check(newRefStream.stream->read(newRefStream.stream,0,oldData,
check(oldRefStream.stream->read(oldRefStream.stream,0,oldData,
oldData+oldRefStream.stream->streamSize),"read old file error!");
_oldRefList.clear();//close files
std::vector<unsigned char> out_diff;
Expand All @@ -484,7 +493,7 @@ void dir_diff(IDirDiffListener* listener,const std::string& oldPath,const std::s
_pushv(out_diff);
}else{
TOffsetStreamOutput ofStream(outDiffStream,writeToPos);
create_compressed_diff_stream(oldRefStream.stream,newRefStream.stream,&ofStream,
create_compressed_diff_stream(newRefStream.stream,oldRefStream.stream,&ofStream,
streamCompressPlugin,matchValue);
diffDataSize=ofStream.outSize;
}
Expand Down
4 changes: 2 additions & 2 deletions dirDiffPatch/dir_diff/dir_diff.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ struct IDirDiffListener:public IDirFilter{
virtual void runHDiffBegin(){}
virtual void runHDiffEnd(hpatch_StreamPos_t diffDataSize){}
virtual void externData(std::vector<unsigned char>& out_externData){}
virtual void externDataPosInOutStream(hpatch_StreamPos_t externDataPos){}
virtual void localePathToUtf8(const std::string& path,std::string& out_utf8){ out_utf8.assign(path); }
virtual void externDataPosInDiffStream(hpatch_StreamPos_t externDataPos){}
virtual void localePathToUtf8(const std::string& path,std::string& out_utf8);
};

void dir_diff(IDirDiffListener* listener,const std::string& oldPatch,const std::string& newPatch,
Expand Down
126 changes: 123 additions & 3 deletions dirDiffPatch/dir_patch/dir_patch.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
static const char* kVersionType="DirDiff19&";
static const TByte kPatchMode =0;

const size_t kPathMaxSize=1024*2;

#define TUInt hpatch_StreamPos_t

#define check(value) { \
Expand All @@ -46,6 +48,39 @@ static const TByte kPatchMode =0;
#define unpackUIntTo(puint,sclip) \
check(_TStreamCacheClip_unpackUIntWithTag(sclip,puint,0))

char* pushDirPath(char* out_path,char* out_pathEnd,const char* rootDir){
char* result=0; //false
size_t rootDirLen=strlen(rootDir);
hpatch_BOOL isNeedDirSeparator;
if (isAsciiString(rootDir,rootDir+rootDirLen)){
isNeedDirSeparator=(rootDirLen>0)&&(rootDir[rootDirLen-1]!=kPatch_dirSeparator);
}else{
size_t CStrByteSize=localePath_to_utf8(rootDir,out_path,out_pathEnd);
check(CStrByteSize>0);
isNeedDirSeparator=((CStrByteSize-1)>0)&&(out_path[(CStrByteSize-1)-1]!=kPatch_dirSeparator);
}
check(rootDirLen+1+1<=(out_pathEnd-out_path));
memcpy(out_path,rootDir,rootDirLen);
out_path+=rootDirLen;
if (isNeedDirSeparator) *out_path++=kPatch_dirSeparator;
*out_path='\0'; //C string end
result=out_path;
clear:
return result;
}

hpatch_BOOL getPath(char* out_path,char* out_pathEnd,const char* utf8fileName){
hpatch_BOOL result=hpatch_TRUE;
size_t utf8fileNameSize=strlen(utf8fileName);
if (isAsciiString(utf8fileName,utf8fileName+utf8fileNameSize)){
check(utf8fileNameSize+1<=(out_pathEnd-out_path));
memcpy(out_path,utf8fileName,utf8fileNameSize+1);
}else{
check(utf8_to_localePath(utf8fileName,out_path,out_pathEnd)>0);
}
clear:
return result;
}

static void formatDirTagForLoad(char* utf8_path,char* utf8_pathEnd){
if (kPatch_dirSeparator==kPatch_dirSeparator_saved) return;
Expand Down Expand Up @@ -281,12 +316,96 @@ hpatch_BOOL TDirPatcher_loadDirData(TDirPatcher* self,hpatch_TDecompress* decomp
}


hpatch_BOOL TDirPatcher_loadOldRefToMem(const TDirPatcher* self,const char* oldRootPath,
hpatch_BOOL TDirPatcher_loadOldRefToMem(TDirPatcher* self,const char* oldRootPath,
unsigned char* out_buf,unsigned char* out_buf_end){
hpatch_BOOL result=hpatch_TRUE;
size_t refCount=self->dirDiffHead.oldRefFileCount;
hpatch_StreamPos_t sumFSize=0;
char fileName[kPathMaxSize];
char* curFileNamePush=fileName;
TFileStreamInput file;
TFileStreamInput_init(&file);
check(out_buf_end-out_buf==self->dirDiffInfo.hdiffInfo.oldDataSize);
assert(self->_pOldRefMem==0);
curFileNamePush=pushDirPath(curFileNamePush,fileName+kPathMaxSize,oldRootPath);
check(curFileNamePush!=0);

for (size_t i=0; i<refCount;++i){
hpatch_StreamPos_t fSize;
const char* utf8fileName=self->oldUtf8PathList[self->oldRefList[i]];
check(getPath(curFileNamePush,fileName+kPathMaxSize,utf8fileName));
check(TFileStreamInput_open(&file,fileName));
fSize=file.base.streamSize;
sumFSize+=fSize;
check(fSize<=(out_buf_end-out_buf));
check(file.base.read(&file.base,0,out_buf,out_buf+fSize)); out_buf+=fSize;
check(TFileStreamInput_close(&file));
TFileStreamInput_init(&file);
}
check(sumFSize==self->dirDiffInfo.hdiffInfo.oldDataSize);
clear:
return TFileStreamInput_close(&file) & result;
}




static hpatch_BOOL _closeOldRefStream(TDirPatcher* self,const hpatch_TStreamInput** slist,size_t count){
hpatch_BOOL result=hpatch_TRUE;
if (self->_pOldRefMem){
for (size_t i=0; i<count; ++i) {
TFileStreamInput* file=(TFileStreamInput*)slist[i]->streamImport;
result&=TFileStreamInput_close(file);
}
RefStream_close(&self->_oldRefStream);
free(self->_pOldRefMem);
self->_pOldRefMem=0;
}
return result;
}

hpatch_BOOL TDirPatcher_loadOldRefAsStream(TDirPatcher* self,const char* oldRootPath,
const hpatch_TStreamInput** out_oldRefStream){
hpatch_BOOL result=hpatch_TRUE;
size_t refCount=self->dirDiffHead.oldRefFileCount;
size_t memSize=(sizeof(hpatch_TStreamInput**)+sizeof(TFileStreamInput))*refCount;
hpatch_StreamPos_t sumFSize=0;
char fileName[kPathMaxSize];
char* curFileNamePush=fileName;
const hpatch_TStreamInput** slist=0;
TFileStreamInput* flist=0;
assert(self->_pOldRefMem==0);
self->_pOldRefMem=malloc(memSize);
check(self->_pOldRefMem!=0);
curFileNamePush=pushDirPath(curFileNamePush,fileName+kPathMaxSize,oldRootPath);
check(curFileNamePush!=0);

slist=(const hpatch_TStreamInput**)self->_pOldRefMem;
flist=(TFileStreamInput*)(&slist[refCount]);
for (size_t i=0; i<refCount;++i){
TFileStreamInput_init(&flist[i]);
slist[i]=&flist[i].base;
}
for (size_t i=0; i<refCount;++i){
const char* utf8fileName=self->oldUtf8PathList[self->oldRefList[i]];
check(getPath(curFileNamePush,fileName+kPathMaxSize,utf8fileName));
check(TFileStreamInput_open(&flist[i],fileName));
sumFSize+=flist[i].base.streamSize;
}
check(sumFSize==self->dirDiffInfo.hdiffInfo.oldDataSize);

return hpatch_FALSE;
check(RefStream_open(&self->_oldRefStream,slist,refCount));
*out_oldRefStream=self->_oldRefStream.stream;
clear:
if (!result)
_closeOldRefStream(self,slist,slist?refCount:0);
return result;
}

hpatch_BOOL TDirPatcher_closeOldRefStream(TDirPatcher* self){
return _closeOldRefStream(self,self->_oldRefStream._refList,
self->_oldRefStream._rangeCount);
}

hpatch_BOOL TDirPatcher_patch(const TDirPatcher* self,const hpatch_TStreamOutput* out_newData,
const hpatch_TStreamInput* oldData,
Expand All @@ -299,9 +418,10 @@ hpatch_BOOL TDirPatcher_patch(const TDirPatcher* self,const hpatch_TStreamOutput
}

hpatch_BOOL TDirPatcher_close(TDirPatcher* self){
hpatch_BOOL result=TDirPatcher_closeOldRefStream(self);
if (self->_pmem){
free(self->_pmem);
self->_pmem=0;
}
return hpatch_TRUE;
return result;
}
21 changes: 10 additions & 11 deletions dirDiffPatch/dir_patch/dir_patch.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#ifndef DirPatch_dir_patch_h
#define DirPatch_dir_patch_h
#include "../../libHDiffPatch/HPatch/patch_types.h"
#include "ref_stream.h"
#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -89,6 +90,8 @@ typedef struct TDirPatcher{
//private:
const hpatch_TStreamInput* _dirDiffData;
hpatch_TDecompress* _decompressPlugin;
RefStream _oldRefStream;
void* _pOldRefMem;
void* _pmem;
} TDirPatcher;

Expand All @@ -97,22 +100,18 @@ static void TDirPatcher_init(TDirPatcher* self) { memset(self,0,sizeof(*sel
hpatch_BOOL TDirPatcher_open(TDirPatcher* self,const hpatch_TStreamInput* dirDiffData);

hpatch_BOOL TDirPatcher_loadDirData(TDirPatcher* self,hpatch_TDecompress* decompressPlugin);

typedef enum TLoadOldRefResult{
LOAD_OLDREF_SUCCESS=0,
LOAD_OLDREF_OPENREAD_ERROR,
LOAD_OLDREF_FILEREAD_ERROR,
LOAD_OLDREF_FILECLOSE_ERROR,
LOAD_OLDREF_DATASIZE_ERROR,
} TLoadOldRefResult;
TLoadOldRefResult TDirPatcher_loadOldRefToMem(const TDirPatcher* self,const char* oldRootPath,
unsigned char* out_buf,unsigned char* out_buf_end);
TLoadOldRefResult TDirPatcher_loadOldRefAsStream(const TDirPatcher* self,const char* oldRootPath);

hpatch_BOOL TDirPatcher_loadOldRefToMem(TDirPatcher* self,const char* oldRootPath,
unsigned char* out_buf,unsigned char* out_buf_end);
hpatch_BOOL TDirPatcher_loadOldRefAsStream(TDirPatcher* self,const char* oldRootPath,
const hpatch_TStreamInput** out_oldRefStream);

hpatch_BOOL TDirPatcher_patch(const TDirPatcher* self,const hpatch_TStreamOutput* out_newData,
const hpatch_TStreamInput* oldData,
unsigned char* temp_cache,unsigned char* temp_cache_end);

hpatch_BOOL TDirPatcher_closeOldRefStream(TDirPatcher* self);//for TDirPatcher_loadOldRefAsStream

hpatch_BOOL TDirPatcher_close(TDirPatcher* self);


Expand Down
5 changes: 0 additions & 5 deletions dirDiffPatch/dir_patch/ref_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,10 @@
#include <stdio.h>
#include <stdlib.h>

void RefStream_init(RefStream* self){
memset(self,0,sizeof(*self));
}

void RefStream_close(RefStream* self){
if (self->_buf) { free(self->_buf); self->_buf=0; }
}


#define check(value) { \
if (!(value)){ printf(#value" ERROR!\n"); \
result=hpatch_FALSE; assert(hpatch_FALSE); goto clear; } }
Expand Down
5 changes: 3 additions & 2 deletions dirDiffPatch/dir_patch/ref_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ typedef struct RefStream{
unsigned char* _buf;
} RefStream;

void RefStream_init(RefStream* self);
hpatch_inline static
void RefStream_init(RefStream* self) { memset(self,0,sizeof(*self)); }
hpatch_BOOL RefStream_open(RefStream* self,const hpatch_TStreamInput** refList,size_t refCount);
void RefStream_close(RefStream* self);
void RefStream_close(RefStream* self);

#ifdef __cplusplus
}
Expand Down
11 changes: 0 additions & 11 deletions dirDiffPatch/file_for_dirDiff.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,4 @@ void dirClose(TDirHandle dirHandle){
}


static inline //if error return 0 else return outCStringBufSize
size_t localePath_to_utf8(const char* path,char* out_utf8,char* out_utf8BufEnd){
#if (defined(__APPLE__))
return utf8String_to_utf8(path,out_utf8,out_utf8BufEnd);
#else
#warning Path unknown character encoding, probably can not cross-platform
return utf8String_to_utf8(path,out_utf8,out_utf8BufEnd);
#endif
}


#endif
18 changes: 15 additions & 3 deletions dirDiffPatch/file_for_dirPatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ typedef enum TPathType{
kPathType_dir
} TPathType;

static inline
hpatch_inline static
hpatch_BOOL getPathType(const char* path,TPathType* out_type){
assert(out_type!=0);
struct stat s;
Expand Down Expand Up @@ -86,7 +86,7 @@ hpatch_BOOL isSamePath(const char* xPath,const char* yPath){
}
}

static inline //if error return 0 else return outCStringByteSize
hpatch_inline static //if error return 0 else return outCStringByteSize
size_t utf8String_to_utf8(const char* path,char* out_utf8,char* out_utf8BufEnd){
//copy only
size_t size=strlen(path)+1; // with '\0'
Expand All @@ -97,7 +97,7 @@ size_t utf8String_to_utf8(const char* path,char* out_utf8,char* out_utf8BufEnd){
return size;
}

static inline //if error return 0 else return outCStringByteSize
hpatch_inline static //if error return 0 else return outCStringByteSize
size_t utf8_to_localePath(const char* utf8Path,char* out_Path,char* out_PathBufEnd){
#if ( defined(__APPLE__) || defined(__ANDROID__) || defined(__linux__) )
return utf8String_to_utf8(utf8Path,out_Path,out_PathBufEnd);
Expand All @@ -107,4 +107,16 @@ size_t utf8_to_localePath(const char* utf8Path,char* out_Path,char* out_PathBufE
#endif
}


hpatch_inline static //if error return 0 else return outCStringBufSize
size_t localePath_to_utf8(const char* path,char* out_utf8,char* out_utf8BufEnd){
#if (defined(__APPLE__))
return utf8String_to_utf8(path,out_utf8,out_utf8BufEnd);
#else
#warning Path unknown character encoding, probably can not cross-platform
return utf8String_to_utf8(path,out_utf8,out_utf8BufEnd);
#endif
}


#endif
2 changes: 1 addition & 1 deletion file_for_patch.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include <stdlib.h> // malloc free
#include "libHDiffPatch/HPatch/patch_types.h"
typedef unsigned char TByte;
#define kFileIOBestMaxSize (1024*1024)
#define kFileIOBestMaxSize (1<<20)

typedef FILE* hpatch_FileHandle;

Expand Down
Loading

0 comments on commit f35b2e6

Please sign in to comment.