3 * Copyright (c) 1997 Adrian Sun (asun@zoology.washington.edu)
4 * All rights reserved. See COPYRIGHT.
6 * 7 Oct 1997 added checks for 0 data.
11 #endif /* HAVE_CONFIG_H */
13 /* this streams writes */
16 #include <sys/types.h>
22 #include <atalk/dsi.h>
23 #include <atalk/util.h>
24 #include <atalk/logger.h>
26 /* initialize relevant things for dsi_write. this returns the amount
27 * of data in the data buffer. the interface has been reworked to allow
28 * for arbitrary buffers. */
29 size_t dsi_writeinit(DSI *dsi, void *buf, const size_t buflen _U_)
33 /* figure out how much data we have. do a couple checks for 0
35 header = ntohl(dsi->header.dsi_doff);
36 dsi->datasize = header ? ntohl(dsi->header.dsi_len) - header : 0;
38 if (dsi->datasize > 0) {
39 len = MIN(dsi->server_quantum - header, dsi->datasize);
41 /* write last part of command buffer into buf */
42 memmove(buf, dsi->commands + header, len);
44 /* recalculate remaining data */
49 LOG(log_maxdebug, logtype_dsi, "dsi_writeinit: len: %ju, remaining DSI datasize: %jd",
50 (intmax_t)len, (intmax_t)dsi->datasize);
55 /* fill up buf and then return. this should be called repeatedly
56 * until all the data has been read. i block alarm processing
57 * during the transfer to avoid sending unnecessary tickles. */
58 size_t dsi_write(DSI *dsi, void *buf, const size_t buflen)
62 LOG(log_maxdebug, logtype_dsi, "dsi_write: remaining DSI datasize: %jd", (intmax_t)dsi->datasize);
64 if ((length = MIN(buflen, dsi->datasize)) > 0) {
65 if ((length = dsi_stream_read(dsi, buf, length)) > 0) {
66 LOG(log_maxdebug, logtype_dsi, "dsi_write: received: %ju", (intmax_t)length);
67 dsi->datasize -= length;
74 /* flush any unread buffers. */
75 void dsi_writeflush(DSI *dsi)
79 while (dsi->datasize > 0) {
80 length = dsi_stream_read(dsi, dsi->data,
81 MIN(sizeof(dsi->data), dsi->datasize));
83 dsi->datasize -= length;