2017-07-21 136 views
0

我試圖從我的服務器上下載文件;客戶端和服務器都是Linux,但ssh_scp_read()返回不正確的整數。根據文檔,該函數寫入65536字節,但當文件爲37980時僅讀取16384,但這不是我主要關心的問題;在這個16384字節的末尾,它開始用NULL垃圾填充緩衝區,然後將其寫入文件。ssh_scp_read返回垃圾

遞歸目錄的創建工作正常;問題是下載大於16384字節的文件。在這一點上,我將使用sftp而不是scp,但我想知道我做錯了什麼。

這是函數的代碼:

int get(ssh_session gno_ses,ssh_scp scp) 
{ 
    int rc; 
    int size, permissions; 
    char *buff, *filename, path[PATH_MAX]; 

    while(1) 
    { 
     rc = ssh_scp_pull_request(scp); 
     switch (rc) 
     { 
      // cases [...] 
      case SSH_SCP_REQUEST_NEWFILE: 
       size = ssh_scp_request_get_size(scp); 
       printf("Size is %d\n",size); 
       filename = strdup(ssh_scp_request_get_filename(scp)); 
       permissions = ssh_scp_request_get_permissions(scp); 

       FILE *file; 
       file = fopen(filename, "w+"); 
       if (!file) 
       { 
        ssh_scp_deny_request(scp,"Unable to open"); 
        fprintf(stderr, " %s: %s\n", filename, strerror(errno)); 
        fclose(file); 
        break; 
       } 

       buff = malloc(size); 
       printf("Size of buffer is %d\n", size); 
       if (!buff) 
       { 
        fprintf(stderr, "\nBuff memory allocation error.\n"); 
        return SSH_ERROR; 
       } 

       if(ssh_scp_accept_request(scp) != SSH_OK) 
       { 
        fprintf(stderr, "Error accepting request: %s\n", ssh_get_error(gno_ses)); 
        break; 
       } 

       do 
       { 
        rc = ssh_scp_read(scp, buff, size); 
        if (rc == SSH_ERROR) 
        { 
         fprintf(stderr, "Error receiving file data: %s\n", ssh_get_error(gno_ses)); 
         break; 
        } 
        if (fwrite(buff, 1, size, file) != size) 
        { 
         perror("Error at writting to file: "); 
         break; 
        } 
        printf("ssh_scp_read got %d\n",rc); 
       } while (rc != 0); 

       fclose(file); 
       free(filename); 
       free(buff); 
       break; 
     } 
    } 
    return SSH_OK; 
} 

而這是輸出:

Size is 37980 
Size of buffer is 37980 
ssh_scp_read got 16384 
ssh_scp_read got 16384 
ssh_scp_read got 5212 
Error receiving file data: ssh_scp_read called under invalid state 

任何輸入,將不勝感激。

回答

0

改變你的內環爲

int len = size; 
do 
    { 
        rc = ssh_scp_read(scp, buff, size); 
        if (rc == SSH_ERROR) 
        { 
         fprintf(stderr, "Error receiving file data: %s\n", 
         ssh_get_error(gno_ses)); 
         break; 
        } 
        if (fwrite(buff, 1, rc, file) != size) 
        { 
         perror("Error at writting to file: "); 
         break; 
        } 
        printf("ssh_scp_read got %d\n",rc); 
        len-=rc; 
    } while (len); 
+0

感謝您的代碼,它修復es「在無效狀態下調用的ssh_scp_read」,但這不是問題,NULL garbabe仍在寫入文件中。它必須是緩衝區或寫作的東西。 – Vanreyn

1

的問題是,我在寫size字節,而實際上scp_scp_read()報告說,它曾讀過小於:

rc = ssh_scp_read(scp, buff, size); 
fwrite(buff, 1, size, file) 

解決方法是寫只有rc字節:

  int len_loop = size; 
      int len; 
      do 
      { 
       rc = ssh_scp_read(scp, buff, size); 
       if (rc == SSH_ERROR || rc < 0) 
       { 
        fprintf(stderr, "Error receiving file data: %s\n", ssh_get_error(gno_ses)); 
        break; 
       } 
       else if (!rc) 
       { 
        break; 
       } 
       len = fwrite(buff, 1, rc, file); 
       if (len != rc) 
       { 
        perror("Error at writting to file: "); 
        break; 
       } 
       printf("ssh_scp_read got %d\n",rc); 
       len_loop -= rc; 
      } while(len_loop);