death to register
[openafs.git] / src / rx / bulk.example / bulk_io.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <sys/file.h>
17 #include <stdio.h>
18 #include <rx/xdr.h>
19 #include "bulk.h"
20
21 int
22 bulk_SendFile(int fd, struct rx_call *call,
23               struct stat *status)
24 {
25     char *buffer = (char *)0;
26     int blockSize;
27     long length;
28     XDR xdr;
29     long error = 0;
30     blockSize = status->st_blksize;
31     length = status->st_size;
32     buffer = (char *)malloc(status->st_blksize);
33     if (!buffer) {
34         printf("malloc failed\n");
35         return BULK_ERROR;
36     }
37     xdrrx_create(&xdr, call, XDR_ENCODE);
38     if (!xdr_long(&xdr, &length))
39         error = BULK_ERROR;
40     while (!error && length) {
41         int nbytes = (length > blockSize ? blockSize : length);
42         nbytes = read(fd, buffer, nbytes);
43         if (nbytes <= 0) {
44             fprintf(stderr, "File system read failed\n");
45             break;
46         }
47         if (rx_Write(call, buffer, nbytes) != nbytes)
48             break;
49         length -= nbytes;
50     }
51     if (buffer)
52         free(buffer);
53     if (length)
54         error = BULK_ERROR;
55     return error;
56 }
57
58 /* Copy the appropriate number of bytes from the call to fd.  The status should reflect the file's status coming into the routine and will reflect it going out of the routine, in the absence of errors */
59 int
60 bulk_ReceiveFile(int fd, struct rx_call *call,
61                  struct stat *status)
62 {
63     char *buffer = (char *)0;
64     long length;
65     XDR xdr;
66     int blockSize;
67     long error = 0;
68
69     xdrrx_create(&xdr, call, XDR_DECODE);
70     if (!xdr_long(&xdr, &length))
71         return BULK_ERROR;
72     blockSize = status->st_blksize;
73     buffer = (char *)malloc(status->st_blksize);
74     if (!buffer) {
75         fprintf(stderr, "malloc failed\n");
76         return BULK_ERROR;
77     }
78     while (!error && length) {
79         int nbytes = (length > blockSize ? blockSize : length);
80         nbytes = rx_Read(call, buffer, nbytes);
81         if (!nbytes)
82             error = BULK_ERROR;
83         if (write(fd, buffer, nbytes) != nbytes) {
84             fprintf(stderr, "File system write failed!\n");
85             error = BULK_ERROR;
86         }
87         length -= nbytes;
88     }
89     if (buffer)
90         free(buffer);
91     if (!error)
92         fstat(fd, status);
93     return error;
94 }