@@ -777,6 +777,34 @@ fio_fwrite(FILE* f, void const* buf, size_t size)
777
777
return fwrite (buf , 1 , size , f );
778
778
}
779
779
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
+
780
808
/* Write data to the file synchronously */
781
809
ssize_t
782
810
fio_write (int fd , void const * buf , size_t size )
@@ -806,7 +834,7 @@ fio_write(int fd, void const* buf, size_t size)
806
834
}
807
835
else
808
836
{
809
- return write (fd , buf , size );
837
+ return durable_write (fd , buf , size );
810
838
}
811
839
}
812
840
@@ -816,7 +844,7 @@ fio_write_impl(int fd, void const* buf, size_t size, int out)
816
844
int rc ;
817
845
fio_header hdr ;
818
846
819
- rc = write (fd , buf , size );
847
+ rc = durable_write (fd , buf , size );
820
848
821
849
hdr .arg = 0 ;
822
850
hdr .size = 0 ;
@@ -838,34 +866,6 @@ fio_fwrite_async(FILE* f, void const* buf, size_t size)
838
866
: fwrite (buf , 1 , size , f );
839
867
}
840
868
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
-
869
869
/* Write data to the file */
870
870
/* TODO: support async report error */
871
871
ssize_t
@@ -950,23 +950,22 @@ fio_fwrite_async_compressed(FILE* f, void const* buf, size_t size, int compress_
950
950
}
951
951
else
952
952
{
953
- char uncompressed_buf [BLCKSZ ];
954
953
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 );
956
956
957
- if (uncompressed_size < 0 )
957
+ if (decompressed_size < 0 )
958
958
elog (ERROR , "%s" , errormsg );
959
959
960
- return fwrite (uncompressed_buf , 1 , uncompressed_size , f );
960
+ return fwrite (decompressed_buf , 1 , decompressed_size , f );
961
961
}
962
962
}
963
963
964
964
static void
965
965
fio_write_compressed_impl (int fd , void const * buf , size_t size , int compress_alg )
966
966
{
967
- int rc ;
968
- int32 uncompressed_size ;
969
- char uncompressed_buf [BLCKSZ ];
967
+ int32 decompressed_size ;
968
+ char decompressed_buf [BLCKSZ ];
970
969
971
970
/* If the previous command already have failed,
972
971
* 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
975
974
return ;
976
975
977
976
/* 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 );
979
978
980
- if (uncompressed_size < 0 )
979
+ if (decompressed_size < 0 )
981
980
return ;
982
981
983
- rc = write (fd , uncompressed_buf , uncompressed_size );
984
-
985
- if (rc <= 0 )
982
+ if (durable_write (fd , decompressed_buf , decompressed_size ) <= 0 )
986
983
{
987
984
async_errormsg = pgut_malloc (ERRMSG_MAX_LEN );
988
985
snprintf (async_errormsg , ERRMSG_MAX_LEN , "%s" , strerror (errno ));
0 commit comments