librsync  2.3.3
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 <assert.h>
29#include <stdlib.h>
30#include <time.h>
31#include "librsync.h"
32#include "job.h"
33#include "scoop.h"
34#include "trace.h"
35#include "util.h"
36
37static rs_result rs_job_work(rs_job_t *job, rs_buffers_t *buffers);
38
39rs_job_t *rs_job_new(char const *job_name, rs_result (*statefn)(rs_job_t *))
40{
41 rs_job_t *job;
42
44
45 job->job_name = job_name;
46 job->dogtag = RS_JOB_TAG;
47 job->statefn = statefn;
48
49 job->stats.op = job_name;
50 job->stats.start = time(NULL);
51
52 rs_trace("start %s job", job_name);
53
54 return job;
55}
56
57rs_result rs_job_free(rs_job_t *job)
58{
59 free(job->scoop_buf);
60 if (job->job_owns_sig)
62 rs_bzero(job, sizeof *job);
63 free(job);
64
65 return RS_DONE;
66}
67
68static rs_result rs_job_complete(rs_job_t *job, rs_result result)
69{
70 rs_job_check(job);
71 assert(result != RS_RUNNING && result != RS_BLOCKED);
72 assert(rs_tube_is_idle(job) || result != RS_DONE);
73
74 job->final_result = result;
75 job->stats.end = time(NULL);
76 if (result != RS_DONE) {
77 rs_error("%s job failed: %s", job->job_name, rs_strerror(result));
78 } else {
79 rs_trace("%s job complete", job->job_name);
80 }
81 return result;
82}
83
84rs_result rs_job_iter(rs_job_t *job, rs_buffers_t *buffers)
85{
86 rs_result result;
87 size_t orig_in, orig_out;
88
89 rs_job_check(job);
90 assert(buffers);
91
92 orig_in = buffers->avail_in;
93 orig_out = buffers->avail_out;
94 result = rs_job_work(job, buffers);
95 if (result == RS_BLOCKED || result == RS_DONE)
96 if ((orig_in == buffers->avail_in) && (orig_out == buffers->avail_out)
97 && orig_in && orig_out) {
98 rs_error("internal error: job made no progress " "[orig_in="
99 FMT_SIZE ", orig_out=" FMT_SIZE ", final_in=" FMT_SIZE
100 ", final_out=" FMT_SIZE "]", orig_in, orig_out,
101 buffers->avail_in, buffers->avail_out);
102 return RS_INTERNAL_ERROR;
103 }
104 return result;
105}
106
107static rs_result rs_job_work(rs_job_t *job, rs_buffers_t *buffers)
108{
109 rs_result result;
110
111 rs_job_check(job);
112 assert(buffers);
113
114 job->stream = buffers;
115 while (1) {
116 result = rs_tube_catchup(job);
117 if (result == RS_DONE && job->statefn) {
118 result = job->statefn(job);
119 if (result == RS_DONE) {
120 /* The job is done so clear statefn. */
121 job->statefn = NULL;
122 /* There might be stuff in the tube, so keep running. */
123 continue;
124 }
125 }
126 if (result == RS_BLOCKED)
127 return result;
128 if (result != RS_RUNNING)
129 return rs_job_complete(job, result);
130 }
131}
132
133const rs_stats_t *rs_job_statistics(rs_job_t *job)
134{
135 return &job->stats;
136}
137
138rs_result rs_job_drive(rs_job_t *job, rs_buffers_t *buf, rs_driven_cb in_cb,
139 void *in_opaque, rs_driven_cb out_cb, void *out_opaque)
140{
141 rs_result result, iores;
142
143 rs_bzero(buf, sizeof *buf);
144
145 do {
146 if (!buf->eof_in && in_cb) {
147 iores = in_cb(job, buf, in_opaque);
148 if (iores != RS_DONE)
149 return iores;
150 }
151
152 result = rs_job_iter(job, buf);
153 if (result != RS_DONE && result != RS_BLOCKED)
154 return result;
155
156 if (out_cb) {
157 iores = (out_cb) (job, buf, out_opaque);
158 if (iores != RS_DONE)
159 return iores;
160 }
161 } while (result != RS_DONE);
162
163 return result;
164}
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 void rs_free_sumset(rs_signature_t *)
Deep deallocation of checksums.
Definition: sumset.c:293
LIBRSYNC_EXPORT char const * rs_strerror(rs_result r)
Return an English description of a rs_result value.
Definition: msg.c:45
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
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
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:119
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