]> git.ipfire.org Git - thirdparty/git.git/blob - statinfo.c
Merge git-gui into ml/git-gui-exec-path-fix
[thirdparty/git.git] / statinfo.c
1 #include "git-compat-util.h"
2 #include "environment.h"
3 #include "statinfo.h"
4
5 void fill_stat_data(struct stat_data *sd, struct stat *st)
6 {
7 sd->sd_ctime.sec = (unsigned int)st->st_ctime;
8 sd->sd_mtime.sec = (unsigned int)st->st_mtime;
9 sd->sd_ctime.nsec = ST_CTIME_NSEC(*st);
10 sd->sd_mtime.nsec = ST_MTIME_NSEC(*st);
11 sd->sd_dev = st->st_dev;
12 sd->sd_ino = st->st_ino;
13 sd->sd_uid = st->st_uid;
14 sd->sd_gid = st->st_gid;
15 sd->sd_size = st->st_size;
16 }
17
18 int match_stat_data(const struct stat_data *sd, struct stat *st)
19 {
20 int changed = 0;
21
22 if (sd->sd_mtime.sec != (unsigned int)st->st_mtime)
23 changed |= MTIME_CHANGED;
24 if (trust_ctime && check_stat &&
25 sd->sd_ctime.sec != (unsigned int)st->st_ctime)
26 changed |= CTIME_CHANGED;
27
28 #ifdef USE_NSEC
29 if (check_stat && sd->sd_mtime.nsec != ST_MTIME_NSEC(*st))
30 changed |= MTIME_CHANGED;
31 if (trust_ctime && check_stat &&
32 sd->sd_ctime.nsec != ST_CTIME_NSEC(*st))
33 changed |= CTIME_CHANGED;
34 #endif
35
36 if (check_stat) {
37 if (sd->sd_uid != (unsigned int) st->st_uid ||
38 sd->sd_gid != (unsigned int) st->st_gid)
39 changed |= OWNER_CHANGED;
40 if (sd->sd_ino != (unsigned int) st->st_ino)
41 changed |= INODE_CHANGED;
42 }
43
44 #ifdef USE_STDEV
45 /*
46 * st_dev breaks on network filesystems where different
47 * clients will have different views of what "device"
48 * the filesystem is on
49 */
50 if (check_stat && sd->sd_dev != (unsigned int) st->st_dev)
51 changed |= INODE_CHANGED;
52 #endif
53
54 if (sd->sd_size != (unsigned int) st->st_size)
55 changed |= DATA_CHANGED;
56
57 return changed;
58 }
59
60 void stat_validity_clear(struct stat_validity *sv)
61 {
62 FREE_AND_NULL(sv->sd);
63 }
64
65 int stat_validity_check(struct stat_validity *sv, const char *path)
66 {
67 struct stat st;
68
69 if (stat(path, &st) < 0)
70 return sv->sd == NULL;
71 if (!sv->sd)
72 return 0;
73 return S_ISREG(st.st_mode) && !match_stat_data(sv->sd, &st);
74 }
75
76 void stat_validity_update(struct stat_validity *sv, int fd)
77 {
78 struct stat st;
79
80 if (fstat(fd, &st) < 0 || !S_ISREG(st.st_mode))
81 stat_validity_clear(sv);
82 else {
83 if (!sv->sd)
84 CALLOC_ARRAY(sv->sd, 1);
85 fill_stat_data(sv->sd, &st);
86 }
87 }