🌐 AI搜索 & 代理 主页
blob: f2970ec46282a07593366c598f688e5d70c05c83 [file] [log] [blame]
Junio C Hamanof3123c42005-10-22 08:28:131#include "cache.h"
2
3int copy_fd(int ifd, int ofd)
4{
5 while (1) {
Junio C Hamanof3123c42005-10-22 08:28:136 char buffer[8192];
Johan Herland8a912bc2007-05-15 12:49:227 ssize_t len = xread(ifd, buffer, sizeof(buffer));
Junio C Hamanof3123c42005-10-22 08:28:138 if (!len)
9 break;
10 if (len < 0) {
Junio C Hamanof3123c42005-10-22 08:28:1311 return error("copy-fd: read returned %s",
Steffen Prohaskab29763a2014-08-26 15:23:2412 strerror(errno));
Junio C Hamanof3123c42005-10-22 08:28:1313 }
Steffen Prohaskab29763a2014-08-26 15:23:2414 if (write_in_full(ofd, buffer, len) < 0)
15 return error("copy-fd: write returned %s",
16 strerror(errno));
Junio C Hamanof3123c42005-10-22 08:28:1317 }
Junio C Hamanof3123c42005-10-22 08:28:1318 return 0;
19}
Daniel Barkalow1468bd42008-02-25 19:24:4820
Clemens Buchacherf7835a22009-09-12 09:03:4821static int copy_times(const char *dst, const char *src)
22{
23 struct stat st;
24 struct utimbuf times;
25 if (stat(src, &st) < 0)
26 return -1;
27 times.actime = st.st_atime;
28 times.modtime = st.st_mtime;
29 if (utime(dst, &times) < 0)
30 return -1;
31 return 0;
32}
33
Daniel Barkalow1468bd42008-02-25 19:24:4834int copy_file(const char *dst, const char *src, int mode)
35{
36 int fdi, fdo, status;
37
38 mode = (mode & 0111) ? 0777 : 0666;
39 if ((fdi = open(src, O_RDONLY)) < 0)
40 return fdi;
41 if ((fdo = open(dst, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0) {
42 close(fdi);
43 return fdo;
44 }
45 status = copy_fd(fdi, fdo);
Steffen Prohaskab29763a2014-08-26 15:23:2446 close(fdi);
Daniel Barkalow1468bd42008-02-25 19:24:4847 if (close(fdo) != 0)
Ariel Badichi8b1f6de2008-04-23 01:05:2948 return error("%s: close error: %s", dst, strerror(errno));
Daniel Barkalow1468bd42008-02-25 19:24:4849
50 if (!status && adjust_shared_perm(dst))
51 return -1;
52
53 return status;
54}
Clemens Buchacherf7835a22009-09-12 09:03:4855
56int copy_file_with_time(const char *dst, const char *src, int mode)
57{
58 int status = copy_file(dst, src, mode);
59 if (!status)
60 return copy_times(dst, src);
61 return status;
62}