2 * $Id: dsi_read.c,v 1.3.14.3 2004-02-20 20:53:15 bfernhomberg Exp $
4 * Copyright (c) 1997 Adrian Sun (asun@zoology.washington.edu)
5 * All rights reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
16 #endif /* HAVE_UNISTD_H */
18 #include <sys/types.h>
20 #ifdef HAVE_SYS_FILIO_H
21 #include <sys/filio.h>
24 #include <atalk/dsi.h>
25 #include <sys/ioctl.h>
28 #define min(a,b) ((a) < (b) ? (a) : (b))
31 /* streaming i/o for afp_read. this is all from the perspective of the
32 * client. it basically does the reverse of dsi_write. on first entry,
33 * it will send off the header plus whatever is in its command
34 * buffer. it returns the amount of stuff still to be read
35 * (constrained by the buffer size). */
36 ssize_t dsi_readinit(DSI *dsi, void *buf, const size_t buflen,
37 const size_t size, const int err)
39 const struct itimerval none = {{0, 0}, {0, 0}};
41 dsi->noreply = 1; /* we will handle our own replies */
42 dsi->header.dsi_flags = DSIFL_REPLY;
43 /*dsi->header.dsi_command = DSIFUNC_CMD;*/
44 dsi->header.dsi_len = htonl(size);
45 dsi->header.dsi_code = htonl(err);
47 sigprocmask(SIG_BLOCK, &dsi->sigblockset, &dsi->oldset);
49 setitimer(ITIMER_REAL, &none, &dsi->savetimer);
51 if (dsi_stream_send(dsi, buf, buflen)) {
52 dsi->datasize = size - buflen;
53 return min(dsi->datasize, buflen);
56 return -1; /* error */
59 void dsi_readdone(DSI *dsi)
61 setitimer(ITIMER_REAL, &dsi->savetimer, NULL);
62 sigprocmask(SIG_SETMASK, &dsi->oldset, NULL);
67 int dsi_block(DSI *dsi, const int mode)
70 dsi->noblocking = mode;
76 ret = ioctl(dsi->socket, FIONBIO, &adr);
78 dsi->noblocking = mode;
84 /* send off the data */
85 ssize_t dsi_read(DSI *dsi, void *buf, const size_t buflen)
88 int delay = (dsi->datasize != buflen)?1:0;
90 len = dsi_stream_write(dsi, buf, buflen, delay);
94 return min(dsi->datasize, buflen);