files.
A technical report describing the rsync algorithm is included with
-this package.
+this package.
USAGE
{
int i;
MDstruct MD;
- char *buf;
+ struct map_struct *buf;
int fd;
int len = size;
char tmpchunk[CSUM_CHUNK];
sum_put(&MD,sum);
close(fd);
- unmap_file(buf,size);
+ unmap_file(buf);
}
{
int pid;
int status=0;
+ int recv_pipe[2];
if (preserve_hard_links)
init_hard_links(flist);
+ if (pipe(recv_pipe) < 0) {
+ fprintf(FERROR,"pipe failed in do_recv\n");
+ exit(1);
+ }
+
+
if ((pid=fork()) == 0) {
- recv_files(f_in,flist,local_name);
+ recv_files(f_in,flist,local_name,recv_pipe[1]);
if (preserve_hard_links)
do_hard_links(flist);
if (verbose > 2)
exit_cleanup(0);
}
- generate_files(f_out,flist,local_name);
+ generate_files(f_out,flist,local_name,recv_pipe[0]);
waitpid(pid, &status, 0);
static off_t last_match;
-static void matched(int f,struct sum_struct *s,char *buf,off_t len,
+static void matched(int f,struct sum_struct *s,struct map_struct *buf,
+ off_t len,
int offset,int i)
{
int n = offset - last_match;
}
-static void hash_search(int f,struct sum_struct *s,char *buf,off_t len)
+static void hash_search(int f,struct sum_struct *s,
+ struct map_struct *buf,off_t len)
{
int offset,j,k;
int end;
}
-void match_sums(int f,struct sum_struct *s,char *buf,off_t len)
+void match_sums(int f,struct sum_struct *s,struct map_struct *buf,off_t len)
{
last_match = 0;
false_alarms = 0;
extern int always_checksum;
extern time_t starttime;
+extern int remote_version;
+
extern char *backup_suffix;
extern int block_size;
generate approximately one checksum every n bytes
*/
-static struct sum_struct *generate_sums(char *buf,off_t len,int n)
+static struct sum_struct *generate_sums(struct map_struct *buf,off_t len,int n)
{
int i;
struct sum_struct *s;
{
int fd;
struct stat st;
- char *buf;
+ struct map_struct *buf;
struct sum_struct *s;
char sum[SUM_LENGTH];
int statret;
write_flush(f_out);
close(fd);
- unmap_file(buf,st.st_size);
+ unmap_file(buf);
free_sums(s);
}
-static void receive_data(int f_in,char *buf,int fd,char *fname)
+static void receive_data(int f_in,struct map_struct *buf,int fd,char *fname)
{
int i,n,remainder,len,count;
off_t offset = 0;
}
-int recv_files(int f_in,struct file_list *flist,char *local_name)
+int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen)
{
int fd1,fd2;
struct stat st;
char *fname;
char fnametmp[MAXPATHLEN];
- char *buf;
+ struct map_struct *buf;
int i;
struct file_struct *file;
+ int phase=0;
if (verbose > 2) {
fprintf(FERROR,"recv_files(%d) starting\n",flist->count);
while (1)
{
i = read_int(f_in);
- if (i == -1) break;
+ if (i == -1) {
+ if (phase==0 && remote_version >= 13) {
+ phase++;
+ write_int(f_gen,-1);
+ write_flush(f_gen);
+ continue;
+ }
+ break;
+ }
file = &flist->files[i];
fname = file->name;
receive_data(f_in,buf,fd2,fname);
if (fd1 != -1) {
- unmap_file(buf,st.st_size);
+ unmap_file(buf);
close(fd1);
}
close(fd2);
{
int fd;
struct sum_struct *s;
- char *buf;
+ struct map_struct *buf;
struct stat st;
char fname[MAXPATHLEN];
off_t total=0;
int i;
struct file_struct *file;
+ int phase = 0;
if (verbose > 2)
fprintf(FERROR,"send_files starting\n");
while (1)
{
i = read_int(f_in);
- if (i == -1) break;
+ if (i == -1) {
+ if (phase==0 && remote_version >= 13) {
+ phase++;
+ write_int(f_out,-1);
+ write_flush(f_out);
+ continue;
+ }
+ break;
+ }
file = &flist->files[i];
match_sums(f_out,s,buf,st.st_size);
write_flush(f_out);
- unmap_file(buf,st.st_size);
+ unmap_file(buf);
close(fd);
free_sums(s);
-void generate_files(int f,struct file_list *flist,char *local_name)
+void generate_files(int f,struct file_list *flist,char *local_name,int f_recv)
{
int i;
recv_generator(local_name?local_name:file->name,
flist,i,f);
}
+
write_int(f,-1);
write_flush(f);
+
+ if (remote_version >= 13) {
+ /* go to the full checksum if anything has failed so far */
+ csum_length = SUM_LENGTH;
+
+ for (i=read_int(f_recv); i != -1; i=read_int(f_recv)) {
+ struct file_struct *file = &flist->files[i];
+ recv_generator(local_name?local_name:file->name,
+ flist,i,f);
+ }
+
+ write_int(f,-1);
+ write_flush(f);
+ }
+
+
if (verbose > 2)
fprintf(FERROR,"generator wrote %d\n",write_total());
}
#define SAME_TIME (1<<7)
/* update this if you make incompatible changes */
-#define PROTOCOL_VERSION 12
+#define PROTOCOL_VERSION 13
#define MIN_PROTOCOL_VERSION 10
#define MAX_PROTOCOL_VERSION 20
struct sum_buf *sums; /* points to info for each chunk */
};
+struct map_struct {
+ char *map,*p;
+ int fd,size,p_size,p_offset,p_len;
+};
#include "byteorder.h"
#include "version.h"
/* non-compressing send token */
-static void simple_send_token(int f,int token,char *buf,int offset,int n)
+static void simple_send_token(int f,int token,
+ struct map_struct *buf,int offset,int n)
{
if (n > 0) {
int l = 0;
* transmit a verbatim buffer of length n followed by a token
* If token == -1 then we have reached EOF
* If n == 0 then don't send a buffer
- * Note that "buf" must be used via map_ptr() starting at "offset"
*/
-void send_token(int f,int token,char *buf,int offset,int n)
+void send_token(int f,int token,struct map_struct *buf,int offset,int n)
{
if (!do_compression) {
simple_send_token(f,token,buf,offset,n);
return(len);
}
-static int map_fd = -1;
-static off_t map_size = 0;
-static char *p = NULL;
-static int p_size = 0;
-static int p_offset = 0;
-static int p_len = 0;
-
-char *map_file(int fd,off_t len)
+struct map_struct *map_file(int fd,off_t len)
{
- char *ret = NULL;
+ struct map_struct *ret;
+ ret = (struct map_struct *)malloc(sizeof(*ret));
+ if (!ret) out_of_memory("map_file");
+
+ ret->map = NULL;
+ ret->fd = fd;
+ ret->size = len;
+ ret->p = NULL;
+ ret->p_size = 0;
+ ret->p_offset = 0;
+ ret->p_len = 0;
+
#ifdef HAVE_MMAP
if (len < MAX_MAP_SIZE)
- ret = (char *)mmap(NULL,len,PROT_READ,MAP_SHARED,fd,0);
+ ret->map = (char *)mmap(NULL,len,PROT_READ,MAP_SHARED,fd,0);
#endif
- map_fd = fd;
- map_size = len;
return ret;
}
-char *map_ptr(char *buf,off_t offset,int len)
+char *map_ptr(struct map_struct *map,off_t offset,int len)
{
- if (buf)
- return buf+offset;
+ if (map->map)
+ return map->map+offset;
if (len == 0)
return NULL;
- len = MIN(len,map_size-offset);
+ len = MIN(len,map->size-offset);
- if (offset >= p_offset &&
- offset+len <= p_offset+p_len) {
- return (p + (offset - p_offset));
+ if (offset >= map->p_offset &&
+ offset+len <= map->p_offset+map->p_len) {
+ return (map->p + (offset - map->p_offset));
}
len = MAX(len,CHUNK_SIZE);
- len = MIN(len,map_size - offset);
+ len = MIN(len,map->size - offset);
- if (len > p_size) {
- if (p) free(p);
- p = (char *)malloc(len);
- if (!p) out_of_memory("map_ptr");
- p_size = len;
+ if (len > map->p_size) {
+ if (map->p) free(map->p);
+ map->p = (char *)malloc(len);
+ if (!map->p) out_of_memory("map_ptr");
+ map->p_size = len;
}
- if (lseek(map_fd,offset,SEEK_SET) != offset ||
- read(map_fd,p,len) != len) {
+ if (lseek(map->fd,offset,SEEK_SET) != offset ||
+ read(map->fd,map->p,len) != len) {
fprintf(FERROR,"EOF in map_ptr!\n");
exit_cleanup(1);
}
- p_offset = offset;
- p_len = len;
+ map->p_offset = offset;
+ map->p_len = len;
- return p;
+ return map->p;
}
-void unmap_file(char *buf,off_t len)
+void unmap_file(struct map_struct *map)
{
#ifdef HAVE_MMAP
- if (len > 0 && buf)
- munmap(buf,len);
+ if (map->map)
+ munmap(map->map,map->size);
#endif
- map_fd = -1;
- map_size = 0;
- p_len = 0;
+ if (map->p) free(map->p);
+ free(map);
}