Skip to content

Commit 6b3aa65

Browse files
committed
[refactoring] unification for some fio_ functions
1 parent 4247f33 commit 6b3aa65

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
@@ -533,17 +533,19 @@ fio_close(int fd)
533533
{
534534
if (fio_is_remote_fd(fd))
535535
{
536-
fio_header hdr;
536+
fio_header hdr = {
537+
.cop = FIO_CLOSE,
538+
.handle = fd & ~FIO_PIPE_MARKER,
539+
.size = 0,
540+
.arg = 0,
541+
};
537542

538-
hdr.cop = FIO_CLOSE;
539-
hdr.handle = fd & ~FIO_PIPE_MARKER;
540-
hdr.size = 0;
541543
fio_fdset &= ~(1 << hdr.handle);
542-
543544
IO_CHECK(fio_write_all(fio_stdout, &hdr, sizeof(hdr)), sizeof(hdr));
544545

545546
/* Wait for response */
546547
IO_CHECK(fio_read_all(fio_stdin, &hdr, sizeof(hdr)), sizeof(hdr));
548+
Assert(hdr.arg == FIO_CLOSE);
547549

548550
if (hdr.arg != 0)
549551
{
@@ -563,10 +565,12 @@ fio_close(int fd)
563565
static void
564566
fio_close_impl(int fd, int out)
565567
{
566-
fio_header hdr;
567-
568-
hdr.cop = FIO_CLOSE;
569-
hdr.arg = 0;
568+
fio_header hdr = {
569+
.cop = FIO_CLOSE,
570+
.handle = -1,
571+
.size = 0,
572+
.arg = 0,
573+
};
570574

571575
if (close(fd) != 0)
572576
hdr.arg = errno;
@@ -592,12 +596,12 @@ fio_truncate(int fd, off_t size)
592596
{
593597
if (fio_is_remote_fd(fd))
594598
{
595-
fio_header hdr;
596-
597-
hdr.cop = FIO_TRUNCATE;
598-
hdr.handle = fd & ~FIO_PIPE_MARKER;
599-
hdr.size = 0;
600-
hdr.arg = size;
599+
fio_header hdr = {
600+
.cop = FIO_TRUNCATE,
601+
.handle = fd & ~FIO_PIPE_MARKER,
602+
.size = 0,
603+
.arg = size,
604+
};
601605

602606
IO_CHECK(fio_write_all(fio_stdout, &hdr, sizeof(hdr)), sizeof(hdr));
603607

@@ -745,17 +749,19 @@ fio_write(int fd, void const* buf, size_t size)
745749
{
746750
if (fio_is_remote_fd(fd))
747751
{
748-
fio_header hdr;
749-
750-
hdr.cop = FIO_WRITE;
751-
hdr.handle = fd & ~FIO_PIPE_MARKER;
752-
hdr.size = size;
752+
fio_header hdr = {
753+
.cop = FIO_WRITE,
754+
.handle = fd & ~FIO_PIPE_MARKER,
755+
.size = size,
756+
.arg = 0,
757+
};
753758

754759
IO_CHECK(fio_write_all(fio_stdout, &hdr, sizeof(hdr)), sizeof(hdr));
755760
IO_CHECK(fio_write_all(fio_stdout, buf, size), size);
756761

757762
/* check results */
758763
IO_CHECK(fio_read_all(fio_stdin, &hdr, sizeof(hdr)), sizeof(hdr));
764+
Assert(hdr.arg == FIO_WRITE);
759765

760766
/* set errno */
761767
if (hdr.arg > 0)
@@ -775,14 +781,16 @@ fio_write(int fd, void const* buf, size_t size)
775781
static void
776782
fio_write_impl(int fd, void const* buf, size_t size, int out)
777783
{
784+
fio_header hdr = {
785+
.cop = FIO_WRITE,
786+
.handle = -1,
787+
.size = 0,
788+
.arg = 0,
789+
};
778790
int rc;
779-
fio_header hdr;
780791

781792
rc = durable_write(fd, buf, size);
782793

783-
hdr.arg = 0;
784-
hdr.size = 0;
785-
786794
if (rc < 0)
787795
hdr.arg = errno;
788796

@@ -810,19 +818,19 @@ fio_write_async(int fd, void const* buf, size_t size)
810818

811819
if (fio_is_remote_fd(fd))
812820
{
813-
fio_header hdr;
814-
815-
hdr.cop = FIO_WRITE_ASYNC;
816-
hdr.handle = fd & ~FIO_PIPE_MARKER;
817-
hdr.size = size;
821+
fio_header hdr = {
822+
.cop = FIO_WRITE_ASYNC,
823+
.handle = fd & ~FIO_PIPE_MARKER,
824+
.size = size,
825+
.arg = 0,
826+
};
818827

819828
IO_CHECK(fio_write_all(fio_stdout, &hdr, sizeof(hdr)), sizeof(hdr));
820829
IO_CHECK(fio_write_all(fio_stdout, buf, size), size);
830+
return size;
821831
}
822832
else
823833
return durable_write(fd, buf, size);
824-
825-
return size;
826834
}
827835

828836
static void
@@ -1019,12 +1027,12 @@ fio_read(int fd, void* buf, size_t size)
10191027
{
10201028
if (fio_is_remote_fd(fd))
10211029
{
1022-
fio_header hdr;
1023-
1024-
hdr.cop = FIO_READ;
1025-
hdr.handle = fd & ~FIO_PIPE_MARKER;
1026-
hdr.size = 0;
1027-
hdr.arg = size;
1030+
fio_header hdr = {
1031+
.cop = FIO_READ,
1032+
.handle = fd & ~FIO_PIPE_MARKER,
1033+
.size = 0,
1034+
.arg = size,
1035+
};
10281036

10291037
IO_CHECK(fio_write_all(fio_stdout, &hdr, sizeof(hdr)), sizeof(hdr));
10301038

@@ -1046,16 +1054,15 @@ fio_stat(fio_location location, const char* path, struct stat* st, bool follow_s
10461054
{
10471055
if (fio_is_remote(location))
10481056
{
1049-
fio_header hdr;
1050-
size_t path_len = strlen(path) + 1;
1051-
1052-
hdr.cop = FIO_STAT;
1053-
hdr.handle = -1;
1054-
hdr.arg = follow_symlink;
1055-
hdr.size = path_len;
1057+
fio_header hdr = {
1058+
.cop = FIO_STAT,
1059+
.handle = -1,
1060+
.size = strlen(path) + 1,
1061+
.arg = follow_symlink,
1062+
};
10561063

10571064
IO_CHECK(fio_write_all(fio_stdout, &hdr, sizeof(hdr)), sizeof(hdr));
1058-
IO_CHECK(fio_write_all(fio_stdout, path, path_len), path_len);
1065+
IO_CHECK(fio_write_all(fio_stdout, path, hdr.size), hdr.size);
10591066

10601067
IO_CHECK(fio_read_all(fio_stdin, &hdr, sizeof(hdr)), sizeof(hdr));
10611068
Assert(hdr.cop == FIO_STAT);

0 commit comments

Comments
 (0)