/*-
* Copyright (c) 2006 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Steve C. Woodford.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This looks weird for a reason. The toppy protocol allows for data to be
* transferred in 65535-byte chunks only. Anything more than this has to be
* split within the driver. The following value leaves enough space for the
* packet header plus some alignmnent slop.
*/
#define TOPPY_IO_SIZE 0xffec
if ((buf = malloc(TOPPY_IO_SIZE)) == NULL)
err(EX_OSERR, "malloc(TOPPY_IO_SIZE)");
if (strcmp(dst, "-") == 0) {
ofp = stdout;
to = stderr;
if (reget)
warnx("Ignoring -r option in combination with stdout");
} else {
to = stdout;
if (reget) {
if (stat(dst, &st) < 0) {
if (errno != ENOENT)
err(EX_OSERR, "stat(%s)", dst);
} else
if (!S_ISREG(st.st_mode))
errx(EX_DATAERR, "-r only works with regular "
"files");
else
ur.ur_offset = st.st_size;
}
if ((ofp = fopen(dst, reget ? "a" : "w")) == NULL)
err(EX_OSERR, "fopen(%s)", dst);
}
if (progbar) {
if (find_toppy_dirent(ur.ur_path, &ud) == 0)
ud.ud_size = 0;
init_progress(to, dst, ud.ud_size, ur.ur_offset);
}
if (ioctl(toppy_fd, UTOPPYIOTURBO, &turbo_mode) < 0)
err(EX_OSERR, "ioctl(UTOPPYIOTURBO, %d)", turbo_mode);
if (ioctl(toppy_fd, UTOPPYIOREADFILE, &ur) < 0)
err(EX_OSERR, "ioctl(UTOPPYIOREADFILE, %s)", ur.ur_path);
if (progbar)
progressmeter(-1);
for (;;) {
while ((l = read(toppy_fd, buf, TOPPY_IO_SIZE)) < 0 &&
errno == EINTR)
;
if (l <= 0)
break;
rv = fwrite(buf, 1, l, ofp);
if (rv != (size_t)l) {
if (ofp != stdout)
fclose(ofp);
progressmeter(1);
err(EX_OSERR, "fwrite(%s)", dst);
}
bytes += l;
}
if (progbar)
progressmeter(1);
if (ofp != stdout)
fclose(ofp);
if (l < 0)
err(EX_OSERR, "read(TOPPY: ur.ur_path)");
if (find_toppy_dirent(uw.uw_path, &ud)) {
if (ud.ud_type == UTOPPY_DIRENT_DIRECTORY) {
snprintf(dstbuf, sizeof(dstbuf), "%s/%s", uw.uw_path,
basename(src));
uw.uw_path = dstbuf;
} else
if (ud.ud_type != UTOPPY_DIRENT_FILE)
errx(EX_DATAERR, "'%s' is not a regular file.",
uw.uw_path);
else
if (reput) {
if (ud.ud_size > uw.uw_size)
errx(EX_DATAERR, "'%s' is already larger than "
"'%s'", uw.uw_path, src);