]>
Commit | Line | Data |
---|---|---|
90cbae9c EN |
1 | #include "git-compat-util.h" |
2 | #include "environment.h" | |
3 | #include "statinfo.h" | |
4 | ||
5143ac07 JH |
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 | ||
90cbae9c EN |
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; | |
5143ac07 | 31 | sd->sd_size = munge_st_size(st->st_size); |
90cbae9c EN |
32 | } |
33 | ||
34 | int match_stat_data(const struct stat_data *sd, struct stat *st) | |
35 | { | |
36 | int changed = 0; | |
37 | ||
38 | if (sd->sd_mtime.sec != (unsigned int)st->st_mtime) | |
39 | changed |= MTIME_CHANGED; | |
40 | if (trust_ctime && check_stat && | |
41 | sd->sd_ctime.sec != (unsigned int)st->st_ctime) | |
42 | changed |= CTIME_CHANGED; | |
43 | ||
44 | #ifdef USE_NSEC | |
45 | if (check_stat && sd->sd_mtime.nsec != ST_MTIME_NSEC(*st)) | |
46 | changed |= MTIME_CHANGED; | |
47 | if (trust_ctime && check_stat && | |
48 | sd->sd_ctime.nsec != ST_CTIME_NSEC(*st)) | |
49 | changed |= CTIME_CHANGED; | |
50 | #endif | |
51 | ||
52 | if (check_stat) { | |
53 | if (sd->sd_uid != (unsigned int) st->st_uid || | |
54 | sd->sd_gid != (unsigned int) st->st_gid) | |
55 | changed |= OWNER_CHANGED; | |
56 | if (sd->sd_ino != (unsigned int) st->st_ino) | |
57 | changed |= INODE_CHANGED; | |
58 | } | |
59 | ||
60 | #ifdef USE_STDEV | |
61 | /* | |
62 | * st_dev breaks on network filesystems where different | |
63 | * clients will have different views of what "device" | |
64 | * the filesystem is on | |
65 | */ | |
66 | if (check_stat && sd->sd_dev != (unsigned int) st->st_dev) | |
67 | changed |= INODE_CHANGED; | |
68 | #endif | |
69 | ||
5143ac07 | 70 | if (sd->sd_size != munge_st_size(st->st_size)) |
90cbae9c EN |
71 | changed |= DATA_CHANGED; |
72 | ||
73 | return changed; | |
74 | } | |
75 | ||
76 | void stat_validity_clear(struct stat_validity *sv) | |
77 | { | |
78 | FREE_AND_NULL(sv->sd); | |
79 | } | |
80 | ||
81 | int stat_validity_check(struct stat_validity *sv, const char *path) | |
82 | { | |
83 | struct stat st; | |
84 | ||
85 | if (stat(path, &st) < 0) | |
86 | return sv->sd == NULL; | |
87 | if (!sv->sd) | |
88 | return 0; | |
89 | return S_ISREG(st.st_mode) && !match_stat_data(sv->sd, &st); | |
90 | } | |
91 | ||
92 | void stat_validity_update(struct stat_validity *sv, int fd) | |
93 | { | |
94 | struct stat st; | |
95 | ||
96 | if (fstat(fd, &st) < 0 || !S_ISREG(st.st_mode)) | |
97 | stat_validity_clear(sv); | |
98 | else { | |
99 | if (!sv->sd) | |
100 | CALLOC_ARRAY(sv->sd, 1); | |
101 | fill_stat_data(sv->sd, &st); | |
102 | } | |
103 | } |