Project

General

Profile

Bug #7123 ยป bug-readdir.c

Jan Schlien, 2016-06-21 12:28 PM

 
1
#include <stdio.h>
2
#include <sys/types.h>
3
#include <sys/stat.h>
4
#include <unistd.h>
5
#include <fcntl.h>
6
#include <dirent.h>
7
#include <strings.h>
8

    
9
int
10
main(int argc, char **argv)
11
{
12
	char *f = argv[1];
13
	char *d;
14
	char *slash;
15
	int fdf;
16
	int fdd;
17
	struct stat st;
18
	char buf[4096];
19
	struct timeval tv[2];
20
	struct timeval *tmp;
21
	char dentbuf[8192];
22

    
23
	if (argc != 2) {
24
		printf("usage: %s <filename>\n", argv[0]);
25
		return 1;
26
	}
27

    
28
	slash = strrchr(f, '/');
29
	if (!slash) {
30
		printf("filename must contain a slash\n");
31
		return 1;
32
	}
33

    
34
	*slash = '\0';
35
	d = strdup(f);
36
	*slash = '/';
37

    
38
	unlink(f);
39
	fdf = open(f, O_WRONLY | O_CREAT | O_TRUNC, 0666);
40
	if (fdf == -1) {
41
		perror("open");
42
		return 1;
43
	}
44

    
45
	tv[0].tv_sec = 0;
46
	tv[0].tv_usec = 0;
47
	tv[1].tv_sec = 11232000;
48
	tv[1].tv_usec = 0;
49
	futimesat(AT_FDCWD, f, tv);
50
	close(fdf);
51

    
52
	/*
53
	 * If utimes for d is removed, stat-2 and stat-3 return the same wrong
54
	 * result as stat-1.
55
	 */
56
#if 1
57
	tv[1].tv_sec = 9763200;
58
	futimesat(AT_FDCWD, d, tv);
59
#endif
60

    
61
	tv[1].tv_sec = -604800; /* 18446744073708946816 */
62
	tv[1].tv_usec = 1;
63
	futimesat(AT_FDCWD, f, tv);
64

    
65
	lstat(f, &st);
66
	tmp = (struct timeval *)&st.st_mtime;
67
	printf("stat-0 mtime: %ld.%09ld\n", tmp->tv_sec, tmp->tv_usec);
68

    
69
	fdd = open(d, O_RDONLY | O_NDELAY | O_LARGEFILE);
70
	if (fdd == -1) {
71
		perror("open dir");
72
		return 1;
73
	}
74

    
75
	/*
76
	 * If getdents() is removed, stat-1 gives correct results
77
	 */
78
#if 1
79
	getdents(fdd, (struct dirent *)dentbuf, sizeof(dentbuf));
80
#endif
81

    
82
	/*
83
	 * stat-1: This will deliver wrong mtime
84
	 */
85
	lstat(f, &st);
86
	tmp = (struct timeval *)&st.st_mtime;
87
	printf("stat-1 mtime: %ld.%09ld\n", tmp->tv_sec, tmp->tv_usec);
88

    
89
	fdf = open(f, O_RDONLY);
90
	if (fdf == -1) {
91
		perror("open 2");
92
		return 1;
93
	}
94

    
95
	/*
96
	 * stat-2: This gives expected negative mtime (modulo a problem of
97
	 * -1000000000 nsec)
98
	 */
99
	fstat(fdf, &st);
100
	tmp = (struct timeval *)&st.st_mtime;
101
	printf("stat-2 mtime: %ld.%09ld\n", tmp->tv_sec, tmp->tv_usec);
102

    
103
	close(fdf);
104
	close(fdd);
105

    
106
	/*
107
	 * stat-3: Gives the same result as stat-2
108
	 */
109
	lstat(f, &st);
110
	tmp = (struct timeval *)&st.st_mtime;
111
	printf("stat-3 mtime: %ld.%09ld\n", tmp->tv_sec, tmp->tv_usec);
112

    
113
	return 0;
114
}
    (1-1/1)