来自远程机器的scp .tar文件,使用C程序& libssh

时间:2016-04-29 09:35:11

标签: c linux tar scp libssh

我编写了一个使用libssh scp API从远程设备读取文件的代码。

我有一个特定的要求,其中我想从远程设备scp .tar文件。 我能够将.tar内容读入缓冲区,但我不确定如何从该缓冲区创建.tar文件。

任何帮助都将不胜感激。

感谢。

代码段:

   char      *t_filename, t_buffer[32768];
   ....
   t_rc = ssh_scp_pull_request(t_scp);
   switch(t_rc)
   {
       case SSH_SCP_REQUEST_NEWFILE:
            t_filesize = ssh_scp_request_get_size(t_scp);
            t_filename = strdup(ssh_scp_request_get_filename(t_scp));
            t_filemode = ssh_scp_request_get_permissions(t_scp);
            fprintf(stderr, "Receiving file %s, size %d, permisssions 0%o\n", t_filename, t_filesize, t_filemode);
            ssh_scp_accept_request(t_scp);
            t_rc = ssh_scp_read(t_scp, t_buffer, sizeof(t_buffer));
            if(t_rc == SSH_ERROR)
            {
               fprintf(stderr, "Error receiving file data: %s\n", ssh_get_error(in_session));
               ssh_scp_close(t_scp);
               ssh_scp_free(t_scp);
               return t_rc;
            }
            fprintf(stderr, "Bytes received = %d\n", t_rc);
            FILE *fptr = fopen(t_filename, "w");
            if(NULL != fptr)
            {
               fwrite(t_buffer,sizeof(t_buffer),1,fptr);
               fclose(fptr);
            }
            break;
    }

1 个答案:

答案 0 :(得分:0)

使用open()fopen()创建本地文件,然后使用write()fwrite()输入原始数据。完成后,请致电关闭()fclose()

更新了您的代码段,未经过编译测试,但为您提供了这个想法。

应该重复远程读取,直到收到整个文件,同样,你可能会收到小于sizeof(t_buffer)的块,所以不要写出比你收到的更多的数据。

   char      *t_filename, t_buffer[32768];
   ....
   t_rc = ssh_scp_pull_request(t_scp);
   switch(t_rc)
   {
       case SSH_SCP_REQUEST_NEWFILE:
            t_filesize = ssh_scp_request_get_size(t_scp);
            t_filename = strdup(ssh_scp_request_get_filename(t_scp));
            t_filemode = ssh_scp_request_get_permissions(t_scp);
            fprintf(stderr, "Receiving file %s, size %d, permisssions 0%o\n", t_filename, t_filesize, t_filemode);
            FILE *fptr = fopen(t_filename, "w");
            if(NULL == fptr)
            {
                fprintf (stderr, "Error opening local file: %s, error %s\n", t_filename, strerror (errno));
                ssh_scp_deny_request (t_scp, "Unable to open local file");
                break;
            }
            ssh_scp_accept_request(t_scp);

            do
            {
                t_rc = ssh_scp_read(t_scp, t_buffer, sizeof(t_buffer));
                if(t_rc == SSH_ERROR)
                {
                    fprintf(stderr, "Error receiving file data: %s\n", ssh_get_error(in_session));
                    fclose(fptr);
                    ssh_scp_close(t_scp);
                    ssh_scp_free(t_scp);
                    return t_rc;
                }
                fprintf(stderr, "Bytes received = %d\n", t_rc);
                if (fwrite(t_buffer,t_rc,1,fptr) != 1)
                {
                    fprintf (stderr, "Error writing file data: %s\n", strerror (errno));
                    fclose(fptr);
                    ssh_scp_close(t_scp);
                    ssh_scp_free(t_scp);
                    return t_rc;
                }             
            } while (t_rc != 0);
            fclose (fptr);
            break;
    }