• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2019 SUSE LLC
4  * Author: Christian Amann <camann@suse.com>
5  */
6 
7 /*
8  * Copies the contents of one file into another and
9  * checks if the timestamp gets updated in the process.
10  */
11 
12 #define _GNU_SOURCE
13 
14 #include "tst_test.h"
15 #include "copy_file_range.h"
16 
17 static int fd_src;
18 static int fd_dest;
19 
get_timestamp(int fd)20 unsigned long get_timestamp(int fd)
21 {
22 	struct stat filestat;
23 
24 	fstat(fd, &filestat);
25 	return filestat.st_mtime;
26 }
27 
verify_copy_file_range_timestamp(void)28 static void verify_copy_file_range_timestamp(void)
29 {
30 	loff_t offset;
31 	unsigned long timestamp, updated_timestamp;
32 
33 	timestamp = get_timestamp(fd_dest);
34 	usleep(1000000);
35 
36 	offset = 0;
37 	TEST(sys_copy_file_range(fd_src, &offset,
38 			fd_dest, 0, CONTSIZE, 0));
39 	if (TST_RET == -1)
40 		tst_brk(TBROK | TTERRNO,
41 				"copy_file_range unexpectedly failed");
42 
43 	updated_timestamp = get_timestamp(fd_dest);
44 
45 	if (timestamp == updated_timestamp)
46 		tst_brk(TFAIL, "copy_file_range did not update timestamp.");
47 
48 	tst_res(TPASS, "copy_file_range sucessfully updated the timestamp");
49 }
50 
cleanup(void)51 static void cleanup(void)
52 {
53 	if (fd_dest > 0)
54 		SAFE_CLOSE(fd_dest);
55 	if (fd_src  > 0)
56 		SAFE_CLOSE(fd_src);
57 }
58 
setup(void)59 static void setup(void)
60 {
61 	syscall_info();
62 
63 	fd_dest = SAFE_OPEN(FILE_DEST_PATH, O_RDWR | O_CREAT, 0664);
64 	fd_src  = SAFE_OPEN(FILE_SRC_PATH,  O_RDWR | O_CREAT, 0664);
65 	SAFE_WRITE(1, fd_src,  CONTENT,  CONTSIZE);
66 	SAFE_CLOSE(fd_src);
67 	fd_src = SAFE_OPEN(FILE_SRC_PATH, O_RDONLY);
68 }
69 
70 
71 static struct tst_test test = {
72 	.test_all = verify_copy_file_range_timestamp,
73 	.setup = setup,
74 	.cleanup = cleanup,
75 	.needs_tmpdir = 1,
76 	.test_variants = TEST_VARIANTS,
77 };
78