forked from librsync/librsync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buf.c
223 lines (184 loc) · 6 KB
/
buf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*-
*
* librsync -- the library for network deltas
*
* Copyright (C) 2000, 2001 by Martin Pool <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
| Pick a window, Jimmy, you're leaving.
*/
/*
* buf.c -- Buffers that map between stdio file streams and librsync
* streams. As the stream consumes input and produces output, it is
* refilled from appropriate input and output FILEs. A dynamically
* allocated buffer of configurable size is used as an intermediary.
*
* TODO: Perhaps be more efficient by filling the buffer on every call
* even if not yet completely empty. Check that it's really our
* buffer, and shuffle remaining data down to the front.
*
* TODO: Perhaps expose a routine for shuffling the buffers.
*/
#include <config.h>
#include <sys/types.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include "librsync.h"
#include "trace.h"
#include "buf.h"
#include "util.h"
/* use fseeko instead of fseek for long file support if we have it */
#ifdef HAVE_FSEEKO
#define fseek fseeko
#endif
/**
* File IO buffer sizes.
*/
int rs_inbuflen = 16000, rs_outbuflen = 16000;
struct rs_filebuf {
FILE *f;
char *buf;
size_t buf_len;
};
rs_filebuf_t *rs_filebuf_new(FILE *f, size_t buf_len)
{
rs_filebuf_t *pf = rs_alloc_struct(rs_filebuf_t);
pf->buf = rs_alloc(buf_len, "file buffer");
pf->buf_len = buf_len;
pf->f = f;
return pf;
}
void rs_filebuf_free(rs_filebuf_t *fb)
{
free(fb->buf);
rs_bzero(fb, sizeof *fb);
free(fb);
}
/*
* If the stream has no more data available, read some from F into
* BUF, and let the stream use that. On return, SEEN_EOF is true if
* the end of file has passed into the stream.
*/
rs_result rs_infilebuf_fill(rs_job_t *job, rs_buffers_t *buf,
void *opaque)
{
size_t len;
rs_filebuf_t *fb = (rs_filebuf_t *) opaque;
FILE *f = fb->f;
/* This is only allowed if either the buf has no input buffer
* yet, or that buffer could possibly be BUF. */
if (buf->next_in != NULL) {
assert(buf->avail_in <= fb->buf_len);
assert(buf->next_in >= fb->buf);
assert(buf->next_in <= fb->buf + fb->buf_len);
} else {
assert(buf->avail_in == 0);
}
if (buf->eof_in || (buf->eof_in = feof(f))) {
rs_trace("seen end of file on input");
buf->eof_in = 1;
return RS_DONE;
}
if (buf->avail_in)
/* Still some data remaining. Perhaps we should read
anyhow? */
return RS_DONE;
len = fread(fb->buf, 1, fb->buf_len, f);
if (len <= 0) {
/* This will happen if file size is a multiple of input block len
*/
if (feof(f)) {
rs_trace("seen end of file on input");
buf->eof_in = 1;
return RS_DONE;
}
if (ferror(f)) {
rs_error("error filling buf from file: %s",
strerror(errno));
return RS_IO_ERROR;
} else {
rs_error("no error bit, but got %d return when trying to read",
len);
return RS_IO_ERROR;
}
}
buf->avail_in = len;
buf->next_in = fb->buf;
return RS_DONE;
}
/*
* The buf is already using BUF for an output buffer, and probably
* contains some buffered output now. Write this out to F, and reset
* the buffer cursor.
*/
rs_result rs_outfilebuf_drain(rs_job_t *job, rs_buffers_t *buf, void *opaque)
{
size_t present;
rs_filebuf_t *fb = (rs_filebuf_t *) opaque;
FILE *f = fb->f;
/* This is only allowed if either the buf has no output buffer
* yet, or that buffer could possibly be BUF. */
if (buf->next_out == NULL) {
assert(buf->avail_out == 0);
buf->next_out = fb->buf;
buf->avail_out = fb->buf_len;
return RS_DONE;
}
assert(buf->avail_out <= fb->buf_len);
assert(buf->next_out >= fb->buf);
assert(buf->next_out <= fb->buf + fb->buf_len);
present = buf->next_out - fb->buf;
if (present > 0) {
int result;
assert(present > 0);
result = fwrite(fb->buf, 1, present, f);
if (present != result) {
rs_error("error draining buf to file: %s",
strerror(errno));
return RS_IO_ERROR;
}
buf->next_out = fb->buf;
buf->avail_out = fb->buf_len;
}
return RS_DONE;
}
/**
* Default copy implementation that retrieves a part of a stdio file.
*/
rs_result rs_file_copy_cb(void *arg, rs_long_t pos, size_t *len, void **buf)
{
int got;
FILE *f = (FILE *) arg;
if (fseek(f, pos, SEEK_SET)) {
rs_log(RS_LOG_ERR, "seek failed: %s", strerror(errno));
return RS_IO_ERROR;
}
got = fread(*buf, 1, *len, f);
if (got == -1) {
rs_error("read error: %s", strerror(errno));
return RS_IO_ERROR;
} else if (got == 0) {
rs_error("unexpected eof on fd%d", fileno(f));
return RS_INPUT_ENDED;
} else {
*len = got;
return RS_DONE;
}
}