Skip to content

Commit 9e9e86f

Browse files
committed
[refactoring] unification for some fio_ functions
1 parent a2b7f3b commit 9e9e86f

File tree

1 file changed

+52
-45
lines changed

1 file changed

+52
-45
lines changed

src/utils/file.c

Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -603,17 +603,19 @@ fio_close(int fd)
603603
{
604604
if (fio_is_remote_fd(fd))
605605
{
606-
fio_header hdr;
606+
fio_header hdr = {
607+
.cop = FIO_CLOSE,
608+
.handle = fd & ~FIO_PIPE_MARKER,
609+
.size = 0,
610+
.arg = 0,
611+
};
607612

608-
hdr.cop = FIO_CLOSE;
609-
hdr.handle = fd & ~FIO_PIPE_MARKER;
610-
hdr.size = 0;
611613
fio_fdset &= ~(1 << hdr.handle);
612-
613614
IO_CHECK(fio_write_all(fio_stdout, &hdr, sizeof(hdr)), sizeof(hdr));
614615

615616
/* Wait for response */
616617
IO_CHECK(fio_read_all(fio_stdin, &hdr, sizeof(hdr)), sizeof(hdr));
618+
Assert(hdr.arg == FIO_CLOSE);
617619

618620
if (hdr.arg != 0)
619621
{
@@ -633,10 +635,12 @@ fio_close(int fd)
633635
static void
634636
fio_close_impl(int fd, int out)
635637
{
636-
fio_header hdr;
637-
638-
hdr.cop = FIO_CLOSE;
639-
hdr.arg = 0;
638+
fio_header hdr = {
639+
.cop = FIO_CLOSE,
640+
.handle = -1,
641+
.size = 0,
642+
.arg = 0,
643+
};
640644

641645
if (close(fd) != 0)
642646
hdr.arg = errno;
@@ -662,12 +666,12 @@ fio_truncate(int fd, off_t size)
662666
{
663667
if (fio_is_remote_fd(fd))
664668
{
665-
fio_header hdr;
666-
667-
hdr.cop = FIO_TRUNCATE;
668-
hdr.handle = fd & ~FIO_PIPE_MARKER;
669-
hdr.size = 0;
670-
hdr.arg = size;
669+
fio_header hdr = {
670+
.cop = FIO_TRUNCATE,
671+
.handle = fd & ~FIO_PIPE_MARKER,
672+
.size = 0,
673+
.arg = size,
674+
};
671675

672676
IO_CHECK(fio_write_all(fio_stdout, &hdr, sizeof(hdr)), sizeof(hdr));
673677

@@ -815,17 +819,19 @@ fio_write(int fd, void const* buf, size_t size)
815819
{
816820
if (fio_is_remote_fd(fd))
817821
{
818-
fio_header hdr;
819-
820-
hdr.cop = FIO_WRITE;
821-
hdr.handle = fd & ~FIO_PIPE_MARKER;
822-
hdr.size = size;
822+
fio_header hdr = {
823+
.cop = FIO_WRITE,
824+
.handle = fd & ~FIO_PIPE_MARKER,
825+
.size = size,
826+
.arg = 0,
827+
};
823828

824829
IO_CHECK(fio_write_all(fio_stdout, &hdr, sizeof(hdr)), sizeof(hdr));
825830
IO_CHECK(fio_write_all(fio_stdout, buf, size), size);
826831

827832
/* check results */
828833
IO_CHECK(fio_read_all(fio_stdin, &hdr, sizeof(hdr)), sizeof(hdr));
834+
Assert(hdr.arg == FIO_WRITE);
829835

830836
/* set errno */
831837
if (hdr.arg > 0)
@@ -845,14 +851,16 @@ fio_write(int fd, void const* buf, size_t size)
845851
static void
846852
fio_write_impl(int fd, void const* buf, size_t size, int out)
847853
{
854+
fio_header hdr = {
855+
.cop = FIO_WRITE,
856+
.handle = -1,
857+
.size = 0,
858+
.arg = 0,
859+
};
848860
int rc;
849-
fio_header hdr;
850861

851862
rc = durable_write(fd, buf, size);
852863

853-
hdr.arg = 0;
854-
hdr.size = 0;
855-
856864
if (rc < 0)
857865
hdr.arg = errno;
858866

@@ -880,19 +888,19 @@ fio_write_async(int fd, void const* buf, size_t size)
880888

881889
if (fio_is_remote_fd(fd))
882890
{
883-
fio_header hdr;
884-
885-
hdr.cop = FIO_WRITE_ASYNC;
886-
hdr.handle = fd & ~FIO_PIPE_MARKER;
887-
hdr.size = size;
891+
fio_header hdr = {
892+
.cop = FIO_WRITE_ASYNC,
893+
.handle = fd & ~FIO_PIPE_MARKER,
894+
.size = size,
895+
.arg = 0,
896+
};
888897

889898
IO_CHECK(fio_write_all(fio_stdout, &hdr, sizeof(hdr)), sizeof(hdr));
890899
IO_CHECK(fio_write_all(fio_stdout, buf, size), size);
900+
return size;
891901
}
892902
else
893903
return durable_write(fd, buf, size);
894-
895-
return size;
896904
}
897905

898906
static void
@@ -1089,12 +1097,12 @@ fio_read(int fd, void* buf, size_t size)
10891097
{
10901098
if (fio_is_remote_fd(fd))
10911099
{
1092-
fio_header hdr;
1093-
1094-
hdr.cop = FIO_READ;
1095-
hdr.handle = fd & ~FIO_PIPE_MARKER;
1096-
hdr.size = 0;
1097-
hdr.arg = size;
1100+
fio_header hdr = {
1101+
.cop = FIO_READ,
1102+
.handle = fd & ~FIO_PIPE_MARKER,
1103+
.size = 0,
1104+
.arg = size,
1105+
};
10981106

10991107
IO_CHECK(fio_write_all(fio_stdout, &hdr, sizeof(hdr)), sizeof(hdr));
11001108

@@ -1116,16 +1124,15 @@ fio_stat(fio_location location, const char* path, struct stat* st, bool follow_s
11161124
{
11171125
if (fio_is_remote(location))
11181126
{
1119-
fio_header hdr;
1120-
size_t path_len = strlen(path) + 1;
1121-
1122-
hdr.cop = FIO_STAT;
1123-
hdr.handle = -1;
1124-
hdr.arg = follow_symlink;
1125-
hdr.size = path_len;
1127+
fio_header hdr = {
1128+
.cop = FIO_STAT,
1129+
.handle = -1,
1130+
.size = strlen(path) + 1,
1131+
.arg = follow_symlink,
1132+
};
11261133

11271134
IO_CHECK(fio_write_all(fio_stdout, &hdr, sizeof(hdr)), sizeof(hdr));
1128-
IO_CHECK(fio_write_all(fio_stdout, path, path_len), path_len);
1135+
IO_CHECK(fio_write_all(fio_stdout, path, hdr.size), hdr.size);
11291136

11301137
IO_CHECK(fio_read_all(fio_stdin, &hdr, sizeof(hdr)), sizeof(hdr));
11311138
Assert(hdr.cop == FIO_STAT);

0 commit comments

Comments
 (0)