\doxysubsection{buf.\+c}
\label{buf_8c_source}\index{buf.c@{buf.c}}

\begin{DoxyCode}{0}
\DoxyCodeLine{00001 \textcolor{comment}{/*= -\/*-\/ c-\/basic-\/offset: 4; indent-\/tabs-\/mode: nil; -\/*-\/}}
\DoxyCodeLine{00002 \textcolor{comment}{ *}}
\DoxyCodeLine{00003 \textcolor{comment}{ * librsync -\/-\/ the library for network deltas}}
\DoxyCodeLine{00004 \textcolor{comment}{ *}}
\DoxyCodeLine{00005 \textcolor{comment}{ * Copyright (C) 2000, 2001 by Martin Pool <mbp@sourcefrog.net>}}
\DoxyCodeLine{00006 \textcolor{comment}{ *}}
\DoxyCodeLine{00007 \textcolor{comment}{ * This program is free software; you can redistribute it and/or}}
\DoxyCodeLine{00008 \textcolor{comment}{ * modify it under the terms of the GNU Lesser General Public License}}
\DoxyCodeLine{00009 \textcolor{comment}{ * as published by the Free Software Foundation; either version 2.1 of}}
\DoxyCodeLine{00010 \textcolor{comment}{ * the License, or (at your option) any later version.}}
\DoxyCodeLine{00011 \textcolor{comment}{ *}}
\DoxyCodeLine{00012 \textcolor{comment}{ * This program is distributed in the hope that it will be useful, but}}
\DoxyCodeLine{00013 \textcolor{comment}{ * WITHOUT ANY WARRANTY; without even the implied warranty of}}
\DoxyCodeLine{00014 \textcolor{comment}{ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU}}
\DoxyCodeLine{00015 \textcolor{comment}{ * Lesser General Public License for more details.}}
\DoxyCodeLine{00016 \textcolor{comment}{ *}}
\DoxyCodeLine{00017 \textcolor{comment}{ * You should have received a copy of the GNU Lesser General Public}}
\DoxyCodeLine{00018 \textcolor{comment}{ * License along with this program; if not, write to the Free Software}}
\DoxyCodeLine{00019 \textcolor{comment}{ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.}}
\DoxyCodeLine{00020 \textcolor{comment}{ */}}
\DoxyCodeLine{00021 }
\DoxyCodeLine{00022                               \textcolor{comment}{/*=}}
\DoxyCodeLine{00023 \textcolor{comment}{                               | Pick a window, Jimmy, you're leaving.}}
\DoxyCodeLine{00024 \textcolor{comment}{                               */}}
\DoxyCodeLine{00025 }
\DoxyCodeLine{00026 \textcolor{preprocessor}{\#include "{}config.h"{}}             \textcolor{comment}{/* IWYU pragma: keep */}}
\DoxyCodeLine{00027 \textcolor{preprocessor}{\#include <assert.h>}}
\DoxyCodeLine{00028 \textcolor{preprocessor}{\#include <stdlib.h>}}
\DoxyCodeLine{00029 \textcolor{preprocessor}{\#include <errno.h>}}
\DoxyCodeLine{00030 \textcolor{preprocessor}{\#include <string.h>}}
\DoxyCodeLine{00031 \textcolor{preprocessor}{\#include "{}librsync.h"{}}}
\DoxyCodeLine{00032 \textcolor{preprocessor}{\#include "{}buf.h"{}}}
\DoxyCodeLine{00033 \textcolor{preprocessor}{\#include "{}job.h"{}}}
\DoxyCodeLine{00034 \textcolor{preprocessor}{\#include "{}trace.h"{}}}
\DoxyCodeLine{00035 \textcolor{preprocessor}{\#include "{}util.h"{}}}
\DoxyCodeLine{00036 }
\DoxyCodeLine{00037 \textcolor{keyword}{struct }rs\_filebuf \{}
\DoxyCodeLine{00038     FILE *f;}
\DoxyCodeLine{00039     \textcolor{keywordtype}{char} *buf;}
\DoxyCodeLine{00040     \textcolor{keywordtype}{size\_t} buf\_len;}
\DoxyCodeLine{00041 \};}
\DoxyCodeLine{00042 }
\DoxyCodeLine{00043 rs\_filebuf\_t *rs\_filebuf\_new(FILE *f, \textcolor{keywordtype}{size\_t} buf\_len)}
\DoxyCodeLine{00044 \{}
\DoxyCodeLine{00045     rs\_filebuf\_t *pf = rs\_alloc\_struct(rs\_filebuf\_t);}
\DoxyCodeLine{00046 }
\DoxyCodeLine{00047     pf-\/>buf = rs\_alloc(buf\_len, \textcolor{stringliteral}{"{}file buffer"{}});}
\DoxyCodeLine{00048     pf-\/>buf\_len = buf\_len;}
\DoxyCodeLine{00049     pf-\/>f = f;}
\DoxyCodeLine{00050     \textcolor{keywordflow}{return} pf;}
\DoxyCodeLine{00051 \}}
\DoxyCodeLine{00052 }
\DoxyCodeLine{00053 \textcolor{keywordtype}{void} rs\_filebuf\_free(rs\_filebuf\_t *fb)}
\DoxyCodeLine{00054 \{}
\DoxyCodeLine{00055     free(fb-\/>buf);}
\DoxyCodeLine{00056     rs\_bzero(fb, \textcolor{keyword}{sizeof} *fb);}
\DoxyCodeLine{00057     free(fb);}
\DoxyCodeLine{00058 \}}
\DoxyCodeLine{00059 }
\DoxyCodeLine{00060 \textcolor{comment}{/* If the stream has no more data available, read some from F into BUF, and let}}
\DoxyCodeLine{00061 \textcolor{comment}{   the stream use that. On return, SEEN\_EOF is true if the end of file has}}
\DoxyCodeLine{00062 \textcolor{comment}{   passed into the stream. */}}
\DoxyCodeLine{00063 rs\_result rs\_infilebuf\_fill(rs\_job\_t *job, rs\_buffers\_t *buf, \textcolor{keywordtype}{void} *opaque)}
\DoxyCodeLine{00064 \{}
\DoxyCodeLine{00065     \textcolor{keywordtype}{size\_t} len;}
\DoxyCodeLine{00066     rs\_filebuf\_t *fb = (rs\_filebuf\_t *)opaque;}
\DoxyCodeLine{00067     FILE *f = fb-\/>f;}
\DoxyCodeLine{00068 }
\DoxyCodeLine{00069     \textcolor{comment}{/* If buf has data, it must be in the buffer. */}}
\DoxyCodeLine{00070     \textcolor{keywordflow}{if} (buf-\/>avail\_in) \{}
\DoxyCodeLine{00071         assert(buf-\/>next\_in >= fb-\/>buf);}
\DoxyCodeLine{00072         assert(buf-\/>next\_in + buf-\/>avail\_in <= fb-\/>buf + fb-\/>buf\_len);}
\DoxyCodeLine{00073     \}}
\DoxyCodeLine{00074     \textcolor{keywordflow}{if} (buf-\/>eof\_in) \{}
\DoxyCodeLine{00075         \textcolor{keywordflow}{return} RS\_DONE;}
\DoxyCodeLine{00076     \} \textcolor{keywordflow}{else} \textcolor{keywordflow}{if} (buf-\/>avail\_in > fb-\/>buf\_len / 2) \{}
\DoxyCodeLine{00077         \textcolor{comment}{/* Buf is already full enough, do nothing. */}}
\DoxyCodeLine{00078         \textcolor{keywordflow}{return} RS\_DONE;}
\DoxyCodeLine{00079     \} \textcolor{keywordflow}{else} \textcolor{keywordflow}{if} (buf-\/>avail\_in) \{}
\DoxyCodeLine{00080         \textcolor{comment}{/* Some leftover tail data, move it to the front of the buffer. */}}
\DoxyCodeLine{00081         rs\_trace(\textcolor{stringliteral}{"{}moving buffer "{}} FMT\_SIZE \textcolor{stringliteral}{"{} bytes to reuse "{}} FMT\_SIZE \textcolor{stringliteral}{"{} bytes"{}},}
\DoxyCodeLine{00082                  buf-\/>avail\_in, (\textcolor{keywordtype}{size\_t})(buf-\/>next\_in -\/ fb-\/>buf));}
\DoxyCodeLine{00083         memmove(fb-\/>buf, buf-\/>next\_in, buf-\/>avail\_in);}
\DoxyCodeLine{00084     \}}
\DoxyCodeLine{00085     buf-\/>next\_in = fb-\/>buf;}
\DoxyCodeLine{00086     len = fread(fb-\/>buf + buf-\/>avail\_in, 1, fb-\/>buf\_len -\/ buf-\/>avail\_in, f);}
\DoxyCodeLine{00087     \textcolor{keywordflow}{if} (len == 0) \{}
\DoxyCodeLine{00088         \textcolor{keywordflow}{if} ((buf-\/>eof\_in = feof(f))) \{}
\DoxyCodeLine{00089             rs\_trace(\textcolor{stringliteral}{"{}seen end of file on input"{}});}
\DoxyCodeLine{00090             \textcolor{keywordflow}{return} RS\_DONE;}
\DoxyCodeLine{00091         \} \textcolor{keywordflow}{else} \{}
\DoxyCodeLine{00092             rs\_error(\textcolor{stringliteral}{"{}error filling buf from file: \%s"{}}, strerror(errno));}
\DoxyCodeLine{00093             \textcolor{keywordflow}{return} RS\_IO\_ERROR;}
\DoxyCodeLine{00094         \}}
\DoxyCodeLine{00095     \}}
\DoxyCodeLine{00096     buf-\/>avail\_in += len;}
\DoxyCodeLine{00097     job-\/>stats.in\_bytes += len;}
\DoxyCodeLine{00098     \textcolor{keywordflow}{return} RS\_DONE;}
\DoxyCodeLine{00099 \}}
\DoxyCodeLine{00100 }
\DoxyCodeLine{00101 \textcolor{comment}{/* The buf is already using BUF for an output buffer, and probably contains}}
\DoxyCodeLine{00102 \textcolor{comment}{   some buffered output now. Write this out to F, and reset the buffer cursor. */}}
\DoxyCodeLine{00103 rs\_result rs\_outfilebuf\_drain(rs\_job\_t *job, rs\_buffers\_t *buf, \textcolor{keywordtype}{void} *opaque)}
\DoxyCodeLine{00104 \{}
\DoxyCodeLine{00105     rs\_filebuf\_t *fb = (rs\_filebuf\_t *)opaque;}
\DoxyCodeLine{00106     FILE *f = fb-\/>f;}
\DoxyCodeLine{00107 }
\DoxyCodeLine{00108     \textcolor{comment}{/* If next\_out is NULL, we haven't pointed it at fb-\/>buf yet. */}}
\DoxyCodeLine{00109     \textcolor{keywordflow}{if} (!buf-\/>next\_out) \{}
\DoxyCodeLine{00110         assert(buf-\/>avail\_out == 0);}
\DoxyCodeLine{00111         buf-\/>next\_out = fb-\/>buf;}
\DoxyCodeLine{00112         buf-\/>avail\_out = fb-\/>buf\_len;}
\DoxyCodeLine{00113     \}}
\DoxyCodeLine{00114     \textcolor{comment}{/* The buf output buffer must be at the end of fb-\/>buf. */}}
\DoxyCodeLine{00115     assert(buf-\/>next\_out >= fb-\/>buf);}
\DoxyCodeLine{00116     assert(buf-\/>next\_out + buf-\/>avail\_out == fb-\/>buf + fb-\/>buf\_len);}
\DoxyCodeLine{00117 }
\DoxyCodeLine{00118     \textcolor{keywordtype}{size\_t} present = buf-\/>next\_out -\/ fb-\/>buf;}
\DoxyCodeLine{00119     \textcolor{keywordflow}{if} (present > 0) \{}
\DoxyCodeLine{00120         \textcolor{keywordtype}{size\_t} result = fwrite(fb-\/>buf, 1, present, f);}
\DoxyCodeLine{00121         \textcolor{keywordflow}{if} (present != result) \{}
\DoxyCodeLine{00122             rs\_error(\textcolor{stringliteral}{"{}error draining buf to file: \%s"{}}, strerror(errno));}
\DoxyCodeLine{00123             \textcolor{keywordflow}{return} RS\_IO\_ERROR;}
\DoxyCodeLine{00124         \}}
\DoxyCodeLine{00125         buf-\/>next\_out = fb-\/>buf;}
\DoxyCodeLine{00126         buf-\/>avail\_out = fb-\/>buf\_len;}
\DoxyCodeLine{00127         job-\/>stats.out\_bytes += result;}
\DoxyCodeLine{00128     \}}
\DoxyCodeLine{00129     \textcolor{keywordflow}{return} RS\_DONE;}
\DoxyCodeLine{00130 \}}

\end{DoxyCode}
