[BACK]Return to fargs.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / rsync

Annotation of src/usr.bin/rsync/fargs.c, Revision 1.8

1.8     ! benno       1: /*     $Id: fargs.c,v 1.7 2019/02/12 18:06:25 benno Exp $ */
1.1       benno       2: /*
                      3:  * Copyright (c) 2019 Kristaps Dzonsons <kristaps@bsd.lv>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16:  */
                     17: #include <sys/stat.h>
                     18:
                     19: #include <assert.h>
                     20: #include <stdint.h>
                     21: #include <stdlib.h>
                     22:
                     23: #include "extern.h"
                     24:
1.2       benno      25: #define        RSYNC_PATH      "rsync"
1.1       benno      26:
                     27: char **
                     28: fargs_cmdline(struct sess *sess, const struct fargs *f)
                     29: {
                     30:        char            **args;
                     31:        size_t            i = 0, j, argsz = 0;
1.6       deraadt    32:        char             *rsync_path, *ssh_prog;
1.1       benno      33:
1.3       deraadt    34:        assert(f != NULL);
1.1       benno      35:        assert(f->sourcesz > 0);
                     36:
1.3       deraadt    37:        if ((rsync_path = sess->opts->rsync_path) == NULL)
1.1       benno      38:                rsync_path = RSYNC_PATH;
1.6       deraadt    39:        if ((ssh_prog = sess->opts->ssh_prog) == NULL)
                     40:                ssh_prog = "ssh";
1.1       benno      41:
                     42:        /* Be explicit with array size. */
                     43:
1.2       benno      44:        argsz += 1;     /* dot separator */
                     45:        argsz += 1;     /* sink file */
                     46:        argsz += 5;     /* per-mode maximum */
1.7       benno      47:        argsz += 11;    /* shared args */
1.1       benno      48:        argsz += 1;     /* NULL pointer */
                     49:        argsz += f->sourcesz;
                     50:
                     51:        args = calloc(argsz, sizeof(char *));
1.3       deraadt    52:        if (args == NULL) {
1.1       benno      53:                ERR(sess, "calloc");
                     54:                return NULL;
                     55:        }
                     56:
1.3       deraadt    57:        if (f->host != NULL) {
                     58:                assert(f->host != NULL);
1.8     ! benno      59:
1.6       deraadt    60:                args[i++] = ssh_prog;
1.1       benno      61:                args[i++] = f->host;
1.5       deraadt    62:                args[i++] = rsync_path;
1.1       benno      63:                args[i++] = "--server";
1.3       deraadt    64:                if (f->mode == FARGS_RECEIVER)
1.1       benno      65:                        args[i++] = "--sender";
                     66:        } else {
1.5       deraadt    67:                args[i++] = rsync_path;
1.1       benno      68:                args[i++] = "--server";
                     69:        }
                     70:
                     71:        /* Shared arguments. */
                     72:
1.7       benno      73:        if (sess->opts->del)
                     74:                args[i++] = "--delete";
                     75:        if (sess->opts->preserve_gids)
                     76:                args[i++] = "-g";
                     77:        if (sess->opts->preserve_links)
                     78:                args[i++] = "-l";
                     79:        if (sess->opts->dry_run)
                     80:                args[i++] = "-n";
                     81:        if (sess->opts->preserve_perms)
                     82:                args[i++] = "-p";
                     83:        if (sess->opts->recursive)
                     84:                args[i++] = "-r";
                     85:        if (sess->opts->preserve_times)
                     86:                args[i++] = "-t";
1.1       benno      87:        if (sess->opts->verbose > 3)
                     88:                args[i++] = "-v";
                     89:        if (sess->opts->verbose > 2)
                     90:                args[i++] = "-v";
                     91:        if (sess->opts->verbose > 1)
                     92:                args[i++] = "-v";
                     93:        if (sess->opts->verbose > 0)
                     94:                args[i++] = "-v";
                     95:
                     96:        /* Terminate with a full-stop for reasons unknown. */
                     97:
                     98:        args[i++] = ".";
                     99:
1.3       deraadt   100:        if (f->mode == FARGS_RECEIVER) {
1.1       benno     101:                for (j = 0; j < f->sourcesz; j++)
                    102:                        args[i++] = f->sources[j];
                    103:        } else
                    104:                args[i++] = f->sink;
                    105:
                    106:        args[i] = NULL;
                    107:        return args;
                    108: }