通过posix_spawn进行Rsync

时间:2015-01-16 10:00:10

标签: c++ c posix rsync

我有一个rsync守护程序正在运行,我可以成功执行以下命令:

rsync --port = 1873 -avWh 127.0.0.1::jackfruit_peers/data.0a6 / home / v / data

但是来自代码:

local_dir = / home / v / data

remote_dir = 127.0.0.1::jackfruit_peers/data.0a6

    pid_t child_pid;
    char cmd[] = "rsync -avWh --port=1873";
    char *argv[] = {cmd, remote_dir, local_dir,
                    (char*) 0}; 
    if (0 != posix_spawn(&child_pid, "/usr/bin/rsync", NULL, NULL, argv, environ)) {
        logger::error("posix spawn");
        return ERR_INTERNAL_SERVER_ERROR;
    }   

我收到错误:

  

rsync:无法连接到127.0.0.1(127.0.0.1):连接被拒绝   (111)rsync错误:clientserver.c上的套接字IO(代码10)中的错误(128)   [Receiver = 3.1.0] 2015-01-16 15:27:08.421.732 28623 0x7fbc01914010 INFO   唤醒孩子pid = 30060的父母。错误号= 0

知道为什么吗?

编辑:rsync句柄定义为:

 [jackfruit_peers]
    comment = for data transfer
    path = /home/jackfruit/
    read only = yes
    timeout  =  60

1 个答案:

答案 0 :(得分:0)

更改为这种丑陋的格式并且有效:

   pid_t child_pid;
    char a1[] = "rsync";
    char a2[] = "-avWh";
    char a3[] = "--port=1873";
    char *argv[] = {a1, a2, a3, remote_dir, local_dir,
                   (char*)0};

    if (0 != posix_spawn(&child_pid, "/usr/bin/rsync", NULL, NULL, argv, environ)) {
        logger::error("posix spawn");
        return ERR_INTERNAL_SERVER_ERROR;
    }   

当我使用实际的字符串常量代替a1,a2,a3时,我得到了从const char *到char *的弃用字符串转换,因此我愚蠢地将所有内容归入:

char cmd[] = "rsync -avWh --port=1873";