Skip to content
Advertisement

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.

I have a specific requirement wherein I want to scp a .tar file from a remote device. I am able to read .tar content into a buffer, but I am not sure how to create .tar file out of that buffer.

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%on", 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: %sn", ssh_get_error(in_session));
               ssh_scp_close(t_scp);
               ssh_scp_free(t_scp);
               return t_rc;
            }
            fprintf(stderr, "Bytes received = %dn", t_rc);
            FILE *fptr = fopen(t_filename, "w");
            if(NULL != fptr)
            {
               fwrite(t_buffer,sizeof(t_buffer),1,fptr);
               fclose(fptr);
            }
            break;
    }

Advertisement

Answer

Create a local file using either open() or fopen(), then feed in the raw data using write() or fwrite(). When finished, call close() or 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.

   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%on", t_filename, t_filesize, t_filemode);
            FILE *fptr = fopen(t_filename, "w");
            if(NULL == fptr)
            {
                fprintf (stderr, "Error opening local file: %s, error %sn", 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: %sn", ssh_get_error(in_session));
                    fclose(fptr);
                    ssh_scp_close(t_scp);
                    ssh_scp_free(t_scp);
                    return t_rc;
                }
                fprintf(stderr, "Bytes received = %dn", t_rc);
                if (fwrite(t_buffer,t_rc,1,fptr) != 1)
                {
                    fprintf (stderr, "Error writing file data: %sn", strerror (errno));
                    fclose(fptr);
                    ssh_scp_close(t_scp);
                    ssh_scp_free(t_scp);
                    return t_rc;
                }             
            } while (t_rc != 0);
            fclose (fptr);
            break;
    }
Advertisement