]> arthur.barton.de Git - netatalk.git/blob - libatalk/dsi/dsi_read.c
cd004dbca8e3056f26cd1764b94cbe80888fbdfb
[netatalk.git] / libatalk / dsi / dsi_read.c
1 /*
2  * $Id: dsi_read.c,v 1.6 2009-10-22 04:59:50 didg 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 #ifdef HAVE_SYS_FILIO_H
21 #include <sys/filio.h>
22 #endif
23
24 #include <atalk/dsi.h>
25 #include <sys/ioctl.h> 
26
27 #ifndef min
28 #define min(a,b)   ((a) < (b) ? (a) : (b))
29 #endif /* ! min */
30
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)
38 {
39
40   dsi->noreply = 1; /* we will handle our own replies */
41   dsi->header.dsi_flags = DSIFL_REPLY;
42   /*dsi->header.dsi_command = DSIFUNC_CMD;*/
43   dsi->header.dsi_len = htonl(size);
44   dsi->header.dsi_code = htonl(err);
45
46   sigprocmask(SIG_BLOCK, &dsi->sigblockset, &dsi->oldset);
47   dsi->sigblocked = 1;
48   dsi->in_write++;
49   if (dsi_stream_send(dsi, buf, buflen)) {
50     dsi->datasize = size - buflen;
51     return min(dsi->datasize, buflen);
52   }
53
54   return -1; /* error */
55 }
56
57 void dsi_readdone(DSI *dsi)
58 {
59   sigprocmask(SIG_SETMASK, &dsi->oldset, NULL);
60   dsi->sigblocked = 0;
61   dsi->in_write--;
62 }
63
64 /* */
65 int dsi_block(DSI *dsi, const int mode)
66 {
67 #if 0
68     dsi->noblocking = mode;
69     return 0;
70 #else
71     int adr = mode;
72     int ret;
73     
74     ret = ioctl(dsi->socket, FIONBIO, &adr);
75     if (!ret) {
76         dsi->noblocking = mode;
77     }
78     return ret;
79 #endif    
80 }
81
82 /* send off the data */
83 ssize_t dsi_read(DSI *dsi, void *buf, const size_t buflen)
84 {
85   size_t len;
86   int delay = (dsi->datasize != buflen)?1:0;
87   
88   len  = dsi_stream_write(dsi, buf, buflen, delay);
89
90   if (len == buflen) {
91     dsi->datasize -= len;
92     return min(dsi->datasize, buflen);
93   }
94
95   return -1;
96 }