Coverage Report

Created: 2024-09-16 06:10

/src/git/statinfo.c
Line
Count
Source (jump to first uncovered line)
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
0
static unsigned int munge_st_size(off_t st_size) {
9
0
  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
0
  if (!sd_size && st_size)
16
0
    return 0x80000000;
17
0
  else
18
0
    return sd_size;
19
0
}
20
21
void fill_stat_data(struct stat_data *sd, struct stat *st)
22
0
{
23
0
  sd->sd_ctime.sec = (unsigned int)st->st_ctime;
24
0
  sd->sd_mtime.sec = (unsigned int)st->st_mtime;
25
0
  sd->sd_ctime.nsec = ST_CTIME_NSEC(*st);
26
0
  sd->sd_mtime.nsec = ST_MTIME_NSEC(*st);
27
0
  sd->sd_dev = st->st_dev;
28
0
  sd->sd_ino = st->st_ino;
29
0
  sd->sd_uid = st->st_uid;
30
0
  sd->sd_gid = st->st_gid;
31
0
  sd->sd_size = munge_st_size(st->st_size);
32
0
}
33
34
static void set_times(struct stat *st, const struct stat_data *sd)
35
0
{
36
0
  st->st_ctime = sd->sd_ctime.sec;
37
0
  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
0
  st->st_ctim.tv_nsec = sd->sd_ctime.nsec;
46
0
  st->st_mtim.tv_nsec = sd->sd_mtime.nsec;
47
0
#endif
48
0
#endif
49
0
}
50
51
void fake_lstat_data(const struct stat_data *sd, struct stat *st)
52
0
{
53
0
  set_times(st, sd);
54
0
  st->st_dev = sd->sd_dev;
55
0
  st->st_ino = sd->sd_ino;
56
0
  st->st_uid = sd->sd_uid;
57
0
  st->st_gid = sd->sd_gid;
58
0
  st->st_size = sd->sd_size;
59
0
}
60
61
int match_stat_data(const struct stat_data *sd, struct stat *st)
62
0
{
63
0
  int changed = 0;
64
65
0
  if (sd->sd_mtime.sec != (unsigned int)st->st_mtime)
66
0
    changed |= MTIME_CHANGED;
67
0
  if (trust_ctime && check_stat &&
68
0
      sd->sd_ctime.sec != (unsigned int)st->st_ctime)
69
0
    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
0
  if (check_stat) {
80
0
    if (sd->sd_uid != (unsigned int) st->st_uid ||
81
0
      sd->sd_gid != (unsigned int) st->st_gid)
82
0
      changed |= OWNER_CHANGED;
83
0
    if (sd->sd_ino != (unsigned int) st->st_ino)
84
0
      changed |= INODE_CHANGED;
85
0
  }
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
0
  if (sd->sd_size != munge_st_size(st->st_size))
98
0
    changed |= DATA_CHANGED;
99
100
0
  return changed;
101
0
}
102
103
void stat_validity_clear(struct stat_validity *sv)
104
0
{
105
0
  FREE_AND_NULL(sv->sd);
106
0
}
107
108
int stat_validity_check(struct stat_validity *sv, const char *path)
109
0
{
110
0
  struct stat st;
111
112
0
  if (stat(path, &st) < 0)
113
0
    return sv->sd == NULL;
114
0
  if (!sv->sd)
115
0
    return 0;
116
0
  return S_ISREG(st.st_mode) && !match_stat_data(sv->sd, &st);
117
0
}
118
119
void stat_validity_update(struct stat_validity *sv, int fd)
120
0
{
121
0
  struct stat st;
122
123
0
  if (fstat(fd, &st) < 0 || !S_ISREG(st.st_mode))
124
0
    stat_validity_clear(sv);
125
0
  else {
126
0
    if (!sv->sd)
127
0
      CALLOC_ARRAY(sv->sd, 1);
128
0
    fill_stat_data(sv->sd, &st);
129
0
  }
130
0
}