xdiff: give up scanning similar lines early
[git] / copy.c
1 #include "cache.h"
2
3 int copy_fd(int ifd, int ofd)
4 {
5         while (1) {
6                 char buffer[8192];
7                 char *buf = buffer;
8                 ssize_t len = xread(ifd, buffer, sizeof(buffer));
9                 if (!len)
10                         break;
11                 if (len < 0) {
12                         int read_error = errno;
13                         close(ifd);
14                         return error("copy-fd: read returned %s",
15                                      strerror(read_error));
16                 }
17                 while (len) {
18                         int written = xwrite(ofd, buf, len);
19                         if (written > 0) {
20                                 buf += written;
21                                 len -= written;
22                         }
23                         else if (!written) {
24                                 close(ifd);
25                                 return error("copy-fd: write returned 0");
26                         } else {
27                                 int write_error = errno;
28                                 close(ifd);
29                                 return error("copy-fd: write returned %s",
30                                              strerror(write_error));
31                         }
32                 }
33         }
34         close(ifd);
35         return 0;
36 }
37
38 int copy_file(const char *dst, const char *src, int mode)
39 {
40         int fdi, fdo, status;
41
42         mode = (mode & 0111) ? 0777 : 0666;
43         if ((fdi = open(src, O_RDONLY)) < 0)
44                 return fdi;
45         if ((fdo = open(dst, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0) {
46                 close(fdi);
47                 return fdo;
48         }
49         status = copy_fd(fdi, fdo);
50         if (close(fdo) != 0)
51                 return error("%s: close error: %s", dst, strerror(errno));
52
53         if (!status && adjust_shared_perm(dst))
54                 return -1;
55
56         return status;
57 }