Coverage Report

Created: 2025-12-31 07:01

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