@@ -1106,8 +1106,6 @@ int zfile_compress(IFile *file, IFile *as, const CompressArgs *args) {
11061106 if (ret < 0 ) {
11071107 LOG_ERRNO_RETURN (0 , -1 , " failed to write header" );
11081108 }
1109- auto raw_data_size = file->lseek (0 , SEEK_END);
1110- LOG_INFO (" source data size: `" , raw_data_size);
11111109 auto block_size = opt.block_size ;
11121110 LOG_INFO (" block size: `" , block_size);
11131111 auto buf_size = block_size + BUF_SIZE;
@@ -1124,39 +1122,41 @@ int zfile_compress(IFile *file, IFile *as, const CompressArgs *args) {
11241122 compressed_len.resize (nbatch);
11251123 raw_chunk_len.resize (nbatch);
11261124 LOG_INFO (" compress with start...." );
1127- off_t i = 0 ;
1128- while (i < raw_data_size ) {
1125+ off_t infile_size = 0 ;
1126+ while (true ) {
11291127 int n = 0 ;
1130- auto step = std::min ((ssize_t )block_size * nbatch, (ssize_t )(raw_data_size - i));
1131- auto ret = file->pread (raw_data, step, i);
1132- if (ret < step) {
1133- LOG_ERRNO_RETURN (0 , -1 , " failed to read from source file. (readn: `)" , ret);
1134- }
1135- i += step;
1136- while (step > 0 ) {
1137- if (step < block_size) {
1138- raw_chunk_len[n++] = step;
1128+ auto readn = file->read (raw_data, block_size * nbatch);
1129+ if (readn == 0 ) {
1130+ break ;
1131+ }
1132+ if (readn < 0 ) {
1133+ LOG_ERRNO_RETURN (0 , -1 , " failed to read from source file. (readn: `)" , readn);
1134+ }
1135+ infile_size += readn;
1136+ while (readn > 0 ) {
1137+ if (readn < block_size) {
1138+ raw_chunk_len[n++] = readn;
11391139 break ;
11401140 }
11411141 raw_chunk_len[n++] = block_size;
1142- step -= block_size;
1142+ readn -= block_size;
11431143 }
1144- ret = compressor->compress_batch (raw_data, &(raw_chunk_len[0 ]), compressed_data,
1144+ readn = compressor->compress_batch (raw_data, &(raw_chunk_len[0 ]), compressed_data,
11451145 n * buf_size, &(compressed_len[0 ]), n);
1146- if (ret != 0 )
1146+ if (readn != 0 )
11471147 return -1 ;
11481148 for (off_t j = 0 ; j < n; j++) {
1149- ret = as->write (&compressed_data[j * buf_size], compressed_len[j]);
1150- if (ret < (ssize_t )compressed_len[j]) {
1149+ readn = as->write (&compressed_data[j * buf_size], compressed_len[j]);
1150+ if (readn < (ssize_t )compressed_len[j]) {
11511151 LOG_ERRNO_RETURN (0 , -1 , " failed to write compressed data." );
11521152 }
11531153 if (crc32_verify) {
11541154 auto crc32_code = crc32c_salt (&compressed_data[j * buf_size], compressed_len[j]);
11551155 LOG_DEBUG (" append ` bytes crc32_code: {offset: `, count: `, crc32: `}" ,
11561156 sizeof (uint32_t ), moffset, compressed_len[j], HEX (crc32_code).width (8 ));
11571157 compressed_len[j] += sizeof (uint32_t );
1158- ret = as->write (&crc32_code, sizeof (uint32_t ));
1159- if (ret < (ssize_t )sizeof (uint32_t )) {
1158+ readn = as->write (&crc32_code, sizeof (uint32_t ));
1159+ if (readn < (ssize_t )sizeof (uint32_t )) {
11601160 LOG_ERRNO_RETURN (0 , -1 , " failed to write crc32code, offset: `, crc32: `" ,
11611161 moffset, HEX (crc32_code).width (8 ));
11621162 }
@@ -1176,8 +1176,8 @@ int zfile_compress(IFile *file, IFile *as, const CompressArgs *args) {
11761176 LOG_INFO (" index checksum: `" , HEX (pht->index_crc ).width (8 ));
11771177 pht->index_offset = index_offset;
11781178 pht->index_size = index_size;
1179- pht->original_file_size = raw_data_size ;
1180- LOG_INFO (" write trailer." );
1179+ pht->original_file_size = infile_size ;
1180+ LOG_INFO (" write trailer. (source file size: `) " , infile_size );
11811181 ret = write_header_trailer (as, false , true , true , pht);
11821182 if (ret < 0 )
11831183 LOG_ERRNO_RETURN (0 , -1 , " failed to write trailer" );
0 commit comments