]> arthur.barton.de Git - netatalk.git/blob - libatalk/dsi/dsi_write.c
Initial revision
[netatalk.git] / libatalk / dsi / dsi_write.c
1 /*
2  * Copyright (c) 1997 Adrian Sun (asun@zoology.washington.edu)
3  * All rights reserved. See COPYRIGHT.
4  *
5  * 7 Oct 1997 added checks for 0 data.
6  */
7
8 /* this streams writes */
9 #include <stdio.h>
10 #include <unistd.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <sys/time.h>
14 #include <fcntl.h>
15 #include <string.h>
16
17 #include <atalk/dsi.h>
18 #include <netatalk/endian.h>
19
20 #ifndef MIN
21 #define MIN(a,b)     ((a) < (b) ? (a) : (b))
22 #endif
23
24 /* initialize relevant things for dsi_write. this returns the amount
25  * of data in the data buffer. the interface has been reworked to allow
26  * for arbitrary buffers. */
27 size_t dsi_writeinit(DSI *dsi, void *buf, const size_t buflen)
28 {
29   const struct itimerval none = {{0, 0}, {0, 0}};
30   size_t len, header;
31
32   /* figure out how much data we have. do a couple checks for 0 
33    * data */
34   header = ntohl(dsi->header.dsi_code);
35   dsi->datasize = header ? ntohl(dsi->header.dsi_len) - header : 0;
36   if (dsi->datasize > 0) {
37     len = MIN(sizeof(dsi->commands) - header, dsi->datasize);
38     
39     /* write last part of command buffer into buf */
40     memcpy(buf, dsi->commands + header, len);
41     
42     /* recalculate remaining data */
43     dsi->datasize -= len;
44   } else
45     len = 0;
46
47   /* deal with signals. i'm doing it this way to ensure that we don't
48    * get confused if a writeflush on zero remaining data is, for some
49    * reason, needed. */
50   sigprocmask(SIG_BLOCK, &dsi->sigblockset, NULL);
51   setitimer(ITIMER_REAL, &none, &dsi->savetimer);
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   if (((length = MIN(buflen, dsi->datasize)) > 0) &&
63       ((length = dsi_stream_read(dsi, buf, length)) > 0)) {
64     dsi->datasize -= length;
65     return length;
66   }
67
68   setitimer(ITIMER_REAL, &dsi->savetimer, NULL);
69   sigprocmask(SIG_UNBLOCK, &dsi->sigblockset, NULL);
70   return 0;
71 }
72
73 /* flush any unread buffers. */
74 void dsi_writeflush(DSI *dsi)
75 {
76   size_t length;
77
78   while (dsi->datasize > 0) { 
79     length = dsi_stream_read(dsi, dsi->data,
80                              MIN(sizeof(dsi->data), dsi->datasize));
81     if (length > 0)
82       dsi->datasize -= length;
83     else
84       break;
85   }
86
87   setitimer(ITIMER_REAL, &dsi->savetimer, NULL);
88   sigprocmask(SIG_UNBLOCK, &dsi->sigblockset, NULL);
89 }