【问题标题】:ssh_scp_read returns garbagessh_scp_read 返回垃圾
【发布时间】:2017-07-25 22:54:20
【问题描述】:

我正在尝试从我的服务器下载文件;客户端和服务器都是 Linux,但 ssh_scp_read() 返回的整数不正确。根据文档,该函数最多可写入 65536 个字节,但在文件为 37980 时仅读取 16384,但这不是我主要关心的问题;在这 16384 字节的末尾附近,它开始用 NULL 垃圾填充缓冲区,然后将其写入文件。

递归目录的创建工作正常;问题是下载大于 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

我们将不胜感激。

【问题讨论】:

    标签: c scp libssh


    【解决方案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);
    

    【讨论】:

      【解决方案2】:

      将你的内部循环更改为

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

      【讨论】:

      • 感谢代码,它修复了“在无效状态下调用 ssh_scp_read”,但这不是问题,NULL garbabe 仍然被写入文件中。它必须是缓冲区或写入的东西。
      猜你喜欢
      • 2013-06-07
      • 1970-01-01
      • 1970-01-01
      • 2014-12-04
      • 2015-06-27
      • 2016-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多