summaryrefslogtreecommitdiff
path: root/src/t_truncate_cmtime.c
blob: 4c5d3c01b9f4c426277ec0b78551a38d4a399ffb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// SPDX-License-Identifier: GPL-2.0
/*
 * Test ctime and mtime are updated on truncate(2) and ftruncate(2)
 * Copyright (c) 2013 Red Hat, Inc.  All Rights Reserved.
 */

#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define TEST_MSG "this is a test string"

int do_test(const char *file, int is_ftrunc)
{
	int ret;
	int fd;
	struct stat statbuf1;
	struct stat statbuf2;

	ret = 0;
	fd = open(file, O_RDWR | O_CREAT | O_TRUNC, 0644);
	if (fd == -1) {
		perror("open(2) failed");
		exit(EXIT_FAILURE);
	}

	ret = write(fd, TEST_MSG, sizeof(TEST_MSG));
	if (ret == -1) {
		perror("write(2) failed");
		exit(EXIT_FAILURE);
	}

	/* Get timestamps before [f]truncate(2) */
	ret = fstat(fd, &statbuf1);
	if (ret == -1) {
		perror("fstat(2) failed");
		exit(EXIT_FAILURE);
	}

	/* Test [f]truncate(2) down */
	sleep(1);
	if (is_ftrunc) {
		ret = ftruncate(fd, 0);
		if (ret == -1) {
			perror("ftruncate(2) down failed");
			exit(EXIT_FAILURE);
		}
	} else {
		ret = truncate(file, 0);
		if (ret == -1) {
			perror("truncate(2) down failed");
			exit(EXIT_FAILURE);
		}
	}

	/* Get timestamps after [f]truncate(2) */
	ret = fstat(fd, &statbuf2);
	if (ret == -1) {
		perror("fstat(2) failed");
		exit(EXIT_FAILURE);
	}

	/* Check whether timestamps got updated on [f]truncate(2) down */
	if (statbuf1.st_ctime == statbuf2.st_ctime) {
		fprintf(stderr, "ctime not updated after %s\n",
			is_ftrunc ? "ftruncate" : "truncate" " down");
		ret++;
	}
	if (statbuf1.st_mtime == statbuf2.st_mtime) {
		fprintf(stderr, "mtime not updated after %s\n",
			is_ftrunc ? "ftruncate" : "truncate" " down");
		ret++;
	}

	/* Test [f]truncate(2) up */
	sleep(1);
	if (is_ftrunc) {
		ret = ftruncate(fd, 123);
		if (ret == -1) {
			perror("ftruncate(2) up failed");
			exit(EXIT_FAILURE);
		}
	} else {
		ret = truncate(file, 123);
		if (ret == -1) {
			perror("truncate(2) up failed");
			exit(EXIT_FAILURE);
		}
	}
	ret = fstat(fd, &statbuf1);
	if (ret == -1) {
		perror("fstat(2) failed");
		exit(EXIT_FAILURE);
	}
	/* Check whether timestamps got updated on [f]truncate(2) up */
	if (statbuf1.st_ctime == statbuf2.st_ctime) {
		fprintf(stderr, "ctime not updated after %s\n",
			is_ftrunc ? "ftruncate" : "truncate" " up");
		ret++;
	}
	if (statbuf1.st_mtime == statbuf2.st_mtime) {
		fprintf(stderr, "mtime not updated after %s\n",
			is_ftrunc ? "ftruncate" : "truncate" " up");
		ret++;
	}

	close(fd);
	return ret;
}

int main(int argc, char *argv[])
{
	int ret;
	char *testfile;

	ret = 0;
	if (argc != 2) {
		fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
		exit(EXIT_FAILURE);
	}
	testfile = argv[1];

	ret = do_test(testfile, 0);
	ret += do_test(testfile, 1);

	exit(ret);
}