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 "tst_timer.h"
16 #include "copy_file_range.h"
17
18 static int fd_src;
19 static int fd_dest;
20
get_timestamp(int fd)21 struct timespec get_timestamp(int fd)
22 {
23 struct stat filestat;
24
25 fstat(fd, &filestat);
26 return filestat.st_mtim;
27 }
28
verify_copy_file_range_timestamp(void)29 static void verify_copy_file_range_timestamp(void)
30 {
31 loff_t offset;
32 struct timespec timestamp1, timestamp2;
33 long long diff_us;
34
35 timestamp1 = get_timestamp(fd_dest);
36 usleep(1500000);
37
38 offset = 0;
39 TEST(sys_copy_file_range(fd_src, &offset,
40 fd_dest, 0, CONTSIZE, 0));
41 if (TST_RET == -1)
42 tst_brk(TBROK | TTERRNO,
43 "copy_file_range unexpectedly failed");
44
45 timestamp2 = get_timestamp(fd_dest);
46
47 diff_us = tst_timespec_diff_us(timestamp2, timestamp1);
48
49 if (diff_us >= 1000000 && diff_us <= 30000000)
50 tst_res(TPASS, "copy_file_range sucessfully updated the timestamp");
51 else
52 tst_brk(TFAIL, "diff_us = %lld, copy_file_range might not update timestamp", diff_us);
53 }
54
cleanup(void)55 static void cleanup(void)
56 {
57 if (fd_dest > 0)
58 SAFE_CLOSE(fd_dest);
59 if (fd_src > 0)
60 SAFE_CLOSE(fd_src);
61 }
62
setup(void)63 static void setup(void)
64 {
65 syscall_info();
66
67 fd_dest = SAFE_OPEN(FILE_DEST_PATH, O_RDWR | O_CREAT, 0664);
68 fd_src = SAFE_OPEN(FILE_SRC_PATH, O_RDWR | O_CREAT, 0664);
69 SAFE_WRITE(1, fd_src, CONTENT, CONTSIZE);
70 SAFE_CLOSE(fd_src);
71 fd_src = SAFE_OPEN(FILE_SRC_PATH, O_RDONLY);
72 }
73
74
75 static struct tst_test test = {
76 .test_all = verify_copy_file_range_timestamp,
77 .setup = setup,
78 .cleanup = cleanup,
79 .needs_tmpdir = 1,
80 .test_variants = TEST_VARIANTS,
81 };
82