Coverage Report

Created: 2026-09-28 06:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/reftable/system.c
Line
Count
Source
1
#include "../git-compat-util.h"
2
3
#include "system.h"
4
#include "basics.h"
5
#include "reftable-error.h"
6
#include "../lockfile.h"
7
#include "../trace.h"
8
#include "../tempfile.h"
9
#include "../write-or-die.h"
10
11
uint32_t reftable_rand(void)
12
0
{
13
0
  return git_rand(CSPRNG_BYTES_INSECURE);
14
0
}
15
16
int tmpfile_from_pattern(struct reftable_tmpfile *out, const char *pattern)
17
0
{
18
0
  struct tempfile *tempfile;
19
20
0
  tempfile = mks_tempfile(pattern);
21
0
  if (!tempfile)
22
0
    return REFTABLE_IO_ERROR;
23
24
0
  out->path = tempfile->filename.buf;
25
0
  out->fd = tempfile->fd;
26
0
  out->priv = tempfile;
27
28
0
  return 0;
29
0
}
30
31
int tmpfile_close(struct reftable_tmpfile *t)
32
0
{
33
0
  struct tempfile *tempfile = t->priv;
34
0
  int ret = close_tempfile_gently(tempfile);
35
0
  t->fd = -1;
36
0
  if (ret < 0)
37
0
    return REFTABLE_IO_ERROR;
38
0
  return 0;
39
0
}
40
41
int tmpfile_delete(struct reftable_tmpfile *t)
42
0
{
43
0
  struct tempfile *tempfile = t->priv;
44
0
  int ret = delete_tempfile(&tempfile);
45
0
  *t = REFTABLE_TMPFILE_INIT;
46
0
  if (ret < 0)
47
0
    return REFTABLE_IO_ERROR;
48
0
  return 0;
49
0
}
50
51
int tmpfile_rename(struct reftable_tmpfile *t, const char *path)
52
0
{
53
0
  struct tempfile *tempfile = t->priv;
54
0
  int ret = rename_tempfile(&tempfile, path);
55
0
  *t = REFTABLE_TMPFILE_INIT;
56
0
  if (ret < 0)
57
0
    return REFTABLE_IO_ERROR;
58
0
  return 0;
59
0
}
60
61
int flock_acquire(struct reftable_flock *l, const char *target_path,
62
      long timeout_ms)
63
0
{
64
0
  struct lock_file *lockfile;
65
0
  int err;
66
67
0
  lockfile = reftable_malloc(sizeof(*lockfile));
68
0
  if (!lockfile)
69
0
    return REFTABLE_OUT_OF_MEMORY_ERROR;
70
71
0
  err = hold_lock_file_for_update_timeout(lockfile, target_path, LOCK_NO_DEREF,
72
0
            timeout_ms);
73
0
  if (err < 0) {
74
0
    reftable_free(lockfile);
75
0
    if (errno == EEXIST)
76
0
      return REFTABLE_LOCK_ERROR;
77
0
    return REFTABLE_IO_ERROR;
78
0
  }
79
80
0
  l->fd = get_lock_file_fd(lockfile);
81
0
  l->path = get_lock_file_path(lockfile);
82
0
  l->priv = lockfile;
83
84
0
  return 0;
85
0
}
86
87
int flock_close(struct reftable_flock *l)
88
0
{
89
0
  struct lock_file *lockfile = l->priv;
90
0
  int ret;
91
92
0
  if (!lockfile)
93
0
    return REFTABLE_API_ERROR;
94
95
0
  ret = close_lock_file_gently(lockfile);
96
0
  l->fd = -1;
97
0
  if (ret < 0)
98
0
    return REFTABLE_IO_ERROR;
99
100
0
  return 0;
101
0
}
102
103
int flock_release(struct reftable_flock *l)
104
0
{
105
0
  struct lock_file *lockfile = l->priv;
106
0
  int ret;
107
108
0
  if (!lockfile)
109
0
    return 0;
110
111
0
  ret = rollback_lock_file(lockfile);
112
0
  reftable_free(lockfile);
113
0
  *l = REFTABLE_FLOCK_INIT;
114
0
  if (ret < 0)
115
0
    return REFTABLE_IO_ERROR;
116
117
0
  return 0;
118
0
}
119
120
int flock_commit(struct reftable_flock *l)
121
0
{
122
0
  struct lock_file *lockfile = l->priv;
123
0
  int ret;
124
125
0
  if (!lockfile)
126
0
    return REFTABLE_API_ERROR;
127
128
0
  ret = commit_lock_file(lockfile);
129
0
  reftable_free(lockfile);
130
0
  *l = REFTABLE_FLOCK_INIT;
131
0
  if (ret < 0)
132
0
    return REFTABLE_IO_ERROR;
133
134
0
  return 0;
135
0
}
136
137
int reftable_fsync(int fd)
138
0
{
139
0
  return fsync_component(FSYNC_COMPONENT_REFERENCE, fd);
140
0
}
141
142
uint64_t reftable_time_ms(void)
143
0
{
144
0
  return getnanotime() / 1000000;
145
0
}
146
147
int reftable_mmap(struct reftable_mmap *out, int fd, size_t len)
148
0
{
149
0
  void *data = xmmap_gently(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
150
0
  if (data == MAP_FAILED)
151
0
    return REFTABLE_IO_ERROR;
152
153
0
  out->data = data;
154
0
  out->size = len;
155
156
0
  return 0;
157
0
}
158
159
int reftable_munmap(struct reftable_mmap *mmap)
160
0
{
161
0
  if (munmap(mmap->data, mmap->size) < 0)
162
0
    return REFTABLE_IO_ERROR;
163
0
  memset(mmap, 0, sizeof(*mmap));
164
0
  return 0;
165
0
}