• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2019 FUJITSU LIMITED. All rights reserved.
4  * Author: Jinhui Huang <huangjh.jy@cn.fujitsu.com>
5  */
6 /*
7  * Description:
8  * Testcase to check the basic functionality of the pwritev2(2).
9  * 1) If the file offset argument is not -1, pwritev2() should succeed
10  *    in writing the expected content of data and the file offset is
11  *    not changed after writing.
12  * 2) If the file offset argument is -1, pwritev2() should succeed in
13  *    writing the expected content of data and the current file offset
14  *    is used and changed after writing.
15  */
16 
17 #define _GNU_SOURCE
18 #include <string.h>
19 #include <sys/uio.h>
20 
21 #include "tst_test.h"
22 #include "lapi/pwritev2.h"
23 #include "tst_safe_prw.h"
24 
25 #define CHUNK	64
26 
27 static int fd;
28 static char initbuf[CHUNK * 2];
29 static char buf[CHUNK];
30 
31 static struct iovec wr_iovec[] = {
32 	{buf, CHUNK},
33 	{NULL, 0},
34 };
35 
36 static struct tcase {
37 	off_t seek_off;
38 	int count;
39 	off_t write_off;
40 	ssize_t size;
41 	off_t exp_off;
42 } tcases[] = {
43 	{0,     1, 0,          CHUNK, 0},
44 	{CHUNK, 2, 0,          CHUNK, CHUNK},
45 	{0,     1, CHUNK / 2,  CHUNK, 0},
46 	{0,     1, -1,         CHUNK, CHUNK},
47 	{0,     2, -1,         CHUNK, CHUNK},
48 	{CHUNK, 1, -1,         CHUNK, CHUNK * 2},
49 };
50 
verify_pwritev2(unsigned int n)51 static void verify_pwritev2(unsigned int n)
52 {
53 	int i;
54 	char preadbuf[CHUNK];
55 	struct tcase *tc = &tcases[n];
56 
57 	SAFE_PWRITE(1, fd, initbuf, sizeof(initbuf), 0);
58 	SAFE_LSEEK(fd, tc->seek_off, SEEK_SET);
59 
60 	TEST(pwritev2(fd, wr_iovec, tc->count, tc->write_off, 0));
61 	if (TST_RET < 0) {
62 		tst_res(TFAIL | TTERRNO, "pwritev2() failed");
63 		return;
64 	}
65 
66 	if (TST_RET != tc->size) {
67 		tst_res(TFAIL, "pwritev2() wrote %li bytes, expected %zi",
68 			 TST_RET, tc->size);
69 		return;
70 	}
71 
72 	if (SAFE_LSEEK(fd, 0, SEEK_CUR) != tc->exp_off) {
73 		tst_res(TFAIL, "pwritev2() had changed file offset");
74 		return;
75 	}
76 
77 	memset(preadbuf, 0, CHUNK);
78 
79 	if (tc->write_off != -1)
80 		SAFE_PREAD(1, fd, preadbuf, tc->size, tc->write_off);
81 	else
82 		SAFE_PREAD(1, fd, preadbuf, tc->size, tc->seek_off);
83 
84 	for (i = 0; i < tc->size; i++) {
85 		if (preadbuf[i] != 0x61)
86 			break;
87 	}
88 
89 	if (i != tc->size) {
90 		tst_res(TFAIL, "buffer wrong at %i have %c expected 'a'",
91 			 i, preadbuf[i]);
92 		return;
93 	}
94 
95 	tst_res(TPASS, "pwritev2() wrote %zi bytes successfully "
96 		 "with content 'a' expectedly ", tc->size);
97 }
98 
setup(void)99 static void setup(void)
100 {
101 	memset(buf, 0x61, CHUNK);
102 	memset(initbuf, 0, CHUNK * 2);
103 
104 	fd = SAFE_OPEN("file", O_RDWR | O_CREAT, 0644);
105 }
106 
cleanup(void)107 static void cleanup(void)
108 {
109 	if (fd > 0)
110 		SAFE_CLOSE(fd);
111 }
112 
113 static struct tst_test test = {
114 	.tcnt = ARRAY_SIZE(tcases),
115 	.setup = setup,
116 	.cleanup = cleanup,
117 	.test = verify_pwritev2,
118 	.needs_tmpdir = 1,
119 };
120