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");
58 ret = ftruncate(fd, 0);
60 perror("ftruncate(2) failed");
64 ret = truncate(file, 0);
66 perror("truncate(2) failed");
71 /* Get timestamps after [f]truncate(2) */
72 ret = fstat(fd, &statbuf2);
74 perror("fstat(2) failed");
78 /* Check whether timestamps got updated */
79 if (statbuf1.st_ctime == statbuf2.st_ctime) {
80 fprintf(stderr, "ctime not updated after %s\n",
81 is_ftrunc ? "ftruncate" : "truncate");
84 if (statbuf1.st_mtime == statbuf2.st_mtime) {
85 fprintf(stderr, "mtime not updated after %s\n",
86 is_ftrunc ? "ftruncate" : "truncate");
94 int main(int argc, char *argv[])
101 fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
106 ret = do_test(testfile, 0);
107 ret += do_test(testfile, 1);