@@ -46,6 +46,44 @@ static off_t offtin(u_char *buf)
4646 return y ;
4747}
4848
49+ static off_t readFileToBuffer (int fd , uint8_t * buffer , off_t bufferSize )
50+ {
51+ off_t bytesRead = 0 ;
52+ int ret ;
53+ while (bytesRead < bufferSize )
54+ {
55+ ret = read (fd , buffer + bytesRead , bufferSize - bytesRead );
56+ if (ret > 0 )
57+ {
58+ bytesRead += ret ;
59+ }
60+ else
61+ {
62+ break ;
63+ }
64+ }
65+ return bytesRead ;
66+ }
67+
68+ static off_t writeFileFromBuffer (int fd , uint8_t * buffer , off_t bufferSize )
69+ {
70+ off_t bytesWritten = 0 ;
71+ int ret ;
72+ while (bytesWritten < bufferSize )
73+ {
74+ ret = write (fd , buffer + bytesWritten , bufferSize - bytesWritten );
75+ if (ret > 0 )
76+ {
77+ bytesWritten += ret ;
78+ }
79+ else
80+ {
81+ break ;
82+ }
83+ }
84+ return bytesWritten ;
85+ }
86+
4987int bspatch (const char * error , const char * oldfile , const char * newfile , const char * patchfile , void * progressWorker , void (* callback )(off_t , off_t , void * ))
5088{
5189 FILE * f , * cpf , * dpf , * epf ;
@@ -160,7 +198,7 @@ int bspatch(const char* error, const char* oldfile, const char* newfile, const c
160198 ((oldsize = lseek (fd ,0 ,SEEK_END ))== -1 ) ||
161199 ((old = malloc (oldsize + 1 ))== NULL ) ||
162200 (lseek (fd ,0 ,SEEK_SET )!= 0 ) ||
163- (read (fd ,old ,oldsize )!= oldsize ) ||
201+ (readFileToBuffer (fd ,old ,oldsize )!= oldsize ) ||
164202 (close (fd )== -1 )) {
165203 sprintf ((char * )error , "\"%s\" %s" , oldfile , strerror (errno ));
166204 return -1 ;
@@ -243,7 +281,8 @@ int bspatch(const char* error, const char* oldfile, const char* newfile, const c
243281 }
244282
245283 /* Write the new file */
246- if (((fd = open (newfile ,OPEN_FLAGS_CREATE ,0666 ))< 0 ) || (write (fd ,new ,header .newsize )!= header .newsize ) || (close (fd )== -1 ))
284+ if (((fd = open (newfile ,OPEN_FLAGS_CREATE ,0666 ))< 0 ) ||
285+ (writeFileFromBuffer (fd ,new ,header .newsize )!= header .newsize ) || (close (fd )== -1 ))
247286 {
248287 sprintf ((char * )error , "\"%s\" %s" , newfile , strerror (errno ));
249288 return -1 ;
0 commit comments