gitattributes: Fix subdirectory attributes specified from root directory
[git/git.git] / copy.c
CommitLineData
f3123c4a
JH
1#include "cache.h"
2
3int copy_fd(int ifd, int ofd)
4{
5 while (1) {
f3123c4a
JH
6 char buffer[8192];
7 char *buf = buffer;
8a912bcb 8 ssize_t len = xread(ifd, buffer, sizeof(buffer));
f3123c4a
JH
9 if (!len)
10 break;
11 if (len < 0) {
e6c64fc1 12 int read_error;
e6c64fc1
JH
13 read_error = errno;
14 close(ifd);
f3123c4a 15 return error("copy-fd: read returned %s",
e6c64fc1 16 strerror(read_error));
f3123c4a 17 }
1c15afb9
JH
18 while (len) {
19 int written = xwrite(ofd, buf, len);
f3123c4a
JH
20 if (written > 0) {
21 buf += written;
22 len -= written;
f3123c4a 23 }
08337a97
SR
24 else if (!written) {
25 close(ifd);
f3123c4a 26 return error("copy-fd: write returned 0");
08337a97
SR
27 } else {
28 close(ifd);
1c15afb9
JH
29 return error("copy-fd: write returned %s",
30 strerror(errno));
08337a97 31 }
f3123c4a
JH
32 }
33 }
34 close(ifd);
35 return 0;
36}
1468bd47
DB
37
38int 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: write error: %s", dst, strerror(errno));
52
53 if (!status && adjust_shared_perm(dst))
54 return -1;
55
56 return status;
57}