$treeview $search $mathjax $extrastylesheet
librsync
2.3.1
$projectbrief
|
$projectbrief
|
$searchbox |
00001 /*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- 00002 * 00003 * librsync -- the library for network deltas 00004 * 00005 * Copyright (C) 2000, 2001 by Martin Pool <mbp@sourcefrog.net> 00006 * 00007 * This program is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public License 00009 * as published by the Free Software Foundation; either version 2.1 of 00010 * the License, or (at your option) any later version. 00011 * 00012 * This program is distributed in the hope that it will be useful, but 00013 * WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with this program; if not, write to the Free Software 00019 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00020 */ 00021 00022 /*= 00023 | The hard, lifeless I covered up the 00024 | warm, pulsing It; protecting and 00025 | sheltering. 00026 */ 00027 00028 /** \file job.c 00029 * Generic state-machine interface. 00030 * 00031 * The point of this is that we need to be able to suspend and resume 00032 * processing at any point at which the buffers may block. 00033 * 00034 * \sa \ref api_streaming \sa rs_job_iter() \sa ::rs_job */ 00035 00036 #include "config.h" 00037 #include <assert.h> 00038 #include <stdlib.h> 00039 #include <time.h> 00040 #include "librsync.h" 00041 #include "job.h" 00042 #include "stream.h" 00043 #include "trace.h" 00044 #include "util.h" 00045 00046 static rs_result rs_job_work(rs_job_t *job, rs_buffers_t *buffers); 00047 00048 rs_job_t *rs_job_new(char const *job_name, rs_result (*statefn)(rs_job_t *)) 00049 { 00050 rs_job_t *job; 00051 00052 job = rs_alloc_struct(rs_job_t); 00053 00054 job->job_name = job_name; 00055 job->dogtag = RS_JOB_TAG; 00056 job->statefn = statefn; 00057 00058 job->stats.op = job_name; 00059 job->stats.start = time(NULL); 00060 00061 rs_trace("start %s job", job_name); 00062 00063 return job; 00064 } 00065 00066 rs_result rs_job_free(rs_job_t *job) 00067 { 00068 free(job->scoop_buf); 00069 if (job->job_owns_sig) 00070 rs_free_sumset(job->signature); 00071 rs_bzero(job, sizeof *job); 00072 free(job); 00073 00074 return RS_DONE; 00075 } 00076 00077 static rs_result rs_job_complete(rs_job_t *job, rs_result result) 00078 { 00079 rs_job_check(job); 00080 assert(result != RS_RUNNING && result != RS_BLOCKED); 00081 assert(rs_tube_is_idle(job) || result != RS_DONE); 00082 00083 job->final_result = result; 00084 job->stats.end = time(NULL); 00085 if (result != RS_DONE) { 00086 rs_error("%s job failed: %s", job->job_name, rs_strerror(result)); 00087 } else { 00088 rs_trace("%s job complete", job->job_name); 00089 } 00090 return result; 00091 } 00092 00093 rs_result rs_job_iter(rs_job_t *job, rs_buffers_t *buffers) 00094 { 00095 rs_result result; 00096 size_t orig_in, orig_out; 00097 00098 rs_job_check(job); 00099 assert(buffers); 00100 00101 orig_in = buffers->avail_in; 00102 orig_out = buffers->avail_out; 00103 result = rs_job_work(job, buffers); 00104 if (result == RS_BLOCKED || result == RS_DONE) 00105 if ((orig_in == buffers->avail_in) && (orig_out == buffers->avail_out) 00106 && orig_in && orig_out) { 00107 rs_error("internal error: job made no progress " "[orig_in=" 00108 FMT_SIZE ", orig_out=" FMT_SIZE ", final_in=" FMT_SIZE 00109 ", final_out=" FMT_SIZE "]", orig_in, orig_out, 00110 buffers->avail_in, buffers->avail_out); 00111 return RS_INTERNAL_ERROR; 00112 } 00113 return result; 00114 } 00115 00116 static rs_result rs_job_work(rs_job_t *job, rs_buffers_t *buffers) 00117 { 00118 rs_result result; 00119 00120 rs_job_check(job); 00121 assert(buffers); 00122 00123 job->stream = buffers; 00124 while (1) { 00125 result = rs_tube_catchup(job); 00126 if (result == RS_DONE && job->statefn) { 00127 result = job->statefn(job); 00128 if (result == RS_DONE) { 00129 /* The job is done so clear statefn. */ 00130 job->statefn = NULL; 00131 /* There might be stuff in the tube, so keep running. */ 00132 continue; 00133 } 00134 } 00135 if (result == RS_BLOCKED) 00136 return result; 00137 if (result != RS_RUNNING) 00138 return rs_job_complete(job, result); 00139 } 00140 } 00141 00142 const rs_stats_t *rs_job_statistics(rs_job_t *job) 00143 { 00144 return &job->stats; 00145 } 00146 00147 int rs_job_input_is_ending(rs_job_t *job) 00148 { 00149 return job->stream->eof_in; 00150 } 00151 00152 rs_result rs_job_drive(rs_job_t *job, rs_buffers_t *buf, rs_driven_cb in_cb, 00153 void *in_opaque, rs_driven_cb out_cb, void *out_opaque) 00154 { 00155 rs_result result, iores; 00156 00157 rs_bzero(buf, sizeof *buf); 00158 00159 do { 00160 if (!buf->eof_in && in_cb) { 00161 iores = in_cb(job, buf, in_opaque); 00162 if (iores != RS_DONE) 00163 return iores; 00164 } 00165 00166 result = rs_job_iter(job, buf); 00167 if (result != RS_DONE && result != RS_BLOCKED) 00168 return result; 00169 00170 if (out_cb) { 00171 iores = (out_cb) (job, buf, out_opaque); 00172 if (iores != RS_DONE) 00173 return iores; 00174 } 00175 } while (result != RS_DONE); 00176 00177 return result; 00178 }