Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 79 lines (71 sloc) 1.59 kb
f3123c4 pack-objects: Allow use of pre-generated pack.
Junio C Hamano authored
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;
8a912bc @jherland Ensure return value from xread() is always stored into an ssize_t
jherland authored
8 ssize_t len = xread(ifd, buffer, sizeof(buffer));
f3123c4 pack-objects: Allow use of pre-generated pack.
Junio C Hamano authored
9 if (!len)
10 break;
11 if (len < 0) {
8b1f6de copy.c: copy_fd - correctly report write errors
Ariel Badichi authored
12 int read_error = errno;
e6c64fc copy.c::copy_fd() - do not leak file descriptor on error return.
Junio C Hamano authored
13 close(ifd);
f3123c4 pack-objects: Allow use of pre-generated pack.
Junio C Hamano authored
14 return error("copy-fd: read returned %s",
e6c64fc copy.c::copy_fd() - do not leak file descriptor on error return.
Junio C Hamano authored
15 strerror(read_error));
f3123c4 pack-objects: Allow use of pre-generated pack.
Junio C Hamano authored
16 }
1c15afb xread/xwrite: do not worry about EINTR at calling sites.
Junio C Hamano authored
17 while (len) {
18 int written = xwrite(ofd, buf, len);
f3123c4 pack-objects: Allow use of pre-generated pack.
Junio C Hamano authored
19 if (written > 0) {
20 buf += written;
21 len -= written;
22 }
08337a9 copy_fd: close ifd on error
Sam Ravnborg authored
23 else if (!written) {
24 close(ifd);
f3123c4 pack-objects: Allow use of pre-generated pack.
Junio C Hamano authored
25 return error("copy-fd: write returned 0");
08337a9 copy_fd: close ifd on error
Sam Ravnborg authored
26 } else {
8b1f6de copy.c: copy_fd - correctly report write errors
Ariel Badichi authored
27 int write_error = errno;
08337a9 copy_fd: close ifd on error
Sam Ravnborg authored
28 close(ifd);
1c15afb xread/xwrite: do not worry about EINTR at calling sites.
Junio C Hamano authored
29 return error("copy-fd: write returned %s",
8b1f6de copy.c: copy_fd - correctly report write errors
Ariel Badichi authored
30 strerror(write_error));
08337a9 copy_fd: close ifd on error
Sam Ravnborg authored
31 }
f3123c4 pack-objects: Allow use of pre-generated pack.
Junio C Hamano authored
32 }
33 }
34 close(ifd);
35 return 0;
36 }
1468bd4 Use a single implementation and API for copy_file()
Daniel Barkalow authored
37
f7835a2 @drizzd preserve mtime of local clone
drizzd authored
38 static int copy_times(const char *dst, const char *src)
39 {
40 struct stat st;
41 struct utimbuf times;
42 if (stat(src, &st) < 0)
43 return -1;
44 times.actime = st.st_atime;
45 times.modtime = st.st_mtime;
46 if (utime(dst, &times) < 0)
47 return -1;
48 return 0;
49 }
50
1468bd4 Use a single implementation and API for copy_file()
Daniel Barkalow authored
51 int copy_file(const char *dst, const char *src, int mode)
52 {
53 int fdi, fdo, status;
54
55 mode = (mode & 0111) ? 0777 : 0666;
56 if ((fdi = open(src, O_RDONLY)) < 0)
57 return fdi;
58 if ((fdo = open(dst, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0) {
59 close(fdi);
60 return fdo;
61 }
62 status = copy_fd(fdi, fdo);
63 if (close(fdo) != 0)
8b1f6de copy.c: copy_fd - correctly report write errors
Ariel Badichi authored
64 return error("%s: close error: %s", dst, strerror(errno));
1468bd4 Use a single implementation and API for copy_file()
Daniel Barkalow authored
65
66 if (!status && adjust_shared_perm(dst))
67 return -1;
68
69 return status;
70 }
f7835a2 @drizzd preserve mtime of local clone
drizzd authored
71
72 int copy_file_with_time(const char *dst, const char *src, int mode)
73 {
74 int status = copy_file(dst, src, mode);
75 if (!status)
76 return copy_times(dst, src);
77 return status;
78 }
Something went wrong with that request. Please try again.