Raw
1 #define USE_THE_REPOSITORY_VARIABLE
2
3 #include "git-compat-util.h"
4 #include "environment.h"
5 #include "statinfo.h"
6 #include "repository.h"
7
8 /*
9 * Munge st_size into an unsigned int.
10 */
11 static unsigned int munge_st_size(off_t st_size) {
12 unsigned int sd_size = st_size;
13
14 /*
15 * If the file is an exact multiple of 4 GiB, modify the value so it
16 * doesn't get marked as racily clean (zero).
17 */
18 if (!sd_size && st_size)
19 return 0x80000000;
20 else
21 return sd_size;
22 }
23
24 void fill_stat_data(struct stat_data *sd, struct stat *st)
25 {
26 sd->sd_ctime.sec = (unsigned int)st->st_ctime;
27 sd->sd_mtime.sec = (unsigned int)st->st_mtime;
28 sd->sd_ctime.nsec = ST_CTIME_NSEC(*st);
29 sd->sd_mtime.nsec = ST_MTIME_NSEC(*st);
30 sd->sd_dev = st->st_dev;
31 sd->sd_ino = st->st_ino;
32 sd->sd_uid = st->st_uid;
33 sd->sd_gid = st->st_gid;
34 sd->sd_size = munge_st_size(st->st_size);
35 }
36
37 static void set_times(struct stat *st, const struct stat_data *sd)
38 {
39 st->st_ctime = sd->sd_ctime.sec;
40 st->st_mtime = sd->sd_mtime.sec;
41 #ifdef NO_NSEC
42 ; /* nothing */
43 #else
44 #ifdef USE_ST_TIMESPEC
45 st->st_ctimespec.tv_nsec = sd->sd_ctime.nsec;
46 st->st_mtimespec.tv_nsec = sd->sd_mtime.nsec;
47 #else
48 st->st_ctim.tv_nsec = sd->sd_ctime.nsec;
49 st->st_mtim.tv_nsec = sd->sd_mtime.nsec;
50 #endif
51 #endif
52 }
53
54 void fake_lstat_data(const struct stat_data *sd, struct stat *st)
55 {
56 set_times(st, sd);
57 st->st_dev = sd->sd_dev;
58 st->st_ino = sd->sd_ino;
59 st->st_uid = sd->sd_uid;
60 st->st_gid = sd->sd_gid;
61 st->st_size = sd->sd_size;
62 }
63
64 int match_stat_data(const struct stat_data *sd, struct stat *st)
65 {
66 int changed = 0;
67 struct repo_config_values *cfg = repo_config_values(the_repository);
68
69 if (sd->sd_mtime.sec != (unsigned int)st->st_mtime)
70 changed |= MTIME_CHANGED;
71 if (cfg->trust_ctime && cfg->check_stat &&
72 sd->sd_ctime.sec != (unsigned int)st->st_ctime)
73 changed |= CTIME_CHANGED;
74
75 #ifdef USE_NSEC
76 if (cfg->check_stat && sd->sd_mtime.nsec != ST_MTIME_NSEC(*st))
77 changed |= MTIME_CHANGED;
78 if (cfg->trust_ctime && cfg->check_stat &&
79 sd->sd_ctime.nsec != ST_CTIME_NSEC(*st))
80 changed |= CTIME_CHANGED;
81 #endif
82
83 if (cfg->check_stat) {
84 if (sd->sd_uid != (unsigned int) st->st_uid ||
85 sd->sd_gid != (unsigned int) st->st_gid)
86 changed |= OWNER_CHANGED;
87 if (sd->sd_ino != (unsigned int) st->st_ino)
88 changed |= INODE_CHANGED;
89 }
90
91 #ifdef USE_STDEV
92 /*
93 * st_dev breaks on network filesystems where different
94 * clients will have different views of what "device"
95 * the filesystem is on
96 */
97 if (cfg->check_stat && sd->sd_dev != (unsigned int) st->st_dev)
98 changed |= INODE_CHANGED;
99 #endif
100
101 if (sd->sd_size != munge_st_size(st->st_size))
102 changed |= DATA_CHANGED;
103
104 return changed;
105 }
106
107 void stat_validity_clear(struct stat_validity *sv)
108 {
109 FREE_AND_NULL(sv->sd);
110 }
111
112 int stat_validity_check(struct stat_validity *sv, const char *path)
113 {
114 struct stat st;
115
116 if (stat(path, &st) < 0)
117 return sv->sd == NULL;
118 if (!sv->sd)
119 return 0;
120 return S_ISREG(st.st_mode) && !match_stat_data(sv->sd, &st);
121 }
122
123 void stat_validity_update(struct stat_validity *sv, int fd)
124 {
125 struct stat st;
126
127 if (fstat(fd, &st) < 0 || !S_ISREG(st.st_mode))
128 stat_validity_clear(sv);
129 else {
130 if (!sv->sd)
131 CALLOC_ARRAY(sv->sd, 1);
132 fill_stat_data(sv->sd, &st);
133 }
134 }