3 int copy_fd(int ifd
, int ofd
)
8 ssize_t len
= xread(ifd
, buffer
, sizeof(buffer
));
12 int save_errno
= errno
;
14 error("copy-fd: read returned %s",
15 strerror(save_errno
));
20 int written
= xwrite(ofd
, buf
, len
);
27 error("copy-fd: write returned 0");
31 int save_errno
= errno
;
33 error("copy-fd: write returned %s",
34 strerror(save_errno
));
44 static int copy_times(const char *dst
, const char *src
)
48 if (stat(src
, &st
) < 0)
50 times
.actime
= st
.st_atime
;
51 times
.modtime
= st
.st_mtime
;
52 if (utime(dst
, ×
) < 0)
57 int copy_file(const char *dst
, const char *src
, int mode
)
61 mode
= (mode
& 0111) ? 0777 : 0666;
62 if ((fdi
= open(src
, O_RDONLY
)) < 0)
64 if ((fdo
= open(dst
, O_WRONLY
| O_CREAT
| O_EXCL
, mode
)) < 0) {
68 status
= copy_fd(fdi
, fdo
);
70 return error("%s: close error: %s", dst
, strerror(errno
));
72 if (!status
&& adjust_shared_perm(dst
))
78 int copy_file_with_time(const char *dst
, const char *src
, int mode
)
80 int status
= copy_file(dst
, src
, mode
);
82 return copy_times(dst
, src
);