/*
 * The pattern used out of tree (e.g. percona/pg_tde
 * src/bin/pg_tde_archive_decrypt.c): a PGAlignedXLogBlock as a page buffer
 * for plain I/O on a WAL segment.  With an argument, the same buffer is used
 * with O_DIRECT, the case that needs PG_IO_ALIGN_SIZE.
 */
#include "postgres_fe.h"

#include <fcntl.h>
#include <unistd.h>

int
main(int argc, char **argv)
{
	PGAlignedXLogBlock buf;
	bool		direct = argc > 2;
	int			fd;
	ssize_t		r;

	printf("alignof(PGAlignedXLogBlock) = %zu, buf %% 4096 = %zu\n",
		   alignof(PGAlignedXLogBlock), (size_t) ((uintptr_t) buf.data % 4096));

	fd = open(argv[1], O_RDONLY | (direct ? O_DIRECT : 0));
	if (fd < 0)
	{
		perror("open");
		return 1;
	}
	r = pread(fd, buf.data, sizeof(buf.data), 0);
	if (r < 0)
		printf("pread%s: %m\n", direct ? " with O_DIRECT" : "");
	else
		printf("pread%s: %zd bytes\n", direct ? " with O_DIRECT" : "", r);
	close(fd);
	return 0;
}
