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