Coverage Report

Created: 2026-10-17 07:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/copy.c
Line
Count
Source
1
#include "git-compat-util.h"
2
#include "copy.h"
3
#include "path.h"
4
#include "gettext.h"
5
#include "strbuf.h"
6
#include "abspath.h"
7
8
int copy_fd(int ifd, int ofd)
9
0
{
10
0
  while (1) {
11
0
    char buffer[8192];
12
0
    ssize_t len = xread(ifd, buffer, sizeof(buffer));
13
0
    if (!len)
14
0
      break;
15
0
    if (len < 0)
16
0
      return COPY_READ_ERROR;
17
0
    if (write_in_full(ofd, buffer, len) < 0)
18
0
      return COPY_WRITE_ERROR;
19
0
  }
20
0
  return 0;
21
0
}
22
23
static int copy_times(const char *dst, const char *src)
24
0
{
25
0
  struct stat st;
26
0
  struct utimbuf times;
27
0
  if (stat(src, &st) < 0)
28
0
    return -1;
29
0
  times.actime = st.st_atime;
30
0
  times.modtime = st.st_mtime;
31
0
  if (utime(dst, &times) < 0)
32
0
    return -1;
33
0
  return 0;
34
0
}
35
36
int copy_file(struct repository *repo,
37
        const char *dst, const char *src, int mode)
38
0
{
39
0
  int fdi, fdo, status;
40
41
0
  mode = (mode & 0111) ? 0777 : 0666;
42
0
  if ((fdi = open(src, O_RDONLY)) < 0)
43
0
    return fdi;
44
0
  if ((fdo = open(dst, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0) {
45
0
    close(fdi);
46
0
    return fdo;
47
0
  }
48
0
  status = copy_fd(fdi, fdo);
49
0
  switch (status) {
50
0
  case COPY_READ_ERROR:
51
0
    error_errno("copy-fd: read returned");
52
0
    break;
53
0
  case COPY_WRITE_ERROR:
54
0
    error_errno("copy-fd: write returned");
55
0
    break;
56
0
  }
57
0
  close(fdi);
58
0
  if (close(fdo) != 0)
59
0
    return error_errno("%s: close error", dst);
60
61
0
  if (!status && adjust_shared_perm(repo, dst))
62
0
    return -1;
63
64
0
  return status;
65
0
}
66
67
int copy_file_with_time(struct repository *repo,
68
      const char *dst, const char *src, int mode)
69
0
{
70
0
  int status = copy_file(repo, dst, src, mode);
71
0
  if (!status)
72
0
    return copy_times(dst, src);
73
0
  return status;
74
0
}