Skip to content

Commit 4ab860a

Browse files
authored
Merge pull request #352 from sisong/dev
import ftell; used ftell to get opened file's size (replaced stat)
2 parents ae58359 + c251308 commit 4ab860a

File tree

3 files changed

+66
-20
lines changed

3 files changed

+66
-20
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# [HDiffPatch](https://github.com/sisong/HDiffPatch)
2-
[![release](https://img.shields.io/badge/release-v4.6.4-blue.svg)](https://github.com/sisong/HDiffPatch/releases)
2+
[![release](https://img.shields.io/badge/release-v4.6.5-blue.svg)](https://github.com/sisong/HDiffPatch/releases)
33
[![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/sisong/HDiffPatch/blob/master/LICENSE)
44
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-blue.svg)](https://github.com/sisong/HDiffPatch/pulls)
55
[![+issue Welcome](https://img.shields.io/github/issues-raw/sisong/HDiffPatch?color=green&label=%2Bissue%20welcome)](https://github.com/sisong/HDiffPatch/issues)

Diff for: file_for_patch.c

+64-18
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2828
OTHER DEALINGS IN THE SOFTWARE.
2929
*/
30+
#define _LARGEFILE64_SOURCE
31+
#define _FILE_OFFSET_BITS 64
3032
#include "file_for_patch.h"
3133
#include <sys/stat.h> //stat mkdir
3234
/*
@@ -264,8 +266,11 @@ hpatch_BOOL hpatch_setIsExecuteFile(const char* fileName){
264266
#include <unistd.h>
265267
static off64_t _import_lseek64(hpatch_FileHandle file,hpatch_StreamPos_t seekPos,int whence){
266268
int fd;
267-
if (feof(file))
269+
if (feof(file)){
270+
if (whence==SEEK_CUR)
271+
return -1;
268272
rewind(file);
273+
}
269274
setbuf(file,NULL);
270275
fd = fileno(file);
271276
if (fd<0) return -1;
@@ -274,16 +279,15 @@ static off64_t _import_lseek64(hpatch_FileHandle file,hpatch_StreamPos_t seekPos
274279
#endif
275280

276281
hpatch_inline static
277-
hpatch_BOOL _import_fileSeek64(hpatch_FileHandle file,hpatch_StreamPos_t seekPos){
278-
const int whence=SEEK_SET;
282+
hpatch_BOOL _import_fileSeek64(hpatch_FileHandle file,hpatch_StreamPos_t seekPos,int whence){
279283
#ifdef _MSC_VER
280284
return _fseeki64(file,seekPos,whence)==0;
281285
#else
282286
# ifdef ANDROID
283287
# if __ANDROID_API__ >= 24
284288
return fseeko64(file,seekPos,whence)==0;
285289
# else
286-
if (((off64_t)seekPos)==((long)seekPos)) return fseek(file,(long)seekPos,whence)==0;
290+
if ((((off64_t)seekPos)==((long)seekPos))&&(whence==SEEK_SET)) return fseek(file,(long)seekPos,whence)==0;
287291
else return _import_lseek64(file,seekPos,whence)>=0;
288292
# endif
289293
# else
@@ -292,6 +296,29 @@ hpatch_BOOL _import_fileSeek64(hpatch_FileHandle file,hpatch_StreamPos_t seekPos
292296
#endif
293297
}
294298

299+
hpatch_inline static
300+
hpatch_BOOL _import_fileSeek64To(hpatch_FileHandle file,hpatch_StreamPos_t seekPos){
301+
return _import_fileSeek64(file,seekPos,SEEK_SET);
302+
}
303+
304+
hpatch_inline static
305+
hpatch_BOOL _import_fileTell64(hpatch_FileHandle file,hpatch_StreamPos_t* outPos){
306+
#ifdef _MSC_VER
307+
__int64 pos=_ftelli64(file);
308+
#else
309+
# ifdef ANDROID
310+
# if __ANDROID_API__ >= 24
311+
off64_t pos=ftello64(file);
312+
# else
313+
off64_t pos=_import_lseek64(file,0,SEEK_CUR);
314+
# endif
315+
# else
316+
off_t pos=ftello(file);
317+
# endif
318+
#endif
319+
*outPos=(hpatch_StreamPos_t)pos;
320+
return (pos>=0);
321+
}
295322

296323
hpatch_inline static
297324
hpatch_BOOL _import_fileClose(hpatch_FileHandle* pfile){
@@ -355,17 +382,19 @@ hpatch_BOOL _import_fileTruncate(hpatch_FileHandle file,hpatch_StreamPos_t new_f
355382
}*/
356383

357384
#if (_IS_USED_WIN32_UTF8_WAPI)
385+
# define _FileModeType const wchar_t*
358386
# define _kFileReadMode L"rb"
359387
# define _kFileWriteMode L"wb+"
360388
# define _kFileReadWriteMode L"rb+"
361389
#else
390+
# define _FileModeType const char*
362391
# define _kFileReadMode "rb"
363392
# define _kFileWriteMode "wb+"
364393
# define _kFileReadWriteMode "rb+"
365394
#endif
366395

367396
#if (_IS_USED_WIN32_UTF8_WAPI)
368-
static hpatch_FileHandle _import_fileOpenByMode(const char* fileName_utf8,const wchar_t* mode_w){
397+
static hpatch_FileHandle _import_fileOpen(const char* fileName_utf8,_FileModeType mode_w){
369398
wchar_t fileName_w[hpatch_kPathMaxSize];
370399
int wsize=_utf8FileName_to_w(fileName_utf8,fileName_w,hpatch_kPathMaxSize);
371400
if (wsize>0) {
@@ -382,45 +411,62 @@ static hpatch_FileHandle _import_fileOpenByMode(const char* fileName_utf8,const
382411
}
383412
#else
384413
hpatch_inline static
385-
hpatch_FileHandle _import_fileOpenByMode(const char* fileName_utf8,const char* mode){
414+
hpatch_FileHandle _import_fileOpen(const char* fileName_utf8,_FileModeType mode){
386415
return fopen(fileName_utf8,mode); }
387416
#endif
388417

418+
static hpatch_FileHandle _import_fileOpenWithSize(const char* fileName_utf8,_FileModeType mode,hpatch_StreamPos_t* out_fileSize){
419+
hpatch_FileHandle file=0;
420+
file=_import_fileOpen(fileName_utf8,mode);
421+
if ((out_fileSize==0)||(file==0)) return file;
422+
423+
if (_import_fileSeek64(file,0,SEEK_END)){
424+
if (_import_fileTell64(file,out_fileSize)){
425+
if (_import_fileSeek64(file,0,SEEK_SET)){
426+
return file;
427+
}
428+
}
429+
}
430+
431+
//error clear
432+
_import_fileClose(&file);
433+
return 0;
434+
}
435+
436+
static hpatch_inline
389437
hpatch_BOOL _import_fileOpenRead(const char* fileName_utf8,hpatch_FileHandle* out_fileHandle,
390-
hpatch_StreamPos_t* out_fileLength){
438+
hpatch_StreamPos_t* out_fileSize){
391439
hpatch_FileHandle file=0;
392440
assert(out_fileHandle!=0);
393441
if (out_fileHandle==0) { _set_errno_new(EINVAL); return hpatch_FALSE; }
394-
if (out_fileLength!=0){
395-
if (!hpatch_getFileSize(fileName_utf8,out_fileLength)) return hpatch_FALSE;
396-
}
397-
file=_import_fileOpenByMode(fileName_utf8,_kFileReadMode);
442+
file=_import_fileOpenWithSize(fileName_utf8,_kFileReadMode,out_fileSize);
398443
if (file==0) return hpatch_FALSE;
399444
*out_fileHandle=file;
400445
return hpatch_TRUE;
401446
}
402447

448+
static hpatch_inline
403449
hpatch_BOOL _import_fileOpenCreateOrReWrite(const char* fileName_utf8,hpatch_FileHandle* out_fileHandle){
404450
hpatch_FileHandle file=0;
405451
assert(out_fileHandle!=0);
406452
if (out_fileHandle==0) { _set_errno_new(EINVAL); return hpatch_FALSE; }
407-
file=_import_fileOpenByMode(fileName_utf8,_kFileWriteMode);
453+
file=_import_fileOpen(fileName_utf8,_kFileWriteMode);
408454
if (file==0) return hpatch_FALSE;
409455
*out_fileHandle=file;
410456
return hpatch_TRUE;
411457
}
412458

459+
static
413460
hpatch_BOOL _import_fileReopenWrite(const char* fileName_utf8,hpatch_FileHandle* out_fileHandle,
414461
hpatch_StreamPos_t* out_curFileWritePos){
415462
hpatch_FileHandle file=0;
416463
hpatch_StreamPos_t curFileSize=0;
417464
assert(out_fileHandle!=0);
418465
if (out_fileHandle==0) { _set_errno_new(EINVAL); return hpatch_FALSE; }
419-
if (!hpatch_getFileSize(fileName_utf8,&curFileSize)) return hpatch_FALSE;
420-
if (out_curFileWritePos!=0) *out_curFileWritePos=curFileSize;
421-
file=_import_fileOpenByMode(fileName_utf8,_kFileReadWriteMode);
466+
file=_import_fileOpenWithSize(fileName_utf8,_kFileReadWriteMode,&curFileSize);
422467
if (file==0) return hpatch_FALSE;
423-
if (!_import_fileSeek64(file,curFileSize))
468+
if (out_curFileWritePos!=0) *out_curFileWritePos=curFileSize;
469+
if (!_import_fileSeek64To(file,curFileSize))
424470
{ _import_fileClose_No_errno(&file); return hpatch_FALSE; }
425471
*out_fileHandle=file;
426472
return hpatch_TRUE;
@@ -442,7 +488,7 @@ hpatch_BOOL _import_fileReopenWrite(const char* fileName_utf8,hpatch_FileHandle*
442488
if ((readLen>self->base.streamSize)
443489
||(readFromPos>self->base.streamSize-readLen)) _ferr_returnv(EFBIG);
444490
if (self->m_fpos!=readFromPos+self->m_offset){
445-
if (!_import_fileSeek64(self->m_file,readFromPos+self->m_offset)) _rw_ferr_return();
491+
if (!_import_fileSeek64To(self->m_file,readFromPos+self->m_offset)) _rw_ferr_return();
446492
}
447493
if (!_import_fileRead(self->m_file,out_data,out_data+readLen)) _rw_ferr_return();
448494
self->m_fpos=readFromPos+self->m_offset+readLen;
@@ -494,7 +540,7 @@ hpatch_BOOL hpatch_TFileStreamInput_close(hpatch_TFileStreamInput* self){
494540
if (writeToPos!=self->m_fpos){
495541
if (self->is_random_out){
496542
if (!_import_fileFlush(self->m_file)) _rw_ferr_return(); //for lseek64 safe
497-
if (!_import_fileSeek64(self->m_file,writeToPos)) _rw_ferr_return();
543+
if (!_import_fileSeek64To(self->m_file,writeToPos)) _rw_ferr_return();
498544
self->m_fpos=writeToPos;
499545
}else{
500546
_ferr_returnv(ERANGE); //must continue write at self->m_fpos

Diff for: libHDiffPatch/HPatch/patch_types.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ extern "C" {
3838

3939
#define HDIFFPATCH_VERSION_MAJOR 4
4040
#define HDIFFPATCH_VERSION_MINOR 6
41-
#define HDIFFPATCH_VERSION_RELEASE 4
41+
#define HDIFFPATCH_VERSION_RELEASE 5
4242

4343
#define _HDIFFPATCH_VERSION HDIFFPATCH_VERSION_MAJOR.HDIFFPATCH_VERSION_MINOR.HDIFFPATCH_VERSION_RELEASE
4444
#define _HDIFFPATCH_QUOTE(str) #str

0 commit comments

Comments
 (0)