3 int copy_fd(int ifd
, int ofd
)
8 ssize_t len
= xread(ifd
, buffer
, sizeof(buffer
));
12 int read_error
= errno
;
14 return error("copy-fd: read returned %s",
15 strerror(read_error
));
18 int written
= xwrite(ofd
, buf
, len
);
25 return error("copy-fd: write returned 0");
27 int write_error
= errno
;
29 return error("copy-fd: write returned %s",
30 strerror(write_error
));
38 static int copy_times(const char *dst
, const char *src
)
42 if (stat(src
, &st
) < 0)
44 times
.actime
= st
.st_atime
;
45 times
.modtime
= st
.st_mtime
;
46 if (utime(dst
, ×
) < 0)
51 int copy_file(const char *dst
, const char *src
, int mode
)
55 mode
= (mode
& 0111) ? 0777 : 0666;
56 if ((fdi
= open(src
, O_RDONLY
)) < 0)
58 if ((fdo
= open(dst
, O_WRONLY
| O_CREAT
| O_EXCL
, mode
)) < 0) {
62 status
= copy_fd(fdi
, fdo
);
64 return error("%s: close error: %s", dst
, strerror(errno
));
66 if (!status
&& adjust_shared_perm(dst
))
72 int copy_file_with_time(const char *dst
, const char *src
, int mode
)
74 int status
= copy_file(dst
, src
, mode
);
76 return copy_times(dst
, src
);