-2

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%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 Answers1

0

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%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;
    }
Stian Skjelstad
  • 2,277
  • 1
  • 9
  • 19
  • Thanks Stian. I used fwrite but after writing file size was very small and tar was not recognizing it as archive file. So, I increased the size of buffer which was used to read from remote device. Now, I am getting half the actual size of tar file and it is recognizable by tar utility. Any idea why I am not receiving all the contents of tar file? – Aradhana Kumar May 01 '16 at 17:59
  • Can you share some code, else it will only be wild guess from us what is wrong – Stian Skjelstad May 01 '16 at 20:22
  • Code snippet added in problem statement. If you want I can share the whole function with you separately. Please suggest. – Aradhana Kumar May 03 '16 at 05:07
  • Thanks Stian. It worked :) I just tested as I did not have the setup yesterday. – Aradhana Kumar May 04 '16 at 04:59