2 * Test ctime and mtime are updated on truncate(2) and ftruncate(2)
4 * Copyright (c) 2013 Red Hat, Inc. All Rights Reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <sys/types.h>
27 #define TEST_MSG "this is a test string"
29 int do_test(const char *file, int is_ftrunc)
37 fd = open(file, O_RDWR | O_CREAT | O_TRUNC, 0644);
39 perror("open(2) failed");
43 ret = write(fd, TEST_MSG, sizeof(TEST_MSG));
45 perror("write(2) failed");
49 /* Get timestamps before [f]truncate(2) */
50 ret = fstat(fd, &statbuf1);
52 perror("fstat(2) failed");
56 /* Test [f]truncate(2) down */
59 ret = ftruncate(fd, 0);
61 perror("ftruncate(2) down failed");
65 ret = truncate(file, 0);
67 perror("truncate(2) down failed");
72 /* Get timestamps after [f]truncate(2) */
73 ret = fstat(fd, &statbuf2);
75 perror("fstat(2) failed");
79 /* Check whether timestamps got updated on [f]truncate(2) down */
80 if (statbuf1.st_ctime == statbuf2.st_ctime) {
81 fprintf(stderr, "ctime not updated after %s\n",
82 is_ftrunc ? "ftruncate" : "truncate" " down");
85 if (statbuf1.st_mtime == statbuf2.st_mtime) {
86 fprintf(stderr, "mtime not updated after %s\n",
87 is_ftrunc ? "ftruncate" : "truncate" " down");
91 /* Test [f]truncate(2) up */
94 ret = ftruncate(fd, 123);
96 perror("ftruncate(2) up failed");
100 ret = truncate(file, 123);
102 perror("truncate(2) up failed");
106 ret = fstat(fd, &statbuf1);
108 perror("fstat(2) failed");
111 /* Check whether timestamps got updated on [f]truncate(2) up */
112 if (statbuf1.st_ctime == statbuf2.st_ctime) {
113 fprintf(stderr, "ctime not updated after %s\n",
114 is_ftrunc ? "ftruncate" : "truncate" " up");
117 if (statbuf1.st_mtime == statbuf2.st_mtime) {
118 fprintf(stderr, "mtime not updated after %s\n",
119 is_ftrunc ? "ftruncate" : "truncate" " up");
127 int main(int argc, char *argv[])
134 fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
139 ret = do_test(testfile, 0);
140 ret += do_test(testfile, 1);