]> arthur.barton.de Git - netatalk.git/blob - libatalk/dsi/dsi_read.c
implemented config.h
[netatalk.git] / libatalk / dsi / dsi_read.c
1 /*
2  * Copyright (c) 1997 Adrian Sun (asun@zoology.washington.edu)
3  * All rights reserved. See COPYRIGHT.
4  */
5
6 #ifdef HAVE_CONFIG_H
7 #include "config.h"
8 #endif
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <signal.h>
14 #include <sys/types.h>
15 #include <sys/time.h>
16
17 #include <atalk/dsi.h>
18
19 #ifndef min
20 #define min(a,b)   ((a) < (b) ? (a) : (b))
21 #endif
22
23 /* streaming i/o for afp_read. this is all from the perspective of the
24  * client. it basically does the reverse of dsi_write. on first entry,
25  * it will send off the header plus whatever is in its command
26  * buffer. it returns the amount of stuff still to be read
27  * (constrained by the buffer size). */
28 ssize_t dsi_readinit(DSI *dsi, void *buf, const size_t buflen,
29                     const size_t size, const int err)
30 {
31   const struct itimerval none = {{0, 0}, {0, 0}};
32
33   dsi->noreply = 1; /* we will handle our own replies */
34   dsi->header.dsi_flags = DSIFL_REPLY;
35   /*dsi->header.dsi_command = DSIFUNC_CMD;*/
36   dsi->header.dsi_len = htonl(size);
37   dsi->header.dsi_code = htonl(err);
38
39   sigprocmask(SIG_BLOCK, &dsi->sigblockset, NULL);
40   setitimer(ITIMER_REAL, &none, &dsi->savetimer);
41   if (dsi_stream_send(dsi, buf, buflen)) {
42     dsi->datasize = size - buflen;
43     return min(dsi->datasize, buflen);
44   }
45
46   return -1; /* error */
47 }
48
49 void dsi_readdone(DSI *dsi)
50 {
51   setitimer(ITIMER_REAL, &dsi->savetimer, NULL);
52   sigprocmask(SIG_UNBLOCK, &dsi->sigblockset, NULL);
53 }
54
55 /* send off the data */
56 ssize_t dsi_read(DSI *dsi, void *buf, const size_t buflen)
57 {
58   size_t len = dsi_stream_write(dsi, buf, buflen);
59
60   if (len == buflen) {
61     dsi->datasize -= len;
62     return min(dsi->datasize, buflen);
63   }
64
65   return -1;
66 }