]> arthur.barton.de Git - netatalk.git/blob - libatalk/dsi/dsi_write.c
dc35cafa9307a76df2f407dddf9f2f4c704b577a
[netatalk.git] / libatalk / dsi / dsi_write.c
1 /*
2  *
3  * Copyright (c) 1997 Adrian Sun (asun@zoology.washington.edu)
4  * All rights reserved. See COPYRIGHT.
5  *
6  * 7 Oct 1997 added checks for 0 data.
7  */
8
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif /* HAVE_CONFIG_H */
12
13 /* this streams writes */
14 #include <stdio.h>
15 #include <unistd.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <sys/time.h>
19 #include <fcntl.h>
20 #include <string.h>
21
22 #include <atalk/dsi.h>
23 #include <atalk/util.h>
24 #include <atalk/logger.h>
25
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_)
30 {
31   size_t len, header;
32
33   /* figure out how much data we have. do a couple checks for 0 
34    * data */
35   header = ntohl(dsi->header.dsi_data.dsi_doff);
36   dsi->datasize = header ? ntohl(dsi->header.dsi_len) - header : 0;
37
38   if (dsi->datasize > 0) {
39       len = MIN(dsi->server_quantum - header, dsi->datasize);
40
41       /* write last part of command buffer into buf */
42       memmove(buf, dsi->commands + header, len);
43
44       /* recalculate remaining data */
45       dsi->datasize -= len;
46   } else
47     len = 0;
48
49   LOG(log_maxdebug, logtype_dsi, "dsi_writeinit: len: %ju, remaining DSI datasize: %jd",
50       (intmax_t)len, (intmax_t)dsi->datasize);
51
52   return len;
53 }
54
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)
59 {
60   size_t length;
61
62   LOG(log_maxdebug, logtype_dsi, "dsi_write: remaining DSI datasize: %jd", (intmax_t)dsi->datasize);
63
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;
68           return length;
69       }
70   }
71   return 0;
72 }
73
74 /* flush any unread buffers. */
75 void dsi_writeflush(DSI *dsi)
76 {
77   size_t length;
78
79   while (dsi->datasize > 0) { 
80     length = dsi_stream_read(dsi, dsi->data,
81                              MIN(sizeof(dsi->data), dsi->datasize));
82     if (length > 0)
83       dsi->datasize -= length;
84     else
85       break;
86   }
87 }