\doxysubsection{job.\+c}
\label{job_8c_source}\index{job.c@{job.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}{                               | The hard, lifeless I covered up the}}
\DoxyCodeLine{00024 \textcolor{comment}{                               | warm, pulsing It; protecting and}}
\DoxyCodeLine{00025 \textcolor{comment}{                               | sheltering.}}
\DoxyCodeLine{00026 \textcolor{comment}{                               */}}
\DoxyCodeLine{00027 }
\DoxyCodeLine{00028 \textcolor{preprocessor}{\#include "{}config.h"{}}             \textcolor{comment}{/* IWYU pragma: keep */}}
\DoxyCodeLine{00029 \textcolor{preprocessor}{\#include <assert.h>}}
\DoxyCodeLine{00030 \textcolor{preprocessor}{\#include <stdlib.h>}}
\DoxyCodeLine{00031 \textcolor{preprocessor}{\#include <time.h>}}
\DoxyCodeLine{00032 \textcolor{preprocessor}{\#include "{}librsync.h"{}}}
\DoxyCodeLine{00033 \textcolor{preprocessor}{\#include "{}job.h"{}}}
\DoxyCodeLine{00034 \textcolor{preprocessor}{\#include "{}scoop.h"{}}}
\DoxyCodeLine{00035 \textcolor{preprocessor}{\#include "{}trace.h"{}}}
\DoxyCodeLine{00036 \textcolor{preprocessor}{\#include "{}util.h"{}}}
\DoxyCodeLine{00037 }
\DoxyCodeLine{00038 \textcolor{keyword}{static} rs\_result rs\_job\_work(rs\_job\_t *job, rs\_buffers\_t *buffers);}
\DoxyCodeLine{00039 }
\DoxyCodeLine{00040 rs\_job\_t *rs\_job\_new(\textcolor{keywordtype}{char} \textcolor{keyword}{const} *job\_name, rs\_result (*statefn)(rs\_job\_t *))}
\DoxyCodeLine{00041 \{}
\DoxyCodeLine{00042     rs\_job\_t *job;}
\DoxyCodeLine{00043 }
\DoxyCodeLine{00044     job = rs\_alloc\_struct(rs\_job\_t);}
\DoxyCodeLine{00045 }
\DoxyCodeLine{00046     job-\/>job\_name = job\_name;}
\DoxyCodeLine{00047     job-\/>dogtag = RS\_JOB\_TAG;}
\DoxyCodeLine{00048     job-\/>statefn = statefn;}
\DoxyCodeLine{00049 }
\DoxyCodeLine{00050     job-\/>stats.op = job\_name;}
\DoxyCodeLine{00051     job-\/>stats.start = time(NULL);}
\DoxyCodeLine{00052 }
\DoxyCodeLine{00053     rs\_trace(\textcolor{stringliteral}{"{}start \%s job"{}}, job\_name);}
\DoxyCodeLine{00054 }
\DoxyCodeLine{00055     \textcolor{keywordflow}{return} job;}
\DoxyCodeLine{00056 \}}
\DoxyCodeLine{00057 }
\DoxyCodeLine{00058 rs\_result rs\_job\_free(rs\_job\_t *job)}
\DoxyCodeLine{00059 \{}
\DoxyCodeLine{00060     free(job-\/>scoop\_buf);}
\DoxyCodeLine{00061     \textcolor{keywordflow}{if} (job-\/>job\_owns\_sig)}
\DoxyCodeLine{00062         rs\_free\_sumset(job-\/>signature);}
\DoxyCodeLine{00063     rs\_bzero(job, \textcolor{keyword}{sizeof} *job);}
\DoxyCodeLine{00064     free(job);}
\DoxyCodeLine{00065 }
\DoxyCodeLine{00066     \textcolor{keywordflow}{return} RS\_DONE;}
\DoxyCodeLine{00067 \}}
\DoxyCodeLine{00068 }
\DoxyCodeLine{00069 \textcolor{keyword}{static} rs\_result rs\_job\_complete(rs\_job\_t *job, rs\_result result)}
\DoxyCodeLine{00070 \{}
\DoxyCodeLine{00071     rs\_job\_check(job);}
\DoxyCodeLine{00072     assert(result != RS\_RUNNING \&\& result != RS\_BLOCKED);}
\DoxyCodeLine{00073     assert(rs\_tube\_is\_idle(job) || result != RS\_DONE);}
\DoxyCodeLine{00074 }
\DoxyCodeLine{00075     job-\/>final\_result = result;}
\DoxyCodeLine{00076     job-\/>stats.end = time(NULL);}
\DoxyCodeLine{00077     \textcolor{keywordflow}{if} (result != RS\_DONE) \{}
\DoxyCodeLine{00078         rs\_error(\textcolor{stringliteral}{"{}\%s job failed: \%s"{}}, job-\/>job\_name, rs\_strerror(result));}
\DoxyCodeLine{00079     \} \textcolor{keywordflow}{else} \{}
\DoxyCodeLine{00080         rs\_trace(\textcolor{stringliteral}{"{}\%s job complete"{}}, job-\/>job\_name);}
\DoxyCodeLine{00081     \}}
\DoxyCodeLine{00082     \textcolor{keywordflow}{return} result;}
\DoxyCodeLine{00083 \}}
\DoxyCodeLine{00084 }
\DoxyCodeLine{00085 rs\_result rs\_job\_iter(rs\_job\_t *job, rs\_buffers\_t *buffers)}
\DoxyCodeLine{00086 \{}
\DoxyCodeLine{00087     rs\_result result;}
\DoxyCodeLine{00088     \textcolor{keywordtype}{size\_t} orig\_in, orig\_out;}
\DoxyCodeLine{00089 }
\DoxyCodeLine{00090     rs\_job\_check(job);}
\DoxyCodeLine{00091     assert(buffers);}
\DoxyCodeLine{00092 }
\DoxyCodeLine{00093     orig\_in = buffers-\/>avail\_in;}
\DoxyCodeLine{00094     orig\_out = buffers-\/>avail\_out;}
\DoxyCodeLine{00095     result = rs\_job\_work(job, buffers);}
\DoxyCodeLine{00096     \textcolor{keywordflow}{if} (result == RS\_BLOCKED || result == RS\_DONE)}
\DoxyCodeLine{00097         \textcolor{keywordflow}{if} ((orig\_in == buffers-\/>avail\_in) \&\& (orig\_out == buffers-\/>avail\_out)}
\DoxyCodeLine{00098             \&\& orig\_in \&\& orig\_out) \{}
\DoxyCodeLine{00099             rs\_error(\textcolor{stringliteral}{"{}internal error: job made no progress "{}} \textcolor{stringliteral}{"{}[orig\_in="{}}}
\DoxyCodeLine{00100                      FMT\_SIZE \textcolor{stringliteral}{"{}, orig\_out="{}} FMT\_SIZE \textcolor{stringliteral}{"{}, final\_in="{}} FMT\_SIZE}
\DoxyCodeLine{00101                      \textcolor{stringliteral}{"{}, final\_out="{}} FMT\_SIZE \textcolor{stringliteral}{"{}]"{}}, orig\_in, orig\_out,}
\DoxyCodeLine{00102                      buffers-\/>avail\_in, buffers-\/>avail\_out);}
\DoxyCodeLine{00103             \textcolor{keywordflow}{return} RS\_INTERNAL\_ERROR;}
\DoxyCodeLine{00104         \}}
\DoxyCodeLine{00105     \textcolor{keywordflow}{return} result;}
\DoxyCodeLine{00106 \}}
\DoxyCodeLine{00107 }
\DoxyCodeLine{00108 \textcolor{keyword}{static} rs\_result rs\_job\_work(rs\_job\_t *job, rs\_buffers\_t *buffers)}
\DoxyCodeLine{00109 \{}
\DoxyCodeLine{00110     rs\_result result;}
\DoxyCodeLine{00111 }
\DoxyCodeLine{00112     rs\_job\_check(job);}
\DoxyCodeLine{00113     assert(buffers);}
\DoxyCodeLine{00114 }
\DoxyCodeLine{00115     job-\/>stream = buffers;}
\DoxyCodeLine{00116     \textcolor{keywordflow}{while} (1) \{}
\DoxyCodeLine{00117         result = rs\_tube\_catchup(job);}
\DoxyCodeLine{00118         \textcolor{keywordflow}{if} (result == RS\_DONE \&\& job-\/>statefn) \{}
\DoxyCodeLine{00119             result = job-\/>statefn(job);}
\DoxyCodeLine{00120             \textcolor{keywordflow}{if} (result == RS\_DONE) \{}
\DoxyCodeLine{00121                 \textcolor{comment}{/* The job is done so clear statefn. */}}
\DoxyCodeLine{00122                 job-\/>statefn = NULL;}
\DoxyCodeLine{00123                 \textcolor{comment}{/* There might be stuff in the tube, so keep running. */}}
\DoxyCodeLine{00124                 \textcolor{keywordflow}{continue};}
\DoxyCodeLine{00125             \}}
\DoxyCodeLine{00126         \}}
\DoxyCodeLine{00127         \textcolor{keywordflow}{if} (result == RS\_BLOCKED)}
\DoxyCodeLine{00128             \textcolor{keywordflow}{return} result;}
\DoxyCodeLine{00129         \textcolor{keywordflow}{if} (result != RS\_RUNNING)}
\DoxyCodeLine{00130             \textcolor{keywordflow}{return} rs\_job\_complete(job, result);}
\DoxyCodeLine{00131     \}}
\DoxyCodeLine{00132 \}}
\DoxyCodeLine{00133 }
\DoxyCodeLine{00134 \textcolor{keyword}{const} rs\_stats\_t *rs\_job\_statistics(rs\_job\_t *job)}
\DoxyCodeLine{00135 \{}
\DoxyCodeLine{00136     \textcolor{keywordflow}{return} \&job-\/>stats;}
\DoxyCodeLine{00137 \}}
\DoxyCodeLine{00138 }
\DoxyCodeLine{00139 rs\_result rs\_job\_drive(rs\_job\_t *job, rs\_buffers\_t *buf, rs\_driven\_cb in\_cb,}
\DoxyCodeLine{00140                        \textcolor{keywordtype}{void} *in\_opaque, rs\_driven\_cb out\_cb, \textcolor{keywordtype}{void} *out\_opaque)}
\DoxyCodeLine{00141 \{}
\DoxyCodeLine{00142     rs\_result result, iores;}
\DoxyCodeLine{00143 }
\DoxyCodeLine{00144     rs\_bzero(buf, \textcolor{keyword}{sizeof} *buf);}
\DoxyCodeLine{00145 }
\DoxyCodeLine{00146     \textcolor{keywordflow}{do} \{}
\DoxyCodeLine{00147         \textcolor{keywordflow}{if} (!buf-\/>eof\_in \&\& in\_cb) \{}
\DoxyCodeLine{00148             iores = in\_cb(job, buf, in\_opaque);}
\DoxyCodeLine{00149             \textcolor{keywordflow}{if} (iores != RS\_DONE)}
\DoxyCodeLine{00150                 \textcolor{keywordflow}{return} iores;}
\DoxyCodeLine{00151         \}}
\DoxyCodeLine{00152 }
\DoxyCodeLine{00153         result = rs\_job\_iter(job, buf);}
\DoxyCodeLine{00154         \textcolor{keywordflow}{if} (result != RS\_DONE \&\& result != RS\_BLOCKED)}
\DoxyCodeLine{00155             \textcolor{keywordflow}{return} result;}
\DoxyCodeLine{00156 }
\DoxyCodeLine{00157         \textcolor{keywordflow}{if} (out\_cb) \{}
\DoxyCodeLine{00158             iores = (out\_cb) (job, buf, out\_opaque);}
\DoxyCodeLine{00159             \textcolor{keywordflow}{if} (iores != RS\_DONE)}
\DoxyCodeLine{00160                 \textcolor{keywordflow}{return} iores;}
\DoxyCodeLine{00161         \}}
\DoxyCodeLine{00162     \} \textcolor{keywordflow}{while} (result != RS\_DONE);}
\DoxyCodeLine{00163 }
\DoxyCodeLine{00164     \textcolor{keywordflow}{return} result;}
\DoxyCodeLine{00165 \}}

\end{DoxyCode}
