1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci * Copyright (c) 2015 Cedric Hnyda <chnyda@suse.com>
3f08c3bdfSopenharmony_ci *
4f08c3bdfSopenharmony_ci * This program is free software; you can redistribute it and/or
5f08c3bdfSopenharmony_ci * modify it under the terms of the GNU General Public License as
6f08c3bdfSopenharmony_ci * published by the Free Software Foundation; either version 2 of
7f08c3bdfSopenharmony_ci * the License, or (at your option) any later version.
8f08c3bdfSopenharmony_ci *
9f08c3bdfSopenharmony_ci * This program is distributed in the hope that it would be useful,
10f08c3bdfSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of
11f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12f08c3bdfSopenharmony_ci * GNU General Public License for more details.
13f08c3bdfSopenharmony_ci *
14f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License
15f08c3bdfSopenharmony_ci * along with this program; if not, write the Free Software Foundation,
16f08c3bdfSopenharmony_ci * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17f08c3bdfSopenharmony_ci */
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_ci /* Description:
20f08c3bdfSopenharmony_ci *   Calls renameat2(2) with the flag RENAME_EXCHANGE and check that
21f08c3bdfSopenharmony_ci *   the content was swapped
22f08c3bdfSopenharmony_ci */
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_ci#define _GNU_SOURCE
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_ci#include "test.h"
27f08c3bdfSopenharmony_ci#include "safe_macros.h"
28f08c3bdfSopenharmony_ci#include "lapi/fcntl.h"
29f08c3bdfSopenharmony_ci#include "renameat2.h"
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_ci#define TEST_DIR "test_dir/"
32f08c3bdfSopenharmony_ci#define TEST_DIR2 "test_dir2/"
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_ci#define TEST_FILE "test_file"
35f08c3bdfSopenharmony_ci#define TEST_FILE2 "test_file2"
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_cichar *TCID = "renameat202";
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_cistatic int olddirfd;
40f08c3bdfSopenharmony_cistatic int newdirfd;
41f08c3bdfSopenharmony_cistatic int fd = -1;
42f08c3bdfSopenharmony_cistatic int cnt;
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_cistatic const char content[] = "content";
45f08c3bdfSopenharmony_cistatic long fs_type;
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_ciint TST_TOTAL = 1;
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_cistatic void setup(void);
51f08c3bdfSopenharmony_cistatic void cleanup(void);
52f08c3bdfSopenharmony_cistatic void renameat2_verify(void);
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_ciint main(int ac, char **av)
56f08c3bdfSopenharmony_ci{
57f08c3bdfSopenharmony_ci	int lc;
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_ci	tst_parse_opts(ac, av, NULL, NULL);
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_ci	setup();
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_ci	for (lc = 0; TEST_LOOPING(lc); lc++) {
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_ci		tst_count = 0;
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_ci		TEST(renameat2(olddirfd, TEST_FILE,
68f08c3bdfSopenharmony_ci				newdirfd, TEST_FILE2, RENAME_EXCHANGE));
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_ci		cnt++;
71f08c3bdfSopenharmony_ci
72f08c3bdfSopenharmony_ci		renameat2_verify();
73f08c3bdfSopenharmony_ci	}
74f08c3bdfSopenharmony_ci
75f08c3bdfSopenharmony_ci	cleanup();
76f08c3bdfSopenharmony_ci	tst_exit();
77f08c3bdfSopenharmony_ci}
78f08c3bdfSopenharmony_ci
79f08c3bdfSopenharmony_cistatic void setup(void)
80f08c3bdfSopenharmony_ci{
81f08c3bdfSopenharmony_ci	if ((tst_kvercmp(3, 15, 0)) < 0) {
82f08c3bdfSopenharmony_ci		tst_brkm(TCONF, NULL,
83f08c3bdfSopenharmony_ci			"This test can only run on kernels that are 3.15. and higher");
84f08c3bdfSopenharmony_ci	}
85f08c3bdfSopenharmony_ci
86f08c3bdfSopenharmony_ci	tst_tmpdir();
87f08c3bdfSopenharmony_ci
88f08c3bdfSopenharmony_ci	fs_type = tst_fs_type(cleanup, ".");
89f08c3bdfSopenharmony_ci
90f08c3bdfSopenharmony_ci	SAFE_MKDIR(cleanup, TEST_DIR, 0700);
91f08c3bdfSopenharmony_ci	SAFE_MKDIR(cleanup, TEST_DIR2, 0700);
92f08c3bdfSopenharmony_ci
93f08c3bdfSopenharmony_ci	SAFE_TOUCH(cleanup, TEST_DIR TEST_FILE, 0600, NULL);
94f08c3bdfSopenharmony_ci	SAFE_TOUCH(cleanup, TEST_DIR2 TEST_FILE2, 0600, NULL);
95f08c3bdfSopenharmony_ci
96f08c3bdfSopenharmony_ci	olddirfd = SAFE_OPEN(cleanup, TEST_DIR, O_DIRECTORY);
97f08c3bdfSopenharmony_ci	newdirfd = SAFE_OPEN(cleanup, TEST_DIR2, O_DIRECTORY);
98f08c3bdfSopenharmony_ci
99f08c3bdfSopenharmony_ci	SAFE_FILE_PRINTF(cleanup, TEST_DIR TEST_FILE, "%s", content);
100f08c3bdfSopenharmony_ci
101f08c3bdfSopenharmony_ci}
102f08c3bdfSopenharmony_ci
103f08c3bdfSopenharmony_cistatic void cleanup(void)
104f08c3bdfSopenharmony_ci{
105f08c3bdfSopenharmony_ci	if (olddirfd > 0 && close(olddirfd) < 0)
106f08c3bdfSopenharmony_ci		tst_resm(TWARN | TERRNO, "close olddirfd failed");
107f08c3bdfSopenharmony_ci
108f08c3bdfSopenharmony_ci	if (newdirfd > 0 && close(newdirfd) < 0)
109f08c3bdfSopenharmony_ci		tst_resm(TWARN | TERRNO, "close newdirfd failed");
110f08c3bdfSopenharmony_ci
111f08c3bdfSopenharmony_ci	if (fd > 0 && close(fd) < 0)
112f08c3bdfSopenharmony_ci		tst_resm(TWARN | TERRNO, "close fd failed");
113f08c3bdfSopenharmony_ci
114f08c3bdfSopenharmony_ci	tst_rmdir();
115f08c3bdfSopenharmony_ci
116f08c3bdfSopenharmony_ci}
117f08c3bdfSopenharmony_ci
118f08c3bdfSopenharmony_cistatic void renameat2_verify(void)
119f08c3bdfSopenharmony_ci{
120f08c3bdfSopenharmony_ci	char str[BUFSIZ] = { 0 };
121f08c3bdfSopenharmony_ci	struct stat st;
122f08c3bdfSopenharmony_ci	char *emptyfile;
123f08c3bdfSopenharmony_ci	char *contentfile;
124f08c3bdfSopenharmony_ci	int readn, data_len;
125f08c3bdfSopenharmony_ci
126f08c3bdfSopenharmony_ci	if (TEST_ERRNO == EINVAL && TST_BTRFS_MAGIC == fs_type) {
127f08c3bdfSopenharmony_ci		tst_brkm(TCONF, cleanup,
128f08c3bdfSopenharmony_ci			"RENAME_EXCHANGE flag is not implemeted on %s",
129f08c3bdfSopenharmony_ci			tst_fs_type_name(fs_type));
130f08c3bdfSopenharmony_ci	}
131f08c3bdfSopenharmony_ci
132f08c3bdfSopenharmony_ci	if (TEST_RETURN != 0) {
133f08c3bdfSopenharmony_ci		tst_resm(TFAIL | TTERRNO, "renameat2() failed unexpectedly");
134f08c3bdfSopenharmony_ci		return;
135f08c3bdfSopenharmony_ci	}
136f08c3bdfSopenharmony_ci
137f08c3bdfSopenharmony_ci	if (cnt % 2 == 1) {
138f08c3bdfSopenharmony_ci		emptyfile = TEST_DIR TEST_FILE;
139f08c3bdfSopenharmony_ci		contentfile = TEST_DIR2 TEST_FILE2;
140f08c3bdfSopenharmony_ci	} else {
141f08c3bdfSopenharmony_ci		emptyfile = TEST_DIR2 TEST_FILE2;
142f08c3bdfSopenharmony_ci		contentfile = TEST_DIR TEST_FILE;
143f08c3bdfSopenharmony_ci	}
144f08c3bdfSopenharmony_ci
145f08c3bdfSopenharmony_ci	fd = SAFE_OPEN(cleanup, contentfile, O_RDONLY);
146f08c3bdfSopenharmony_ci
147f08c3bdfSopenharmony_ci	SAFE_STAT(cleanup, emptyfile, &st);
148f08c3bdfSopenharmony_ci
149f08c3bdfSopenharmony_ci	readn = SAFE_READ(cleanup, 0, fd, str, BUFSIZ);
150f08c3bdfSopenharmony_ci
151f08c3bdfSopenharmony_ci	if (close(fd) < 0)
152f08c3bdfSopenharmony_ci		tst_brkm(TERRNO | TFAIL, cleanup, "close fd failed");
153f08c3bdfSopenharmony_ci	fd = 0;
154f08c3bdfSopenharmony_ci
155f08c3bdfSopenharmony_ci	data_len = sizeof(content) - 1;
156f08c3bdfSopenharmony_ci	if (readn != data_len) {
157f08c3bdfSopenharmony_ci		tst_resm(TFAIL, "Wrong number of bytes read after renameat2(). "
158f08c3bdfSopenharmony_ci				"Expect %d, got %d", data_len, readn);
159f08c3bdfSopenharmony_ci		return;
160f08c3bdfSopenharmony_ci	}
161f08c3bdfSopenharmony_ci	if (strncmp(content, str, data_len)) {
162f08c3bdfSopenharmony_ci		tst_resm(TFAIL, "File content changed after renameat2(). "
163f08c3bdfSopenharmony_ci				"Expect '%s', got '%s'", content, str);
164f08c3bdfSopenharmony_ci		return;
165f08c3bdfSopenharmony_ci	}
166f08c3bdfSopenharmony_ci	if (st.st_size) {
167f08c3bdfSopenharmony_ci		tst_resm(TFAIL, "emptyfile has non-zero file size");
168f08c3bdfSopenharmony_ci		return;
169f08c3bdfSopenharmony_ci	}
170f08c3bdfSopenharmony_ci	tst_resm(TPASS, "renameat2() test passed");
171f08c3bdfSopenharmony_ci}
172