librsync  2.3.4
job.c
1/*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*-
2 *
3 * librsync -- the library for network deltas
4 *
5 * Copyright (C) 2000, 2001 by Martin Pool <mbp@sourcefrog.net>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * as published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 /*=
23 | The hard, lifeless I covered up the
24 | warm, pulsing It; protecting and
25 | sheltering.
26 */
27
28#include "config.h" /* IWYU pragma: keep */
29#include <assert.h>
30#include <stdlib.h>
31#include <time.h>
32#include "librsync.h"
33#include "job.h"
34#include "scoop.h"
35#include "trace.h"
36#include "util.h"
37
38static rs_result rs_job_work(rs_job_t *job, rs_buffers_t *buffers);
39
40rs_job_t *rs_job_new(char const *job_name, rs_result (*statefn)(rs_job_t *))
41{
42 rs_job_t *job;
43
45
46 job->job_name = job_name;
47 job->dogtag = RS_JOB_TAG;
48 job->statefn = statefn;
49
50 job->stats.op = job_name;
51 job->stats.start = time(NULL);
52
53 rs_trace("start %s job", job_name);
54
55 return job;
56}
57
59{
60 free(job->scoop_buf);
61 if (job->job_owns_sig)
63 rs_bzero(job, sizeof *job);
64 free(job);
65
66 return RS_DONE;
67}
68
69static rs_result rs_job_complete(rs_job_t *job, rs_result result)
70{
71 rs_job_check(job);
72 assert(result != RS_RUNNING && result != RS_BLOCKED);
73 assert(rs_tube_is_idle(job) || result != RS_DONE);
74
75 job->final_result = result;
76 job->stats.end = time(NULL);
77 if (result != RS_DONE) {
78 rs_error("%s job failed: %s", job->job_name, rs_strerror(result));
79 } else {
80 rs_trace("%s job complete", job->job_name);
81 }
82 return result;
83}
84
86{
87 rs_result result;
88 size_t orig_in, orig_out;
89
90 rs_job_check(job);
91 assert(buffers);
92
93 orig_in = buffers->avail_in;
94 orig_out = buffers->avail_out;
95 result = rs_job_work(job, buffers);
96 if (result == RS_BLOCKED || result == RS_DONE)
97 if ((orig_in == buffers->avail_in) && (orig_out == buffers->avail_out)
98 && orig_in && orig_out) {
99 rs_error("internal error: job made no progress " "[orig_in="
100 FMT_SIZE ", orig_out=" FMT_SIZE ", final_in=" FMT_SIZE
101 ", final_out=" FMT_SIZE "]", orig_in, orig_out,
102 buffers->avail_in, buffers->avail_out);
103 return RS_INTERNAL_ERROR;
104 }
105 return result;
106}
107
108static rs_result rs_job_work(rs_job_t *job, rs_buffers_t *buffers)
109{
110 rs_result result;
111
112 rs_job_check(job);
113 assert(buffers);
114
115 job->stream = buffers;
116 while (1) {
117 result = rs_tube_catchup(job);
118 if (result == RS_DONE && job->statefn) {
119 result = job->statefn(job);
120 if (result == RS_DONE) {
121 /* The job is done so clear statefn. */
122 job->statefn = NULL;
123 /* There might be stuff in the tube, so keep running. */
124 continue;
125 }
126 }
127 if (result == RS_BLOCKED)
128 return result;
129 if (result != RS_RUNNING)
130 return rs_job_complete(job, result);
131 }
132}
133
135{
136 return &job->stats;
137}
138
140 void *in_opaque, rs_driven_cb out_cb, void *out_opaque)
141{
142 rs_result result, iores;
143
144 rs_bzero(buf, sizeof *buf);
145
146 do {
147 if (!buf->eof_in && in_cb) {
148 iores = in_cb(job, buf, in_opaque);
149 if (iores != RS_DONE)
150 return iores;
151 }
152
153 result = rs_job_iter(job, buf);
154 if (result != RS_DONE && result != RS_BLOCKED)
155 return result;
156
157 if (out_cb) {
158 iores = (out_cb) (job, buf, out_opaque);
159 if (iores != RS_DONE)
160 return iores;
161 }
162 } while (result != RS_DONE);
163
164 return result;
165}
Generic state-machine interface.
#define RS_JOB_TAG
Magic job tag number for checking jobs have been initialized.
Definition: job.h:39
#define rs_job_check(job)
Assert that a job is valid.
Definition: job.h:130
Public header for librsync.
LIBRSYNC_EXPORT rs_result rs_job_free(rs_job_t *)
Deallocate job state.
Definition: job.c:58
LIBRSYNC_EXPORT void rs_free_sumset(rs_signature_t *)
Deep deallocation of checksums.
Definition: sumset.c:294
LIBRSYNC_EXPORT char const * rs_strerror(rs_result r)
Return an English description of a rs_result value.
Definition: msg.c:46
rs_result
Return codes from nonblocking rsync operations.
Definition: librsync.h:180
@ RS_RUNNING
The job is still running, and not yet finished or blocked.
Definition: librsync.h:183
@ RS_DONE
Completed successfully.
Definition: librsync.h:181
@ RS_INTERNAL_ERROR
Probably a library bug.
Definition: librsync.h:199
@ RS_BLOCKED
Blocked waiting for more data.
Definition: librsync.h:182
LIBRSYNC_EXPORT rs_result rs_job_drive(rs_job_t *job, rs_buffers_t *buf, rs_driven_cb in_cb, void *in_opaque, rs_driven_cb out_cb, void *out_opaque)
Actively process a job, by making callbacks to fill and empty the buffers until the job is done.
Definition: job.c:139
LIBRSYNC_EXPORT const rs_stats_t * rs_job_statistics(rs_job_t *job)
Return a pointer to the statistics in a job.
Definition: job.c:134
rs_result rs_driven_cb(rs_job_t *job, rs_buffers_t *buf, void *opaque)
Type of application-supplied function for rs_job_drive().
Definition: librsync.h:405
LIBRSYNC_EXPORT rs_result rs_job_iter(rs_job_t *job, rs_buffers_t *buffers)
Run a rs_job state machine until it blocks (RS_BLOCKED), returns an error, or completes (RS_DONE).
Definition: job.c:85
Manage librsync streams of IO.
rs_result rs_tube_catchup(rs_job_t *job)
Put whatever will fit from the tube into the output of the stream.
Definition: tube.c:120
Description of input and output buffers.
Definition: librsync.h:328
size_t avail_in
Number of bytes available at next_in.
Definition: librsync.h:342
int eof_in
True if there is no more data after this.
Definition: librsync.h:345
size_t avail_out
Remaining free space at next_out.
Definition: librsync.h:357
The contents of this structure are private.
Definition: job.h:47
const char * job_name
Human-readable job operation name.
Definition: job.h:51
rs_byte_t * scoop_buf
Buffer of data in the scoop.
Definition: job.h:97
int job_owns_sig
Flag indicating signature should be destroyed with the job.
Definition: job.h:75
rs_result(* statefn)(rs_job_t *)
Callback for each processing step.
Definition: job.h:56
rs_result final_result
Final result of processing job.
Definition: job.h:59
rs_signature_t * signature
Pointer to the signature that's being used by the operation.
Definition: job.h:72
rs_stats_t stats
Encoding statistics.
Definition: job.h:93
Performance statistics from a librsync encoding or decoding operation.
Definition: librsync.h:210
char const * op
Human-readable name of current operation.
Definition: librsync.h:211
logging functions.
Misc utility functions used by librsync.
#define rs_alloc_struct(type)
Allocate and zero-fill an instance of TYPE.
Definition: util.h:41