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