Skip to content

Commit d1106e5

Browse files
committed
[Issue #394] use durable_write in fio_write
1 parent 8ae217b commit d1106e5

File tree

1 file changed

+39
-42
lines changed

1 file changed

+39
-42
lines changed

src/utils/file.c

+39-42
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,34 @@ fio_fwrite(FILE* f, void const* buf, size_t size)
735735
return fwrite(buf, 1, size, f);
736736
}
737737

738+
/*
739+
* Write buffer to descriptor by calling write(),
740+
* If size of written data is less than buffer size,
741+
* then try to write what is left.
742+
* We do this to get honest errno if there are some problems
743+
* with filesystem, since writing less than buffer size
744+
* is not considered an error.
745+
*/
746+
static ssize_t
747+
durable_write(int fd, const char* buf, size_t size)
748+
{
749+
off_t current_pos = 0;
750+
size_t bytes_left = size;
751+
752+
while (bytes_left > 0)
753+
{
754+
int rc = write(fd, buf + current_pos, bytes_left);
755+
756+
if (rc <= 0)
757+
return rc;
758+
759+
bytes_left -= rc;
760+
current_pos += rc;
761+
}
762+
763+
return size;
764+
}
765+
738766
/* Write data to the file synchronously */
739767
ssize_t
740768
fio_write(int fd, void const* buf, size_t size)
@@ -764,7 +792,7 @@ fio_write(int fd, void const* buf, size_t size)
764792
}
765793
else
766794
{
767-
return write(fd, buf, size);
795+
return durable_write(fd, buf, size);
768796
}
769797
}
770798

@@ -774,7 +802,7 @@ fio_write_impl(int fd, void const* buf, size_t size, int out)
774802
int rc;
775803
fio_header hdr;
776804

777-
rc = write(fd, buf, size);
805+
rc = durable_write(fd, buf, size);
778806

779807
hdr.arg = 0;
780808
hdr.size = 0;
@@ -796,34 +824,6 @@ fio_fwrite_async(FILE* f, void const* buf, size_t size)
796824
: fwrite(buf, 1, size, f);
797825
}
798826

799-
/*
800-
* Write buffer to descriptor by calling write(),
801-
* If size of written data is less than buffer size,
802-
* then try to write what is left.
803-
* We do this to get honest errno if there are some problems
804-
* with filesystem, since writing less than buffer size
805-
* is not considered an error.
806-
*/
807-
static ssize_t
808-
durable_write(int fd, const char* buf, size_t size)
809-
{
810-
off_t current_pos = 0;
811-
size_t bytes_left = size;
812-
813-
while (bytes_left > 0)
814-
{
815-
int rc = write(fd, buf + current_pos, bytes_left);
816-
817-
if (rc <= 0)
818-
return rc;
819-
820-
bytes_left -= rc;
821-
current_pos += rc;
822-
}
823-
824-
return size;
825-
}
826-
827827
/* Write data to the file */
828828
/* TODO: support async report error */
829829
ssize_t
@@ -908,23 +908,22 @@ fio_fwrite_async_compressed(FILE* f, void const* buf, size_t size, int compress_
908908
}
909909
else
910910
{
911-
char uncompressed_buf[BLCKSZ];
912911
char *errormsg = NULL;
913-
int32 uncompressed_size = fio_decompress(uncompressed_buf, buf, size, compress_alg, &errormsg);
912+
char decompressed_buf[BLCKSZ];
913+
int32 decompressed_size = fio_decompress(decompressed_buf, buf, size, compress_alg, &errormsg);
914914

915-
if (uncompressed_size < 0)
915+
if (decompressed_size < 0)
916916
elog(ERROR, "%s", errormsg);
917917

918-
return fwrite(uncompressed_buf, 1, uncompressed_size, f);
918+
return fwrite(decompressed_buf, 1, decompressed_size, f);
919919
}
920920
}
921921

922922
static void
923923
fio_write_compressed_impl(int fd, void const* buf, size_t size, int compress_alg)
924924
{
925-
int rc;
926-
int32 uncompressed_size;
927-
char uncompressed_buf[BLCKSZ];
925+
int32 decompressed_size;
926+
char decompressed_buf[BLCKSZ];
928927

929928
/* If the previous command already have failed,
930929
* then there is no point in bashing a head against the wall
@@ -933,14 +932,12 @@ fio_write_compressed_impl(int fd, void const* buf, size_t size, int compress_alg
933932
return;
934933

935934
/* decompress chunk */
936-
uncompressed_size = fio_decompress(uncompressed_buf, buf, size, compress_alg, &async_errormsg);
935+
decompressed_size = fio_decompress(decompressed_buf, buf, size, compress_alg, &async_errormsg);
937936

938-
if (uncompressed_size < 0)
937+
if (decompressed_size < 0)
939938
return;
940939

941-
rc = write(fd, uncompressed_buf, uncompressed_size);
942-
943-
if (rc <= 0)
940+
if (durable_write(fd, decompressed_buf, decompressed_size) <= 0)
944941
{
945942
async_errormsg = pgut_malloc(ERRMSG_MAX_LEN);
946943
snprintf(async_errormsg, ERRMSG_MAX_LEN, "%s", strerror(errno));

0 commit comments

Comments
 (0)