Annotation of embedaddon/rsync/compat.c, revision 1.1.1.1.2.1

1.1       misho       1: /*
                      2:  * Compatibility routines for older rsync protocol versions.
                      3:  *
                      4:  * Copyright (C) Andrew Tridgell 1996
                      5:  * Copyright (C) Paul Mackerras 1996
                      6:  * Copyright (C) 2004-2009 Wayne Davison
                      7:  *
                      8:  * This program is free software; you can redistribute it and/or modify
                      9:  * it under the terms of the GNU General Public License as published by
                     10:  * the Free Software Foundation; either version 3 of the License, or
                     11:  * (at your option) any later version.
                     12:  *
                     13:  * This program is distributed in the hope that it will be useful,
                     14:  * but WITHOUT ANY WARRANTY; without even the implied warranty of
                     15:  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     16:  * GNU General Public License for more details.
                     17:  *
                     18:  * You should have received a copy of the GNU General Public License along
                     19:  * with this program; if not, visit the http://fsf.org website.
                     20:  */
                     21: 
                     22: #include "rsync.h"
                     23: 
                     24: int remote_protocol = 0;
                     25: int file_extra_cnt = 0; /* count of file-list extras that everyone gets */
                     26: int inc_recurse = 0;
                     27: int compat_flags = 0;
                     28: int use_safe_inc_flist = 0;
                     29: 
                     30: extern int verbose;
                     31: extern int am_server;
                     32: extern int am_sender;
                     33: extern int local_server;
                     34: extern int inplace;
                     35: extern int recurse;
                     36: extern int use_qsort;
                     37: extern int allow_inc_recurse;
                     38: extern int append_mode;
                     39: extern int fuzzy_basis;
                     40: extern int read_batch;
                     41: extern int delay_updates;
                     42: extern int checksum_seed;
                     43: extern int basis_dir_cnt;
1.1.1.1.2.1! misho      44: extern int detect_renamed;
1.1       misho      45: extern int prune_empty_dirs;
                     46: extern int protocol_version;
                     47: extern int protect_args;
                     48: extern int preserve_uid;
                     49: extern int preserve_gid;
                     50: extern int preserve_acls;
                     51: extern int preserve_xattrs;
                     52: extern int need_messages_from_generator;
                     53: extern int delete_mode, delete_before, delete_during, delete_after;
                     54: extern char *shell_cmd;
                     55: extern char *partial_dir;
                     56: extern char *dest_option;
                     57: extern char *files_from;
                     58: extern char *filesfrom_host;
                     59: extern struct filter_list_struct filter_list;
                     60: extern int need_unsorted_flist;
                     61: #ifdef ICONV_OPTION
                     62: extern iconv_t ic_send, ic_recv;
                     63: extern char *iconv_opt;
                     64: #endif
                     65: 
                     66: /* These index values are for the file-list's extra-attribute array. */
                     67: int uid_ndx, gid_ndx, acls_ndx, xattrs_ndx, unsort_ndx;
                     68: 
                     69: int receiver_symlink_times = 0; /* receiver can set the time on a symlink */
                     70: int sender_symlink_iconv = 0;  /* sender should convert symlink content */
                     71: 
                     72: #ifdef ICONV_OPTION
                     73: int filesfrom_convert = 0;
                     74: #endif
                     75: 
                     76: #define CF_INC_RECURSE  (1<<0)
                     77: #define CF_SYMLINK_TIMES (1<<1)
                     78: #define CF_SYMLINK_ICONV (1<<2)
                     79: #define CF_SAFE_FLIST   (1<<3)
                     80: 
                     81: static const char *client_info;
                     82: 
                     83: /* The server makes sure that if either side only supports a pre-release
                     84:  * version of a protocol, that both sides must speak a compatible version
                     85:  * of that protocol for it to be advertised as available. */
                     86: static void check_sub_protocol(void)
                     87: {
                     88:        char *dot;
                     89:        int their_protocol, their_sub;
                     90: #if SUBPROTOCOL_VERSION != 0
                     91:        int our_sub = protocol_version < PROTOCOL_VERSION ? 0 : SUBPROTOCOL_VERSION;
                     92: #else
                     93:        int our_sub = 0;
                     94: #endif
                     95: 
                     96:        /* client_info starts with VER.SUB string if client is a pre-release. */
                     97:        if (!(their_protocol = atoi(client_info))
                     98:         || !(dot = strchr(client_info, '.'))
                     99:         || !(their_sub = atoi(dot+1))) {
                    100: #if SUBPROTOCOL_VERSION != 0
                    101:                if (our_sub)
                    102:                        protocol_version--;
                    103: #endif
                    104:                return;
                    105:        }
                    106: 
                    107:        if (their_protocol < protocol_version) {
                    108:                if (their_sub)
                    109:                        protocol_version = their_protocol - 1;
                    110:                return;
                    111:        }
                    112: 
                    113:        if (their_protocol > protocol_version)
                    114:                their_sub = 0; /* 0 == final version of older protocol */
                    115:        if (their_sub != our_sub)
                    116:                protocol_version--;
                    117: }
                    118: 
                    119: void set_allow_inc_recurse(void)
                    120: {
                    121:        client_info = shell_cmd ? shell_cmd : "";
                    122: 
                    123:        if (!recurse || use_qsort)
                    124:                allow_inc_recurse = 0;
                    125:        else if (!am_sender
                    126:         && (delete_before || delete_after
1.1.1.1.2.1! misho     127:          || detect_renamed
1.1       misho     128:          || delay_updates || prune_empty_dirs))
                    129:                allow_inc_recurse = 0;
                    130:        else if (am_server && !local_server
                    131:         && (strchr(client_info, 'i') == NULL))
                    132:                allow_inc_recurse = 0;
                    133: }
                    134: 
                    135: void setup_protocol(int f_out,int f_in)
                    136: {
                    137:        if (am_sender)
                    138:                file_extra_cnt += PTR_EXTRA_CNT;
                    139:        else
                    140:                file_extra_cnt++;
                    141:        if (preserve_uid)
                    142:                uid_ndx = ++file_extra_cnt;
                    143:        if (preserve_gid)
                    144:                gid_ndx = ++file_extra_cnt;
                    145:        if (preserve_acls && !am_sender)
                    146:                acls_ndx = ++file_extra_cnt;
                    147:        if (preserve_xattrs)
                    148:                xattrs_ndx = ++file_extra_cnt;
                    149: 
                    150:        if (am_server)
                    151:                set_allow_inc_recurse();
                    152: 
                    153:        if (remote_protocol == 0) {
                    154:                if (am_server && !local_server)
                    155:                        check_sub_protocol();
                    156:                if (!read_batch)
                    157:                        write_int(f_out, protocol_version);
                    158:                remote_protocol = read_int(f_in);
                    159:                if (protocol_version > remote_protocol)
                    160:                        protocol_version = remote_protocol;
                    161:        }
                    162:        if (read_batch && remote_protocol > protocol_version) {
                    163:                rprintf(FERROR, "The protocol version in the batch file is too new (%d > %d).\n",
                    164:                        remote_protocol, protocol_version);
                    165:                exit_cleanup(RERR_PROTOCOL);
                    166:        }
                    167: 
                    168:        if (verbose > 3) {
                    169:                rprintf(FINFO, "(%s) Protocol versions: remote=%d, negotiated=%d\n",
                    170:                        am_server? "Server" : "Client", remote_protocol, protocol_version);
                    171:        }
                    172:        if (remote_protocol < MIN_PROTOCOL_VERSION
                    173:         || remote_protocol > MAX_PROTOCOL_VERSION) {
                    174:                rprintf(FERROR,"protocol version mismatch -- is your shell clean?\n");
                    175:                rprintf(FERROR,"(see the rsync man page for an explanation)\n");
                    176:                exit_cleanup(RERR_PROTOCOL);
                    177:        }
                    178:        if (remote_protocol < OLD_PROTOCOL_VERSION) {
                    179:                rprintf(FINFO,"%s is very old version of rsync, upgrade recommended.\n",
                    180:                        am_server? "Client" : "Server");
                    181:        }
                    182:        if (protocol_version < MIN_PROTOCOL_VERSION) {
                    183:                rprintf(FERROR, "--protocol must be at least %d on the %s.\n",
                    184:                        MIN_PROTOCOL_VERSION, am_server? "Server" : "Client");
                    185:                exit_cleanup(RERR_PROTOCOL);
                    186:        }
                    187:        if (protocol_version > PROTOCOL_VERSION) {
                    188:                rprintf(FERROR, "--protocol must be no more than %d on the %s.\n",
                    189:                        PROTOCOL_VERSION, am_server? "Server" : "Client");
                    190:                exit_cleanup(RERR_PROTOCOL);
                    191:        }
                    192:        if (read_batch)
                    193:                check_batch_flags();
                    194: 
                    195:        if (protocol_version < 30) {
                    196:                if (append_mode == 1)
                    197:                        append_mode = 2;
                    198:                if (preserve_acls && !local_server) {
                    199:                        rprintf(FERROR,
                    200:                            "--acls requires protocol 30 or higher"
                    201:                            " (negotiated %d).\n",
                    202:                            protocol_version);
                    203:                        exit_cleanup(RERR_PROTOCOL);
                    204:                }
                    205:                if (preserve_xattrs && !local_server) {
                    206:                        rprintf(FERROR,
                    207:                            "--xattrs requires protocol 30 or higher"
                    208:                            " (negotiated %d).\n",
                    209:                            protocol_version);
                    210:                        exit_cleanup(RERR_PROTOCOL);
                    211:                }
                    212:        }
                    213: 
                    214:        if (delete_mode && !(delete_before+delete_during+delete_after)) {
                    215:                if (protocol_version < 30)
                    216:                        delete_before = 1;
                    217:                else
                    218:                        delete_during = 1;
                    219:        }
                    220: 
                    221:        if (protocol_version < 29) {
                    222:                if (fuzzy_basis) {
                    223:                        rprintf(FERROR,
                    224:                            "--fuzzy requires protocol 29 or higher"
                    225:                            " (negotiated %d).\n",
                    226:                            protocol_version);
                    227:                        exit_cleanup(RERR_PROTOCOL);
                    228:                }
                    229: 
                    230:                if (basis_dir_cnt && inplace) {
                    231:                        rprintf(FERROR,
                    232:                            "%s with --inplace requires protocol 29 or higher"
                    233:                            " (negotiated %d).\n",
                    234:                            dest_option, protocol_version);
                    235:                        exit_cleanup(RERR_PROTOCOL);
                    236:                }
                    237: 
                    238:                if (basis_dir_cnt > 1) {
                    239:                        rprintf(FERROR,
                    240:                            "Using more than one %s option requires protocol"
                    241:                            " 29 or higher (negotiated %d).\n",
                    242:                            dest_option, protocol_version);
                    243:                        exit_cleanup(RERR_PROTOCOL);
                    244:                }
                    245: 
                    246:                if (prune_empty_dirs) {
                    247:                        rprintf(FERROR,
                    248:                            "--prune-empty-dirs requires protocol 29 or higher"
                    249:                            " (negotiated %d).\n",
                    250:                            protocol_version);
                    251:                        exit_cleanup(RERR_PROTOCOL);
                    252:                }
                    253:        } else if (protocol_version >= 30) {
                    254:                if (am_server) {
                    255:                        compat_flags = allow_inc_recurse ? CF_INC_RECURSE : 0;
                    256: #ifdef CAN_SET_SYMLINK_TIMES
                    257:                        compat_flags |= CF_SYMLINK_TIMES;
                    258: #endif
                    259: #ifdef ICONV_OPTION
                    260:                        compat_flags |= CF_SYMLINK_ICONV;
                    261: #endif
                    262:                        if (local_server || strchr(client_info, 'f') != NULL)
                    263:                                compat_flags |= CF_SAFE_FLIST;
                    264:                        write_byte(f_out, compat_flags);
                    265:                } else
                    266:                        compat_flags = read_byte(f_in);
                    267:                /* The inc_recurse var MUST be set to 0 or 1. */
                    268:                inc_recurse = compat_flags & CF_INC_RECURSE ? 1 : 0;
                    269:                if (am_sender) {
                    270:                        receiver_symlink_times = am_server
                    271:                            ? strchr(client_info, 'L') != NULL
                    272:                            : !!(compat_flags & CF_SYMLINK_TIMES);
                    273:                }
                    274: #ifdef CAN_SET_SYMLINK_TIMES
                    275:                else
                    276:                        receiver_symlink_times = 1;
                    277: #endif
                    278: #ifdef ICONV_OPTION
                    279:                sender_symlink_iconv = iconv_opt && (am_server
                    280:                    ? local_server || strchr(client_info, 's') != NULL
                    281:                    : !!(compat_flags & CF_SYMLINK_ICONV));
                    282: #endif
                    283:                if (inc_recurse && !allow_inc_recurse) {
                    284:                        /* This should only be able to happen in a batch. */
                    285:                        fprintf(stderr,
                    286:                            "Incompatible options specified for inc-recursive %s.\n",
                    287:                            read_batch ? "batch file" : "connection");
                    288:                        exit_cleanup(RERR_SYNTAX);
                    289:                }
                    290:                use_safe_inc_flist = !!(compat_flags & CF_SAFE_FLIST);
                    291:                need_messages_from_generator = 1;
                    292: #ifdef CAN_SET_SYMLINK_TIMES
                    293:        } else if (!am_sender) {
                    294:                receiver_symlink_times = 1;
                    295: #endif
                    296:        }
                    297: 
                    298:        if (need_unsorted_flist && (!am_sender || inc_recurse))
                    299:                unsort_ndx = ++file_extra_cnt;
                    300: 
                    301:        if (partial_dir && *partial_dir != '/' && (!am_server || local_server)) {
                    302:                int flags = MATCHFLG_NO_PREFIXES | MATCHFLG_DIRECTORY;
                    303:                if (!am_sender || protocol_version >= 30)
                    304:                        flags |= MATCHFLG_PERISHABLE;
                    305:                parse_rule(&filter_list, partial_dir, flags, 0);
                    306:        }
                    307: 
                    308: 
                    309: #ifdef ICONV_OPTION
                    310:        if (protect_args && files_from) {
                    311:                if (am_sender)
                    312:                        filesfrom_convert = filesfrom_host && ic_send != (iconv_t)-1;
                    313:                else
                    314:                        filesfrom_convert = !filesfrom_host && ic_recv != (iconv_t)-1;
                    315:        }
                    316: #endif
                    317: 
                    318:        if (am_server) {
                    319:                if (!checksum_seed)
                    320:                        checksum_seed = time(NULL);
                    321:                write_int(f_out, checksum_seed);
                    322:        } else {
                    323:                checksum_seed = read_int(f_in);
                    324:        }
                    325: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>