| 1 |
/* bench_sftp.c |
| 2 |
* |
| 3 |
* This file is part of the SSH Library |
| 4 |
* |
| 5 |
* Copyright (c) 2011 by Aris Adamantiadis |
| 6 |
* |
| 7 |
* The SSH Library is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU Lesser General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2.1 of the License, or (at your |
| 10 |
* option) any later version. |
| 11 |
* |
| 12 |
* The SSH Library is distributed in the hope that it will be useful, but |
| 13 |
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 14 |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 15 |
* License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Lesser General Public License |
| 18 |
* along with the SSH Library; see the file COPYING. If not, write to |
| 19 |
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 20 |
* MA 02111-1307, USA. |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "benchmarks.h" |
| 24 |
#include <libssh/libssh.h> |
| 25 |
#include <libssh/sftp.h> |
| 26 |
#include <libssh/misc.h> |
| 27 |
#include <stdio.h> |
| 28 |
#include <fcntl.h> |
| 29 |
#include <stdlib.h> |
| 30 |
|
| 31 |
#define SFTPDIR "/tmp/" |
| 32 |
#define SFTPFILE "scpbenchmark" |
| 33 |
|
| 34 |
/** @internal |
| 35 |
* @brief benchmarks a synchronous sftp upload using an |
| 36 |
* existing SSH session. |
| 37 |
* @param[in] session Open SSH session |
| 38 |
* @param[in] args Parsed command line arguments |
| 39 |
* @param[out] bps The calculated bytes per second obtained via benchmark. |
| 40 |
* @return 0 on success, -1 on error. |
| 41 |
*/ |
| 42 |
int benchmarks_sync_sftp_up (ssh_session session, struct argument_s *args, |
| 43 |
float *bps){ |
| 44 |
unsigned long bytes; |
| 45 |
struct timestamp_struct ts; |
| 46 |
float ms=0.0; |
| 47 |
unsigned long total=0; |
| 48 |
sftp_session sftp; |
| 49 |
sftp_file file = NULL; |
| 50 |
|
| 51 |
bytes = args->datasize * 1024 * 1024; |
| 52 |
sftp = sftp_new(session); |
| 53 |
if(sftp == NULL) |
| 54 |
goto error; |
| 55 |
if(sftp_init(sftp)==SSH_ERROR) |
| 56 |
goto error; |
| 57 |
file = sftp_open(sftp,SFTPDIR SFTPFILE,O_RDWR | O_CREAT | O_TRUNC, 0777); |
| 58 |
if(!file) |
| 59 |
goto error; |
| 60 |
if(args->verbose>0) |
| 61 |
fprintf(stdout,"Starting upload of %lu bytes now\n",bytes); |
| 62 |
timestamp_init(&ts); |
| 63 |
while(total < bytes){ |
| 64 |
unsigned long towrite = bytes - total; |
| 65 |
int w; |
| 66 |
if(towrite > args->chunksize) |
| 67 |
towrite = args->chunksize; |
| 68 |
w=sftp_write(file,buffer,towrite); |
| 69 |
if(w == SSH_ERROR) |
| 70 |
goto error; |
| 71 |
total += w; |
| 72 |
} |
| 73 |
sftp_close(file); |
| 74 |
ms=elapsed_time(&ts); |
| 75 |
*bps=8000 * (float)bytes / ms; |
| 76 |
if(args->verbose > 0) |
| 77 |
fprintf(stdout,"Upload took %f ms for %lu bytes, at %f bps\n",ms, |
| 78 |
bytes,*bps); |
| 79 |
sftp_free(sftp); |
| 80 |
return 0; |
| 81 |
error: |
| 82 |
fprintf(stderr,"Error during scp upload : %s\n",ssh_get_error(session)); |
| 83 |
if(file) |
| 84 |
sftp_close(file); |
| 85 |
if(sftp) |
| 86 |
sftp_free(sftp); |
| 87 |
return -1; |
| 88 |
} |
| 89 |
|
| 90 |
/** @internal |
| 91 |
* @brief benchmarks a synchronous sftp download using an |
| 92 |
* existing SSH session. |
| 93 |
* @param[in] session Open SSH session |
| 94 |
* @param[in] args Parsed command line arguments |
| 95 |
* @param[out] bps The calculated bytes per second obtained via benchmark. |
| 96 |
* @return 0 on success, -1 on error. |
| 97 |
*/ |
| 98 |
int benchmarks_sync_sftp_down (ssh_session session, struct argument_s *args, |
| 99 |
float *bps){ |
| 100 |
unsigned long bytes; |
| 101 |
struct timestamp_struct ts; |
| 102 |
float ms=0.0; |
| 103 |
unsigned long total=0; |
| 104 |
sftp_session sftp; |
| 105 |
sftp_file file = NULL; |
| 106 |
int r; |
| 107 |
|
| 108 |
bytes = args->datasize * 1024 * 1024; |
| 109 |
sftp = sftp_new(session); |
| 110 |
if(sftp == NULL) |
| 111 |
goto error; |
| 112 |
if(sftp_init(sftp)==SSH_ERROR) |
| 113 |
goto error; |
| 114 |
file = sftp_open(sftp,SFTPDIR SFTPFILE,O_RDONLY,0); |
| 115 |
if(!file) |
| 116 |
goto error; |
| 117 |
if(args->verbose>0) |
| 118 |
fprintf(stdout,"Starting download of %lu bytes now\n",bytes); |
| 119 |
timestamp_init(&ts); |
| 120 |
while(total < bytes){ |
| 121 |
unsigned long toread = bytes - total; |
| 122 |
if(toread > args->chunksize) |
| 123 |
toread = args->chunksize; |
| 124 |
r=sftp_read(file,buffer,toread); |
| 125 |
if(r == SSH_ERROR) |
| 126 |
goto error; |
| 127 |
total += r; |
| 128 |
/* we had a smaller file */ |
| 129 |
if(r==0){ |
| 130 |
fprintf(stdout,"File smaller than expected : %lu (expected %lu).\n",total,bytes); |
| 131 |
bytes = total; |
| 132 |
break; |
| 133 |
} |
| 134 |
} |
| 135 |
sftp_close(file); |
| 136 |
ms=elapsed_time(&ts); |
| 137 |
*bps=8000 * (float)bytes / ms; |
| 138 |
if(args->verbose > 0) |
| 139 |
fprintf(stdout,"download took %f ms for %lu bytes, at %f bps\n",ms, |
| 140 |
bytes,*bps); |
| 141 |
sftp_free(sftp); |
| 142 |
return 0; |
| 143 |
error: |
| 144 |
fprintf(stderr,"Error during sftp download : %s\n",ssh_get_error(session)); |
| 145 |
if(file) |
| 146 |
sftp_close(file); |
| 147 |
if(sftp) |
| 148 |
sftp_free(sftp); |
| 149 |
return -1; |
| 150 |
} |
| 151 |
|
| 152 |
/** @internal |
| 153 |
* @brief benchmarks an asynchronous sftp download using an |
| 154 |
* existing SSH session. |
| 155 |
* @param[in] session Open SSH session |
| 156 |
* @param[in] args Parsed command line arguments |
| 157 |
* @param[out] bps The calculated bytes per second obtained via benchmark. |
| 158 |
* @return 0 on success, -1 on error. |
| 159 |
*/ |
| 160 |
int benchmarks_async_sftp_down (ssh_session session, struct argument_s *args, |
| 161 |
float *bps){ |
| 162 |
unsigned long bytes; |
| 163 |
struct timestamp_struct ts; |
| 164 |
float ms=0.0; |
| 165 |
unsigned long total=0; |
| 166 |
sftp_session sftp; |
| 167 |
sftp_file file = NULL; |
| 168 |
int r,i; |
| 169 |
int warned = 0; |
| 170 |
unsigned long toread; |
| 171 |
int *ids=NULL; |
| 172 |
int concurrent_downloads = args->concurrent_requests; |
| 173 |
|
| 174 |
bytes = args->datasize * 1024 * 1024; |
| 175 |
sftp = sftp_new(session); |
| 176 |
if(sftp == NULL) |
| 177 |
goto error; |
| 178 |
if(sftp_init(sftp)==SSH_ERROR) |
| 179 |
goto error; |
| 180 |
file = sftp_open(sftp,SFTPDIR SFTPFILE,O_RDONLY,0); |
| 181 |
if(!file) |
| 182 |
goto error; |
| 183 |
ids = malloc(concurrent_downloads * sizeof(int)); |
| 184 |
if (ids == NULL) { |
| 185 |
return -1; |
| 186 |
} |
| 187 |
if(args->verbose>0) |
| 188 |
fprintf(stdout,"Starting download of %lu bytes now, using %d concurrent downloads\n",bytes, |
| 189 |
concurrent_downloads); |
| 190 |
timestamp_init(&ts); |
| 191 |
for (i=0;i<concurrent_downloads;++i){ |
| 192 |
ids[i]=sftp_async_read_begin(file, args->chunksize); |
| 193 |
if(ids[i]==SSH_ERROR) |
| 194 |
goto error; |
| 195 |
} |
| 196 |
i=0; |
| 197 |
while(total < bytes){ |
| 198 |
r = sftp_async_read(file, buffer, args->chunksize, ids[i]); |
| 199 |
if(r == SSH_ERROR) |
| 200 |
goto error; |
| 201 |
total += r; |
| 202 |
if(r != (int)args->chunksize && total != bytes && !warned){ |
| 203 |
fprintf(stderr,"async_sftp_download : receiving short reads (%d, requested %d) " |
| 204 |
"the received file will be corrupted and shorted. Adapt chunksize to %d\n", |
| 205 |
r, args->chunksize,r); |
| 206 |
warned = 1; |
| 207 |
} |
| 208 |
/* we had a smaller file */ |
| 209 |
if(r==0){ |
| 210 |
fprintf(stdout,"File smaller than expected : %lu (expected %lu).\n",total,bytes); |
| 211 |
bytes = total; |
| 212 |
break; |
| 213 |
} |
| 214 |
toread = bytes - total; |
| 215 |
if(toread < args->chunksize * concurrent_downloads){ |
| 216 |
/* we've got enough launched downloads */ |
| 217 |
ids[i]=-1; |
| 218 |
} |
| 219 |
if(toread > args->chunksize) |
| 220 |
toread = args->chunksize; |
| 221 |
ids[i]=sftp_async_read_begin(file,toread); |
| 222 |
if(ids[i] == SSH_ERROR) |
| 223 |
goto error; |
| 224 |
i = (i+1) % concurrent_downloads; |
| 225 |
} |
| 226 |
sftp_close(file); |
| 227 |
ms=elapsed_time(&ts); |
| 228 |
*bps=8000 * (float)bytes / ms; |
| 229 |
if(args->verbose > 0) |
| 230 |
fprintf(stdout,"download took %f ms for %lu bytes, at %f bps\n",ms, |
| 231 |
bytes,*bps); |
| 232 |
sftp_free(sftp); |
| 233 |
free(ids); |
| 234 |
return 0; |
| 235 |
error: |
| 236 |
fprintf(stderr,"Error during sftp download : %s\n",ssh_get_error(session)); |
| 237 |
if(file) |
| 238 |
sftp_close(file); |
| 239 |
if(sftp) |
| 240 |
sftp_free(sftp); |
| 241 |
free(ids); |
| 242 |
return -1; |
| 243 |
} |
| 244 |
|
| 245 |
int benchmarks_async_sftp_aio_down(ssh_session session, |
| 246 |
struct argument_s *args, |
| 247 |
float *bps) |
| 248 |
{ |
| 249 |
sftp_session sftp = NULL; |
| 250 |
sftp_limits_t li = NULL; |
| 251 |
sftp_file file = NULL; |
| 252 |
sftp_aio aio = NULL; |
| 253 |
|
| 254 |
struct ssh_list *aio_queue = NULL; |
| 255 |
|
| 256 |
int concurrent_downloads = args->concurrent_requests; |
| 257 |
size_t chunksize; |
| 258 |
struct timestamp_struct ts = {0}; |
| 259 |
float ms = 0.0f; |
| 260 |
|
| 261 |
size_t total_bytes = args->datasize * 1024 * 1024; |
| 262 |
size_t total_bytes_requested = 0, total_bytes_read = 0; |
| 263 |
size_t bufsize = args->chunksize; |
| 264 |
size_t to_read; |
| 265 |
ssize_t bytes_read, bytes_requested; |
| 266 |
int warned = 0, i, rc; |
| 267 |
|
| 268 |
sftp = sftp_new(session); |
| 269 |
if (sftp == NULL) { |
| 270 |
fprintf(stderr, "Error during sftp aio download: %s\n", |
| 271 |
ssh_get_error(session)); |
| 272 |
return -1; |
| 273 |
} |
| 274 |
|
| 275 |
/* |
| 276 |
* Errors which are logged in the ssh session are reported after |
| 277 |
* jumping to the goto label, errors which aren't logged inside the |
| 278 |
* ssh session are reported before jumping to that label |
| 279 |
*/ |
| 280 |
|
| 281 |
rc = sftp_init(sftp); |
| 282 |
if (rc == SSH_ERROR) { |
| 283 |
goto error; |
| 284 |
} |
| 285 |
|
| 286 |
li = sftp_limits(sftp); |
| 287 |
if (li == NULL) { |
| 288 |
goto error; |
| 289 |
} |
| 290 |
|
| 291 |
if (args->chunksize > li->max_read_length) { |
| 292 |
chunksize = li->max_read_length; |
| 293 |
if (args->verbose > 0) { |
| 294 |
fprintf(stdout, |
| 295 |
"Using the chunk size %zu (not the set size %u), " |
| 296 |
"to respect the max data limit for read packet\n", |
| 297 |
chunksize, args->chunksize); |
| 298 |
} |
| 299 |
} else { |
| 300 |
chunksize = args->chunksize; |
| 301 |
} |
| 302 |
|
| 303 |
file = sftp_open(sftp, SFTPDIR SFTPFILE, O_RDONLY, 0); |
| 304 |
if (file == NULL) { |
| 305 |
goto error; |
| 306 |
} |
| 307 |
|
| 308 |
aio_queue = ssh_list_new(); |
| 309 |
if (aio_queue == NULL) { |
| 310 |
fprintf(stderr, |
| 311 |
"Error during sftp aio download: Insufficient memory\n"); |
| 312 |
goto error; |
| 313 |
} |
| 314 |
|
| 315 |
if (args->verbose > 0) { |
| 316 |
fprintf(stdout, |
| 317 |
"Starting download of %zu bytes now, " |
| 318 |
"using %d concurrent downloads.\n", |
| 319 |
total_bytes, concurrent_downloads); |
| 320 |
} |
| 321 |
|
| 322 |
timestamp_init(&ts); |
| 323 |
|
| 324 |
for (i = 0; |
| 325 |
i < concurrent_downloads && total_bytes_requested < total_bytes; |
| 326 |
++i) { |
| 327 |
to_read = total_bytes - total_bytes_requested; |
| 328 |
if (to_read > chunksize) { |
| 329 |
to_read = chunksize; |
| 330 |
} |
| 331 |
|
| 332 |
bytes_requested = sftp_aio_begin_read(file, to_read, &aio); |
| 333 |
if (bytes_requested == SSH_ERROR) { |
| 334 |
goto error; |
| 335 |
} |
| 336 |
|
| 337 |
if ((size_t)bytes_requested != to_read) { |
| 338 |
fprintf(stderr, |
| 339 |
"Error during sftp aio download: sftp_aio_begin_read() " |
| 340 |
"requesting less bytes even when the number of bytes " |
| 341 |
"asked to read are within the max limit"); |
| 342 |
sftp_aio_free(aio); |
| 343 |
goto error; |
| 344 |
} |
| 345 |
|
| 346 |
total_bytes_requested += (size_t)bytes_requested; |
| 347 |
|
| 348 |
/* enqueue */ |
| 349 |
rc = ssh_list_append(aio_queue, aio); |
| 350 |
if (rc == SSH_ERROR) { |
| 351 |
fprintf(stderr, |
| 352 |
"Error during sftp aio download: Insufficient memory"); |
| 353 |
sftp_aio_free(aio); |
| 354 |
goto error; |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
while ((aio = ssh_list_pop_head(sftp_aio, aio_queue)) != NULL) { |
| 359 |
bytes_read = sftp_aio_wait_read(&aio, buffer, bufsize); |
| 360 |
if (bytes_read == -1) { |
| 361 |
goto error; |
| 362 |
} |
| 363 |
|
| 364 |
total_bytes_read += (size_t)bytes_read; |
| 365 |
if (bytes_read == 0) { |
| 366 |
fprintf(stdout , |
| 367 |
"File smaller than expected: %zu bytes (expected %zu).\n", |
| 368 |
total_bytes_read, total_bytes); |
| 369 |
break; |
| 370 |
} |
| 371 |
|
| 372 |
if (total_bytes_read != total_bytes && |
| 373 |
(size_t)bytes_read != chunksize && |
| 374 |
warned != 1) { |
| 375 |
fprintf(stderr, |
| 376 |
"async_sftp_aio_download: Receiving short reads " |
| 377 |
"(%zu, expected %zu) before encountering eof, " |
| 378 |
"the received file will be corrupted and shorted. " |
| 379 |
"Adapt chunksize to %zu.\n", |
| 380 |
bytes_read, chunksize, bytes_read); |
| 381 |
warned = 1; |
| 382 |
} |
| 383 |
|
| 384 |
if (total_bytes_requested == total_bytes) { |
| 385 |
/* No need to issue more requests */ |
| 386 |
continue; |
| 387 |
} |
| 388 |
|
| 389 |
/* else issue a request */ |
| 390 |
to_read = total_bytes - total_bytes_requested; |
| 391 |
if (to_read > chunksize) { |
| 392 |
to_read = chunksize; |
| 393 |
} |
| 394 |
|
| 395 |
bytes_requested = sftp_aio_begin_read(file, to_read, &aio); |
| 396 |
if (bytes_requested == SSH_ERROR) { |
| 397 |
goto error; |
| 398 |
} |
| 399 |
|
| 400 |
if ((size_t)bytes_requested != to_read) { |
| 401 |
fprintf(stderr, |
| 402 |
"Error during sftp aio download: sftp_aio_begin_read() " |
| 403 |
"requesting less bytes even when the number of bytes " |
| 404 |
"asked to read are within the max limit"); |
| 405 |
sftp_aio_free(aio); |
| 406 |
goto error; |
| 407 |
} |
| 408 |
|
| 409 |
total_bytes_requested += (size_t)bytes_requested; |
| 410 |
|
| 411 |
/* enqueue */ |
| 412 |
rc = ssh_list_append(aio_queue, aio); |
| 413 |
if (rc == SSH_ERROR) { |
| 414 |
fprintf(stderr, |
| 415 |
"Error during sftp aio download: Insufficient memory\n"); |
| 416 |
sftp_aio_free(aio); |
| 417 |
goto error; |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
sftp_close(file); |
| 422 |
ms = elapsed_time(&ts); |
| 423 |
*bps = (float)(8000 * total_bytes_read) / ms; |
| 424 |
if (args->verbose > 0) { |
| 425 |
fprintf(stdout, "Download took %f ms for %zu bytes at %f bps.\n", |
| 426 |
ms, total_bytes_read, *bps); |
| 427 |
} |
| 428 |
|
| 429 |
ssh_list_free(aio_queue); |
| 430 |
sftp_limits_free(li); |
| 431 |
sftp_free(sftp); |
| 432 |
return 0; |
| 433 |
|
| 434 |
error: |
| 435 |
rc = ssh_get_error_code(session); |
| 436 |
if (rc != SSH_NO_ERROR) { |
| 437 |
fprintf(stderr, "Error during sftp aio download: %s\n", |
| 438 |
ssh_get_error(session)); |
| 439 |
} |
| 440 |
|
| 441 |
/* Release aio structures corresponding to outstanding requests */ |
| 442 |
while ((aio = ssh_list_pop_head(sftp_aio, aio_queue)) != NULL) { |
| 443 |
sftp_aio_free(aio); |
| 444 |
} |
| 445 |
|
| 446 |
ssh_list_free(aio_queue); |
| 447 |
sftp_close(file); |
| 448 |
sftp_limits_free(li); |
| 449 |
sftp_free(sftp); |
| 450 |
return -1; |
| 451 |
} |
| 452 |
|
| 453 |
int benchmarks_async_sftp_aio_up(ssh_session session, |
| 454 |
struct argument_s *args, |
| 455 |
float *bps) |
| 456 |
{ |
| 457 |
sftp_session sftp = NULL; |
| 458 |
sftp_limits_t li = NULL; |
| 459 |
sftp_file file = NULL; |
| 460 |
sftp_aio aio = NULL; |
| 461 |
struct ssh_list *aio_queue = NULL; |
| 462 |
|
| 463 |
int concurrent_uploads = args->concurrent_requests; |
| 464 |
size_t chunksize; |
| 465 |
struct timestamp_struct ts = {0}; |
| 466 |
float ms = 0.0f; |
| 467 |
|
| 468 |
size_t total_bytes = args->datasize * 1024 * 1024; |
| 469 |
size_t to_write, total_bytes_requested = 0; |
| 470 |
ssize_t bytes_written, bytes_requested; |
| 471 |
int i, rc; |
| 472 |
|
| 473 |
sftp = sftp_new(session); |
| 474 |
if (sftp == NULL) { |
| 475 |
fprintf(stderr, "Error during sftp aio upload: %s\n", |
| 476 |
ssh_get_error(session)); |
| 477 |
return -1; |
| 478 |
} |
| 479 |
|
| 480 |
/* |
| 481 |
* Errors which are logged in the ssh session are reported after |
| 482 |
* jumping to the goto label, errors which aren't logged inside the |
| 483 |
* ssh session are reported before jumping to that label |
| 484 |
*/ |
| 485 |
|
| 486 |
rc = sftp_init(sftp); |
| 487 |
if (rc == SSH_ERROR) { |
| 488 |
goto error; |
| 489 |
} |
| 490 |
|
| 491 |
li = sftp_limits(sftp); |
| 492 |
if (li == NULL) { |
| 493 |
goto error; |
| 494 |
} |
| 495 |
|
| 496 |
if (args->chunksize > li->max_write_length) { |
| 497 |
chunksize = li->max_write_length; |
| 498 |
if (args->verbose > 0) { |
| 499 |
fprintf(stdout, |
| 500 |
"Using the chunk size %zu (not the set size %u), " |
| 501 |
"to respect the max data limit for write packet\n", |
| 502 |
chunksize, args->chunksize); |
| 503 |
} |
| 504 |
} else { |
| 505 |
chunksize = args->chunksize; |
| 506 |
} |
| 507 |
|
| 508 |
file = sftp_open(sftp, SFTPDIR SFTPFILE, |
| 509 |
O_WRONLY | O_CREAT | O_TRUNC, 0777); |
| 510 |
if (file == NULL) { |
| 511 |
goto error; |
| 512 |
} |
| 513 |
|
| 514 |
aio_queue = ssh_list_new(); |
| 515 |
if (aio_queue == NULL) { |
| 516 |
fprintf(stderr, "Error during sftp aio upload: Insufficient memory\n"); |
| 517 |
goto error; |
| 518 |
} |
| 519 |
|
| 520 |
if (args->verbose > 0) { |
| 521 |
fprintf(stdout, |
| 522 |
"Starting upload of %zu bytes now, " |
| 523 |
"using %d concurrent uploads.\n", |
| 524 |
total_bytes, concurrent_uploads); |
| 525 |
} |
| 526 |
|
| 527 |
timestamp_init(&ts); |
| 528 |
|
| 529 |
for (i = 0; |
| 530 |
i < concurrent_uploads && total_bytes_requested < total_bytes; |
| 531 |
++i) { |
| 532 |
to_write = total_bytes - total_bytes_requested; |
| 533 |
if (to_write > chunksize) { |
| 534 |
to_write = chunksize; |
| 535 |
} |
| 536 |
|
| 537 |
bytes_requested = sftp_aio_begin_write(file, buffer, to_write, &aio); |
| 538 |
if (bytes_requested == SSH_ERROR) { |
| 539 |
goto error; |
| 540 |
} |
| 541 |
|
| 542 |
if ((size_t)bytes_requested != to_write) { |
| 543 |
fprintf(stderr, |
| 544 |
"Error during sftp aio upload: sftp_aio_begin_write() " |
| 545 |
"requesting less bytes even when the number of bytes " |
| 546 |
"asked to write are within the max write limit"); |
| 547 |
sftp_aio_free(aio); |
| 548 |
goto error; |
| 549 |
} |
| 550 |
|
| 551 |
total_bytes_requested += (size_t)bytes_requested; |
| 552 |
|
| 553 |
/* enqueue */ |
| 554 |
rc = ssh_list_append(aio_queue, aio); |
| 555 |
if (rc == SSH_ERROR) { |
| 556 |
fprintf(stderr, |
| 557 |
"Error during sftp aio upload: Insufficient memory\n"); |
| 558 |
sftp_aio_free(aio); |
| 559 |
goto error; |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
while ((aio = ssh_list_pop_head(sftp_aio, aio_queue)) != NULL) { |
| 564 |
bytes_written = sftp_aio_wait_write(&aio); |
| 565 |
if (bytes_written == SSH_ERROR) { |
| 566 |
goto error; |
| 567 |
} |
| 568 |
|
| 569 |
if (total_bytes_requested == total_bytes) { |
| 570 |
/* No need to issue more requests */ |
| 571 |
continue; |
| 572 |
} |
| 573 |
|
| 574 |
/* else issue a request */ |
| 575 |
to_write = total_bytes - total_bytes_requested; |
| 576 |
if (to_write > chunksize) { |
| 577 |
to_write = chunksize; |
| 578 |
} |
| 579 |
|
| 580 |
bytes_requested = sftp_aio_begin_write(file, buffer, to_write, &aio); |
| 581 |
if (bytes_requested == SSH_ERROR) { |
| 582 |
goto error; |
| 583 |
} |
| 584 |
|
| 585 |
if ((size_t)bytes_requested != to_write) { |
| 586 |
fprintf(stderr, |
| 587 |
"Error during sftp aio upload: sftp_aio_begin_write() " |
| 588 |
"requesting less bytes even when the number of bytes " |
| 589 |
"asked to write are within the max write limit"); |
| 590 |
sftp_aio_free(aio); |
| 591 |
goto error; |
| 592 |
} |
| 593 |
|
| 594 |
total_bytes_requested += bytes_requested; |
| 595 |
|
| 596 |
/* enqueue */ |
| 597 |
rc = ssh_list_append(aio_queue, aio); |
| 598 |
if (rc == SSH_ERROR) { |
| 599 |
fprintf(stderr, |
| 600 |
"Error during sftp aio upload: Insufficient memory\n"); |
| 601 |
sftp_aio_free(aio); |
| 602 |
goto error; |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
sftp_close(file); |
| 607 |
ms = elapsed_time(&ts); |
| 608 |
*bps = (float)(8000 * total_bytes) / ms; |
| 609 |
if (args->verbose > 0) { |
| 610 |
fprintf(stdout, "Upload took %f ms for %zu bytes at %f bps.\n", |
| 611 |
ms, total_bytes, *bps); |
| 612 |
} |
| 613 |
|
| 614 |
ssh_list_free(aio_queue); |
| 615 |
sftp_limits_free(li); |
| 616 |
sftp_free(sftp); |
| 617 |
return 0; |
| 618 |
|
| 619 |
error: |
| 620 |
rc = ssh_get_error_code(session); |
| 621 |
if (rc != SSH_NO_ERROR) { |
| 622 |
fprintf(stderr, "Error during sftp aio upload: %s\n", |
| 623 |
ssh_get_error(session)); |
| 624 |
} |
| 625 |
|
| 626 |
/* Release aio structures corresponding to outstanding requests */ |
| 627 |
while ((aio = ssh_list_pop_head(sftp_aio, aio_queue)) != NULL) { |
| 628 |
sftp_aio_free(aio); |
| 629 |
} |
| 630 |
|
| 631 |
ssh_list_free(aio_queue); |
| 632 |
sftp_close(file); |
| 633 |
sftp_limits_free(li); |
| 634 |
sftp_free(sftp); |
| 635 |
return -1; |
| 636 |
} |