Don't chop IO into small pieces, follow cp(1) and just use MAXPHYS.
This commit is contained in:
parent
3ddd7d96bb
commit
a23ffe9673
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=225954
22
bin/mv/mv.c
22
bin/mv/mv.c
@ -260,40 +260,34 @@ static int
|
|||||||
fastcopy(const char *from, const char *to, struct stat *sbp)
|
fastcopy(const char *from, const char *to, struct stat *sbp)
|
||||||
{
|
{
|
||||||
struct timeval tval[2];
|
struct timeval tval[2];
|
||||||
static u_int blen;
|
static u_int blen = MAXPHYS;
|
||||||
static char *bp;
|
static char *bp = NULL;
|
||||||
mode_t oldmode;
|
mode_t oldmode;
|
||||||
int nread, from_fd, to_fd;
|
int nread, from_fd, to_fd;
|
||||||
|
|
||||||
if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
|
if ((from_fd = open(from, O_RDONLY, 0)) < 0) {
|
||||||
warn("%s", from);
|
warn("fastcopy: open() failed (from): %s", from);
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
if (blen < sbp->st_blksize) {
|
if (bp == NULL && (bp = malloc((size_t)blen)) == NULL) {
|
||||||
if (bp != NULL)
|
warnx("malloc(%u) failed", blen);
|
||||||
free(bp);
|
|
||||||
if ((bp = malloc((size_t)sbp->st_blksize)) == NULL) {
|
|
||||||
blen = 0;
|
|
||||||
warnx("malloc failed");
|
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
blen = sbp->st_blksize;
|
|
||||||
}
|
|
||||||
while ((to_fd =
|
while ((to_fd =
|
||||||
open(to, O_CREAT | O_EXCL | O_TRUNC | O_WRONLY, 0)) < 0) {
|
open(to, O_CREAT | O_EXCL | O_TRUNC | O_WRONLY, 0)) < 0) {
|
||||||
if (errno == EEXIST && unlink(to) == 0)
|
if (errno == EEXIST && unlink(to) == 0)
|
||||||
continue;
|
continue;
|
||||||
warn("%s", to);
|
warn("fastcopy: open() failed (to): %s", to);
|
||||||
(void)close(from_fd);
|
(void)close(from_fd);
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
while ((nread = read(from_fd, bp, (size_t)blen)) > 0)
|
while ((nread = read(from_fd, bp, (size_t)blen)) > 0)
|
||||||
if (write(to_fd, bp, (size_t)nread) != nread) {
|
if (write(to_fd, bp, (size_t)nread) != nread) {
|
||||||
warn("%s", to);
|
warn("fastcopy: write() failed: %s", to);
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
if (nread < 0) {
|
if (nread < 0) {
|
||||||
warn("%s", from);
|
warn("fastcopy: read() failed: %s", from);
|
||||||
err: if (unlink(to))
|
err: if (unlink(to))
|
||||||
warn("%s: remove", to);
|
warn("%s: remove", to);
|
||||||
(void)close(from_fd);
|
(void)close(from_fd);
|
||||||
|
Loading…
Reference in New Issue
Block a user