Coverage Report

Created: 2024-09-08 06:24

/src/git/tempfile.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * State diagram and cleanup
3
 * -------------------------
4
 *
5
 * If the program exits while a temporary file is active, we want to
6
 * make sure that we remove it. This is done by remembering the active
7
 * temporary files in a linked list, `tempfile_list`. An `atexit(3)`
8
 * handler and a signal handler are registered, to clean up any active
9
 * temporary files.
10
 *
11
 * Because the signal handler can run at any time, `tempfile_list` and
12
 * the `tempfile` objects that comprise it must be kept in
13
 * self-consistent states at all times.
14
 *
15
 * The possible states of a `tempfile` object are as follows:
16
 *
17
 * - Inactive/unallocated. The only way to get a tempfile is via a creation
18
 *   function like create_tempfile(). Once allocated, the tempfile is on the
19
 *   global tempfile_list and considered active.
20
 *
21
 * - Active, file open (after `create_tempfile()` or
22
 *   `reopen_tempfile()`). In this state:
23
 *
24
 *   - the temporary file exists
25
 *   - `filename` holds the filename of the temporary file
26
 *   - `fd` holds a file descriptor open for writing to it
27
 *   - `fp` holds a pointer to an open `FILE` object if and only if
28
 *     `fdopen_tempfile()` has been called on the object
29
 *   - `owner` holds the PID of the process that created the file
30
 *
31
 * - Active, file closed (after `close_tempfile_gently()`). Same
32
 *   as the previous state, except that the temporary file is closed,
33
 *   `fd` is -1, and `fp` is `NULL`.
34
 *
35
 * - Inactive (after `delete_tempfile()`, `rename_tempfile()`, or a
36
 *   failed attempt to create a temporary file). The struct is removed from
37
 *   the global tempfile_list and deallocated.
38
 *
39
 * A temporary file is owned by the process that created it. The
40
 * `tempfile` has an `owner` field that records the owner's PID. This
41
 * field is used to prevent a forked process from deleting a temporary
42
 * file created by its parent.
43
 */
44
45
#include "git-compat-util.h"
46
#include "abspath.h"
47
#include "path.h"
48
#include "tempfile.h"
49
#include "sigchain.h"
50
51
static VOLATILE_LIST_HEAD(tempfile_list);
52
53
static int remove_template_directory(struct tempfile *tempfile,
54
              int in_signal_handler)
55
27.2k
{
56
27.2k
  if (tempfile->directory) {
57
0
    if (in_signal_handler)
58
0
      return rmdir(tempfile->directory);
59
0
    else
60
0
      return rmdir_or_warn(tempfile->directory);
61
0
  }
62
63
27.2k
  return 0;
64
27.2k
}
65
66
static void remove_tempfiles(int in_signal_handler)
67
2
{
68
2
  pid_t me = getpid();
69
2
  volatile struct volatile_list_head *pos;
70
71
2
  list_for_each(pos, &tempfile_list) {
72
0
    struct tempfile *p = list_entry(pos, struct tempfile, list);
73
74
0
    if (!is_tempfile_active(p) || p->owner != me)
75
0
      continue;
76
77
0
    if (p->fd >= 0)
78
0
      close(p->fd);
79
80
0
    if (in_signal_handler)
81
0
      unlink(p->filename.buf);
82
0
    else
83
0
      unlink_or_warn(p->filename.buf);
84
0
    remove_template_directory(p, in_signal_handler);
85
0
  }
86
2
}
87
88
static void remove_tempfiles_on_exit(void)
89
2
{
90
2
  remove_tempfiles(0);
91
2
}
92
93
static void remove_tempfiles_on_signal(int signo)
94
0
{
95
0
  remove_tempfiles(1);
96
0
  sigchain_pop(signo);
97
0
  raise(signo);
98
0
}
99
100
static struct tempfile *new_tempfile(void)
101
63.0k
{
102
63.0k
  struct tempfile *tempfile = xmalloc(sizeof(*tempfile));
103
63.0k
  tempfile->fd = -1;
104
63.0k
  tempfile->fp = NULL;
105
63.0k
  tempfile->owner = 0;
106
63.0k
  INIT_LIST_HEAD(&tempfile->list);
107
63.0k
  strbuf_init(&tempfile->filename, 0);
108
63.0k
  tempfile->directory = NULL;
109
63.0k
  return tempfile;
110
63.0k
}
111
112
static void activate_tempfile(struct tempfile *tempfile)
113
63.0k
{
114
63.0k
  static int initialized;
115
116
63.0k
  if (!initialized) {
117
2
    sigchain_push_common(remove_tempfiles_on_signal);
118
2
    atexit(remove_tempfiles_on_exit);
119
2
    initialized = 1;
120
2
  }
121
122
63.0k
  volatile_list_add(&tempfile->list, &tempfile_list);
123
63.0k
  tempfile->owner = getpid();
124
63.0k
}
125
126
static void deactivate_tempfile(struct tempfile *tempfile)
127
63.0k
{
128
63.0k
  volatile_list_del(&tempfile->list);
129
63.0k
  strbuf_release(&tempfile->filename);
130
63.0k
  free(tempfile->directory);
131
63.0k
  free(tempfile);
132
63.0k
}
133
134
/* Make sure errno contains a meaningful value on error */
135
struct tempfile *create_tempfile_mode(const char *path, int mode)
136
63.0k
{
137
63.0k
  struct tempfile *tempfile = new_tempfile();
138
139
63.0k
  strbuf_add_absolute_path(&tempfile->filename, path);
140
63.0k
  tempfile->fd = open(tempfile->filename.buf,
141
63.0k
          O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, mode);
142
63.0k
  if (O_CLOEXEC && tempfile->fd < 0 && errno == EINVAL)
143
    /* Try again w/o O_CLOEXEC: the kernel might not support it */
144
0
    tempfile->fd = open(tempfile->filename.buf,
145
0
            O_RDWR | O_CREAT | O_EXCL, mode);
146
63.0k
  if (tempfile->fd < 0) {
147
35
    deactivate_tempfile(tempfile);
148
35
    return NULL;
149
35
  }
150
63.0k
  activate_tempfile(tempfile);
151
63.0k
  if (adjust_shared_perm(tempfile->filename.buf)) {
152
0
    int save_errno = errno;
153
0
    error("cannot fix permission bits on %s", tempfile->filename.buf);
154
0
    delete_tempfile(&tempfile);
155
0
    errno = save_errno;
156
0
    return NULL;
157
0
  }
158
159
63.0k
  return tempfile;
160
63.0k
}
161
162
struct tempfile *register_tempfile(const char *path)
163
0
{
164
0
  struct tempfile *tempfile = new_tempfile();
165
0
  strbuf_add_absolute_path(&tempfile->filename, path);
166
0
  activate_tempfile(tempfile);
167
0
  return tempfile;
168
0
}
169
170
struct tempfile *mks_tempfile_sm(const char *filename_template, int suffixlen, int mode)
171
0
{
172
0
  struct tempfile *tempfile = new_tempfile();
173
174
0
  strbuf_add_absolute_path(&tempfile->filename, filename_template);
175
0
  tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode);
176
0
  if (tempfile->fd < 0) {
177
0
    deactivate_tempfile(tempfile);
178
0
    return NULL;
179
0
  }
180
0
  activate_tempfile(tempfile);
181
0
  return tempfile;
182
0
}
183
184
struct tempfile *mks_tempfile_tsm(const char *filename_template, int suffixlen, int mode)
185
0
{
186
0
  struct tempfile *tempfile = new_tempfile();
187
0
  const char *tmpdir;
188
189
0
  tmpdir = getenv("TMPDIR");
190
0
  if (!tmpdir)
191
0
    tmpdir = "/tmp";
192
193
0
  strbuf_addf(&tempfile->filename, "%s/%s", tmpdir, filename_template);
194
0
  tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode);
195
0
  if (tempfile->fd < 0) {
196
0
    deactivate_tempfile(tempfile);
197
0
    return NULL;
198
0
  }
199
0
  activate_tempfile(tempfile);
200
0
  return tempfile;
201
0
}
202
203
struct tempfile *mks_tempfile_dt(const char *directory_template,
204
         const char *filename)
205
0
{
206
0
  struct tempfile *tempfile;
207
0
  const char *tmpdir;
208
0
  struct strbuf sb = STRBUF_INIT;
209
0
  int fd;
210
0
  size_t directorylen;
211
212
0
  if (!ends_with(directory_template, "XXXXXX")) {
213
0
    errno = EINVAL;
214
0
    return NULL;
215
0
  }
216
217
0
  tmpdir = getenv("TMPDIR");
218
0
  if (!tmpdir)
219
0
    tmpdir = "/tmp";
220
221
0
  strbuf_addf(&sb, "%s/%s", tmpdir, directory_template);
222
0
  directorylen = sb.len;
223
0
  if (!mkdtemp(sb.buf)) {
224
0
    int orig_errno = errno;
225
0
    strbuf_release(&sb);
226
0
    errno = orig_errno;
227
0
    return NULL;
228
0
  }
229
230
0
  strbuf_addf(&sb, "/%s", filename);
231
0
  fd = open(sb.buf, O_CREAT | O_EXCL | O_RDWR, 0600);
232
0
  if (fd < 0) {
233
0
    int orig_errno = errno;
234
0
    strbuf_setlen(&sb, directorylen);
235
0
    rmdir(sb.buf);
236
0
    strbuf_release(&sb);
237
0
    errno = orig_errno;
238
0
    return NULL;
239
0
  }
240
241
0
  tempfile = new_tempfile();
242
0
  strbuf_swap(&tempfile->filename, &sb);
243
0
  tempfile->directory = xmemdupz(tempfile->filename.buf, directorylen);
244
0
  tempfile->fd = fd;
245
0
  activate_tempfile(tempfile);
246
0
  return tempfile;
247
0
}
248
249
struct tempfile *xmks_tempfile_m(const char *filename_template, int mode)
250
0
{
251
0
  struct tempfile *tempfile;
252
0
  struct strbuf full_template = STRBUF_INIT;
253
254
0
  strbuf_add_absolute_path(&full_template, filename_template);
255
0
  tempfile = mks_tempfile_m(full_template.buf, mode);
256
0
  if (!tempfile)
257
0
    die_errno("Unable to create temporary file '%s'",
258
0
        full_template.buf);
259
260
0
  strbuf_release(&full_template);
261
0
  return tempfile;
262
0
}
263
264
FILE *fdopen_tempfile(struct tempfile *tempfile, const char *mode)
265
1.45k
{
266
1.45k
  if (!is_tempfile_active(tempfile))
267
0
    BUG("fdopen_tempfile() called for inactive object");
268
1.45k
  if (tempfile->fp)
269
0
    BUG("fdopen_tempfile() called for open object");
270
271
1.45k
  tempfile->fp = fdopen(tempfile->fd, mode);
272
1.45k
  return tempfile->fp;
273
1.45k
}
274
275
const char *get_tempfile_path(struct tempfile *tempfile)
276
106k
{
277
106k
  if (!is_tempfile_active(tempfile))
278
0
    BUG("get_tempfile_path() called for inactive object");
279
106k
  return tempfile->filename.buf;
280
106k
}
281
282
int get_tempfile_fd(struct tempfile *tempfile)
283
20.9k
{
284
20.9k
  if (!is_tempfile_active(tempfile))
285
0
    BUG("get_tempfile_fd() called for inactive object");
286
20.9k
  return tempfile->fd;
287
20.9k
}
288
289
FILE *get_tempfile_fp(struct tempfile *tempfile)
290
1.45k
{
291
1.45k
  if (!is_tempfile_active(tempfile))
292
0
    BUG("get_tempfile_fp() called for inactive object");
293
1.45k
  return tempfile->fp;
294
1.45k
}
295
296
int close_tempfile_gently(struct tempfile *tempfile)
297
120k
{
298
120k
  int fd;
299
120k
  FILE *fp;
300
120k
  int err;
301
302
120k
  if (!is_tempfile_active(tempfile) || tempfile->fd < 0)
303
57.4k
    return 0;
304
305
63.0k
  fd = tempfile->fd;
306
63.0k
  fp = tempfile->fp;
307
63.0k
  tempfile->fd = -1;
308
63.0k
  if (fp) {
309
1.45k
    tempfile->fp = NULL;
310
1.45k
    if (ferror(fp)) {
311
0
      err = -1;
312
0
      if (!fclose(fp))
313
0
        errno = EIO;
314
1.45k
    } else {
315
1.45k
      err = fclose(fp);
316
1.45k
    }
317
61.5k
  } else {
318
61.5k
    err = close(fd);
319
61.5k
  }
320
321
63.0k
  return err ? -1 : 0;
322
120k
}
323
324
int reopen_tempfile(struct tempfile *tempfile)
325
0
{
326
0
  if (!is_tempfile_active(tempfile))
327
0
    BUG("reopen_tempfile called for an inactive object");
328
0
  if (0 <= tempfile->fd)
329
0
    BUG("reopen_tempfile called for an open object");
330
0
  tempfile->fd = open(tempfile->filename.buf, O_WRONLY|O_TRUNC);
331
0
  return tempfile->fd;
332
0
}
333
334
int rename_tempfile(struct tempfile **tempfile_p, const char *path)
335
35.7k
{
336
35.7k
  struct tempfile *tempfile = *tempfile_p;
337
338
35.7k
  if (!is_tempfile_active(tempfile))
339
0
    BUG("rename_tempfile called for inactive object");
340
341
35.7k
  if (close_tempfile_gently(tempfile)) {
342
0
    delete_tempfile(tempfile_p);
343
0
    return -1;
344
0
  }
345
346
35.7k
  if (rename(tempfile->filename.buf, path)) {
347
0
    int save_errno = errno;
348
0
    delete_tempfile(tempfile_p);
349
0
    errno = save_errno;
350
0
    return -1;
351
0
  }
352
353
35.7k
  deactivate_tempfile(tempfile);
354
35.7k
  *tempfile_p = NULL;
355
35.7k
  return 0;
356
35.7k
}
357
358
int delete_tempfile(struct tempfile **tempfile_p)
359
63.0k
{
360
63.0k
  struct tempfile *tempfile = *tempfile_p;
361
63.0k
  int err = 0;
362
363
63.0k
  if (!is_tempfile_active(tempfile))
364
35.7k
    return 0;
365
366
27.2k
  err |= close_tempfile_gently(tempfile);
367
27.2k
  err |= unlink_or_warn(tempfile->filename.buf);
368
27.2k
  err |= remove_template_directory(tempfile, 0);
369
27.2k
  deactivate_tempfile(tempfile);
370
27.2k
  *tempfile_p = NULL;
371
372
27.2k
  return err ? -1 : 0;
373
63.0k
}