00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifdef HAVE_CONFIG_H
00034 #include "config.h"
00035 #endif
00036
00037 #include <stdio.h>
00038 #include <stdlib.h>
00039 #include <string.h>
00040
00041 #include "rdd.h"
00042 #include "rdd_internals.h"
00043 #include "reader.h"
00044 #include "writer.h"
00045 #include "filter.h"
00046 #include "filterset.h"
00047 #include "copier.h"
00048
00052 #define SIMPLE_READ_SIZE 65536
00053
00061 typedef struct _RDD_SIMPLE_COPIER {
00062 unsigned char readbuf[SIMPLE_READ_SIZE];
00063 rdd_proghandler_t progressfun;
00064 void *progressenv;
00065 } RDD_SIMPLE_COPIER;
00066
00067 static int simple_exec(RDD_COPIER *c, RDD_READER *r, RDD_FILTERSET *fset,
00068 RDD_COPIER_RETURN *ret);
00069
00070 static RDD_COPY_OPS simple_ops = {
00071 simple_exec,
00072 0
00073 };
00074
00075 int
00076 rdd_new_simple_copier(RDD_COPIER **self, RDD_SIMPLE_PARAMS *params)
00077 {
00078 RDD_COPIER *c = 0;
00079 RDD_SIMPLE_COPIER *state = 0;
00080 int rc;
00081
00082 rc = rdd_new_copier(&c, &simple_ops, sizeof(RDD_SIMPLE_COPIER));
00083 if (rc != RDD_OK) {
00084 return rc;
00085 }
00086
00087 state = (RDD_SIMPLE_COPIER *) c->state;
00088 memset(state->readbuf, 0, sizeof(state->readbuf));
00089
00090 if (params) {
00091 state->progressfun = params->progressfun;
00092 state->progressenv = params->progressenv;
00093 } else {
00094 state->progressfun = 0;
00095 state->progressenv = 0;
00096 }
00097
00098 *self = c;
00099 return RDD_OK;
00100 }
00101
00102 static int
00103 simple_exec(RDD_COPIER *c, RDD_READER *r, RDD_FILTERSET *fset,
00104 RDD_COPIER_RETURN *ret)
00105 {
00106 RDD_SIMPLE_COPIER *s = (RDD_SIMPLE_COPIER *) c->state;
00107 rdd_count_t copied = 0;
00108 int aborted = 0;
00109 unsigned nread;
00110 int rc;
00111
00112 ret->nbyte = 0;
00113 ret->nlost = 0;
00114 ret->nread_err = 0;
00115 ret->nsubst = 0;
00116
00117 while (1) {
00118 nread = 0;
00119 rc = rdd_reader_read(r, s->readbuf, SIMPLE_READ_SIZE,
00120 &nread);
00121 if (rc != RDD_OK) return rc;
00122
00123 if (nread == 0) break;
00124
00125 if ((rc = rdd_fset_push(fset, s->readbuf, nread)) != RDD_OK) {
00126 return rc;
00127 }
00128
00129 copied += nread;
00130
00131 if (s->progressfun != 0) {
00132 rc = (*s->progressfun)(copied, 0, s->progressenv);
00133 if (rc == RDD_ABORTED) {
00134 aborted = 1;
00135 break;
00136 } else if (rc != RDD_OK) {
00137 return rc;
00138 }
00139 }
00140 }
00141
00142 if (s->progressfun != 0) {
00143 rc = (*s->progressfun)(copied, 0, s->progressenv);
00144 if (rc == RDD_ABORTED) {
00145 aborted = 1;
00146 } else if (rc != RDD_OK) {
00147 return rc;
00148 }
00149 }
00150
00151 if ((rc = rdd_fset_close(fset)) != RDD_OK) {
00152 return rc;
00153 }
00154
00155 ret->nbyte = copied;
00156
00157 return aborted ? RDD_ABORTED : RDD_OK;
00158 }