1 // SPDX-License-Identifier: GPL-2.0
3 * Test ctime and mtime are updated on truncate(2) and ftruncate(2)
4 * Copyright (c) 2013 Red Hat, Inc. All Rights Reserved.
14 #define TEST_MSG "this is a test string"
16 int do_test(const char *file, int is_ftrunc)
24 fd = open(file, O_RDWR | O_CREAT | O_TRUNC, 0644);
26 perror("open(2) failed");
30 ret = write(fd, TEST_MSG, sizeof(TEST_MSG));
32 perror("write(2) failed");
36 /* Get timestamps before [f]truncate(2) */
37 ret = fstat(fd, &statbuf1);
39 perror("fstat(2) failed");
43 /* Test [f]truncate(2) down */
46 ret = ftruncate(fd, 0);
48 perror("ftruncate(2) down failed");
52 ret = truncate(file, 0);
54 perror("truncate(2) down failed");
59 /* Get timestamps after [f]truncate(2) */
60 ret = fstat(fd, &statbuf2);
62 perror("fstat(2) failed");
66 /* Check whether timestamps got updated on [f]truncate(2) down */
67 if (statbuf1.st_ctime == statbuf2.st_ctime) {
68 fprintf(stderr, "ctime not updated after %s\n",
69 is_ftrunc ? "ftruncate" : "truncate" " down");
72 if (statbuf1.st_mtime == statbuf2.st_mtime) {
73 fprintf(stderr, "mtime not updated after %s\n",
74 is_ftrunc ? "ftruncate" : "truncate" " down");
78 /* Test [f]truncate(2) up */
81 ret = ftruncate(fd, 123);
83 perror("ftruncate(2) up failed");
87 ret = truncate(file, 123);
89 perror("truncate(2) up failed");
93 ret = fstat(fd, &statbuf1);
95 perror("fstat(2) failed");
98 /* Check whether timestamps got updated on [f]truncate(2) up */
99 if (statbuf1.st_ctime == statbuf2.st_ctime) {
100 fprintf(stderr, "ctime not updated after %s\n",
101 is_ftrunc ? "ftruncate" : "truncate" " up");
104 if (statbuf1.st_mtime == statbuf2.st_mtime) {
105 fprintf(stderr, "mtime not updated after %s\n",
106 is_ftrunc ? "ftruncate" : "truncate" " up");
114 int main(int argc, char *argv[])
121 fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
126 ret = do_test(testfile, 0);
127 ret += do_test(testfile, 1);