4949# define printf (...)
5050# define _log_info_utf8 (...) do{}while(0)
5151#endif
52+ #ifndef _IS_NEED_PRINT_PROGRESS
53+ # define _IS_NEED_PRINT_PROGRESS _IS_NEED_PRINT_LOG
54+ #endif
5255
5356#ifndef _IS_NEED_MAIN
5457# define _IS_NEED_MAIN 1
@@ -1196,6 +1199,47 @@ static TByte* getPatchMemCache(hpatch_BOOL isLoadOldAll,size_t patchCacheSize,si
11961199 return temp_cache ;
11971200}
11981201
1202+ #if (_IS_NEED_PRINT_PROGRESS )
1203+ typedef struct hpatch_TProgressStreamOutput {
1204+ hpatch_TStreamOutput base ;
1205+ const hpatch_TStreamOutput * streamOutput ;
1206+ double time0 ;
1207+ double progressR ;
1208+ unsigned int progress ;
1209+ } hpatch_TProgressStreamOutput ;
1210+ static hpatch_BOOL _progressStreamOutput_write (const struct hpatch_TStreamOutput * stream ,hpatch_StreamPos_t writeToPos ,
1211+ const unsigned char * data ,const unsigned char * data_end ){
1212+ const char * progressStr = "==============================" ;
1213+ hpatch_TProgressStreamOutput * self = (hpatch_TProgressStreamOutput * )stream -> streamImport ;
1214+ hpatch_BOOL result = self -> streamOutput -> write (self -> streamOutput ,writeToPos ,data ,data_end );
1215+ if (result ){
1216+ unsigned int progress = (unsigned int )((writeToPos + (size_t )(data_end - data ))* self -> progressR );
1217+ hpatch_BOOL isEnd = (progress == 1000 );
1218+ if (progress != self -> progress ){
1219+ double time1 = clock_s ();
1220+ if ((time1 >=self -> time0 + 1.0 /3 )|| isEnd ){
1221+ self -> progress = progress ;
1222+ self -> time0 = time1 ;
1223+ printf ("\r patch progress: [%-30s] %3.1f%%" ,progressStr + ((1000 - progress )* 30 /1000 ),progress * 0.1 );
1224+ if (isEnd ) printf ("\n" );
1225+ }
1226+ }
1227+ }
1228+ return result ;
1229+ }
1230+
1231+ static hpatch_TStreamOutput * _progressStreamInput_wrapper (hpatch_TProgressStreamOutput * self ,const hpatch_TStreamOutput * streamOutput ){
1232+ memset (self ,0 ,sizeof (* self ));
1233+ self -> base .streamImport = self ;
1234+ self -> base .streamSize = streamOutput -> streamSize ;
1235+ self -> base .write = _progressStreamOutput_write ;
1236+ self -> streamOutput = streamOutput ;
1237+ self -> progress = -1 ;
1238+ self -> progressR = 1000.0 /(streamOutput -> streamSize ?streamOutput -> streamSize :1 );
1239+ return & self -> base ;
1240+ }
1241+ #endif //_IS_NEED_PRINT_PROGRESS
1242+
11991243int hpatch (const char * oldFileName ,const char * diffFileName ,const char * outNewFileName ,
12001244 hpatch_BOOL isLoadOldAll ,size_t patchCacheSize ,hpatch_StreamPos_t diffDataOffert ,
12011245 hpatch_StreamPos_t diffDataSize ,hpatch_BOOL vcpatch_isChecksum ,hpatch_BOOL vcpatch_isInMem ,size_t threadNum ){
@@ -1208,9 +1252,13 @@ int hpatch(const char* oldFileName,const char* diffFileName,const char* outNewFi
12081252 hpatch_TFileStreamInput diffData ;
12091253 hpatch_TFileStreamInput oldData ;
12101254 hpatch_TStreamInput * poldData = & oldData .base ;
1255+ hpatch_TStreamOutput * pnewData = & newData .base ;
12111256 TByte * temp_cache = 0 ;
12121257 size_t temp_cache_size ;
12131258 int patch_result = HPATCH_SUCCESS ;
1259+ #if (_IS_NEED_PRINT_PROGRESS )
1260+ hpatch_TProgressStreamOutput _progressStreamOutput ;
1261+ #endif
12141262 hpatch_TFileStreamInput_init (& oldData );
12151263 hpatch_TFileStreamInput_init (& diffData );
12161264 hpatch_TFileStreamOutput_init (& newData );
@@ -1284,10 +1332,13 @@ int hpatch(const char* oldFileName,const char* diffFileName,const char* outNewFi
12841332 temp_cache = getPatchMemCache (isLoadOldAll ,patchCacheSize ,mustAppendMemSize ,maxWindowSize , & temp_cache_size );
12851333 }
12861334 check (temp_cache ,HPATCH_MEM_ERROR ,"alloc cache memory" );
1335+ #if (_IS_NEED_PRINT_PROGRESS )
1336+ pnewData = _progressStreamInput_wrapper (& _progressStreamOutput ,pnewData );
1337+ #endif
12871338#if (_IS_NEED_SINGLE_STREAM_DIFF )
12881339 if (diffInfos .isSingleCompressedDiff ){
12891340 check (temp_cache_size >=diffInfos .sdiffInfo .stepMemSize + hpatch_kStreamCacheSize * 3 ,HPATCH_MEM_ERROR ,"alloc cache memory" );
1290- if (!patch_single_compressed_diff (& newData . base ,poldData ,& diffData .base ,diffInfos .sdiffInfo .diffDataPos ,
1341+ if (!patch_single_compressed_diff (pnewData ,poldData ,& diffData .base ,diffInfos .sdiffInfo .diffDataPos ,
12911342 diffInfos .sdiffInfo .uncompressedSize ,diffInfos .sdiffInfo .compressedSize ,decompressPlugin ,
12921343 diffInfos .sdiffInfo .coverCount ,(size_t )diffInfos .sdiffInfo .stepMemSize ,
12931344 temp_cache ,temp_cache + temp_cache_size ,0 ,threadNum ))
@@ -1296,20 +1347,20 @@ int hpatch(const char* oldFileName,const char* diffFileName,const char* outNewFi
12961347#endif
12971348#if (_IS_NEED_BSDIFF )
12981349 if (diffInfos .isBsDiff ){
1299- if (!bspatch_with_cache (& newData . base ,poldData ,& diffData .base ,decompressPlugin ,
1350+ if (!bspatch_with_cache (pnewData ,poldData ,& diffData .base ,decompressPlugin ,
13001351 temp_cache ,temp_cache + temp_cache_size ))
13011352 patch_result = HPATCH_BSPATCH_ERROR ;
13021353 }else
13031354#endif
13041355#if (_IS_NEED_VCDIFF )
13051356 if (diffInfos .isVcDiff ){
1306- if (!vcpatch_with_cache (& newData . base ,poldData ,& diffData .base ,decompressPlugin ,
1357+ if (!vcpatch_with_cache (pnewData ,poldData ,& diffData .base ,decompressPlugin ,
13071358 vcpatch_isChecksum ,temp_cache ,temp_cache + temp_cache_size ))
13081359 patch_result = HPATCH_VCPATCH_ERROR ;
13091360 }else
13101361#endif
13111362 {
1312- if (!patch_decompress_with_cache (& newData . base ,poldData ,& diffData .base ,decompressPlugin ,
1363+ if (!patch_decompress_with_cache (pnewData ,poldData ,& diffData .base ,decompressPlugin ,
13131364 temp_cache ,temp_cache + temp_cache_size ))
13141365 patch_result = HPATCH_HPATCH_ERROR ;
13151366 }
@@ -1355,6 +1406,9 @@ int hpatch_dir(const char* oldPath,const char* diffFileName,const char* outNewPa
13551406 hpatch_TDecompress _decompressPlugin = {0 };
13561407 const hpatch_TStreamInput * oldStream = 0 ;
13571408 const hpatch_TStreamOutput * newStream = 0 ;
1409+ #if (_IS_NEED_PRINT_PROGRESS )
1410+ hpatch_TProgressStreamOutput _progressStreamOutput ;
1411+ #endif
13581412 hpatch_TFileStreamInput_init (& diffData );
13591413 TDirPatcher_init (& dirPatcher );
13601414 if (oldPath ) assert (0 != strcmp (oldPath ,outNewPath ));
@@ -1445,7 +1499,7 @@ int hpatch_dir(const char* oldPath,const char* diffFileName,const char* outNewPa
14451499 LOG_ERR ("not found checksumType \"%s\" ERROR!\n" ,dirDiffInfo -> checksumType );
14461500 check_on_error (DIRPATCH_CHECKSUMTYPE_ERROR );
14471501 }
1448- printf ("hpatchz run with checksum plugin: \"%s\" (checksumSets:%s%s%s%s)\n" ,dirDiffInfo -> checksumType ,
1502+ printf ("hpatchz run with checksum plugin: \"%s\" (checksumSets:%s%s%s%s)\n\n " ,dirDiffInfo -> checksumType ,
14491503 checksumSet -> isCheck_dirDiffData ?" diff" :"" ,checksumSet -> isCheck_oldRefData ?" old" :"" ,
14501504 checksumSet -> isCheck_newRefData ?" new" :"" ,checksumSet -> isCheck_copyFileData ?" copy" :"" );
14511505 if (!TDirPatcher_checksum (& dirPatcher ,checksumSet ,temp_cache ,temp_cache + temp_cache_size )){
@@ -1469,6 +1523,9 @@ int hpatch_dir(const char* oldPath,const char* diffFileName,const char* outNewPa
14691523 check (TDirPatcher_openNewDirAsStream (& dirPatcher ,& hlistener -> base ,& newStream ),
14701524 DIRPATCH_OPEN_NEWPATH_ERROR ,"open newFile" );
14711525 }
1526+ #if (_IS_NEED_PRINT_PROGRESS )
1527+ newStream = _progressStreamInput_wrapper (& _progressStreamOutput ,newStream );
1528+ #endif
14721529 //patch
14731530 if (!TDirPatcher_patch (& dirPatcher ,newStream ,oldStream ,temp_cache ,temp_cache + temp_cache_size ,threadNum )){
14741531 check_dec (_decompressPlugin .decError );
@@ -1480,6 +1537,7 @@ int hpatch_dir(const char* oldPath,const char* diffFileName,const char* outNewPa
14801537 DIRPATCH_CHECKSUM_NEWDATA_ERROR ,"newFile checksum" );
14811538 check (hpatch_FALSE ,DIRPATCH_PATCH_ERROR ,"dir patch run" );
14821539 }
1540+ printf (" patch ok!\n" );
14831541clear :
14841542 _isInClear = hpatch_TRUE ;
14851543 check (hlistener -> patchFinish (hlistener ,result == HPATCH_SUCCESS ),DIRPATCH_PATCHFINISH_ERROR ,"dir patch finish" );
0 commit comments