简体   繁体   English

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

[英]scp .tar file from remote machine using C program & libssh

I have written a code which read file from remote device using libssh scp APIs.我编写了一个使用 libssh scp API 从远程设备读取文件的代码。

I have a specific requirement wherein I want to scp a .tar file from a remote device.我有一个特定要求,我想从远程设备 scp .tar 文件。 I am able to read .tar content into a buffer, but I am not sure how to create .tar file out of that buffer.我能够将 .tar 内容读入缓冲区,但我不确定如何从该缓冲区创建 .tar 文件。

Any help would be appreciated.任何帮助,将不胜感激。

Thanks.谢谢。

Code snippet:代码片段:

   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;
    }

Create a local file using either open() or fopen() , then feed in the raw data using write() or fwrite() .使用open()fopen()创建本地文件,然后使用write()fwrite()输入原始数据。 When finished, call close () or fclose() .完成后,调用 close ()fclose()

Updated your code-snippet, not compile tested, but gives you the idea.更新了您的代码片段,未经过编译测试,但为您提供了想法。

The remote read should be repeated until the whole file has been received, also, you might receive chunks that are smaller than sizeof (t_buffer), so do not write out more data than you received.应该重复远程读取,直到接收到整个文件为止,此外,您可能会收到小于 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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM