@@ -777,6 +777,34 @@ fio_fwrite(FILE* f, void const* buf, size_t size)
777777 return fwrite (buf , 1 , size , f );
778778}
779779
780+ /*
781+ * Write buffer to descriptor by calling write(),
782+ * If size of written data is less than buffer size,
783+ * then try to write what is left.
784+ * We do this to get honest errno if there are some problems
785+ * with filesystem, since writing less than buffer size
786+ * is not considered an error.
787+ */
788+ static ssize_t
789+ durable_write (int fd , const char * buf , size_t size )
790+ {
791+ off_t current_pos = 0 ;
792+ size_t bytes_left = size ;
793+
794+ while (bytes_left > 0 )
795+ {
796+ int rc = write (fd , buf + current_pos , bytes_left );
797+
798+ if (rc <= 0 )
799+ return rc ;
800+
801+ bytes_left -= rc ;
802+ current_pos += rc ;
803+ }
804+
805+ return size ;
806+ }
807+
780808/* Write data to the file synchronously */
781809ssize_t
782810fio_write (int fd , void const * buf , size_t size )
@@ -806,7 +834,7 @@ fio_write(int fd, void const* buf, size_t size)
806834 }
807835 else
808836 {
809- return write (fd , buf , size );
837+ return durable_write (fd , buf , size );
810838 }
811839}
812840
@@ -816,7 +844,7 @@ fio_write_impl(int fd, void const* buf, size_t size, int out)
816844 int rc ;
817845 fio_header hdr ;
818846
819- rc = write (fd , buf , size );
847+ rc = durable_write (fd , buf , size );
820848
821849 hdr .arg = 0 ;
822850 hdr .size = 0 ;
@@ -838,34 +866,6 @@ fio_fwrite_async(FILE* f, void const* buf, size_t size)
838866 : fwrite (buf , 1 , size , f );
839867}
840868
841- /*
842- * Write buffer to descriptor by calling write(),
843- * If size of written data is less than buffer size,
844- * then try to write what is left.
845- * We do this to get honest errno if there are some problems
846- * with filesystem, since writing less than buffer size
847- * is not considered an error.
848- */
849- static ssize_t
850- durable_write (int fd , const char * buf , size_t size )
851- {
852- off_t current_pos = 0 ;
853- size_t bytes_left = size ;
854-
855- while (bytes_left > 0 )
856- {
857- int rc = write (fd , buf + current_pos , bytes_left );
858-
859- if (rc <= 0 )
860- return rc ;
861-
862- bytes_left -= rc ;
863- current_pos += rc ;
864- }
865-
866- return size ;
867- }
868-
869869/* Write data to the file */
870870/* TODO: support async report error */
871871ssize_t
@@ -950,23 +950,22 @@ fio_fwrite_async_compressed(FILE* f, void const* buf, size_t size, int compress_
950950 }
951951 else
952952 {
953- char uncompressed_buf [BLCKSZ ];
954953 char * errormsg = NULL ;
955- int32 uncompressed_size = fio_decompress (uncompressed_buf , buf , size , compress_alg , & errormsg );
954+ char decompressed_buf [BLCKSZ ];
955+ int32 decompressed_size = fio_decompress (decompressed_buf , buf , size , compress_alg , & errormsg );
956956
957- if (uncompressed_size < 0 )
957+ if (decompressed_size < 0 )
958958 elog (ERROR , "%s" , errormsg );
959959
960- return fwrite (uncompressed_buf , 1 , uncompressed_size , f );
960+ return fwrite (decompressed_buf , 1 , decompressed_size , f );
961961 }
962962}
963963
964964static void
965965fio_write_compressed_impl (int fd , void const * buf , size_t size , int compress_alg )
966966{
967- int rc ;
968- int32 uncompressed_size ;
969- char uncompressed_buf [BLCKSZ ];
967+ int32 decompressed_size ;
968+ char decompressed_buf [BLCKSZ ];
970969
971970 /* If the previous command already have failed,
972971 * then there is no point in bashing a head against the wall
@@ -975,14 +974,12 @@ fio_write_compressed_impl(int fd, void const* buf, size_t size, int compress_alg
975974 return ;
976975
977976 /* decompress chunk */
978- uncompressed_size = fio_decompress (uncompressed_buf , buf , size , compress_alg , & async_errormsg );
977+ decompressed_size = fio_decompress (decompressed_buf , buf , size , compress_alg , & async_errormsg );
979978
980- if (uncompressed_size < 0 )
979+ if (decompressed_size < 0 )
981980 return ;
982981
983- rc = write (fd , uncompressed_buf , uncompressed_size );
984-
985- if (rc <= 0 )
982+ if (durable_write (fd , decompressed_buf , decompressed_size ) <= 0 )
986983 {
987984 async_errormsg = pgut_malloc (ERRMSG_MAX_LEN );
988985 snprintf (async_errormsg , ERRMSG_MAX_LEN , "%s" , strerror (errno ));
0 commit comments