]> arthur.barton.de Git - netatalk.git/blob - libatalk/dsi/dsi_read.c
Solaris 'compile hell' stuff...
[netatalk.git] / libatalk / dsi / dsi_read.c
1 /*
2  * $Id: dsi_read.c,v 1.3.14.3 2004-02-20 20:53:15 bfernhomberg 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   const struct itimerval none = {{0, 0}, {0, 0}};
40
41   dsi->noreply = 1; /* we will handle our own replies */
42   dsi->header.dsi_flags = DSIFL_REPLY;
43   /*dsi->header.dsi_command = DSIFUNC_CMD;*/
44   dsi->header.dsi_len = htonl(size);
45   dsi->header.dsi_code = htonl(err);
46
47   sigprocmask(SIG_BLOCK, &dsi->sigblockset, &dsi->oldset);
48   dsi->sigblocked = 1;
49   setitimer(ITIMER_REAL, &none, &dsi->savetimer);
50   
51   if (dsi_stream_send(dsi, buf, buflen)) {
52     dsi->datasize = size - buflen;
53     return min(dsi->datasize, buflen);
54   }
55
56   return -1; /* error */
57 }
58
59 void dsi_readdone(DSI *dsi)
60 {
61   setitimer(ITIMER_REAL, &dsi->savetimer, NULL);
62   sigprocmask(SIG_SETMASK, &dsi->oldset, NULL);
63   dsi->sigblocked = 0;
64 }
65
66 /* */
67 int dsi_block(DSI *dsi, const int mode)
68 {
69 #if 0
70     dsi->noblocking = mode;
71     return 0;
72 #else
73     int adr = mode;
74     int ret;
75     
76     ret = ioctl(dsi->socket, FIONBIO, &adr);
77     if (!ret) {
78         dsi->noblocking = mode;
79     }
80     return ret;
81 #endif    
82 }
83
84 /* send off the data */
85 ssize_t dsi_read(DSI *dsi, void *buf, const size_t buflen)
86 {
87   size_t len;
88   int delay = (dsi->datasize != buflen)?1:0;
89   
90   len  = dsi_stream_write(dsi, buf, buflen, delay);
91
92   if (len == buflen) {
93     dsi->datasize -= len;
94     return min(dsi->datasize, buflen);
95   }
96
97   return -1;
98 }