]> arthur.barton.de Git - netatalk.git/blob - libatalk/dsi/dsi_write.c
Fix data corruption bug
[netatalk.git] / libatalk / dsi / dsi_write.c
1 /*
2  * $Id: dsi_write.c,v 1.5 2009-10-20 04:31:41 didg Exp $
3  *
4  * Copyright (c) 1997 Adrian Sun (asun@zoology.washington.edu)
5  * All rights reserved. See COPYRIGHT.
6  *
7  * 7 Oct 1997 added checks for 0 data.
8  */
9
10 #ifdef HAVE_CONFIG_H
11 #include "config.h"
12 #endif /* HAVE_CONFIG_H */
13
14 /* this streams writes */
15 #include <stdio.h>
16 #include <unistd.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/time.h>
20 #include <fcntl.h>
21 #include <string.h>
22
23 #include <atalk/dsi.h>
24 #include <atalk/util.h>
25 #include <atalk/logger.h>
26
27 /* initialize relevant things for dsi_write. this returns the amount
28  * of data in the data buffer. the interface has been reworked to allow
29  * for arbitrary buffers. */
30 size_t dsi_writeinit(DSI *dsi, void *buf, const size_t buflen _U_)
31 {
32   size_t len, header;
33
34   /* figure out how much data we have. do a couple checks for 0 
35    * data */
36   header = ntohl(dsi->header.dsi_doff);
37   dsi->datasize = header ? ntohl(dsi->header.dsi_len) - header : 0;
38
39   if (dsi->datasize > 0) {
40       len = MIN(dsi->server_quantum - header, dsi->datasize);
41
42       /* write last part of command buffer into buf */
43       memmove(buf, dsi->commands + header, len);
44
45       /* recalculate remaining data */
46       dsi->datasize -= len;
47   } else
48     len = 0;
49
50   LOG(log_maxdebug, logtype_dsi, "dsi_writeinit: len: %ju, remaining DSI datasize: %jd",
51       (intmax_t)len, (intmax_t)dsi->datasize);
52
53   return len;
54 }
55
56 /* fill up buf and then return. this should be called repeatedly
57  * until all the data has been read. i block alarm processing 
58  * during the transfer to avoid sending unnecessary tickles. */
59 size_t dsi_write(DSI *dsi, void *buf, const size_t buflen)
60 {
61   size_t length;
62
63   LOG(log_maxdebug, logtype_dsi, "dsi_write: remaining DSI datasize: %jd", (intmax_t)dsi->datasize);
64
65   if ((length = MIN(buflen, dsi->datasize)) > 0) {
66       if ((length = dsi_stream_read(dsi, buf, length)) > 0) {
67           LOG(log_maxdebug, logtype_dsi, "dsi_write: received: %ju", (intmax_t)length);
68           dsi->datasize -= length;
69           return length;
70       }
71   }
72   return 0;
73 }
74
75 /* flush any unread buffers. */
76 void dsi_writeflush(DSI *dsi)
77 {
78   size_t length;
79
80   while (dsi->datasize > 0) { 
81     length = dsi_stream_read(dsi, dsi->data,
82                              MIN(sizeof(dsi->data), dsi->datasize));
83     if (length > 0)
84       dsi->datasize -= length;
85     else
86       break;
87   }
88 }